Mercurial > thymian
comparison 3rdparty/vmime/tests/utility/urlTest.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 // | |
| 2 // VMime library (http://www.vmime.org) | |
| 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org> | |
| 4 // | |
| 5 // This program is free software; you can redistribute it and/or | |
| 6 // modify it under the terms of the GNU General Public License as | |
| 7 // published by the Free Software Foundation; either version 3 of | |
| 8 // the License, or (at your option) any later version. | |
| 9 // | |
| 10 // This program is distributed in the hope that it will be useful, | |
| 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 // General Public License for more details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU General Public License along | |
| 16 // with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 // | |
| 19 // Linking this library statically or dynamically with other modules is making | |
| 20 // a combined work based on this library. Thus, the terms and conditions of | |
| 21 // the GNU General Public License cover the whole combination. | |
| 22 // | |
| 23 | |
| 24 #include "tests/testUtils.hpp" | |
| 25 | |
| 26 #include "vmime/utility/url.hpp" | |
| 27 #include "vmime/utility/urlUtils.hpp" | |
| 28 | |
| 29 | |
| 30 VMIME_TEST_SUITE_BEGIN(urlTest) | |
| 31 | |
| 32 VMIME_TEST_LIST_BEGIN | |
| 33 VMIME_TEST(testParse1) | |
| 34 VMIME_TEST(testParse2) | |
| 35 VMIME_TEST(testParse3) | |
| 36 VMIME_TEST(testParse4) | |
| 37 VMIME_TEST(testParse5) | |
| 38 VMIME_TEST(testGenerate) | |
| 39 VMIME_TEST(testUtilsEncode) | |
| 40 VMIME_TEST(testUtilsDecode) | |
| 41 VMIME_TEST(testUtilsDecodeSpecialCases) | |
| 42 VMIME_TEST(testUtilsEncodeReservedChars) | |
| 43 VMIME_TEST(testUtilsEncodeUnsafeChars) | |
| 44 VMIME_TEST_LIST_END | |
| 45 | |
| 46 | |
| 47 static bool parseHelper(vmime::utility::url& u, const vmime::string& str) | |
| 48 { | |
| 49 try | |
| 50 { | |
| 51 u = vmime::utility::url(str); | |
| 52 } | |
| 53 catch (vmime::exceptions::malformed_url) | |
| 54 { | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 | |
| 62 void testParse1() | |
| 63 { | |
| 64 // Test some valid constructions | |
| 65 vmime::utility::url u1("", ""); | |
| 66 | |
| 67 VASSERT_EQ("1.1", true, parseHelper(u1, "protocol://user:password@host:12345/path/")); | |
| 68 VASSERT_EQ("1.2", "protocol", u1.getProtocol()); | |
| 69 VASSERT_EQ("1.3", "user", u1.getUsername()); | |
| 70 VASSERT_EQ("1.4", "password", u1.getPassword()); | |
| 71 VASSERT_EQ("1.5", "host", u1.getHost()); | |
| 72 VASSERT_EQ("1.6", 12345, u1.getPort()); | |
| 73 VASSERT_EQ("1.7", "/path/", u1.getPath()); | |
| 74 | |
| 75 vmime::utility::url u2("", ""); | |
| 76 | |
| 77 VASSERT_EQ("2.1", true, parseHelper(u2, "protocol://user@host:12345/path/")); | |
| 78 VASSERT_EQ("2.2", "protocol", u2.getProtocol()); | |
| 79 VASSERT_EQ("2.3", "user", u2.getUsername()); | |
| 80 VASSERT_EQ("2.4", "", u2.getPassword()); | |
| 81 VASSERT_EQ("2.5", "host", u2.getHost()); | |
| 82 VASSERT_EQ("2.6", 12345, u2.getPort()); | |
| 83 VASSERT_EQ("2.7", "/path/", u2.getPath()); | |
| 84 | |
| 85 vmime::utility::url u3("", ""); | |
| 86 | |
| 87 VASSERT_EQ("3.1", true, parseHelper(u3, "protocol://host:12345/path/")); | |
| 88 VASSERT_EQ("3.2", "protocol", u3.getProtocol()); | |
| 89 VASSERT_EQ("3.3", "", u3.getUsername()); | |
| 90 VASSERT_EQ("3.4", "", u3.getPassword()); | |
| 91 VASSERT_EQ("3.5", "host", u3.getHost()); | |
| 92 VASSERT_EQ("3.6", 12345, u3.getPort()); | |
| 93 VASSERT_EQ("3.7", "/path/", u3.getPath()); | |
| 94 | |
| 95 vmime::utility::url u4("", ""); | |
| 96 | |
| 97 VASSERT_EQ("4.1", true, parseHelper(u4, "protocol://host/path/")); | |
| 98 VASSERT_EQ("4.2", "protocol", u4.getProtocol()); | |
| 99 VASSERT_EQ("4.3", "", u4.getUsername()); | |
| 100 VASSERT_EQ("4.4", "", u4.getPassword()); | |
| 101 VASSERT_EQ("4.5", "host", u4.getHost()); | |
| 102 VASSERT_EQ("4.6", vmime::utility::url::UNSPECIFIED_PORT, u4.getPort()); | |
| 103 VASSERT_EQ("4.7", "/path/", u4.getPath()); | |
| 104 | |
| 105 vmime::utility::url u5("", ""); | |
| 106 | |
| 107 VASSERT_EQ("5.1", true, parseHelper(u5, "protocol://host/")); | |
| 108 VASSERT_EQ("5.2", "protocol", u5.getProtocol()); | |
| 109 VASSERT_EQ("5.3", "", u5.getUsername()); | |
| 110 VASSERT_EQ("5.4", "", u5.getPassword()); | |
| 111 VASSERT_EQ("5.5", "host", u5.getHost()); | |
| 112 VASSERT_EQ("5.6", vmime::utility::url::UNSPECIFIED_PORT, u4.getPort()); | |
| 113 VASSERT_EQ("5.7", "", u5.getPath()); | |
| 114 | |
| 115 vmime::utility::url u6("", ""); | |
| 116 | |
| 117 VASSERT_EQ("6.1", true, parseHelper(u4, "protocol://host/path/file")); | |
| 118 VASSERT_EQ("6.2", "protocol", u4.getProtocol()); | |
| 119 VASSERT_EQ("6.3", "", u4.getUsername()); | |
| 120 VASSERT_EQ("6.4", "", u4.getPassword()); | |
| 121 VASSERT_EQ("6.5", "host", u4.getHost()); | |
| 122 VASSERT_EQ("6.6", vmime::utility::url::UNSPECIFIED_PORT, u4.getPort()); | |
| 123 VASSERT_EQ("6.7", "/path/file", u4.getPath()); | |
| 124 } | |
| 125 | |
| 126 void testParse2() | |
| 127 { | |
| 128 // Now, test some ill-formed URLs | |
| 129 | |
| 130 // -- missing protocol | |
| 131 vmime::utility::url u1("", ""); | |
| 132 VASSERT_EQ("1", false, parseHelper(u1, "://host")); | |
| 133 | |
| 134 // -- port can contain only digits | |
| 135 vmime::utility::url u2("", ""); | |
| 136 VASSERT_EQ("2", false, parseHelper(u2, "proto://host:abc123")); | |
| 137 | |
| 138 // -- no host specified | |
| 139 vmime::utility::url u3("", ""); | |
| 140 VASSERT_EQ("3", false, parseHelper(u3, "proto:///path")); | |
| 141 | |
| 142 // -- no protocol separator (://) | |
| 143 vmime::utility::url u4("", ""); | |
| 144 VASSERT_EQ("4", false, parseHelper(u4, "protohost/path")); | |
| 145 } | |
| 146 | |
| 147 void testParse3() | |
| 148 { | |
| 149 // Test decoding | |
| 150 vmime::utility::url u1("", ""); | |
| 151 | |
| 152 VASSERT_EQ("1.1", true, parseHelper(u1, "pro%12to://user%34:pass%56word@ho%78st:12345/pa%abth/")); | |
| 153 VASSERT_EQ("1.2", "pro%12to", u1.getProtocol()); // protocol should not be decoded | |
| 154 VASSERT_EQ("1.3", "user\x34", u1.getUsername()); | |
| 155 VASSERT_EQ("1.4", "pass\x56word", u1.getPassword()); | |
| 156 VASSERT_EQ("1.5", "ho\x78st", u1.getHost()); | |
| 157 VASSERT_EQ("1.6", 12345, u1.getPort()); | |
| 158 VASSERT_EQ("1.7", "/pa\xabth/", u1.getPath()); | |
| 159 } | |
| 160 | |
| 161 void testParse4() | |
| 162 { | |
| 163 // Test parameters | |
| 164 vmime::utility::url u1("", ""); | |
| 165 | |
| 166 VASSERT_EQ("1.1", true, parseHelper(u1, "proto://host/path?p1=v1&p2=v2")); | |
| 167 VASSERT_EQ("1.2", "v1", u1.getParams()["p1"]); | |
| 168 VASSERT_EQ("1.3", "v2", u1.getParams()["p2"]); | |
| 169 VASSERT_EQ("1.4", "/path", u1.getPath()); | |
| 170 | |
| 171 vmime::utility::url u2("", ""); | |
| 172 | |
| 173 VASSERT_EQ("2.1", true, parseHelper(u2, "proto://host/path?p1=v1&p2")); | |
| 174 VASSERT_EQ("2.2", "v1", u2.getParams()["p1"]); | |
| 175 VASSERT_EQ("2.3", "p2", u2.getParams()["p2"]); | |
| 176 VASSERT_EQ("2.4", "/path", u2.getPath()); | |
| 177 | |
| 178 vmime::utility::url u3("", ""); | |
| 179 | |
| 180 VASSERT_EQ("3.1", true, parseHelper(u3, "proto://host/?p1=v1&p2=v2")); | |
| 181 VASSERT_EQ("3.2", "v1", u3.getParams()["p1"]); | |
| 182 VASSERT_EQ("3.3", "v2", u3.getParams()["p2"]); | |
| 183 VASSERT_EQ("3.4", "", u3.getPath()); | |
| 184 | |
| 185 vmime::utility::url u4("", ""); | |
| 186 | |
| 187 VASSERT_EQ("4.1", true, parseHelper(u4, "proto://host/path?p1=%3D&%3D=v2")); | |
| 188 VASSERT_EQ("4.2", "=", u4.getParams()["p1"]); | |
| 189 VASSERT_EQ("4.3", "v2", u4.getParams()["="]); | |
| 190 VASSERT_EQ("4.4", "/path", u4.getPath()); | |
| 191 } | |
| 192 | |
| 193 // '@' symbol in the username part | |
| 194 void testParse5() | |
| 195 { | |
| 196 vmime::utility::url u1("", ""); | |
| 197 | |
| 198 VASSERT_EQ("1", true, parseHelper(u1, "imap://account@myserver.com:password@myserver.com")); | |
| 199 VASSERT_EQ("2", "account@myserver.com", u1.getUsername()); | |
| 200 VASSERT_EQ("3", "password", u1.getPassword()); | |
| 201 VASSERT_EQ("4", "myserver.com", u1.getHost()); | |
| 202 } | |
| 203 | |
| 204 void testGenerate() | |
| 205 { | |
| 206 vmime::utility::url u1("proto", "host", 12345, "path", "user", "password"); | |
| 207 VASSERT_EQ("1", "proto://user:password@host:12345/path", | |
| 208 static_cast <vmime::string>(u1)); | |
| 209 | |
| 210 vmime::utility::url u2("proto", "host"); | |
| 211 VASSERT_EQ("2", "proto://host", static_cast <vmime::string>(u2)); | |
| 212 | |
| 213 vmime::utility::url u3("proto", "host"); | |
| 214 u3.getParams()["p1"] = "v1"; | |
| 215 VASSERT_EQ("3.1", "proto://host/?p1=v1", | |
| 216 static_cast <vmime::string>(u3)); | |
| 217 u3.getParams()["p2"] = "v2"; | |
| 218 VASSERT_EQ("3.2", "proto://host/?p1=v1&p2=v2", | |
| 219 static_cast <vmime::string>(u3)); | |
| 220 | |
| 221 // Test special characters | |
| 222 u3.getParams().clear(); | |
| 223 u3.getParams()["&"] = "="; | |
| 224 VASSERT_EQ("3.3", "proto://host/?%26=%3D", | |
| 225 static_cast <vmime::string>(u3)); | |
| 226 } | |
| 227 | |
| 228 void testUtilsEncode() | |
| 229 { | |
| 230 VASSERT_EQ("1", "%01", vmime::utility::urlUtils::encode("\x01")); | |
| 231 VASSERT_EQ("2", "%20", vmime::utility::urlUtils::encode(" ")); | |
| 232 VASSERT_EQ("3", "%FF", vmime::utility::urlUtils::encode("\xff")); | |
| 233 VASSERT_EQ("4", "a", vmime::utility::urlUtils::encode("a")); | |
| 234 } | |
| 235 | |
| 236 void testUtilsDecode() | |
| 237 { | |
| 238 for (int i = 0 ; i < 255 ; ++i) | |
| 239 { | |
| 240 std::ostringstream ossTest; | |
| 241 ossTest << "%" << "0123456789ABCDEF"[i / 16] | |
| 242 << "0123456789ABCDEF"[i % 16]; | |
| 243 | |
| 244 std::ostringstream ossNum; | |
| 245 ossNum << i; | |
| 246 | |
| 247 vmime::string res; | |
| 248 res += static_cast <unsigned char>(i); | |
| 249 | |
| 250 VASSERT_EQ(ossNum.str(), res, | |
| 251 vmime::utility::urlUtils::decode(ossTest.str())); | |
| 252 } | |
| 253 | |
| 254 } | |
| 255 | |
| 256 void testUtilsDecodeSpecialCases() | |
| 257 { | |
| 258 // Bug #1656547: segfault with '%' at the end of the string | |
| 259 VASSERT_EQ("1.1", "sadfsda%", vmime::utility::urlUtils::decode("sadfsda%")); | |
| 260 VASSERT_EQ("1.2", "sadfsda\x05", vmime::utility::urlUtils::decode("sadfsda%5")); | |
| 261 VASSERT_EQ("1.3", "sadfsda\x42", vmime::utility::urlUtils::decode("sadfsda%42")); | |
| 262 } | |
| 263 | |
| 264 void testUtilsEncodeReservedChars() | |
| 265 { | |
| 266 VASSERT_EQ("1", "%24", vmime::utility::urlUtils::encode("$")); | |
| 267 VASSERT_EQ("2", "%26", vmime::utility::urlUtils::encode("&")); | |
| 268 VASSERT_EQ("3", "%2B", vmime::utility::urlUtils::encode("+")); | |
| 269 VASSERT_EQ("4", "%2C", vmime::utility::urlUtils::encode(",")); | |
| 270 VASSERT_EQ("5", "%2F", vmime::utility::urlUtils::encode("/")); | |
| 271 VASSERT_EQ("6", "%3A", vmime::utility::urlUtils::encode(":")); | |
| 272 VASSERT_EQ("7", "%3B", vmime::utility::urlUtils::encode(";")); | |
| 273 VASSERT_EQ("8", "%3D", vmime::utility::urlUtils::encode("=")); | |
| 274 VASSERT_EQ("9", "%3F", vmime::utility::urlUtils::encode("?")); | |
| 275 VASSERT_EQ("10", "%40", vmime::utility::urlUtils::encode("@")); | |
| 276 } | |
| 277 | |
| 278 void testUtilsEncodeUnsafeChars() | |
| 279 { | |
| 280 VASSERT_EQ("1", "%20", vmime::utility::urlUtils::encode(" ")); | |
| 281 VASSERT_EQ("2", "%22", vmime::utility::urlUtils::encode("\"")); | |
| 282 VASSERT_EQ("3", "%3C", vmime::utility::urlUtils::encode("<")); | |
| 283 VASSERT_EQ("4", "%3E", vmime::utility::urlUtils::encode(">")); | |
| 284 VASSERT_EQ("5", "%23", vmime::utility::urlUtils::encode("#")); | |
| 285 VASSERT_EQ("6", "%25", vmime::utility::urlUtils::encode("%")); | |
| 286 VASSERT_EQ("7", "%7B", vmime::utility::urlUtils::encode("{")); | |
| 287 VASSERT_EQ("8", "%7D", vmime::utility::urlUtils::encode("}")); | |
| 288 VASSERT_EQ("9", "%7C", vmime::utility::urlUtils::encode("|")); | |
| 289 VASSERT_EQ("10", "%5C", vmime::utility::urlUtils::encode("\\")); | |
| 290 VASSERT_EQ("11", "%5E", vmime::utility::urlUtils::encode("^")); | |
| 291 VASSERT_EQ("12", "%7E", vmime::utility::urlUtils::encode("~")); | |
| 292 VASSERT_EQ("13", "%5B", vmime::utility::urlUtils::encode("[")); | |
| 293 VASSERT_EQ("14", "%5D", vmime::utility::urlUtils::encode("]")); | |
| 294 VASSERT_EQ("15", "%60", vmime::utility::urlUtils::encode("`")); | |
| 295 } | |
| 296 | |
| 297 VMIME_TEST_SUITE_END | |
| 298 |
