| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | package dnshosts |
| 30 | |
| 31 | import ( |
| 32 | "fmt" |
| 33 | "net" |
| 34 | "slices" |
| 35 | "strings" |
| 36 | "testing" |
| 37 | |
| 38 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 39 | ) |
| 40 | |
| 41 | func Test_hostsMapIP(t *testing.T) { |
| 42 | hosts := hostMap{} |
| 43 | hosts["sample"] = "1.1.1.1" |
| 44 | hosts["another.sample"] = "1.2.2.1" |
| 45 | hosts["google.com"] = "8.8.8.8" |
| 46 | hosts["google.ae"] = "google.com" |
| 47 | hosts["google.ie"] = "google.ae" |
| 48 | |
| 49 | tests := []struct { |
| 50 | host string |
| 51 | want net.IP |
| 52 | }{ |
| 53 | {host: "sample", want: net.ParseIP("1.1.1.1")}, |
| 54 | {host: "another.sample", want: net.ParseIP("1.2.2.1")}, |
| 55 | {host: "google.com", want: net.ParseIP("8.8.8.8")}, |
| 56 | {host: "google.ae", want: net.ParseIP("8.8.8.8")}, |
| 57 | {host: "google.ie", want: net.ParseIP("8.8.8.8")}, |
| 58 | {host: "google.sample", want: nil}, |
| 59 | } |
| 60 | for i, tt := range tests { |
| 61 | t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 62 | got := hosts.hostIP(tt.host) |
| 63 | if !got.Equal(tt.want) { |
| 64 | t.Errorf("hostsMapIP() = %v, want %v", got, tt.want) |
| 65 | return |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func Test_zoneHost(t *testing.T) { |
| 72 | type val struct { |
| 73 | name string |
| 74 | recordName string |
| 75 | } |
| 76 | tests := []struct { |
| 77 | host zoneHost |
| 78 | want val |
| 79 | }{ |
| 80 | {}, |
| 81 | {host: "sample", want: val{name: "sample"}}, |
| 82 | {host: "another.sample", want: val{name: "sample.", recordName: "another"}}, |
| 83 | {host: "another.sample.com", want: val{name: "com.", recordName: "another.sample"}}, |
| 84 | {host: "a.c", want: val{name: "c.", recordName: "a"}}, |
| 85 | {host: "a.b.c.d", want: val{name: "d.", recordName: "a.b.c"}}, |
| 86 | } |
| 87 | for i, tt := range tests { |
| 88 | t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 89 | got := val{ |
| 90 | name: tt.host.name(), |
| 91 | recordName: tt.host.recordName(), |
| 92 | } |
| 93 | if got != tt.want { |
| 94 | t.Errorf("host = %+v, want %+v", got, tt.want) |
| 95 | return |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestExtractZones(t *testing.T) { |
| 102 | equalZones := func(za, zb []types.Zone) bool { |
| 103 | equal := func(a, b types.Zone) bool { |
| 104 | if a.Name != b.Name { |
| 105 | return false |
| 106 | } |
| 107 | if !a.DefaultIP.Equal(b.DefaultIP) { |
| 108 | return false |
| 109 | } |
| 110 | for i := range a.Records { |
| 111 | a, b := a.Records[i], b.Records[i] |
| 112 | if !a.IP.Equal(b.IP) { |
| 113 | return false |
| 114 | } |
| 115 | if a.Name != b.Name { |
| 116 | return false |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return true |
| 121 | } |
| 122 | |
| 123 | for _, a := range za { |
| 124 | ib := slices.IndexFunc(zb, func(z types.Zone) bool { return z.Name == a.Name }) |
| 125 | if ib < 0 { |
| 126 | return false |
| 127 | } |
| 128 | b := zb[ib] |
| 129 | if !equal(a, b) { |
| 130 | return false |
| 131 | } |
| 132 | } |
| 133 | return true |
| 134 | } |
| 135 | |
| 136 | hosts := hostMap{ |
| 137 | "google.com": "8.8.4.4", |
| 138 | "local.google.com": "8.8.8.8", |
| 139 | "google.ae": "google.com", |
| 140 | "localhost": "127.0.0.1", |
| 141 | "host.lima.internal": "192.168.5.2", |
| 142 | "host.docker.internal": "host.lima.internal", |
| 143 | } |
| 144 | |
| 145 | tests := []struct { |
| 146 | wantZones []types.Zone |
| 147 | }{ |
| 148 | { |
| 149 | wantZones: []types.Zone{ |
| 150 | { |
| 151 | Name: "ae.", |
| 152 | Records: []types.Record{ |
| 153 | {Name: "google", IP: net.ParseIP("8.8.4.4")}, |
| 154 | }, |
| 155 | }, |
| 156 | { |
| 157 | Name: "com.", |
| 158 | Records: []types.Record{ |
| 159 | {Name: "google", IP: net.ParseIP("8.8.4.4")}, |
| 160 | {Name: "local.google", IP: net.ParseIP("8.8.8.8")}, |
| 161 | }, |
| 162 | }, |
| 163 | { |
| 164 | Name: "internal.", |
| 165 | Records: []types.Record{ |
| 166 | {Name: "host.docker", IP: net.ParseIP("192.168.5.2")}, |
| 167 | {Name: "host.lima", IP: net.ParseIP("192.168.5.2")}, |
| 168 | }, |
| 169 | }, |
| 170 | { |
| 171 | Name: "localhost", |
| 172 | DefaultIP: net.ParseIP("127.0.0.1"), |
| 173 | }, |
| 174 | }, |
| 175 | }, |
| 176 | } |
| 177 | |
| 178 | for i, tt := range tests { |
| 179 | t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 180 | gotZones := ExtractZones(hosts) |
| 181 | for _, zone := range gotZones { |
| 182 | slices.SortFunc(zone.Records, func(a, b types.Record) int { return strings.Compare(a.Name, b.Name) }) |
| 183 | } |
| 184 | slices.SortFunc(gotZones, func(a, b types.Zone) int { return strings.Compare(a.Name, b.Name) }) |
| 185 | |
| 186 | if !equalZones(gotZones, tt.wantZones) { |
| 187 | t.Errorf("extractZones() = %+v, want %+v", gotZones, tt.wantZones) |
| 188 | } |
| 189 | }) |
| 190 | } |
| 191 | } |