aboutsummaryrefslogtreecommitdiff
path: root/main_test.go
blob: cd86d0bd020f1b88e42327dbf358b9c99ec5ff56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main

import (
	"bufio"
	"bytes"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestFetchURLList(t *testing.T) {
	tests := []struct {
		name    string
		content string
		want    []string
		wantErr bool
	}{
		{
			name: "basic list with comments",
			content: `# comment
https://example.com/1
https://example.com/2
# another comment
https://example.com/3`,
			want: []string{
				"https://example.com/1",
				"https://example.com/2",
				"https://example.com/3",
			},
		},
		{
			name: "empty lines and whitespace",
			content: `  https://example.com/1  
				
https://example.com/2`,
			want: []string{
				"https://example.com/1",
				"https://example.com/2",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				_, err := w.Write([]byte(tt.content))
				if err != nil {
					t.Errorf("Couldn't write to content %v", err)
				}

			}))
			defer ts.Close()

			got, err := fetchURLList(ts.URL)
			if (err != nil) != tt.wantErr {
				t.Errorf("fetchURLList() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !stringSliceEqual(got, tt.want) {
				t.Errorf("fetchURLList() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestFetchDomainsAndWrite(t *testing.T) {
	tests := []struct {
		name     string
		content  string
		want     string
		wantSeen map[string]struct{}
	}{
		{
			name: "ip and domain format",
			content: `# comment
0.0.0.0 domain1.com
0.0.0.0 domain2.com`,
			want: "\tlocal-zone: \"domain1.com\" refuse\n\tlocal-zone: \"domain2.com\" refuse\n",
			wantSeen: map[string]struct{}{
				"domain1.com": {},
				"domain2.com": {},
			},
		},
		{
			name: "plain domain format",
			content: `# comment
domain1.com
domain2.com`,
			want: "\tlocal-zone: \"domain1.com\" refuse\n\tlocal-zone: \"domain2.com\" refuse\n",
			wantSeen: map[string]struct{}{
				"domain1.com": {},
				"domain2.com": {},
			},
		},
		{
			name: "mixed format with duplicates",
			content: `domain1.com
0.0.0.0 domain1.com
0.0.0.0 DOMAIN1.COM
domain2.com`,
			want: "\tlocal-zone: \"domain1.com\" refuse\n\tlocal-zone: \"domain2.com\" refuse\n",
			wantSeen: map[string]struct{}{
				"domain1.com": {},
				"domain2.com": {},
			},
		},
		{
			name: "domains with pipe and caret",
			content: `||domain1.com^
||domain2.com^
0.0.0.0 ||domain3.com^
||sub.domain4.com^$important
||domain5.com^$third-party`,
			want: "\tlocal-zone: \"domain1.com\" refuse\n\tlocal-zone: \"domain2.com\" refuse\n\tlocal-zone: \"domain3.com\" refuse\n\tlocal-zone: \"sub.domain4.com\" refuse\n\tlocal-zone: \"domain5.com\" refuse\n",
			wantSeen: map[string]struct{}{
				"domain1.com":     {},
				"domain2.com":     {},
				"domain3.com":     {},
				"sub.domain4.com": {},
				"domain5.com":     {},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				_, err := w.Write([]byte(tt.content))
				if err != nil {
					t.Errorf("Couldn't write to content %v", err)
				}

			}))
			defer ts.Close()

			var buf bytes.Buffer
			w := bufio.NewWriter(&buf)
			seen := make(map[string]struct{})

			err := fetchDomainsAndWrite(ts.URL, w, seen)
			if err != nil {
				t.Fatalf("fetchDomainsAndWrite() error = %v", err)
			}
			w.Flush()

			if got := buf.String(); got != tt.want {
				t.Errorf("fetchDomainsAndWrite() output = %q, want %q", got, tt.want)
			}

			if !mapEqual(seen, tt.wantSeen) {
				t.Errorf("fetchDomainsAndWrite() seen = %v, want %v", seen, tt.wantSeen)
			}
		})
	}
}

func stringSliceEqual(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func mapEqual(a, b map[string]struct{}) bool {
	if len(a) != len(b) {
		return false
	}
	for k := range a {
		if _, ok := b[k]; !ok {
			return false
		}
	}
	return true
}