mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-04-01 06:53:30 +03:00
Squashed commit of the following:
commit 37e33ec761cfa30164125af2c5bb40789412355e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Feb 14 15:25:25 2024 +0300
aghalg: imp code
commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea
Merge: b8ea924aa 37736289e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Feb 14 15:04:59 2024 +0300
Merge branch 'master' into AG-27492-client-persistent-storage
commit b8ea924aa7ed4c052760a6068f945d83d184e7e3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Feb 13 19:07:52 2024 +0300
home: imp tests
commit aa6fec03b1a1ead96bc76919b7ad51ae19626633
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Feb 13 14:54:28 2024 +0300
home: imp docs
commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Feb 8 20:16:11 2024 +0300
all: imp code
commit b45c7d868ddb1be73e119b3260e2a866d57baa91
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Feb 7 19:15:11 2024 +0300
aghalg: add tests
commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Feb 6 20:50:22 2024 +0300
all: imp code, tests
commit 4a44e993c9bd393d2cb9853108eae1ad91e64402
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Feb 1 14:59:11 2024 +0300
all: persistent client index
commit 66b16e216e03e9f3d5e69496a89b18a9d732b564
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jan 31 15:06:05 2024 +0300
aghalg: ordered map
210 lines
4.4 KiB
Go
210 lines
4.4 KiB
Go
package home
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestClientIndex(t *testing.T) {
|
|
const (
|
|
cliIPNone = "1.2.3.4"
|
|
cliIP1 = "1.1.1.1"
|
|
cliIP2 = "2.2.2.2"
|
|
|
|
cliIPv6 = "1:2:3::4"
|
|
|
|
cliSubnet = "2.2.2.0/24"
|
|
cliSubnetIP = "2.2.2.222"
|
|
|
|
cliID = "client-id"
|
|
cliMAC = "11:11:11:11:11:11"
|
|
)
|
|
|
|
clients := []*persistentClient{{
|
|
Name: "client1",
|
|
IPs: []netip.Addr{
|
|
netip.MustParseAddr(cliIP1),
|
|
netip.MustParseAddr(cliIPv6),
|
|
},
|
|
}, {
|
|
Name: "client2",
|
|
IPs: []netip.Addr{netip.MustParseAddr(cliIP2)},
|
|
Subnets: []netip.Prefix{netip.MustParsePrefix(cliSubnet)},
|
|
}, {
|
|
Name: "client_with_mac",
|
|
MACs: []net.HardwareAddr{mustParseMAC(cliMAC)},
|
|
}, {
|
|
Name: "client_with_id",
|
|
ClientIDs: []string{cliID},
|
|
}}
|
|
|
|
ci := newIDIndex(clients)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
ids []string
|
|
want *persistentClient
|
|
}{{
|
|
name: "ipv4_ipv6",
|
|
ids: []string{cliIP1, cliIPv6},
|
|
want: clients[0],
|
|
}, {
|
|
name: "ipv4_subnet",
|
|
ids: []string{cliIP2, cliSubnetIP},
|
|
want: clients[1],
|
|
}, {
|
|
name: "mac",
|
|
ids: []string{cliMAC},
|
|
want: clients[2],
|
|
}, {
|
|
name: "client_id",
|
|
ids: []string{cliID},
|
|
want: clients[3],
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
for _, id := range tc.ids {
|
|
c, ok := ci.find(id)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, tc.want, c)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("not_found", func(t *testing.T) {
|
|
_, ok := ci.find(cliIPNone)
|
|
assert.False(t, ok)
|
|
})
|
|
}
|
|
|
|
func TestClientIndex_Clashes(t *testing.T) {
|
|
const (
|
|
cliIP1 = "1.1.1.1"
|
|
cliSubnet = "2.2.2.0/24"
|
|
cliSubnetIP = "2.2.2.222"
|
|
cliID = "client-id"
|
|
cliMAC = "11:11:11:11:11:11"
|
|
)
|
|
|
|
clients := []*persistentClient{{
|
|
Name: "client_with_ip",
|
|
IPs: []netip.Addr{netip.MustParseAddr(cliIP1)},
|
|
}, {
|
|
Name: "client_with_subnet",
|
|
Subnets: []netip.Prefix{netip.MustParsePrefix(cliSubnet)},
|
|
}, {
|
|
Name: "client_with_mac",
|
|
MACs: []net.HardwareAddr{mustParseMAC(cliMAC)},
|
|
}, {
|
|
Name: "client_with_id",
|
|
ClientIDs: []string{cliID},
|
|
}}
|
|
|
|
ci := newIDIndex(clients)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
client *persistentClient
|
|
}{{
|
|
name: "ipv4",
|
|
client: clients[0],
|
|
}, {
|
|
name: "subnet",
|
|
client: clients[1],
|
|
}, {
|
|
name: "mac",
|
|
client: clients[2],
|
|
}, {
|
|
name: "client_id",
|
|
client: clients[3],
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
clone := tc.client.shallowClone()
|
|
clone.UID = MustNewUID()
|
|
|
|
err := ci.clashes(clone)
|
|
require.Error(t, err)
|
|
|
|
ci.del(tc.client)
|
|
err = ci.clashes(clone)
|
|
require.NoError(t, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
// mustParseMAC is wrapper around [net.ParseMAC] that panics if there is an
|
|
// error.
|
|
func mustParseMAC(s string) (mac net.HardwareAddr) {
|
|
mac, err := net.ParseMAC(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return mac
|
|
}
|
|
|
|
func TestMACToKey(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
in string
|
|
want any
|
|
}{{
|
|
name: "column6",
|
|
in: "00:00:5e:00:53:01",
|
|
want: [6]byte(mustParseMAC("00:00:5e:00:53:01")),
|
|
}, {
|
|
name: "column8",
|
|
in: "02:00:5e:10:00:00:00:01",
|
|
want: [8]byte(mustParseMAC("02:00:5e:10:00:00:00:01")),
|
|
}, {
|
|
name: "column20",
|
|
in: "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01",
|
|
want: [20]byte(mustParseMAC("00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01")),
|
|
}, {
|
|
name: "hyphen6",
|
|
in: "00-00-5e-00-53-01",
|
|
want: [6]byte(mustParseMAC("00-00-5e-00-53-01")),
|
|
}, {
|
|
name: "hyphen8",
|
|
in: "02-00-5e-10-00-00-00-01",
|
|
want: [8]byte(mustParseMAC("02-00-5e-10-00-00-00-01")),
|
|
}, {
|
|
name: "hyphen20",
|
|
in: "00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01",
|
|
want: [20]byte(mustParseMAC("00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01")),
|
|
}, {
|
|
name: "dot6",
|
|
in: "0000.5e00.5301",
|
|
want: [6]byte(mustParseMAC("0000.5e00.5301")),
|
|
}, {
|
|
name: "dot8",
|
|
in: "0200.5e10.0000.0001",
|
|
want: [8]byte(mustParseMAC("0200.5e10.0000.0001")),
|
|
}, {
|
|
name: "dot20",
|
|
in: "0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001",
|
|
want: [20]byte(mustParseMAC("0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001")),
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
mac := mustParseMAC(tc.in)
|
|
|
|
key := macToKey(mac)
|
|
assert.Equal(t, tc.want, key)
|
|
})
|
|
}
|
|
|
|
assert.Panics(t, func() {
|
|
mac := net.HardwareAddr([]byte{1, 2, 3})
|
|
_ = macToKey(mac)
|
|
})
|
|
}
|