...

Source file src/github.com/letsencrypt/boulder/bdns/servers_test.go

Documentation: github.com/letsencrypt/boulder/bdns

     1  package bdns
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/letsencrypt/boulder/test"
     7  )
     8  
     9  func Test_validateServerAddress(t *testing.T) {
    10  	type args struct {
    11  		server string
    12  	}
    13  	tests := []struct {
    14  		name    string
    15  		args    args
    16  		wantErr bool
    17  	}{
    18  		// ipv4 cases
    19  		{"ipv4 with port", args{"1.1.1.1:53"}, false},
    20  		// sad path
    21  		{"ipv4 without port", args{"1.1.1.1"}, true},
    22  		{"ipv4 port num missing", args{"1.1.1.1:"}, true},
    23  		{"ipv4 string for port", args{"1.1.1.1:foo"}, true},
    24  		{"ipv4 port out of range high", args{"1.1.1.1:65536"}, true},
    25  		{"ipv4 port out of range low", args{"1.1.1.1:0"}, true},
    26  
    27  		// ipv6 cases
    28  		{"ipv6 with port", args{"[2606:4700:4700::1111]:53"}, false},
    29  		// sad path
    30  		{"ipv6 sans brackets", args{"2606:4700:4700::1111:53"}, true},
    31  		{"ipv6 without port", args{"[2606:4700:4700::1111]"}, true},
    32  		{"ipv6 port num missing", args{"[2606:4700:4700::1111]:"}, true},
    33  		{"ipv6 string for port", args{"[2606:4700:4700::1111]:foo"}, true},
    34  		{"ipv6 port out of range high", args{"[2606:4700:4700::1111]:65536"}, true},
    35  		{"ipv6 port out of range low", args{"[2606:4700:4700::1111]:0"}, true},
    36  
    37  		// hostname cases
    38  		{"hostname with port", args{"foo:53"}, false},
    39  		// sad path
    40  		{"hostname without port", args{"foo"}, true},
    41  		{"hostname port num missing", args{"foo:"}, true},
    42  		{"hostname string for port", args{"foo:bar"}, true},
    43  		{"hostname port out of range high", args{"foo:65536"}, true},
    44  		{"hostname port out of range low", args{"foo:0"}, true},
    45  
    46  		// fqdn cases
    47  		{"fqdn with port", args{"bar.foo.baz:53"}, false},
    48  		// sad path
    49  		{"fqdn without port", args{"bar.foo.baz"}, true},
    50  		{"fqdn port num missing", args{"bar.foo.baz:"}, true},
    51  		{"fqdn string for port", args{"bar.foo.baz:bar"}, true},
    52  		{"fqdn port out of range high", args{"bar.foo.baz:65536"}, true},
    53  		{"fqdn port out of range low", args{"bar.foo.baz:0"}, true},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			err := validateServerAddress(tt.args.server)
    58  			if (err != nil) != tt.wantErr {
    59  				t.Errorf("formatServer() error = %v, wantErr %v", err, tt.wantErr)
    60  				return
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func Test_resolveDNSAuthority(t *testing.T) {
    67  	type want struct {
    68  		host string
    69  		port string
    70  	}
    71  	tests := []struct {
    72  		name    string
    73  		target  string
    74  		want    want
    75  		wantErr bool
    76  	}{
    77  		{"IP4 with port", "10.10.10.10:53", want{"10.10.10.10", "53"}, false},
    78  		{"IP4 without port", "10.10.10.10", want{"10.10.10.10", "53"}, false},
    79  		{"IP6 with port and brackets", "[2606:4700:4700::1111]:53", want{"2606:4700:4700::1111", "53"}, false},
    80  		{"IP6 without port", "2606:4700:4700::1111", want{"2606:4700:4700::1111", "53"}, false},
    81  		{"IP6 with brackets without port", "[2606:4700:4700::1111]", want{"2606:4700:4700::1111", "53"}, false},
    82  		{"hostname with port", "localhost:53", want{"localhost", "53"}, false},
    83  		{"hostname without port", "localhost", want{"localhost", "53"}, false},
    84  		{"only port", ":53", want{"localhost", "53"}, false},
    85  		{"hostname with no port after colon", "localhost:", want{"", ""}, true},
    86  		{"IP4 with no port after colon", "10.10.10.10:", want{"", ""}, true},
    87  		{"IP6 with no port after colon", "[2606:4700:4700::1111]:", want{"", ""}, true},
    88  		{"no hostname or port", "", want{"", ""}, true},
    89  		{"invalid addr", "foo:bar:baz", want{"", ""}, true},
    90  	}
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			gotHost, gotPort, gotErr := ParseTarget(tt.target, "53")
    94  			test.AssertEquals(t, gotHost, tt.want.host)
    95  			test.AssertEquals(t, gotPort, tt.want.port)
    96  			if tt.wantErr {
    97  				test.AssertError(t, gotErr, "expected error")
    98  			} else {
    99  				test.AssertNotError(t, gotErr, "unexpected error")
   100  			}
   101  		})
   102  	}
   103  }
   104  

View as plain text