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