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:
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 
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 
42 TEST(UriParserTest, Minimal) {
43   const UriTestCase testCase("x://y", "x", "y", -1, "/");
44   VALIDATE
45 }
46 
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 
62 TEST(UriParserTest, MinimalPath) {
63   const UriTestCase testCase("x://y/", "x", "y", -1, "/");
64   VALIDATE
65 }
66 
67 TEST(UriParserTest, MinimalPortPath) {
68   const UriTestCase testCase("x://y:1/", "x", "y", 1, "/");
69   VALIDATE
70 }
71 
72 TEST(UriParserTest, LongPath) {
73   const UriTestCase testCase("x://y/abc/def/xyz", "x", "y", -1, "/abc/def/xyz");
74   VALIDATE
75 }
76 
77 TEST(UriParserTest, TypicalPortPath) {
78   const UriTestCase testCase("connect://192.168.100.132:5432/", "connect",
79                              "192.168.100.132", 5432, "/");
80   VALIDATE;
81 }
82 
83 TEST(UriParserTest, BracketedHostnamePort) {
84   const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect",
85                              "192.168.100.132", 5432, "/");
86   llvm::StringRef scheme(kAsdf);
87   llvm::StringRef hostname(kAsdf);
88   int port(1138);
89   llvm::StringRef path(kAsdf);
90   bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
91   EXPECT_EQ(testCase.m_result, result);
92 
93   EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
94   EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
95   EXPECT_EQ(testCase.m_port, port);
96   EXPECT_STREQ(testCase.m_path, path.str().c_str());
97 }
98 
99 TEST(UriParserTest, BracketedHostname) {
100   const UriTestCase testCase("connect://[192.168.100.132]", "connect",
101                              "192.168.100.132", -1, "/");
102   VALIDATE
103 }
104 
105 TEST(UriParserTest, BracketedHostnameWithColon) {
106   const UriTestCase testCase("connect://[192.168.100.132:5555]:1234", "connect",
107                              "192.168.100.132:5555", 1234, "/");
108   VALIDATE
109 }
110 
111 TEST(UriParserTest, SchemeHostSeparator) {
112   const UriTestCase testCase("x:/y");
113   VALIDATE
114 }
115 
116 TEST(UriParserTest, SchemeHostSeparator2) {
117   const UriTestCase testCase("x:y");
118   VALIDATE
119 }
120 
121 TEST(UriParserTest, SchemeHostSeparator3) {
122   const UriTestCase testCase("x//y");
123   VALIDATE
124 }
125 
126 TEST(UriParserTest, SchemeHostSeparator4) {
127   const UriTestCase testCase("x/y");
128   VALIDATE
129 }
130 
131 TEST(UriParserTest, BadPort) {
132   const UriTestCase testCase("x://y:a/");
133   VALIDATE
134 }
135 
136 TEST(UriParserTest, BadPort2) {
137   const UriTestCase testCase("x://y:5432a/");
138   VALIDATE
139 }
140 
141 TEST(UriParserTest, Empty) {
142   const UriTestCase testCase("");
143   VALIDATE
144 }
145 
146 TEST(UriParserTest, PortOverflow) {
147   const UriTestCase testCase("x://"
148                              "y:"
149                              "0123456789012345678901234567890123456789012345678"
150                              "9012345678901234567890123456789012345678901234567"
151                              "89/");
152   VALIDATE
153 }
154