1// Copyright (C) 2019 Storj Labs, Inc. 2// See LICENSE for copying information. 3 4package trust_test 5 6import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "storj.io/storj/storagenode/trust" 14) 15 16func TestNewExcluderFailure(t *testing.T) { 17 for _, tt := range []struct { 18 name string 19 config string 20 errs []string 21 }{ 22 { 23 name: "not a valid URL", 24 config: "://", 25 errs: []string{ 26 `exclusion: node URL error: parse ://: missing protocol scheme`, 27 `exclusion: node URL error: parse "://": missing protocol scheme`, 28 `exclusion: node URL: parse ://: missing protocol scheme`, 29 `exclusion: node URL: parse "://": missing protocol scheme`, 30 }, 31 }, 32 { 33 name: "host exclusion must not include a port", 34 config: "bar.test:7777", 35 errs: []string{"exclusion: host exclusion must not include a port"}, 36 }, 37 { 38 name: "satellite URL exclusion must specify a port", 39 config: "121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@bar.test", 40 errs: []string{"exclusion: satellite URL exclusion must specify a port"}, 41 }, 42 } { 43 tt := tt // quiet linting 44 t.Run(tt.name, func(t *testing.T) { 45 _, err := trust.NewExcluder(tt.config) 46 require.Error(t, err) 47 require.Contains(t, tt.errs, err.Error()) 48 }) 49 } 50 51} 52 53func TestNewExcluder(t *testing.T) { 54 goodURL := makeSatelliteURL("foo.test") 55 badURL := makeSatelliteURL("b.bar.test") 56 57 for _, tt := range []struct { 58 name string 59 config string 60 }{ 61 { 62 name: "filtered by id", 63 config: badURL.ID.String() + "@", 64 }, 65 { 66 name: "filtered by root domain", 67 config: "bar.test", 68 }, 69 { 70 name: "filtered by exact domain", 71 config: "b.bar.test", 72 }, 73 { 74 name: "filtered by full url", 75 config: badURL.String(), 76 }, 77 } { 78 tt := tt // quiet linting 79 t.Run(tt.name, func(t *testing.T) { 80 excluder, err := trust.NewExcluder(tt.config) 81 require.NoError(t, err) 82 83 assert.True(t, excluder.IsTrusted(goodURL), "good URL should not be excluded") 84 assert.False(t, excluder.IsTrusted(badURL), "bad URL should be excluded") 85 }) 86 } 87} 88 89func TestHostExcluder(t *testing.T) { 90 for _, tt := range []struct { 91 exclusion string 92 host string 93 isTrusted bool 94 }{ 95 { 96 exclusion: "foo.test", 97 host: "foo.test", 98 isTrusted: false, 99 }, 100 { 101 exclusion: "foo.test", 102 host: "x.foo.test", 103 isTrusted: false, 104 }, 105 { 106 exclusion: "foo.test", 107 host: ".foo.test", 108 isTrusted: false, 109 }, 110 { 111 exclusion: "foo.test", 112 host: "foo.test.", 113 isTrusted: false, 114 }, 115 { 116 exclusion: "x.bar.test", 117 host: "bar.test", 118 isTrusted: true, 119 }, 120 { 121 exclusion: "x.bar.test", 122 host: "x.bar.test", 123 isTrusted: false, 124 }, 125 { 126 exclusion: "x.bar.test", 127 host: "y.x.bar.test", 128 isTrusted: false, 129 }, 130 { 131 exclusion: ".baz.test", 132 host: "baz.test", 133 isTrusted: false, 134 }, 135 { 136 exclusion: "baz.test.", 137 host: "baz.test", 138 isTrusted: false, 139 }, 140 { 141 exclusion: "satellite", 142 host: "satellite", 143 isTrusted: false, 144 }, 145 { 146 exclusion: "satellite", 147 host: "x.satellite", 148 isTrusted: true, 149 }, 150 } { 151 tt := tt // quiet linting 152 name := fmt.Sprintf("%s-%s-%t", tt.exclusion, tt.host, tt.isTrusted) 153 t.Run(name, func(t *testing.T) { 154 excluder := trust.NewHostExcluder(tt.exclusion) 155 isTrusted := excluder.IsTrusted(trust.SatelliteURL{Host: tt.host}) 156 assert.Equal(t, tt.isTrusted, isTrusted) 157 }) 158 } 159} 160