1 #include "lldb/Utility/UriParser.h"
2 #include "gtest/gtest.h"
3 
4 using namespace lldb_private;
5 
6 // result strings (scheme/hostname/port/path) passed into UriParser::Parse
7 // are initialized to kAsdf so we can verify that they are unmodified if the
8 // URI is invalid
9 static const char *kAsdf = "asdf";
10 
11 class UriTestCase {
12 public:
UriTestCase(const char * uri,const char * scheme,const char * hostname,int port,const char * path)13   UriTestCase(const char *uri, const char *scheme, const char *hostname,
14               int port, const char *path)
15       : m_uri(uri), m_result(true), m_scheme(scheme), m_hostname(hostname),
16         m_port(port), m_path(path) {}
17 
UriTestCase(const char * uri)18   UriTestCase(const char *uri)
19       : m_uri(uri), m_result(false), m_scheme(kAsdf), m_hostname(kAsdf),
20         m_port(1138), m_path(kAsdf) {}
21 
22   const char *m_uri;
23   bool m_result;
24   const char *m_scheme;
25   const char *m_hostname;
26   int m_port;
27   const char *m_path;
28 };
29 
30 #define VALIDATE                                                               \
31   llvm::StringRef scheme(kAsdf);                                               \
32   llvm::StringRef hostname(kAsdf);                                             \
33   int port(1138);                                                              \
34   llvm::StringRef path(kAsdf);                                                 \
35   EXPECT_EQ(testCase.m_result,                                                 \
36             UriParser::Parse(testCase.m_uri, scheme, hostname, port, path));   \
37   EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());                       \
38   EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());                   \
39   EXPECT_EQ(testCase.m_port, port);                                            \
40   EXPECT_STREQ(testCase.m_path, path.str().c_str());
41 
TEST(UriParserTest,Minimal)42 TEST(UriParserTest, Minimal) {
43   const UriTestCase testCase("x://y", "x", "y", -1, "/");
44   VALIDATE
45 }
46 
TEST(UriParserTest,MinimalPort)47 TEST(UriParserTest, MinimalPort) {
48   const UriTestCase testCase("x://y:1", "x", "y", 1, "/");
49   llvm::StringRef scheme(kAsdf);
50   llvm::StringRef hostname(kAsdf);
51   int port(1138);
52   llvm::StringRef path(kAsdf);
53   bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
54   EXPECT_EQ(testCase.m_result, result);
55 
56   EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
57   EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
58   EXPECT_EQ(testCase.m_port, port);
59   EXPECT_STREQ(testCase.m_path, path.str().c_str());
60 }
61 
TEST(UriParserTest,MinimalPath)62 TEST(UriParserTest, MinimalPath) {
63   const UriTestCase testCase("x://y/", "x", "y", -1, "/");
64   VALIDATE
65 }
66 
TEST(UriParserTest,MinimalPortPath)67 TEST(UriParserTest, MinimalPortPath) {
68   const UriTestCase testCase("x://y:1/", "x", "y", 1, "/");
69   VALIDATE
70 }
71 
TEST(UriParserTest,LongPath)72 TEST(UriParserTest, LongPath) {
73   const UriTestCase testCase("x://y/abc/def/xyz", "x", "y", -1, "/abc/def/xyz");
74   VALIDATE
75 }
76 
TEST(UriParserTest,TypicalPortPathIPv4)77 TEST(UriParserTest, TypicalPortPathIPv4) {
78   const UriTestCase testCase("connect://192.168.100.132:5432/", "connect",
79                              "192.168.100.132", 5432, "/");
80   VALIDATE;
81 }
82 
TEST(UriParserTest,TypicalPortPathIPv6)83 TEST(UriParserTest, TypicalPortPathIPv6) {
84   const UriTestCase testCase(
85       "connect://[2601:600:107f:db64:a42b:4faa:284:3082]:5432/", "connect",
86       "2601:600:107f:db64:a42b:4faa:284:3082", 5432, "/");
87   VALIDATE;
88 }
89 
TEST(UriParserTest,BracketedHostnamePort)90 TEST(UriParserTest, BracketedHostnamePort) {
91   const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect",
92                              "192.168.100.132", 5432, "/");
93   llvm::StringRef scheme(kAsdf);
94   llvm::StringRef hostname(kAsdf);
95   int port(1138);
96   llvm::StringRef path(kAsdf);
97   bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
98   EXPECT_EQ(testCase.m_result, result);
99 
100   EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
101   EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
102   EXPECT_EQ(testCase.m_port, port);
103   EXPECT_STREQ(testCase.m_path, path.str().c_str());
104 }
105 
TEST(UriParserTest,BracketedHostname)106 TEST(UriParserTest, BracketedHostname) {
107   const UriTestCase testCase("connect://[192.168.100.132]", "connect",
108                              "192.168.100.132", -1, "/");
109   VALIDATE
110 }
111 
TEST(UriParserTest,BracketedHostnameWithPortIPv4)112 TEST(UriParserTest, BracketedHostnameWithPortIPv4) {
113   // Android device over IPv4: port is a part of the hostname.
114   const UriTestCase testCase("connect://[192.168.100.132:1234]", "connect",
115                              "192.168.100.132:1234", -1, "/");
116   VALIDATE
117 }
118 
TEST(UriParserTest,BracketedHostnameWithPortIPv6)119 TEST(UriParserTest, BracketedHostnameWithPortIPv6) {
120   // Android device over IPv6: port is a part of the hostname.
121   const UriTestCase testCase(
122       "connect://[[2601:600:107f:db64:a42b:4faa:284]:1234]", "connect",
123       "[2601:600:107f:db64:a42b:4faa:284]:1234", -1, "/");
124   VALIDATE
125 }
126 
TEST(UriParserTest,BracketedHostnameWithColon)127 TEST(UriParserTest, BracketedHostnameWithColon) {
128   const UriTestCase testCase("connect://[192.168.100.132:5555]:1234", "connect",
129                              "192.168.100.132:5555", 1234, "/");
130   VALIDATE
131 }
132 
TEST(UriParserTest,SchemeHostSeparator)133 TEST(UriParserTest, SchemeHostSeparator) {
134   const UriTestCase testCase("x:/y");
135   VALIDATE
136 }
137 
TEST(UriParserTest,SchemeHostSeparator2)138 TEST(UriParserTest, SchemeHostSeparator2) {
139   const UriTestCase testCase("x:y");
140   VALIDATE
141 }
142 
TEST(UriParserTest,SchemeHostSeparator3)143 TEST(UriParserTest, SchemeHostSeparator3) {
144   const UriTestCase testCase("x//y");
145   VALIDATE
146 }
147 
TEST(UriParserTest,SchemeHostSeparator4)148 TEST(UriParserTest, SchemeHostSeparator4) {
149   const UriTestCase testCase("x/y");
150   VALIDATE
151 }
152 
TEST(UriParserTest,BadPort)153 TEST(UriParserTest, BadPort) {
154   const UriTestCase testCase("x://y:a/");
155   VALIDATE
156 }
157 
TEST(UriParserTest,BadPort2)158 TEST(UriParserTest, BadPort2) {
159   const UriTestCase testCase("x://y:5432a/");
160   VALIDATE
161 }
162 
TEST(UriParserTest,Empty)163 TEST(UriParserTest, Empty) {
164   const UriTestCase testCase("");
165   VALIDATE
166 }
167 
TEST(UriParserTest,PortOverflow)168 TEST(UriParserTest, PortOverflow) {
169   const UriTestCase testCase("x://"
170                              "y:"
171                              "0123456789012345678901234567890123456789012345678"
172                              "9012345678901234567890123456789012345678901234567"
173                              "89/");
174   VALIDATE
175 }
176