mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
test: Added validI2PHostname
unit test #148
Todo: Validate new format and allow naming service hostnames
This commit is contained in:
parent
f339bc9c3c
commit
e0313bdbe2
2 changed files with 48 additions and 0 deletions
|
@ -323,6 +323,7 @@ func validTorHostname(hostname string) bool {
|
||||||
//
|
//
|
||||||
// Old b32 addresses are always {52 chars}.b32.i2p and new ones are {56+ chars}.b32.i2p.
|
// Old b32 addresses are always {52 chars}.b32.i2p and new ones are {56+ chars}.b32.i2p.
|
||||||
// See: https://geti2p.net/spec/b32encrypted
|
// See: https://geti2p.net/spec/b32encrypted
|
||||||
|
// TODO: Validate new format and allow naming service hostnames
|
||||||
func validI2PHostname(hostname string) bool {
|
func validI2PHostname(hostname string) bool {
|
||||||
return regexp.MustCompile(`^[a-z2-7]{52,}\.b32\.i2p$`).MatchString(hostname)
|
return regexp.MustCompile(`^[a-z2-7]{52,}\.b32\.i2p$`).MatchString(hostname)
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,6 +177,53 @@ func Benchmark_validTorHostname(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Single test:
|
||||||
|
// go test -race ./internal/monero -run=TestValidI2PHostname -v
|
||||||
|
// TODO: Validate new format and allow naming service hostnames
|
||||||
|
func TestValidI2PHostname(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
host string
|
||||||
|
wantValid bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Empty host",
|
||||||
|
host: "",
|
||||||
|
wantValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid b32 i2p host (old format)",
|
||||||
|
host: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst234567.b32.i2p",
|
||||||
|
wantValid: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid b32 i2p host (old format)",
|
||||||
|
host: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst123456.b32.i2p",
|
||||||
|
wantValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "clearnet domain",
|
||||||
|
host: "test.com",
|
||||||
|
wantValid: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if r := validI2PHostname(tt.host); r != tt.wantValid {
|
||||||
|
t.Errorf("ValidTorHostname() error = %v, wantValid %v", r, tt.wantValid)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Single bench test:
|
||||||
|
// go test ./internal/monero -bench validI2PHostname -benchmem -run=^$ -v
|
||||||
|
func Benchmark_validI2PHostname(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = validTorHostname("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst234567.b32.i2p")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// equalArgs is helper function for testing.
|
// equalArgs is helper function for testing.
|
||||||
//
|
//
|
||||||
// This returns true if two slices of interface{} are equal.
|
// This returns true if two slices of interface{} are equal.
|
||||||
|
|
Loading…
Reference in a new issue