Skip to content

Commit dcf879b

Browse files
committed
add tests
1 parent ec5abfb commit dcf879b

File tree

2 files changed

+350
-0
lines changed

2 files changed

+350
-0
lines changed

src/mnemonicode/mnemonicode_test.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package mnemonicode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestWordsRequired(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
length int
11+
want int
12+
}{
13+
{"empty", 0, 0},
14+
{"1 byte", 1, 1},
15+
{"2 bytes", 2, 2},
16+
{"3 bytes", 3, 3},
17+
{"4 bytes", 4, 3},
18+
{"5 bytes", 5, 4},
19+
{"8 bytes", 8, 6},
20+
{"12 bytes", 12, 9},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
if got := WordsRequired(tt.length); got != tt.want {
26+
t.Errorf("WordsRequired(%d) = %d, want %d", tt.length, got, tt.want)
27+
}
28+
})
29+
}
30+
}
31+
32+
func TestEncodeWordList(t *testing.T) {
33+
tests := []struct {
34+
name string
35+
dst []string
36+
src []byte
37+
want int
38+
}{
39+
{
40+
name: "empty input",
41+
dst: []string{},
42+
src: []byte{},
43+
want: 0,
44+
},
45+
{
46+
name: "single byte",
47+
dst: []string{},
48+
src: []byte{0x01},
49+
want: 1,
50+
},
51+
{
52+
name: "two bytes",
53+
dst: []string{},
54+
src: []byte{0x01, 0x02},
55+
want: 2,
56+
},
57+
{
58+
name: "three bytes",
59+
dst: []string{},
60+
src: []byte{0x01, 0x02, 0x03},
61+
want: 3,
62+
},
63+
{
64+
name: "four bytes",
65+
dst: []string{},
66+
src: []byte{0x01, 0x02, 0x03, 0x04},
67+
want: 3,
68+
},
69+
{
70+
name: "eight bytes",
71+
dst: []string{},
72+
src: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
73+
want: 6,
74+
},
75+
{
76+
name: "with existing dst",
77+
dst: []string{"existing"},
78+
src: []byte{0x01},
79+
want: 2,
80+
},
81+
}
82+
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
result := EncodeWordList(tt.dst, tt.src)
86+
if len(result) != tt.want {
87+
t.Errorf("EncodeWordList() returned %d words, want %d", len(result), tt.want)
88+
}
89+
90+
// Check that all words are valid
91+
for i, word := range result {
92+
if word == "" {
93+
t.Errorf("EncodeWordList() returned empty word at index %d", i)
94+
}
95+
}
96+
})
97+
}
98+
}
99+
100+
func TestEncodeWordListConsistency(t *testing.T) {
101+
input := []byte{0x12, 0x34, 0x56, 0x78}
102+
103+
// Encode twice with empty dst
104+
result1 := EncodeWordList([]string{}, input)
105+
result2 := EncodeWordList([]string{}, input)
106+
107+
if len(result1) != len(result2) {
108+
t.Errorf("Inconsistent result lengths: %d vs %d", len(result1), len(result2))
109+
}
110+
111+
for i := range result1 {
112+
if result1[i] != result2[i] {
113+
t.Errorf("Inconsistent result at index %d: %s vs %s", i, result1[i], result2[i])
114+
}
115+
}
116+
}
117+
118+
func TestEncodeWordListCapacityHandling(t *testing.T) {
119+
// Test with dst that has sufficient capacity
120+
dst := make([]string, 1, 10)
121+
dst[0] = "existing"
122+
input := []byte{0x01, 0x02}
123+
124+
result := EncodeWordList(dst, input)
125+
126+
if len(result) != 3 { // 1 existing + 2 new
127+
t.Errorf("Expected 3 words, got %d", len(result))
128+
}
129+
130+
if result[0] != "existing" {
131+
t.Errorf("Expected first word to be 'existing', got %s", result[0])
132+
}
133+
}
134+
135+
func TestEncodeWordListBoundaryValues(t *testing.T) {
136+
tests := []struct {
137+
name string
138+
src []byte
139+
}{
140+
{"max single byte", []byte{0xFF}},
141+
{"max two bytes", []byte{0xFF, 0xFF}},
142+
{"max three bytes", []byte{0xFF, 0xFF, 0xFF}},
143+
{"max four bytes", []byte{0xFF, 0xFF, 0xFF, 0xFF}},
144+
{"all zeros", []byte{0x00, 0x00, 0x00, 0x00}},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
result := EncodeWordList([]string{}, tt.src)
150+
expectedLen := WordsRequired(len(tt.src))
151+
152+
if len(result) != expectedLen {
153+
t.Errorf("Expected %d words, got %d", expectedLen, len(result))
154+
}
155+
156+
// Ensure all words are from the WordList
157+
for _, word := range result {
158+
found := false
159+
for _, validWord := range WordList {
160+
if word == validWord {
161+
found = true
162+
break
163+
}
164+
}
165+
if !found {
166+
t.Errorf("Invalid word generated: %s", word)
167+
}
168+
}
169+
})
170+
}
171+
}

