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