@@ -462,40 +462,126 @@ func TestValidateRuntime(t *testing.T) {
462
462
}
463
463
464
464
func TestValidatePorts (t * testing.T ) {
465
+ isMicrosoftWSL := detect .IsMicrosoftWSL ()
465
466
type portTest struct {
467
+ // isTarget indicates whether or not the test case is covered
468
+ // because validatePorts behaves differently depending on whether process is running in WSL in windows or not.
469
+ isTarget bool
466
470
ports []string
467
471
errorMsg string
468
472
}
469
473
var tests = []portTest {
470
474
{
471
- ports : []string {"test:80" },
472
- errorMsg : "Sorry, one of the ports provided with --ports flag is not valid [test:80]" ,
475
+ isTarget : true ,
476
+ ports : []string {"8080:80" },
477
+ errorMsg : "" ,
478
+ },
479
+ {
480
+ isTarget : true ,
481
+ ports : []string {"8080:80/tcp" , "8080:80/udp" },
482
+ errorMsg : "" ,
483
+ },
484
+ {
485
+ isTarget : true ,
486
+ ports : []string {"test:8080" },
487
+ errorMsg : "Sorry, one of the ports provided with --ports flag is not valid [test:8080] (Invalid hostPort: test)" ,
473
488
},
474
489
{
490
+ isTarget : true ,
475
491
ports : []string {"0:80" },
476
492
errorMsg : "Sorry, one of the ports provided with --ports flag is outside range [0:80]" ,
477
493
},
478
494
{
479
- ports : []string {"8080:80" , "6443:443" },
495
+ isTarget : true ,
496
+ ports : []string {"0:80/tcp" },
497
+ errorMsg : "Sorry, one of the ports provided with --ports flag is outside range [0:80/tcp]" ,
498
+ },
499
+ {
500
+ isTarget : true ,
501
+ ports : []string {"65536:80/udp" },
502
+ errorMsg : "Sorry, one of the ports provided with --ports flag is not valid [65536:80/udp] (Invalid hostPort: 65536)" ,
503
+ },
504
+ {
505
+ isTarget : true ,
506
+ ports : []string {"0-1:80-81/tcp" },
507
+ errorMsg : "Sorry, one of the ports provided with --ports flag is outside range [0-1:80-81/tcp]" ,
508
+ },
509
+ {
510
+ isTarget : true ,
511
+ ports : []string {"0-1:80-81/udp" },
512
+ errorMsg : "Sorry, one of the ports provided with --ports flag is outside range [0-1:80-81/udp]" ,
513
+ },
514
+ {
515
+ isTarget : ! isMicrosoftWSL ,
516
+ ports : []string {"80:80" , "1023-1025:8023-8025" , "1023-1025:8023-8025/tcp" , "1023-1025:8023-8025/udp" },
480
517
errorMsg : "" ,
481
518
},
482
519
{
483
- ports : []string {"127.0.0.1:80:80" },
520
+ isTarget : isMicrosoftWSL ,
521
+ ports : []string {"80:80" },
522
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [80:80]" ,
523
+ },
524
+ {
525
+ isTarget : isMicrosoftWSL ,
526
+ ports : []string {"1023-1025:8023-8025" },
527
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025]" ,
528
+ },
529
+ {
530
+ isTarget : isMicrosoftWSL ,
531
+ ports : []string {"1023-1025:8023-8025/tcp" },
532
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025/tcp]" ,
533
+ },
534
+ {
535
+ isTarget : isMicrosoftWSL ,
536
+ ports : []string {"1023-1025:8023-8025/udp" },
537
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025/udp]" ,
538
+ },
539
+ {
540
+ isTarget : true ,
541
+ ports : []string {"127.0.0.1:8080:80" , "127.0.0.1:8081:80/tcp" , "127.0.0.1:8081:80/udp" , "127.0.0.1:8082-8083:8082-8083/tcp" },
484
542
errorMsg : "" ,
485
543
},
486
544
{
545
+ isTarget : true ,
487
546
ports : []string {"1000.0.0.1:80:80" },
488
- errorMsg : "Sorry, the IP address provided with --ports flag is invalid: 1000.0.0.1" ,
547
+ errorMsg : "Sorry, one of the ports provided with --ports flag is not valid [1000.0.0.1:80:80] (Invalid ip address: 1000.0.0.1)" ,
548
+ },
549
+ {
550
+ isTarget : ! isMicrosoftWSL ,
551
+ ports : []string {"127.0.0.1:80:80" , "127.0.0.1:81:81/tcp" , "127.0.0.1:81:81/udp" , "127.0.0.1:82-83:82-83/tcp" , "127.0.0.1:82-83:82-83/udp" },
552
+ errorMsg : "" ,
553
+ },
554
+ {
555
+ isTarget : isMicrosoftWSL ,
556
+ ports : []string {"127.0.0.1:80:80" },
557
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80:80]" ,
558
+ },
559
+ {
560
+ isTarget : isMicrosoftWSL ,
561
+ ports : []string {"127.0.0.1:81:81/tcp" },
562
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:81:81/tcp]" ,
563
+ },
564
+ {
565
+ isTarget : isMicrosoftWSL ,
566
+ ports : []string {"127.0.0.1:81:81/udp" },
567
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:81:81/udp]" ,
568
+ },
569
+ {
570
+ isTarget : isMicrosoftWSL ,
571
+ ports : []string {"127.0.0.1:80-83:80-83/tcp" },
572
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80-83:80-83/tcp]" ,
573
+ },
574
+ {
575
+ isTarget : isMicrosoftWSL ,
576
+ ports : []string {"127.0.0.1:80-83:80-83/udp" },
577
+ errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80-83:80-83/udp]" ,
489
578
},
490
- }
491
- if detect .IsMicrosoftWSL () {
492
- tests = append (tests , portTest {
493
- ports : []string {"80:80" },
494
- errorMsg : "Sorry, you cannot use privileged ports on the host (below 1024) [80:80]" ,
495
- })
496
579
}
497
580
for _ , test := range tests {
498
581
t .Run (strings .Join (test .ports , "," ), func (t * testing.T ) {
582
+ if ! test .isTarget {
583
+ return
584
+ }
499
585
gotError := ""
500
586
got := validatePorts (test .ports )
501
587
if got != nil {
0 commit comments