src/models/models_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package models
2+
3+
import (
4+
"net"
5+
"strings"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestConstants(t *testing.T) {
11+
if TCP_BUFFER_SIZE != 1024*64 {
12+
t.Errorf("TCP_BUFFER_SIZE = %d, want %d", TCP_BUFFER_SIZE, 1024*64)
13+
}
14+
15+
if DEFAULT_PORT != "9009" {
16+
t.Errorf("DEFAULT_PORT = %s, want %s", DEFAULT_PORT, "9009")
17+
}
18+
19+
if DEFAULT_PASSPHRASE != "pass123" {
20+
t.Errorf("DEFAULT_PASSPHRASE = %s, want %s", DEFAULT_PASSPHRASE, "pass123")
21+
}
22+
}
23+
24+
func TestPublicDNSServers(t *testing.T) {
25+
if len(publicDNS) == 0 {
26+
t.Error("publicDNS list should not be empty")
27+
}
28+
29+
// Check that we have both IPv4 and IPv6 servers
30+
hasIPv4 := false
31+
hasIPv6 := false
32+
33+
for _, dns := range publicDNS {
34+
if strings.Contains(dns, "[") {
35+
hasIPv6 = true
36+
} else {
37+
hasIPv4 = true
38+
}
39+
}
40+
41+
if !hasIPv4 {
42+
t.Error("publicDNS should contain IPv4 servers")
43+
}
44+
45+
if !hasIPv6 {
46+
t.Error("publicDNS should contain IPv6 servers")
47+
}
48+
49+
// Verify known DNS servers are present
50+
expectedServers := []string{
51+
"1.1.1.1", // Cloudflare
52+
"8.8.8.8", // Google
53+
"9.9.9.9", // Quad9
54+
"208.67.220.220", // OpenDNS
55+
}
56+
57+
for _, expected := range expectedServers {
58+
found := false
59+
for _, dns := range publicDNS {
60+
if dns == expected {
61+
found = true
62+
break
63+
}
64+
}
65+
if !found {
66+
t.Errorf("Expected DNS server %s not found in publicDNS", expected)
67+
}
68+
}
69+
}
70+
71+
func TestLocalLookupIP(t *testing.T) {
72+
tests := []struct {
73+
name string
74+
address string
75+
wantErr bool
76+
}{
77+
{
78+
name: "localhost",
79+
address: "localhost",
80+
wantErr: false,
81+
},
82+
{
83+
name: "invalid hostname",
84+
address: "this-hostname-should-not-exist-12345",
85+
wantErr: true,
86+
},
87+
}
88+
89+
for _, tt := range tests {
90+
t.Run(tt.name, func(t *testing.T) {
91+
ip, err := localLookupIP(tt.address)
92+
93+
if (err != nil) != tt.wantErr {
94+
t.Errorf("localLookupIP() error = %v, wantErr %v", err, tt.wantErr)
95+
return
96+
}
97+
98+
if !tt.wantErr && ip == "" {
99+
t.Error("localLookupIP() returned empty IP for valid hostname")
100+
}
101+
102+
if !tt.wantErr {
103+
// Verify it's a valid IP address
104+
if net.ParseIP(ip) == nil {
105+
t.Errorf("localLookupIP() returned invalid IP: %s", ip)
106+
}
107+
}
108+
})
109+
}
110+
}
111+
112+
func TestRemoteLookupIPTimeout(t *testing.T) {
113+
// Test with an invalid DNS server that should timeout
114+
start := time.Now()
115+
_, err := remoteLookupIP("example.com", "192.0.2.1")
116+
duration := time.Since(start)
117+
118+
// Should timeout within reasonable time (we set 500ms timeout)
119+
if duration > time.Second {
120+
t.Errorf("remoteLookupIP took too long: %v", duration)
121+
}
122+
123+
if err == nil {
124+
t.Error("remoteLookupIP should have failed with invalid DNS server")
125+
}
126+
}
127+
128+
func TestLookupFunction(t *testing.T) {
129+
// Test the main lookup function
130+
tests := []struct {
131+
name string
132+
address string
133+
wantErr bool
134+
}{
135+
{
136+
name: "localhost",
137+
address: "localhost",
138+
wantErr: false,
139+
},
140+
{
141+
name: "invalid hostname",
142+
address: "this-hostname-should-definitely-not-exist-98765",
143+
wantErr: true,
144+
},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
ip, err := lookup(tt.address)
150+
151+
if (err != nil) != tt.wantErr {
152+
t.Errorf("lookup() error = %v, wantErr %v", err, tt.wantErr)
153+
return
154+
}
155+
156+
if !tt.wantErr && ip == "" {
157+
t.Error("lookup() returned empty IP for valid hostname")
158+
}
159+
160+
if !tt.wantErr {
161+
// Verify it's a valid IP address
162+
if net.ParseIP(ip) == nil {
163+
t.Errorf("lookup() returned invalid IP: %s", ip)
164+
}
165+
}
166+
})
167+
}
168+
}
169+
170+
func TestGetConfigFile(t *testing.T) {
171+
fname, err := getConfigFile(false)
172+
if err != nil {
173+
t.Skip("Could not get config directory")
174+
}
175+
176+
if !strings.HasSuffix(fname, "internal-dns") {
177+
t.Errorf("Expected config file to end with 'internal-dns', got %s", fname)
178+
}
179+
}

0 commit comments

Comments
 (0)