xref: /llvm-project/lldb/unittests/Utility/UriParserTest.cpp (revision b9c1b51e45b845debb76d8658edabca70ca56079)
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   std::string scheme(kAsdf);                                                   \
34   std::string hostname(kAsdf);                                                 \
35   int port(1138);                                                              \
36   std::string 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.c_str());                             \
40   EXPECT_STREQ(testCase.m_hostname, hostname.c_str());                         \
41   EXPECT_EQ(testCase.m_port, port);                                            \
42   EXPECT_STREQ(testCase.m_path, path.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   VALIDATE
52 }
53 
54 TEST_F(UriParserTest, MinimalPath) {
55   const UriTestCase testCase("x://y/", "x", "y", -1, "/");
56   VALIDATE
57 }
58 
59 TEST_F(UriParserTest, MinimalPortPath) {
60   const UriTestCase testCase("x://y:1/", "x", "y", 1, "/");
61   VALIDATE
62 }
63 
64 TEST_F(UriParserTest, LongPath) {
65   const UriTestCase testCase("x://y/abc/def/xyz", "x", "y", -1, "/abc/def/xyz");
66   VALIDATE
67 }
68 
69 TEST_F(UriParserTest, TypicalPortPath) {
70   const UriTestCase testCase("connect://192.168.100.132:5432/", "connect",
71                              "192.168.100.132", 5432, "/");
72   VALIDATE
73 }
74 
75 TEST_F(UriParserTest, BracketedHostnamePort) {
76   const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect",
77                              "192.168.100.132", 5432, "/");
78   VALIDATE
79 }
80 
81 TEST_F(UriParserTest, BracketedHostname) {
82   const UriTestCase testCase("connect://[192.168.100.132]", "connect",
83                              "192.168.100.132", -1, "/");
84   VALIDATE
85 }
86 
87 TEST_F(UriParserTest, BracketedHostnameWithColon) {
88   const UriTestCase testCase("connect://[192.168.100.132:5555]:1234", "connect",
89                              "192.168.100.132:5555", 1234, "/");
90   VALIDATE
91 }
92 
93 TEST_F(UriParserTest, SchemeHostSeparator) {
94   const UriTestCase testCase("x:/y");
95   VALIDATE
96 }
97 
98 TEST_F(UriParserTest, SchemeHostSeparator2) {
99   const UriTestCase testCase("x:y");
100   VALIDATE
101 }
102 
103 TEST_F(UriParserTest, SchemeHostSeparator3) {
104   const UriTestCase testCase("x//y");
105   VALIDATE
106 }
107 
108 TEST_F(UriParserTest, SchemeHostSeparator4) {
109   const UriTestCase testCase("x/y");
110   VALIDATE
111 }
112 
113 TEST_F(UriParserTest, BadPort) {
114   const UriTestCase testCase("x://y:a/");
115   VALIDATE
116 }
117 
118 TEST_F(UriParserTest, BadPort2) {
119   const UriTestCase testCase("x://y:5432a/");
120   VALIDATE
121 }
122 
123 TEST_F(UriParserTest, Empty) {
124   const UriTestCase testCase("");
125   VALIDATE
126 }
127 
128 TEST_F(UriParserTest, PortOverflow) {
129   const UriTestCase testCase("x://"
130                              "y:"
131                              "0123456789012345678901234567890123456789012345678"
132                              "9012345678901234567890123456789012345678901234567"
133                              "89/");
134   VALIDATE
135 }
136