1 //===-- PathMappingListTest.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/PathMappingList.h"
10 #include "lldb/Utility/FileSpec.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "gtest/gtest.h"
13 #include <utility>
14 
15 using namespace lldb_private;
16 
17 namespace {
18 struct Matches {
19   FileSpec original;
20   FileSpec remapped;
Matches__anon3e0a4b530111::Matches21   Matches(const char *o, const char *r) : original(o), remapped(r) {}
Matches__anon3e0a4b530111::Matches22   Matches(const char *o, llvm::sys::path::Style style, const char *r)
23       : original(o, style), remapped(r) {}
24 };
25 } // namespace
26 
TestPathMappings(const PathMappingList & map,llvm::ArrayRef<Matches> matches,llvm::ArrayRef<ConstString> fails)27 static void TestPathMappings(const PathMappingList &map,
28                              llvm::ArrayRef<Matches> matches,
29                              llvm::ArrayRef<ConstString> fails) {
30   ConstString actual_remapped;
31   for (const auto &fail : fails) {
32     SCOPED_TRACE(fail.GetCString());
33     EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
34         << "actual_remapped: " << actual_remapped.GetCString();
35   }
36   for (const auto &match : matches) {
37     SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
38     std::string orig_normalized = match.original.GetPath();
39     EXPECT_TRUE(
40         map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
41     EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
42     FileSpec unmapped_spec;
43     EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec));
44     std::string unmapped_path = unmapped_spec.GetPath();
45     EXPECT_EQ(unmapped_path, orig_normalized);
46   }
47 }
48 
TEST(PathMappingListTest,RelativeTests)49 TEST(PathMappingListTest, RelativeTests) {
50   Matches matches[] = {
51     {".", "/tmp"},
52     {"./", "/tmp"},
53     {"./////", "/tmp"},
54     {"./foo.c", "/tmp/foo.c"},
55     {"foo.c", "/tmp/foo.c"},
56     {"./bar/foo.c", "/tmp/bar/foo.c"},
57     {"bar/foo.c", "/tmp/bar/foo.c"},
58   };
59   ConstString fails[] = {
60 #ifdef _WIN32
61       ConstString("C:\\"),
62       ConstString("C:\\a"),
63 #else
64       ConstString("/a"),
65       ConstString("/"),
66 #endif
67   };
68   PathMappingList map;
69   map.Append(ConstString("."), ConstString("/tmp"), false);
70   TestPathMappings(map, matches, fails);
71   PathMappingList map2;
72   map2.Append(ConstString(""), ConstString("/tmp"), false);
73   TestPathMappings(map, matches, fails);
74 }
75 
TEST(PathMappingListTest,AbsoluteTests)76 TEST(PathMappingListTest, AbsoluteTests) {
77   PathMappingList map;
78   map.Append(ConstString("/old"), ConstString("/new"), false);
79   Matches matches[] = {
80     {"/old", "/new"},
81     {"/old/", "/new"},
82     {"/old/foo/.", "/new/foo"},
83     {"/old/foo.c", "/new/foo.c"},
84     {"/old/foo.c/.", "/new/foo.c"},
85     {"/old/./foo.c", "/new/foo.c"},
86   };
87   ConstString fails[] = {
88     ConstString("/foo"),
89     ConstString("/"),
90     ConstString("foo.c"),
91     ConstString("./foo.c"),
92     ConstString("../foo.c"),
93     ConstString("../bar/foo.c"),
94   };
95   TestPathMappings(map, matches, fails);
96 }
97 
TEST(PathMappingListTest,RemapRoot)98 TEST(PathMappingListTest, RemapRoot) {
99   PathMappingList map;
100   map.Append(ConstString("/"), ConstString("/new"), false);
101   Matches matches[] = {
102     {"/old", "/new/old"},
103     {"/old/", "/new/old"},
104     {"/old/foo/.", "/new/old/foo"},
105     {"/old/foo.c", "/new/old/foo.c"},
106     {"/old/foo.c/.", "/new/old/foo.c"},
107     {"/old/./foo.c", "/new/old/foo.c"},
108   };
109   ConstString fails[] = {
110     ConstString("foo.c"),
111     ConstString("./foo.c"),
112     ConstString("../foo.c"),
113     ConstString("../bar/foo.c"),
114   };
115   TestPathMappings(map, matches, fails);
116 }
117 
118 #ifndef _WIN32
TEST(PathMappingListTest,CrossPlatformTests)119 TEST(PathMappingListTest, CrossPlatformTests) {
120   PathMappingList map;
121   map.Append(ConstString(R"(C:\old)"), ConstString("/new"), false);
122   Matches matches[] = {
123     {R"(C:\old)", llvm::sys::path::Style::windows, "/new"},
124     {R"(C:\old\)", llvm::sys::path::Style::windows, "/new"},
125     {R"(C:\old\foo\.)", llvm::sys::path::Style::windows, "/new/foo"},
126     {R"(C:\old\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
127     {R"(C:\old\foo.c\.)", llvm::sys::path::Style::windows, "/new/foo.c"},
128     {R"(C:\old\.\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
129   };
130   ConstString fails[] = {
131     ConstString("/foo"),
132     ConstString("/"),
133     ConstString("foo.c"),
134     ConstString("./foo.c"),
135     ConstString("../foo.c"),
136     ConstString("../bar/foo.c"),
137   };
138   TestPathMappings(map, matches, fails);
139 }
140 #endif
141