Path: blob/master/pkg/networks/usernet/dnshosts/dnshosts_test.go
2678 views
// SPDX-FileCopyrightText: Copyright The Lima Authors1// SPDX-License-Identifier: Apache-2.023// From https://raw.githubusercontent.com/abiosoft/colima/v0.5.5/daemon/process/gvproxy/dnshosts_test.go4/*5MIT License67Copyright (c) 2021 Abiola Ibrahim89Permission is hereby granted, free of charge, to any person obtaining a copy10of this software and associated documentation files (the "Software"), to deal11in the Software without restriction, including without limitation the rights12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell13copies of the Software, and to permit persons to whom the Software is14furnished to do so, subject to the following conditions:1516The above copyright notice and this permission notice shall be included in all17copies or substantial portions of the Software.1819THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE25SOFTWARE.26*/2728package dnshosts2930import (31"fmt"32"net"33"slices"34"strings"35"testing"3637"github.com/containers/gvisor-tap-vsock/pkg/types"38)3940func Test_hostsMapIP(t *testing.T) {41hosts := hostMap{}42hosts["sample"] = "1.1.1.1"43hosts["another.sample"] = "1.2.2.1"44hosts["google.com"] = "8.8.8.8"45hosts["google.ae"] = "google.com"46hosts["google.ie"] = "google.ae"4748tests := []struct {49host string50want net.IP51}{52{host: "sample", want: net.ParseIP("1.1.1.1")},53{host: "another.sample", want: net.ParseIP("1.2.2.1")},54{host: "google.com", want: net.ParseIP("8.8.8.8")},55{host: "google.ae", want: net.ParseIP("8.8.8.8")},56{host: "google.ie", want: net.ParseIP("8.8.8.8")},57{host: "google.sample", want: nil},58}59for i, tt := range tests {60t.Run(fmt.Sprint(i), func(t *testing.T) {61got := hosts.hostIP(tt.host)62if !got.Equal(tt.want) {63t.Errorf("hostsMapIP() = %v, want %v", got, tt.want)64return65}66})67}68}6970func Test_zoneHost(t *testing.T) {71type val struct {72name string73recordName string74}75tests := []struct {76host zoneHost77want val78}{79{}, // test for empty value as well80{host: "sample", want: val{name: "sample"}},81{host: "another.sample", want: val{name: "sample.", recordName: "another"}},82{host: "another.sample.com", want: val{name: "com.", recordName: "another.sample"}},83{host: "a.c", want: val{name: "c.", recordName: "a"}},84{host: "a.b.c.d", want: val{name: "d.", recordName: "a.b.c"}},85}86for i, tt := range tests {87t.Run(fmt.Sprint(i), func(t *testing.T) {88got := val{89name: tt.host.name(),90recordName: tt.host.recordName(),91}92if got != tt.want {93t.Errorf("host = %+v, want %+v", got, tt.want)94return95}96})97}98}99100func TestExtractZones(t *testing.T) {101equalZones := func(za, zb []types.Zone) bool {102equal := func(a, b types.Zone) bool {103if a.Name != b.Name {104return false105}106if !a.DefaultIP.Equal(b.DefaultIP) {107return false108}109for i := range a.Records {110a, b := a.Records[i], b.Records[i]111if !a.IP.Equal(b.IP) {112return false113}114if a.Name != b.Name {115return false116}117}118119return true120}121122for _, a := range za {123ib := slices.IndexFunc(zb, func(z types.Zone) bool { return z.Name == a.Name })124if ib < 0 {125return false126}127b := zb[ib]128if !equal(a, b) {129return false130}131}132return true133}134135hosts := hostMap{136"google.com": "8.8.4.4",137"local.google.com": "8.8.8.8",138"google.ae": "google.com",139"localhost": "127.0.0.1",140"host.lima.internal": "192.168.5.2",141"host.docker.internal": "host.lima.internal",142}143144tests := []struct {145wantZones []types.Zone146}{147{148wantZones: []types.Zone{149{150Name: "ae.",151Records: []types.Record{152{Name: "google", IP: net.ParseIP("8.8.4.4")},153},154},155{156Name: "com.",157Records: []types.Record{158{Name: "google", IP: net.ParseIP("8.8.4.4")},159{Name: "local.google", IP: net.ParseIP("8.8.8.8")},160},161},162{163Name: "internal.",164Records: []types.Record{165{Name: "host.docker", IP: net.ParseIP("192.168.5.2")},166{Name: "host.lima", IP: net.ParseIP("192.168.5.2")},167},168},169{170Name: "localhost",171DefaultIP: net.ParseIP("127.0.0.1"),172},173},174},175}176177for i, tt := range tests {178t.Run(fmt.Sprint(i), func(t *testing.T) {179gotZones := ExtractZones(hosts)180for _, zone := range gotZones {181slices.SortFunc(zone.Records, func(a, b types.Record) int { return strings.Compare(a.Name, b.Name) })182}183slices.SortFunc(gotZones, func(a, b types.Zone) int { return strings.Compare(a.Name, b.Name) })184185if !equalZones(gotZones, tt.wantZones) {186t.Errorf("extractZones() = %+v, want %+v", gotZones, tt.wantZones)187}188})189}190}191192193