-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
486 lines (404 loc) · 11.3 KB
/
main_test.go
File metadata and controls
486 lines (404 loc) · 11.3 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"dns-bench/benchmark"
)
func TestCalculateStats(t *testing.T) {
results := []benchmark.Result{
{Server: "8.8.8.8", Domain: "google.com", Duration: 10 * time.Millisecond, Error: nil},
{Server: "8.8.8.8", Domain: "yahoo.com", Duration: 20 * time.Millisecond, Error: nil},
{Server: "1.1.1.1", Domain: "google.com", Duration: 5 * time.Millisecond, Error: nil},
{Server: "8.8.8.8", Domain: "error.com", Duration: 0, Error: os.ErrNotExist},
}
stats := calculateStats(results)
if len(stats) != 2 {
t.Errorf("Expected 2 servers in stats, got %d", len(stats))
}
// Find stats for 8.8.8.8
var googleStats *ServerStats
for _, s := range stats {
if s.Server == "8.8.8.8" {
googleStats = s
break
}
}
if googleStats == nil {
t.Fatal("Expected to find stats for 8.8.8.8")
}
if googleStats.Total != 3 {
t.Errorf("Expected 3 total queries for 8.8.8.8, got %d", googleStats.Total)
}
if googleStats.Success != 2 {
t.Errorf("Expected 2 successful queries, got %d", googleStats.Success)
}
if googleStats.Errors != 1 {
t.Errorf("Expected 1 error, got %d", googleStats.Errors)
}
expectedAvg := 15 * time.Millisecond
if googleStats.Avg != expectedAvg {
t.Errorf("Expected avg %v, got %v", expectedAvg, googleStats.Avg)
}
if googleStats.Min != 10*time.Millisecond {
t.Errorf("Expected min 10ms, got %v", googleStats.Min)
}
if googleStats.Max != 20*time.Millisecond {
t.Errorf("Expected max 20ms, got %v", googleStats.Max)
}
// Check that 1.1.1.1 comes first (lower avg latency)
if stats[0].Server != "1.1.1.1" {
t.Errorf("Expected 1.1.1.1 to be ranked first, got %s", stats[0].Server)
}
}
func TestCalculateStatsAllErrors(t *testing.T) {
results := []benchmark.Result{
{Server: "bad.server", Domain: "google.com", Duration: 0, Error: os.ErrNotExist},
{Server: "bad.server", Domain: "yahoo.com", Duration: 0, Error: os.ErrNotExist},
}
stats := calculateStats(results)
if len(stats) != 1 {
t.Errorf("Expected 1 server in stats, got %d", len(stats))
}
if stats[0].Success != 0 {
t.Errorf("Expected 0 successful queries, got %d", stats[0].Success)
}
if stats[0].LossPct != 100.0 {
t.Errorf("Expected 100%% loss, got %.2f%%", stats[0].LossPct)
}
if stats[0].Min != 0 {
t.Errorf("Expected min to be 0 when all errors, got %v", stats[0].Min)
}
}
func TestReadLines(t *testing.T) {
// Create a temp file
tmpfile, err := os.CreateTemp("", "test-domains-*.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := "google.com\nyahoo.com\n\nexample.com\n "
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
lines, err := readLines(tmpfile.Name())
if err != nil {
t.Fatalf("readLines failed: %v", err)
}
if len(lines) != 3 {
t.Errorf("Expected 3 lines, got %d: %v", len(lines), lines)
}
expected := []string{"google.com", "yahoo.com", "example.com"}
for i, line := range lines {
if line != expected[i] {
t.Errorf("Line %d: expected %q, got %q", i, expected[i], line)
}
}
}
func TestReadCSV(t *testing.T) {
// Create a temp CSV file with header
tmpfile, err := os.CreateTemp("", "test-domains-*.csv")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := "rank,domain,traffic\n1,google.com,high\n2,yahoo.com,medium\n"
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
domains, err := readCSV(tmpfile.Name())
if err != nil {
t.Fatalf("readCSV failed: %v", err)
}
if len(domains) != 2 {
t.Errorf("Expected 2 domains, got %d: %v", len(domains), domains)
}
if domains[0] != "google.com" {
t.Errorf("Expected first domain to be google.com, got %s", domains[0])
}
}
func TestReadCSVNoHeader(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test-domains-*.csv")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := "google.com\nyahoo.com\n"
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
domains, err := readCSV(tmpfile.Name())
if err != nil {
t.Fatalf("readCSV failed: %v", err)
}
if len(domains) != 2 {
t.Errorf("Expected 2 domains, got %d", len(domains))
}
}
func TestReadServersYAML(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test-servers-*.yaml")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := `servers:
- 8.8.8.8
- tls://1.1.1.1
- https://dns.google/dns-query
`
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
servers, err := readServers(tmpfile.Name())
if err != nil {
t.Fatalf("readServers failed: %v", err)
}
if len(servers) != 3 {
t.Errorf("Expected 3 servers, got %d: %v", len(servers), servers)
}
if servers[0] != "8.8.8.8" {
t.Errorf("Expected first server to be 8.8.8.8, got %s", servers[0])
}
}
func TestReadServersTXT(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test-servers-*.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := "8.8.8.8\n1.1.1.1\n"
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
servers, err := readServers(tmpfile.Name())
if err != nil {
t.Fatalf("readServers failed: %v", err)
}
if len(servers) != 2 {
t.Errorf("Expected 2 servers, got %d", len(servers))
}
}
func TestExportCSV(t *testing.T) {
results := []benchmark.Result{
{Server: "8.8.8.8", Domain: "google.com", Duration: 10 * time.Millisecond, Error: nil},
{Server: "8.8.8.8", Domain: "yahoo.com", Duration: 20 * time.Millisecond, Error: nil},
}
tmpfile := filepath.Join(os.TempDir(), "test-export.csv")
defer os.Remove(tmpfile)
err := exportCSV(results, tmpfile)
if err != nil {
t.Fatalf("exportCSV failed: %v", err)
}
// Read back and verify
content, err := os.ReadFile(tmpfile)
if err != nil {
t.Fatalf("Failed to read exported CSV: %v", err)
}
contentStr := string(content)
if !strings.Contains(contentStr, "Server") {
t.Error("Expected CSV to contain header 'Server'")
}
if !strings.Contains(contentStr, "8.8.8.8") {
t.Error("Expected CSV to contain server '8.8.8.8'")
}
if !strings.Contains(contentStr, "google.com") {
t.Error("Expected CSV to contain domain 'google.com'")
}
}
func TestGenerateHTML(t *testing.T) {
stats := []*ServerStats{
{
Server: "8.8.8.8",
Total: 10,
Success: 9,
Errors: 1,
Min: 5 * time.Millisecond,
Max: 50 * time.Millisecond,
Avg: 15 * time.Millisecond,
LossPct: 10.0,
},
}
tmpfile := filepath.Join(os.TempDir(), "test-report.html")
defer os.Remove(tmpfile)
err := generateHTML(stats, 5*time.Second, tmpfile)
if err != nil {
t.Fatalf("generateHTML failed: %v", err)
}
// Read back and verify
content, err := os.ReadFile(tmpfile)
if err != nil {
t.Fatalf("Failed to read generated HTML: %v", err)
}
contentStr := string(content)
if !strings.Contains(contentStr, "<!DOCTYPE html>") {
t.Error("Expected HTML to be valid HTML5")
}
if !strings.Contains(contentStr, "8.8.8.8") {
t.Error("Expected HTML to contain server '8.8.8.8'")
}
if !strings.Contains(contentStr, "DNS Benchmark") {
t.Error("Expected HTML to contain title")
}
}
func TestLoadConfigFile(t *testing.T) {
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "config.yaml")
content := `
servers:
- 8.8.8.8
- 1.1.1.1
domains:
- google.com
concurrency: 100
iterations: 5
timeout: 2s
verbose: true
progress: true
`
if err := os.WriteFile(configFile, []byte(content), 0600); err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
cfg, err := loadConfigFile(configFile)
if err != nil {
t.Fatalf("loadConfigFile failed: %v", err)
}
if len(cfg.Servers) != 2 {
t.Errorf("Expected 2 servers, got %d", len(cfg.Servers))
}
if cfg.Concurrency != 100 {
t.Errorf("Expected concurrency 100, got %d", cfg.Concurrency)
}
if cfg.Iterations != 5 {
t.Errorf("Expected iterations 5, got %d", cfg.Iterations)
}
if !cfg.Verbose {
t.Error("Expected verbose to be true")
}
}
func TestLoadConfigFileInvalid(t *testing.T) {
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "invalid.yaml")
// Invalid YAML
content := "invalid: yaml: content: ["
if err := os.WriteFile(configFile, []byte(content), 0600); err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
_, err := loadConfigFile(configFile)
if err == nil {
t.Error("Expected error for invalid YAML")
}
}
func TestLoadConfigFileNotFound(t *testing.T) {
_, err := loadConfigFile("/nonexistent/config.yaml")
if err == nil {
t.Error("Expected error for non-existent file")
}
}
func TestFindConfigFile(t *testing.T) {
// Save current directory
originalDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Errorf("Failed to restore directory: %v", err)
}
}()
// Create temp directory with config file
tmpDir := t.TempDir()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp directory: %v", err)
}
// No config file should return empty string
result := findConfigFile()
if result != "" {
t.Errorf("Expected empty string when no config exists, got %s", result)
}
// Create config file
configFile := ".dns-bench.yaml"
if err := os.WriteFile(configFile, []byte("servers: []"), 0600); err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
result = findConfigFile()
if result != configFile {
t.Errorf("Expected to find %s, got %s", configFile, result)
}
}
func TestReadDomainsCSV(t *testing.T) {
tmpDir := t.TempDir()
csvFile := filepath.Join(tmpDir, "domains.csv")
content := "domain\nexample.com\ntest.com\n"
if err := os.WriteFile(csvFile, []byte(content), 0600); err != nil {
t.Fatalf("Failed to create CSV file: %v", err)
}
domains, err := readDomains(csvFile)
if err != nil {
t.Fatalf("readDomains failed: %v", err)
}
if len(domains) != 2 {
t.Errorf("Expected 2 domains, got %d", len(domains))
}
}
func TestReadDomainsTXT(t *testing.T) {
tmpDir := t.TempDir()
txtFile := filepath.Join(tmpDir, "domains.txt")
content := "example.com\ntest.com\n"
if err := os.WriteFile(txtFile, []byte(content), 0600); err != nil {
t.Fatalf("Failed to create TXT file: %v", err)
}
domains, err := readDomains(txtFile)
if err != nil {
t.Fatalf("readDomains failed: %v", err)
}
if len(domains) != 2 {
t.Errorf("Expected 2 domains, got %d", len(domains))
}
}
func TestPrintTable(_ *testing.T) {
// This function writes to stdout, so we just ensure it doesn't panic
stats := []*ServerStats{
{
Server: "8.8.8.8",
Total: 10,
Success: 9,
Errors: 1,
Min: 5 * time.Millisecond,
Max: 50 * time.Millisecond,
Avg: 15 * time.Millisecond,
LossPct: 10.0,
},
}
// Should not panic
printTable(stats, 5*time.Second)
}
func TestReadServersInvalidYAML(t *testing.T) {
tmpDir := t.TempDir()
yamlFile := filepath.Join(tmpDir, "servers.yaml")
// Invalid YAML structure
content := "invalid: [yaml"
if err := os.WriteFile(yamlFile, []byte(content), 0600); err != nil {
t.Fatalf("Failed to create YAML file: %v", err)
}
_, err := readServers(yamlFile)
if err == nil {
t.Error("Expected error for invalid YAML")
}
}