3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-05 02:19:25 +01:00
ergo/irc/cloaks/cloak_test.go

107 lines
2.9 KiB
Go
Raw Normal View History

2019-05-12 08:17:57 +02:00
// Copyright (c) 2019 Shivaram Lingamneni
// released under the MIT license
2019-05-12 10:01:47 +02:00
package cloaks
2019-05-12 08:17:57 +02:00
import (
"net"
2019-05-12 10:01:47 +02:00
"reflect"
2019-05-12 08:17:57 +02:00
"testing"
)
2019-05-12 10:01:47 +02:00
func assertEqual(supplied, expected interface{}, t *testing.T) {
if !reflect.DeepEqual(supplied, expected) {
t.Errorf("expected %v but got %v", expected, supplied)
}
}
2019-05-12 08:17:57 +02:00
func easyParseIP(ipstr string) (result net.IP) {
result = net.ParseIP(ipstr)
if result == nil {
panic(ipstr)
}
return
}
func cloakConfForTesting() CloakConfig {
config := CloakConfig{
Enabled: true,
Netname: "oragono",
2020-05-08 07:16:49 +02:00
secret: "_BdVPWB5sray7McbFmeuJL996yaLgG4l9tEyficGXKg",
2019-05-12 08:17:57 +02:00
CidrLenIPv4: 32,
CidrLenIPv6: 64,
NumBits: 80,
}
2019-05-12 10:01:47 +02:00
config.Initialize()
2019-05-12 08:17:57 +02:00
return config
}
func TestCloakDeterminism(t *testing.T) {
config := cloakConfForTesting()
v4ip := easyParseIP("8.8.8.8").To4()
assertEqual(config.ComputeCloak(v4ip), "d2z5guriqhzwazyr.oragono", t)
// use of the 4-in-6 mapping should not affect the cloak
v6mappedIP := v4ip.To16()
assertEqual(config.ComputeCloak(v6mappedIP), "d2z5guriqhzwazyr.oragono", t)
v6ip := easyParseIP("2001:0db8::1")
assertEqual(config.ComputeCloak(v6ip), "w7ren6nxii6f3i3d.oragono", t)
// same CIDR, so same cloak:
v6ipsamecidr := easyParseIP("2001:0db8::2")
assertEqual(config.ComputeCloak(v6ipsamecidr), "w7ren6nxii6f3i3d.oragono", t)
v6ipdifferentcidr := easyParseIP("2001:0db9::1")
// different CIDR, different cloak:
assertEqual(config.ComputeCloak(v6ipdifferentcidr), "ccmptyrjwsxv4f4d.oragono", t)
// cloak values must be sensitive to changes in the secret key
2020-05-08 07:16:49 +02:00
config.SetSecret("HJcXK4lLawxBE4-9SIdPji_21YiL3N5r5f5-SPNrGVY")
2019-05-12 08:17:57 +02:00
assertEqual(config.ComputeCloak(v4ip), "4khy3usk8mfu42pe.oragono", t)
assertEqual(config.ComputeCloak(v6mappedIP), "4khy3usk8mfu42pe.oragono", t)
assertEqual(config.ComputeCloak(v6ip), "mxpk3c83vdxkek9j.oragono", t)
assertEqual(config.ComputeCloak(v6ipsamecidr), "mxpk3c83vdxkek9j.oragono", t)
}
func TestCloakShortv4Cidr(t *testing.T) {
config := CloakConfig{
Enabled: true,
Netname: "oragono",
2020-05-08 07:16:49 +02:00
secret: "_BdVPWB5sray7McbFmeuJL996yaLgG4l9tEyficGXKg",
2019-05-12 08:17:57 +02:00
CidrLenIPv4: 24,
CidrLenIPv6: 64,
NumBits: 60,
}
2019-05-12 10:01:47 +02:00
config.Initialize()
2019-05-12 08:17:57 +02:00
v4ip := easyParseIP("8.8.8.8")
assertEqual(config.ComputeCloak(v4ip), "3cay3zc72tnui.oragono", t)
v4ipsamecidr := easyParseIP("8.8.8.9")
assertEqual(config.ComputeCloak(v4ipsamecidr), "3cay3zc72tnui.oragono", t)
}
func TestCloakZeroBits(t *testing.T) {
config := cloakConfForTesting()
config.NumBits = 0
config.Netname = "example.com"
2019-05-12 10:01:47 +02:00
config.Initialize()
2019-05-12 08:17:57 +02:00
v4ip := easyParseIP("8.8.8.8").To4()
assertEqual(config.ComputeCloak(v4ip), "example.com", t)
}
func TestCloakDisabled(t *testing.T) {
config := cloakConfForTesting()
config.Enabled = false
v4ip := easyParseIP("8.8.8.8").To4()
assertEqual(config.ComputeCloak(v4ip), "", t)
}
func BenchmarkCloaks(b *testing.B) {
config := cloakConfForTesting()
v6ip := easyParseIP("2001:0db8::1")
b.ResetTimer()
for i := 0; i < b.N; i++ {
config.ComputeCloak(v6ip)
}
}