github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/connection/dns_option_test.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package connection 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestNewDNSOption(t *testing.T) { 27 assert := assert.New(t) 28 tests := []struct { 29 input string 30 expect DNSOption 31 expectErr bool 32 }{ 33 {input: "auto", expect: DNSOptionAuto}, 34 {input: "provider", expect: DNSOptionProvider}, 35 {input: "system", expect: DNSOptionSystem}, 36 {input: "1.1.1.1,9.9.9.9", expect: DNSOption("1.1.1.1,9.9.9.9")}, 37 {input: "1.1.1.1", expect: DNSOption("1.1.1.1")}, 38 {input: "", expect: DNSOption("")}, 39 {input: "AA", expectErr: true}, 40 {input: "512.512.512.512", expectErr: true}, 41 {input: "1.1.1.1,512.512.512.512", expectErr: true}, 42 } 43 for i, tt := range tests { 44 option, err := NewDNSOption(tt.input) 45 assert.Equal(tt.expectErr, err != nil, "%v: expected err = %v, actual err = %v", i, tt.expectErr, err) 46 assert.Equal(tt.expect, option) 47 } 48 } 49 50 func TestDNSOption_UnmarshalJSON(t *testing.T) { 51 assert := assert.New(t) 52 tests := []struct { 53 input string 54 expectOption DNSOption 55 expectErr bool 56 }{ 57 {input: "\"auto\"", expectOption: DNSOptionAuto}, 58 {input: "\"provider\"", expectOption: DNSOptionProvider}, 59 {input: "\"system\"", expectOption: DNSOptionSystem}, 60 {input: "\"1.1.1.1,9.9.9.9\"", expectOption: DNSOption("1.1.1.1,9.9.9.9")}, 61 {input: "\"9.9.9.9\"", expectOption: DNSOption("9.9.9.9")}, 62 {input: "\"\"", expectOption: DNSOption("")}, 63 {input: "", expectErr: true}, 64 {input: "\"AA\"", expectErr: true}, 65 {input: "\"512.512.512.512\"", expectErr: true}, 66 {input: "\"1.1.1.1,512.512.512.512\"", expectErr: true}, 67 } 68 for i, tt := range tests { 69 var option DNSOption 70 err := option.UnmarshalJSON([]byte(tt.input)) 71 assert.Equal(tt.expectErr, err != nil, "%v: expected err = %v, actual err = %v", i, tt.expectErr, err) 72 assert.Equal(tt.expectOption, option) 73 } 74 } 75 76 func TestDNSOption_Exact(t *testing.T) { 77 assert := assert.New(t) 78 tests := []struct { 79 option DNSOption 80 expectServers []string 81 expectOK bool 82 }{ 83 {option: DNSOptionAuto, expectOK: false}, 84 {option: DNSOptionProvider, expectOK: false}, 85 {option: DNSOptionSystem, expectOK: false}, 86 {option: DNSOption("1.1.1.1,9.9.9.9"), expectServers: []string{"1.1.1.1", "9.9.9.9"}, expectOK: true}, 87 {option: DNSOption("9.9.9.9"), expectServers: []string{"9.9.9.9"}, expectOK: true}, 88 {option: DNSOption(""), expectServers: nil, expectOK: true}, 89 } 90 for _, tt := range tests { 91 servers, ok := tt.option.Exact() 92 assert.Equal(tt.expectOK, ok) 93 assert.Equal(tt.expectServers, servers) 94 } 95 }