remote.cpp (7860B)
1 #include "helper.hpp" 2 3 #include <errors.hpp> 4 #include <remote.hpp> 5 6 static const char *M = "[remote]"; 7 8 TEST_CASE("construct remote", M) { 9 Remote remote{"name", "url", true}; 10 REQUIRE(remote.name() == "name"); 11 REQUIRE(remote.url() == "url"); 12 REQUIRE(remote.isEnabled()); 13 REQUIRE_FALSE(remote.isProtected()); 14 } 15 16 TEST_CASE("remote name validation", M) { 17 SECTION("empty") { 18 try { 19 Remote remote("", "url"); 20 FAIL("empty name was allowed"); 21 } 22 catch(const reapack_error &e) { 23 REQUIRE(std::string{e.what()} == "empty repository name"); 24 } 25 } 26 27 SECTION("invalid") { 28 const std::string invalidNames[] { 29 "ab/cd", 30 "ab\\cd", 31 "..", 32 ".", 33 "....", 34 ".hidden", 35 "trailing.", 36 " leading", 37 "trailing ", 38 "ctrl\4chars", 39 40 // Windows device names... 41 "CLOCK$", 42 "COM1", 43 "LPT2", 44 "lpt1", 45 }; 46 47 for(const std::string &name : invalidNames) { 48 try { 49 Remote remote(name, "url"); 50 FAIL("'" + name + "' was allowed"); 51 } 52 catch(const reapack_error &e) { 53 REQUIRE(std::string{e.what()} == "invalid repository name '" + name + "'"); 54 } 55 } 56 } 57 58 SECTION("valid") { 59 const std::string validNames[] { 60 "1234", 61 "hello world", 62 "hello_world", 63 "Новая папка", 64 "Hello ~World~", 65 "Repository #1", 66 }; 67 68 for(const std::string &name : validNames) { 69 try { 70 Remote remote(name, "url"); 71 } 72 catch(const reapack_error &e) { 73 FAIL("'" + name + "' was denied (" + e.what() + ')'); 74 } 75 } 76 } 77 } 78 79 TEST_CASE("remote url validation", M) { 80 SECTION("invalid") { 81 const std::string invalidUrls[] { 82 "", 83 "hello world", // space should be %20 84 }; 85 86 for(const std::string &url : invalidUrls) { 87 try { 88 Remote remote("hello", url); 89 FAIL("'" + url + "' was allowed"); 90 } 91 catch(const reapack_error &e) { 92 REQUIRE(std::string{e.what()} == "invalid url"); 93 } 94 } 95 } 96 } 97 98 TEST_CASE("set invalid values", M) { 99 Remote remote; 100 101 SECTION("name") { 102 try { 103 remote.setName("/"); 104 FAIL(); 105 } 106 catch(const reapack_error &e) { 107 REQUIRE(std::string{e.what()} == "invalid repository name '/'"); 108 } 109 } 110 111 SECTION("url") { 112 try { 113 remote.setUrl("http://google.com/hello?invalid=|"); 114 FAIL(); 115 } 116 catch(const reapack_error &e) { 117 REQUIRE(std::string{e.what()} == "invalid url"); 118 } 119 } 120 } 121 122 TEST_CASE("valid remote urls", M) { 123 Remote remote; 124 125 SECTION("uppercase") 126 remote.setUrl("https://google.com/AAA"); 127 128 SECTION("escape sequence") 129 remote.setUrl("https://google.com/?q=hello%20world"); 130 131 SECTION("libstdc++ bug #71500") // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71500 132 remote.setUrl("https://google.com/RRR"); 133 } 134 135 TEST_CASE("null remote", M) { 136 Remote remote; 137 REQUIRE(remote.isNull()); 138 REQUIRE_FALSE(remote); 139 CHECK(remote.isEnabled()); 140 141 SECTION("set name") { 142 remote.setName("test"); 143 REQUIRE(remote.name() == "test"); 144 REQUIRE(remote.isNull()); 145 } 146 147 SECTION("set url") { 148 remote.setUrl("test"); 149 REQUIRE(remote.url() == "test"); 150 REQUIRE(remote.isNull()); 151 } 152 153 SECTION("set both") { 154 remote.setName("hello"); 155 remote.setUrl("world"); 156 REQUIRE_FALSE(remote.isNull()); 157 REQUIRE(remote); 158 } 159 } 160 161 TEST_CASE("protect remote", M) { 162 Remote remote; 163 remote.setUrl("https://cfillion.ca"); 164 REQUIRE_FALSE(remote.isProtected()); 165 166 remote.protect(); 167 REQUIRE(remote.isProtected()); 168 169 try { 170 remote.setUrl("https://google.com"); 171 FAIL(); 172 } 173 catch(const reapack_error &e) { 174 REQUIRE(std::string{e.what()} == 175 "cannot change the URL of a protected repository"); 176 } 177 178 remote.setUrl(remote.url()); // this should work for AddSetRepository API 179 } 180 181 TEST_CASE("autoinstall remote", M) { 182 Remote remote; 183 REQUIRE_FALSE(bool{remote.autoInstall()}); 184 REQUIRE(remote.autoInstall(true)); 185 REQUIRE_FALSE(remote.autoInstall(false)); 186 187 remote.setAutoInstall(true); 188 REQUIRE(bool{remote.autoInstall()}); 189 REQUIRE(remote.autoInstall(true)); 190 REQUIRE(remote.autoInstall(false)); 191 192 remote.setAutoInstall(false); 193 REQUIRE_FALSE(bool{remote.autoInstall()}); 194 REQUIRE_FALSE(remote.autoInstall(true)); 195 REQUIRE_FALSE(remote.autoInstall(false)); 196 } 197 198 TEST_CASE("add remotes to list", M) { 199 RemoteList list; 200 201 REQUIRE(list.empty()); 202 REQUIRE(list.size() == 0); 203 REQUIRE_FALSE(list.hasName("name")); 204 205 list.add({"name", "url"}); 206 207 REQUIRE_FALSE(list.empty()); 208 REQUIRE(list.size() == 1); 209 REQUIRE(list.hasName("name")); 210 } 211 212 TEST_CASE("add invalid remote to list", M) { 213 RemoteList list; 214 list.add({}); 215 216 REQUIRE(list.empty()); 217 } 218 219 TEST_CASE("replace remote", M) { 220 RemoteList list; 221 222 list.add({"name", "url1"}); 223 list.add({"name", "url2"}); 224 225 REQUIRE(list.size() == 1); 226 REQUIRE(list.get("name").url() == "url2"); 227 }; 228 229 TEST_CASE("get remote by name", M) { 230 RemoteList list; 231 REQUIRE(list.get("hello").isNull()); 232 233 list.add({"hello", "world"}); 234 REQUIRE_FALSE(list.get("hello").isNull()); 235 } 236 237 TEST_CASE("unserialize remote", M) { 238 SECTION("incomplete") 239 REQUIRE_FALSE(Remote::fromString("foo")); 240 241 SECTION("empty name") 242 REQUIRE_FALSE(Remote::fromString("|url|1")); 243 244 SECTION("invalid name") 245 REQUIRE_FALSE(Remote::fromString("/|url|1")); 246 247 SECTION("invalid url") 248 REQUIRE_FALSE(Remote::fromString("name||false")); 249 250 SECTION("enabled") { 251 Remote remote = Remote::fromString("name|url|1"); 252 REQUIRE(remote); 253 254 REQUIRE(remote.name() == "name"); 255 REQUIRE(remote.url() == "url"); 256 REQUIRE(remote.isEnabled()); 257 } 258 259 SECTION("disabled") { 260 Remote remote = Remote::fromString("name|url|0"); 261 REQUIRE(remote.name() == "name"); 262 REQUIRE(remote.url() == "url"); 263 REQUIRE_FALSE(remote.isEnabled()); 264 } 265 266 SECTION("missing auto-install") { 267 Remote remote = Remote::fromString("name|url|1"); 268 REQUIRE(boost::logic::indeterminate(remote.autoInstall())); 269 } 270 271 SECTION("indeterminate auto-install") { 272 Remote remote = Remote::fromString("name|url|1|2"); 273 REQUIRE(boost::logic::indeterminate(remote.autoInstall())); 274 } 275 276 SECTION("auto-install enabled") { 277 Remote remote = Remote::fromString("name|url|1|1"); 278 REQUIRE(bool{remote.autoInstall()}); 279 } 280 281 SECTION("auto-install enabled") { 282 Remote remote = Remote::fromString("name|url|1|0"); 283 REQUIRE(bool{remote.autoInstall() == false}); 284 } 285 } 286 287 TEST_CASE("serialize remote", M) { 288 SECTION("default") 289 REQUIRE(Remote("name", "url").toString() == "name|url|1|2"); 290 291 SECTION("enabled") 292 REQUIRE(Remote("name", "url", true, true).toString() == "name|url|1|1"); 293 294 SECTION("disabled") 295 REQUIRE(Remote("name", "url", false, false).toString() == "name|url|0|0"); 296 } 297 298 TEST_CASE("get enabled remotes", M) { 299 RemoteList list; 300 list.add({"hello", "url1", true}); 301 list.add({"world", "url2", false}); 302 303 const std::vector<Remote> array = list.getEnabled(); 304 305 REQUIRE(array.size() == 1); 306 REQUIRE(array[0].name() == "hello"); 307 } 308 309 TEST_CASE("remove remote", M) { 310 const Remote remote{"hello", "url"}; 311 RemoteList list; 312 313 list.add(remote); 314 REQUIRE(list.size() == 1); 315 REQUIRE_FALSE(list.empty()); 316 317 list.remove(remote); 318 REQUIRE(list.size() == 0); 319 REQUIRE(list.empty()); 320 321 list.remove("world"); // no crash 322 } 323 324 TEST_CASE("remove two remotes", M) { 325 RemoteList list; 326 327 list.add({"a_first", "url"}); 328 list.add({"z_second", "url"}); 329 list.add({"b_third", "url"}); 330 REQUIRE(list.size() == 3); 331 332 list.remove("z_second"); 333 REQUIRE(list.size() == 2); 334 335 REQUIRE(list.get("z_first").isNull()); 336 REQUIRE_FALSE(list.get("b_third").isNull()); 337 REQUIRE_FALSE(list.get("a_first").isNull()); 338 } 339 340 TEST_CASE("compare remotes", M) { 341 REQUIRE(Remote("aaa", "aaa") < Remote("bbb", "aaa")); 342 REQUIRE_FALSE(Remote("aaa", "aaa") < Remote("aaa", "bbb")); 343 }