1package urlgetter_test 2 3import ( 4 "crypto/tls" 5 "errors" 6 "net/url" 7 "strings" 8 "testing" 9 10 "github.com/apex/log" 11 "github.com/ooni/probe-engine/experiment/urlgetter" 12 "github.com/ooni/probe-engine/netx" 13 "github.com/ooni/probe-engine/netx/resolver" 14 "github.com/ooni/probe-engine/netx/trace" 15) 16 17func TestConfigurerNewConfigurationVanilla(t *testing.T) { 18 saver := new(trace.Saver) 19 configurer := urlgetter.Configurer{ 20 Logger: log.Log, 21 Saver: saver, 22 } 23 configuration, err := configurer.NewConfiguration() 24 if err != nil { 25 t.Fatal(err) 26 } 27 defer configuration.CloseIdleConnections() 28 if configuration.HTTPConfig.BogonIsError != false { 29 t.Fatal("not the BogonIsError we expected") 30 } 31 if configuration.HTTPConfig.CacheResolutions != true { 32 t.Fatal("not the CacheResolutions we expected") 33 } 34 if configuration.HTTPConfig.ContextByteCounting != true { 35 t.Fatal("not the ContextByteCounting we expected") 36 } 37 if configuration.HTTPConfig.DialSaver != saver { 38 t.Fatal("not the DialSaver we expected") 39 } 40 if configuration.HTTPConfig.HTTPSaver != saver { 41 t.Fatal("not the HTTPSaver we expected") 42 } 43 if configuration.HTTPConfig.Logger != log.Log { 44 t.Fatal("not the Logger we expected") 45 } 46 if configuration.HTTPConfig.ReadWriteSaver != saver { 47 t.Fatal("not the ReadWriteSaver we expected") 48 } 49 if configuration.HTTPConfig.ResolveSaver != saver { 50 t.Fatal("not the ResolveSaver we expected") 51 } 52 if configuration.HTTPConfig.TLSSaver != saver { 53 t.Fatal("not the TLSSaver we expected") 54 } 55 if configuration.HTTPConfig.BaseResolver == nil { 56 t.Fatal("not the BaseResolver we expected") 57 } 58 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 59 t.Fatal("not the TLSConfig we expected") 60 } 61 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 62 t.Fatal("not the TLSConfig we expected") 63 } 64 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 65 t.Fatal("not the TLSConfig we expected") 66 } 67 if configuration.HTTPConfig.NoTLSVerify == true { 68 t.Fatal("not the NoTLSVerify we expected") 69 } 70 if configuration.HTTPConfig.ProxyURL != nil { 71 t.Fatal("not the ProxyURL we expected") 72 } 73} 74 75func TestConfigurerNewConfigurationResolverDNSOverHTTPSPowerdns(t *testing.T) { 76 saver := new(trace.Saver) 77 configurer := urlgetter.Configurer{ 78 Config: urlgetter.Config{ 79 ResolverURL: "doh://google", 80 }, 81 Logger: log.Log, 82 Saver: saver, 83 } 84 configuration, err := configurer.NewConfiguration() 85 if err != nil { 86 t.Fatal(err) 87 } 88 defer configuration.CloseIdleConnections() 89 if configuration.HTTPConfig.BogonIsError != false { 90 t.Fatal("not the BogonIsError we expected") 91 } 92 if configuration.HTTPConfig.CacheResolutions != true { 93 t.Fatal("not the CacheResolutions we expected") 94 } 95 if configuration.HTTPConfig.ContextByteCounting != true { 96 t.Fatal("not the ContextByteCounting we expected") 97 } 98 if configuration.HTTPConfig.DialSaver != saver { 99 t.Fatal("not the DialSaver we expected") 100 } 101 if configuration.HTTPConfig.HTTPSaver != saver { 102 t.Fatal("not the HTTPSaver we expected") 103 } 104 if configuration.HTTPConfig.Logger != log.Log { 105 t.Fatal("not the Logger we expected") 106 } 107 if configuration.HTTPConfig.ReadWriteSaver != saver { 108 t.Fatal("not the ReadWriteSaver we expected") 109 } 110 if configuration.HTTPConfig.ResolveSaver != saver { 111 t.Fatal("not the ResolveSaver we expected") 112 } 113 if configuration.HTTPConfig.TLSSaver != saver { 114 t.Fatal("not the TLSSaver we expected") 115 } 116 if configuration.HTTPConfig.BaseResolver == nil { 117 t.Fatal("not the BaseResolver we expected") 118 } 119 sr, ok := configuration.HTTPConfig.BaseResolver.(resolver.SerialResolver) 120 if !ok { 121 t.Fatal("not the resolver we expected") 122 } 123 stxp, ok := sr.Txp.(resolver.SaverDNSTransport) 124 if !ok { 125 t.Fatal("not the DNS transport we expected") 126 } 127 dohtxp, ok := stxp.RoundTripper.(resolver.DNSOverHTTPS) 128 if !ok { 129 t.Fatal("not the DNS transport we expected") 130 } 131 if dohtxp.URL != "https://dns.google/dns-query" { 132 t.Fatal("not the DoH URL we expected") 133 } 134 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 135 t.Fatal("not the TLSConfig we expected") 136 } 137 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 138 t.Fatal("not the TLSConfig we expected") 139 } 140 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 141 t.Fatal("not the TLSConfig we expected") 142 } 143 if configuration.HTTPConfig.NoTLSVerify == true { 144 t.Fatal("not the NoTLSVerify we expected") 145 } 146 if configuration.HTTPConfig.ProxyURL != nil { 147 t.Fatal("not the ProxyURL we expected") 148 } 149} 150 151func TestConfigurerNewConfigurationResolverDNSOverHTTPSGoogle(t *testing.T) { 152 saver := new(trace.Saver) 153 configurer := urlgetter.Configurer{ 154 Config: urlgetter.Config{ 155 ResolverURL: "doh://google", 156 }, 157 Logger: log.Log, 158 Saver: saver, 159 } 160 configuration, err := configurer.NewConfiguration() 161 if err != nil { 162 t.Fatal(err) 163 } 164 defer configuration.CloseIdleConnections() 165 if configuration.HTTPConfig.BogonIsError != false { 166 t.Fatal("not the BogonIsError we expected") 167 } 168 if configuration.HTTPConfig.CacheResolutions != true { 169 t.Fatal("not the CacheResolutions we expected") 170 } 171 if configuration.HTTPConfig.ContextByteCounting != true { 172 t.Fatal("not the ContextByteCounting we expected") 173 } 174 if configuration.HTTPConfig.DialSaver != saver { 175 t.Fatal("not the DialSaver we expected") 176 } 177 if configuration.HTTPConfig.HTTPSaver != saver { 178 t.Fatal("not the HTTPSaver we expected") 179 } 180 if configuration.HTTPConfig.Logger != log.Log { 181 t.Fatal("not the Logger we expected") 182 } 183 if configuration.HTTPConfig.ReadWriteSaver != saver { 184 t.Fatal("not the ReadWriteSaver we expected") 185 } 186 if configuration.HTTPConfig.ResolveSaver != saver { 187 t.Fatal("not the ResolveSaver we expected") 188 } 189 if configuration.HTTPConfig.TLSSaver != saver { 190 t.Fatal("not the TLSSaver we expected") 191 } 192 if configuration.HTTPConfig.BaseResolver == nil { 193 t.Fatal("not the BaseResolver we expected") 194 } 195 sr, ok := configuration.HTTPConfig.BaseResolver.(resolver.SerialResolver) 196 if !ok { 197 t.Fatal("not the resolver we expected") 198 } 199 stxp, ok := sr.Txp.(resolver.SaverDNSTransport) 200 if !ok { 201 t.Fatal("not the DNS transport we expected") 202 } 203 dohtxp, ok := stxp.RoundTripper.(resolver.DNSOverHTTPS) 204 if !ok { 205 t.Fatal("not the DNS transport we expected") 206 } 207 if dohtxp.URL != "https://dns.google/dns-query" { 208 t.Fatal("not the DoH URL we expected") 209 } 210 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 211 t.Fatal("not the TLSConfig we expected") 212 } 213 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 214 t.Fatal("not the TLSConfig we expected") 215 } 216 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 217 t.Fatal("not the TLSConfig we expected") 218 } 219 if configuration.HTTPConfig.NoTLSVerify == true { 220 t.Fatal("not the NoTLSVerify we expected") 221 } 222 if configuration.HTTPConfig.ProxyURL != nil { 223 t.Fatal("not the ProxyURL we expected") 224 } 225} 226 227func TestConfigurerNewConfigurationResolverDNSOverHTTPSCloudflare(t *testing.T) { 228 saver := new(trace.Saver) 229 configurer := urlgetter.Configurer{ 230 Config: urlgetter.Config{ 231 ResolverURL: "doh://cloudflare", 232 }, 233 Logger: log.Log, 234 Saver: saver, 235 } 236 configuration, err := configurer.NewConfiguration() 237 if err != nil { 238 t.Fatal(err) 239 } 240 defer configuration.CloseIdleConnections() 241 if configuration.HTTPConfig.BogonIsError != false { 242 t.Fatal("not the BogonIsError we expected") 243 } 244 if configuration.HTTPConfig.CacheResolutions != true { 245 t.Fatal("not the CacheResolutions we expected") 246 } 247 if configuration.HTTPConfig.ContextByteCounting != true { 248 t.Fatal("not the ContextByteCounting we expected") 249 } 250 if configuration.HTTPConfig.DialSaver != saver { 251 t.Fatal("not the DialSaver we expected") 252 } 253 if configuration.HTTPConfig.HTTPSaver != saver { 254 t.Fatal("not the HTTPSaver we expected") 255 } 256 if configuration.HTTPConfig.Logger != log.Log { 257 t.Fatal("not the Logger we expected") 258 } 259 if configuration.HTTPConfig.ReadWriteSaver != saver { 260 t.Fatal("not the ReadWriteSaver we expected") 261 } 262 if configuration.HTTPConfig.ResolveSaver != saver { 263 t.Fatal("not the ResolveSaver we expected") 264 } 265 if configuration.HTTPConfig.TLSSaver != saver { 266 t.Fatal("not the TLSSaver we expected") 267 } 268 if configuration.HTTPConfig.BaseResolver == nil { 269 t.Fatal("not the BaseResolver we expected") 270 } 271 sr, ok := configuration.HTTPConfig.BaseResolver.(resolver.SerialResolver) 272 if !ok { 273 t.Fatal("not the resolver we expected") 274 } 275 stxp, ok := sr.Txp.(resolver.SaverDNSTransport) 276 if !ok { 277 t.Fatal("not the DNS transport we expected") 278 } 279 dohtxp, ok := stxp.RoundTripper.(resolver.DNSOverHTTPS) 280 if !ok { 281 t.Fatal("not the DNS transport we expected") 282 } 283 if dohtxp.URL != "https://cloudflare-dns.com/dns-query" { 284 t.Fatal("not the DoH URL we expected") 285 } 286 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 287 t.Fatal("not the TLSConfig we expected") 288 } 289 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 290 t.Fatal("not the TLSConfig we expected") 291 } 292 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 293 t.Fatal("not the TLSConfig we expected") 294 } 295 if configuration.HTTPConfig.NoTLSVerify == true { 296 t.Fatal("not the NoTLSVerify we expected") 297 } 298 if configuration.HTTPConfig.ProxyURL != nil { 299 t.Fatal("not the ProxyURL we expected") 300 } 301} 302 303func TestConfigurerNewConfigurationResolverUDP(t *testing.T) { 304 saver := new(trace.Saver) 305 configurer := urlgetter.Configurer{ 306 Config: urlgetter.Config{ 307 ResolverURL: "udp://8.8.8.8:53", 308 }, 309 Logger: log.Log, 310 Saver: saver, 311 } 312 configuration, err := configurer.NewConfiguration() 313 if err != nil { 314 t.Fatal(err) 315 } 316 defer configuration.CloseIdleConnections() 317 if configuration.HTTPConfig.BogonIsError != false { 318 t.Fatal("not the BogonIsError we expected") 319 } 320 if configuration.HTTPConfig.CacheResolutions != true { 321 t.Fatal("not the CacheResolutions we expected") 322 } 323 if configuration.HTTPConfig.ContextByteCounting != true { 324 t.Fatal("not the ContextByteCounting we expected") 325 } 326 if configuration.HTTPConfig.DialSaver != saver { 327 t.Fatal("not the DialSaver we expected") 328 } 329 if configuration.HTTPConfig.HTTPSaver != saver { 330 t.Fatal("not the HTTPSaver we expected") 331 } 332 if configuration.HTTPConfig.Logger != log.Log { 333 t.Fatal("not the Logger we expected") 334 } 335 if configuration.HTTPConfig.ReadWriteSaver != saver { 336 t.Fatal("not the ReadWriteSaver we expected") 337 } 338 if configuration.HTTPConfig.ResolveSaver != saver { 339 t.Fatal("not the ResolveSaver we expected") 340 } 341 if configuration.HTTPConfig.TLSSaver != saver { 342 t.Fatal("not the TLSSaver we expected") 343 } 344 if configuration.HTTPConfig.BaseResolver == nil { 345 t.Fatal("not the BaseResolver we expected") 346 } 347 sr, ok := configuration.HTTPConfig.BaseResolver.(resolver.SerialResolver) 348 if !ok { 349 t.Fatal("not the resolver we expected") 350 } 351 stxp, ok := sr.Txp.(resolver.SaverDNSTransport) 352 if !ok { 353 t.Fatal("not the DNS transport we expected") 354 } 355 udptxp, ok := stxp.RoundTripper.(resolver.DNSOverUDP) 356 if !ok { 357 t.Fatal("not the DNS transport we expected") 358 } 359 if udptxp.Address() != "8.8.8.8:53" { 360 t.Fatal("not the DoH URL we expected") 361 } 362 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 363 t.Fatal("not the TLSConfig we expected") 364 } 365 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 366 t.Fatal("not the TLSConfig we expected") 367 } 368 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 369 t.Fatal("not the TLSConfig we expected") 370 } 371 if configuration.HTTPConfig.NoTLSVerify == true { 372 t.Fatal("not the NoTLSVerify we expected") 373 } 374 if configuration.HTTPConfig.ProxyURL != nil { 375 t.Fatal("not the ProxyURL we expected") 376 } 377} 378 379func TestConfigurerNewConfigurationDNSCacheInvalidString(t *testing.T) { 380 saver := new(trace.Saver) 381 configurer := urlgetter.Configurer{ 382 Config: urlgetter.Config{ 383 DNSCache: "a", 384 }, 385 Logger: log.Log, 386 Saver: saver, 387 } 388 _, err := configurer.NewConfiguration() 389 if err == nil || !strings.HasSuffix(err.Error(), "invalid DNSCache string") { 390 t.Fatal("not the error we expected") 391 } 392} 393 394func TestConfigurerNewConfigurationDNSCacheNotDomain(t *testing.T) { 395 saver := new(trace.Saver) 396 configurer := urlgetter.Configurer{ 397 Config: urlgetter.Config{ 398 DNSCache: "b b", 399 }, 400 Logger: log.Log, 401 Saver: saver, 402 } 403 _, err := configurer.NewConfiguration() 404 if err == nil || !strings.HasSuffix(err.Error(), "invalid domain in DNSCache") { 405 t.Fatal("not the error we expected") 406 } 407} 408 409func TestConfigurerNewConfigurationDNSCacheNotIP(t *testing.T) { 410 saver := new(trace.Saver) 411 configurer := urlgetter.Configurer{ 412 Config: urlgetter.Config{ 413 DNSCache: "x.org b", 414 }, 415 Logger: log.Log, 416 Saver: saver, 417 } 418 _, err := configurer.NewConfiguration() 419 if err == nil || !strings.HasSuffix(err.Error(), "invalid IP in DNSCache") { 420 t.Fatal("not the error we expected") 421 } 422} 423 424func TestConfigurerNewConfigurationDNSCacheGood(t *testing.T) { 425 saver := new(trace.Saver) 426 configurer := urlgetter.Configurer{ 427 Config: urlgetter.Config{ 428 DNSCache: "dns.google.com 8.8.8.8 8.8.4.4", 429 }, 430 Logger: log.Log, 431 Saver: saver, 432 } 433 configuration, err := configurer.NewConfiguration() 434 if err != nil { 435 t.Fatal(err) 436 } 437 if len(configuration.HTTPConfig.DNSCache) != 1 { 438 t.Fatal("invalid number of entries in DNSCache") 439 } 440 if len(configuration.HTTPConfig.DNSCache["dns.google.com"]) != 2 { 441 t.Fatal("invalid number of IPs saved in DNSCache") 442 } 443 if configuration.HTTPConfig.DNSCache["dns.google.com"][0] != "8.8.8.8" { 444 t.Fatal("invalid IPs saved in DNSCache") 445 } 446 if configuration.HTTPConfig.DNSCache["dns.google.com"][1] != "8.8.4.4" { 447 t.Fatal("invalid IPs saved in DNSCache") 448 } 449} 450 451func TestConfigurerNewConfigurationResolverInvalidURL(t *testing.T) { 452 saver := new(trace.Saver) 453 configurer := urlgetter.Configurer{ 454 Config: urlgetter.Config{ 455 ResolverURL: "\t", 456 }, 457 Logger: log.Log, 458 Saver: saver, 459 } 460 _, err := configurer.NewConfiguration() 461 if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") { 462 t.Fatal("not the error we expected") 463 } 464} 465 466func TestConfigurerNewConfigurationResolverInvalidURLScheme(t *testing.T) { 467 saver := new(trace.Saver) 468 configurer := urlgetter.Configurer{ 469 Config: urlgetter.Config{ 470 ResolverURL: "antani://8.8.8.8:53", 471 }, 472 Logger: log.Log, 473 Saver: saver, 474 } 475 _, err := configurer.NewConfiguration() 476 if err == nil || !strings.HasSuffix(err.Error(), "unsupported resolver scheme") { 477 t.Fatal("not the error we expected") 478 } 479} 480 481func TestConfigurerNewConfigurationTLSServerName(t *testing.T) { 482 saver := new(trace.Saver) 483 configurer := urlgetter.Configurer{ 484 Config: urlgetter.Config{ 485 TLSServerName: "www.x.org", 486 }, 487 Logger: log.Log, 488 Saver: saver, 489 } 490 configuration, err := configurer.NewConfiguration() 491 if err != nil { 492 t.Fatal(err) 493 } 494 if configuration.HTTPConfig.TLSConfig.ServerName != "www.x.org" { 495 t.Fatal("invalid ServerName") 496 } 497 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 498 t.Fatal("invalid len(NextProtos)") 499 } 500 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 501 t.Fatal("invalid NextProtos[0]") 502 } 503 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 504 t.Fatal("invalid NextProtos[1]") 505 } 506} 507 508func TestConfigurerNewConfigurationNoTLSVerify(t *testing.T) { 509 saver := new(trace.Saver) 510 configurer := urlgetter.Configurer{ 511 Config: urlgetter.Config{ 512 NoTLSVerify: true, 513 }, 514 Logger: log.Log, 515 Saver: saver, 516 } 517 configuration, err := configurer.NewConfiguration() 518 if err != nil { 519 t.Fatal(err) 520 } 521 if configuration.HTTPConfig.NoTLSVerify != true { 522 t.Fatal("not the NoTLSVerify we expected") 523 } 524} 525 526func TestConfigurerNewConfigurationTLSv1(t *testing.T) { 527 saver := new(trace.Saver) 528 configurer := urlgetter.Configurer{ 529 Config: urlgetter.Config{ 530 TLSVersion: "TLSv1", 531 }, 532 Logger: log.Log, 533 Saver: saver, 534 } 535 configuration, err := configurer.NewConfiguration() 536 if err != nil { 537 t.Fatal(err) 538 } 539 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 540 t.Fatal("invalid len(NextProtos)") 541 } 542 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 543 t.Fatal("invalid NextProtos[0]") 544 } 545 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 546 t.Fatal("invalid NextProtos[1]") 547 } 548 if configuration.HTTPConfig.TLSConfig.MinVersion != tls.VersionTLS10 { 549 t.Fatal("invalid MinVersion") 550 } 551 if configuration.HTTPConfig.TLSConfig.MaxVersion != tls.VersionTLS10 { 552 t.Fatal("invalid MaxVersion") 553 } 554} 555 556func TestConfigurerNewConfigurationTLSv1dot0(t *testing.T) { 557 saver := new(trace.Saver) 558 configurer := urlgetter.Configurer{ 559 Config: urlgetter.Config{ 560 TLSVersion: "TLSv1.0", 561 }, 562 Logger: log.Log, 563 Saver: saver, 564 } 565 configuration, err := configurer.NewConfiguration() 566 if err != nil { 567 t.Fatal(err) 568 } 569 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 570 t.Fatal("invalid len(NextProtos)") 571 } 572 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 573 t.Fatal("invalid NextProtos[0]") 574 } 575 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 576 t.Fatal("invalid NextProtos[1]") 577 } 578 if configuration.HTTPConfig.TLSConfig.MinVersion != tls.VersionTLS10 { 579 t.Fatal("invalid MinVersion") 580 } 581 if configuration.HTTPConfig.TLSConfig.MaxVersion != tls.VersionTLS10 { 582 t.Fatal("invalid MaxVersion") 583 } 584} 585 586func TestConfigurerNewConfigurationTLSv1dot1(t *testing.T) { 587 saver := new(trace.Saver) 588 configurer := urlgetter.Configurer{ 589 Config: urlgetter.Config{ 590 TLSVersion: "TLSv1.1", 591 }, 592 Logger: log.Log, 593 Saver: saver, 594 } 595 configuration, err := configurer.NewConfiguration() 596 if err != nil { 597 t.Fatal(err) 598 } 599 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 600 t.Fatal("invalid len(NextProtos)") 601 } 602 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 603 t.Fatal("invalid NextProtos[0]") 604 } 605 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 606 t.Fatal("invalid NextProtos[1]") 607 } 608 if configuration.HTTPConfig.TLSConfig.MinVersion != tls.VersionTLS11 { 609 t.Fatal("invalid MinVersion") 610 } 611 if configuration.HTTPConfig.TLSConfig.MaxVersion != tls.VersionTLS11 { 612 t.Fatal("invalid MaxVersion") 613 } 614} 615 616func TestConfigurerNewConfigurationTLSv1dot2(t *testing.T) { 617 saver := new(trace.Saver) 618 configurer := urlgetter.Configurer{ 619 Config: urlgetter.Config{ 620 TLSVersion: "TLSv1.2", 621 }, 622 Logger: log.Log, 623 Saver: saver, 624 } 625 configuration, err := configurer.NewConfiguration() 626 if err != nil { 627 t.Fatal(err) 628 } 629 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 630 t.Fatal("invalid len(NextProtos)") 631 } 632 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 633 t.Fatal("invalid NextProtos[0]") 634 } 635 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 636 t.Fatal("invalid NextProtos[1]") 637 } 638 if configuration.HTTPConfig.TLSConfig.MinVersion != tls.VersionTLS12 { 639 t.Fatal("invalid MinVersion") 640 } 641 if configuration.HTTPConfig.TLSConfig.MaxVersion != tls.VersionTLS12 { 642 t.Fatal("invalid MaxVersion") 643 } 644} 645 646func TestConfigurerNewConfigurationTLSv1dot3(t *testing.T) { 647 saver := new(trace.Saver) 648 configurer := urlgetter.Configurer{ 649 Config: urlgetter.Config{ 650 TLSVersion: "TLSv1.3", 651 }, 652 Logger: log.Log, 653 Saver: saver, 654 } 655 configuration, err := configurer.NewConfiguration() 656 if err != nil { 657 t.Fatal(err) 658 } 659 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 660 t.Fatal("invalid len(NextProtos)") 661 } 662 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 663 t.Fatal("invalid NextProtos[0]") 664 } 665 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 666 t.Fatal("invalid NextProtos[1]") 667 } 668 if configuration.HTTPConfig.TLSConfig.MinVersion != tls.VersionTLS13 { 669 t.Fatal("invalid MinVersion") 670 } 671 if configuration.HTTPConfig.TLSConfig.MaxVersion != tls.VersionTLS13 { 672 t.Fatal("invalid MaxVersion") 673 } 674} 675 676func TestConfigurerNewConfigurationTLSvDefault(t *testing.T) { 677 saver := new(trace.Saver) 678 configurer := urlgetter.Configurer{ 679 Config: urlgetter.Config{}, 680 Logger: log.Log, 681 Saver: saver, 682 } 683 configuration, err := configurer.NewConfiguration() 684 if err != nil { 685 t.Fatal(err) 686 } 687 if len(configuration.HTTPConfig.TLSConfig.NextProtos) != 2 { 688 t.Fatal("invalid len(NextProtos)") 689 } 690 if configuration.HTTPConfig.TLSConfig.NextProtos[0] != "h2" { 691 t.Fatal("invalid NextProtos[0]") 692 } 693 if configuration.HTTPConfig.TLSConfig.NextProtos[1] != "http/1.1" { 694 t.Fatal("invalid NextProtos[1]") 695 } 696 if configuration.HTTPConfig.TLSConfig.MinVersion != 0 { 697 t.Fatal("invalid MinVersion") 698 } 699 if configuration.HTTPConfig.TLSConfig.MaxVersion != 0 { 700 t.Fatal("invalid MaxVersion") 701 } 702} 703 704func TestConfigurerNewConfigurationTLSvInvalid(t *testing.T) { 705 saver := new(trace.Saver) 706 configurer := urlgetter.Configurer{ 707 Config: urlgetter.Config{ 708 TLSVersion: "SSLv3", 709 }, 710 Logger: log.Log, 711 Saver: saver, 712 } 713 _, err := configurer.NewConfiguration() 714 if !errors.Is(err, netx.ErrInvalidTLSVersion) { 715 t.Fatalf("not the error we expected: %+v", err) 716 } 717} 718 719func TestConfigurerNewConfigurationProxyURL(t *testing.T) { 720 URL, _ := url.Parse("socks5://127.0.0.1:9050") 721 saver := new(trace.Saver) 722 configurer := urlgetter.Configurer{ 723 Logger: log.Log, 724 Saver: saver, 725 ProxyURL: URL, 726 } 727 configuration, err := configurer.NewConfiguration() 728 if err != nil { 729 t.Fatal(err) 730 } 731 if configuration.HTTPConfig.ProxyURL != URL { 732 t.Fatal("invalid ProxyURL") 733 } 734} 735