-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcli_test.go
More file actions
602 lines (528 loc) Β· 16.6 KB
/
cli_test.go
File metadata and controls
602 lines (528 loc) Β· 16.6 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/shazow/wifitui/wifi"
"github.com/shazow/wifitui/wifi/mock"
)
func TestRunListAll(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
// Test with all=true (should list invisible known networks)
if err := runList(&buf, false, true, false, mockBackend); err != nil {
t.Fatalf("runList() failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "HideYoKidsHideYoWiFi") {
t.Errorf("runList() output missing expected network. got=%q", output)
}
if !strings.Contains(output, "Unencrypted_Honeypot") {
t.Errorf("runList() output missing expected network. got=%q", output)
}
}
func TestRunListDefault(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
// Default behavior (all=false)
if err := runList(&buf, false, false, false, mockBackend); err != nil {
t.Fatalf("runList() failed: %v", err)
}
output := buf.String()
if strings.Contains(output, "HideYoKidsHideYoWiFi") {
t.Errorf("runList() output should NOT contain invisible network. got=%q", output)
}
if !strings.Contains(output, "Unencrypted_Honeypot") {
t.Errorf("runList() output missing expected visible network. got=%q", output)
}
}
func TestRunShow(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
// Test case: network found and known
if err := runShow(&buf, false, "Password is password", mockBackend); err != nil {
t.Fatalf("runShow() with found network failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "SSID: Password is password") {
t.Errorf("runShow() output missing SSID. got=%q", output)
}
if !strings.Contains(output, "Passphrase: password") {
t.Errorf("runShow() output missing passphrase. got=%q", output)
}
// Test case: network found, but not known (no secret)
buf.Reset()
if err := runShow(&buf, false, "GET off my LAN", mockBackend); err != nil {
// This should not fail, just return no secret.
t.Fatalf("runShow() with network without secret failed: %v", err)
}
output = buf.String()
if !strings.Contains(output, "SSID: GET off my LAN") {
t.Errorf("runShow() output missing SSID. got=%q", output)
}
if !strings.Contains(output, "Passphrase: ") {
t.Errorf("runShow() output should have empty passphrase. got=%q", output)
}
// Test case: network not found
buf.Reset()
{
const doesNotExist = "_DOES NOT EXIST_"
err := runShow(&buf, false, doesNotExist, mockBackend)
if err == nil {
t.Fatalf("runShow() with not found network should have failed, but did not")
}
if !errors.Is(err, wifi.ErrNotFound) {
t.Errorf("runShow() with not found network gave wrong error. got=%q", err)
}
}
}
func TestRunListJSON(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
if err := runList(&buf, true, true, false, mockBackend); err != nil {
t.Fatalf("runList() failed: %v", err)
}
var connections []wifi.Connection
if err := json.Unmarshal(buf.Bytes(), &connections); err != nil {
t.Fatalf("runList() output is not valid JSON: %v. got=%q", err, buf.String())
}
if len(connections) == 0 {
t.Fatalf("runList() output is empty")
}
// Just check for one of the SSIDs
found := false
for _, c := range connections {
if c.SSID == "HideYoKidsHideYoWiFi" {
found = true
break
}
}
if !found {
t.Errorf("runList() JSON output missing expected network. got=%q", buf.String())
}
}
func TestRunShowJSON(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
// Test case: network found and known
if err := runShow(&buf, true, "Password is password", mockBackend); err != nil {
t.Fatalf("runShow() with found network failed: %v", err)
}
type connectionWithSecret struct {
wifi.Connection
Passphrase string `json:"passphrase,omitempty"`
}
var connWithSecretData connectionWithSecret
if err := json.Unmarshal(buf.Bytes(), &connWithSecretData); err != nil {
t.Fatalf("runShow() output is not valid JSON: %v. got=%q", err, buf.String())
}
if connWithSecretData.SSID != "Password is password" {
t.Errorf("runShow() JSON output has wrong SSID. got=%q", connWithSecretData.SSID)
}
if connWithSecretData.Passphrase != "password" {
t.Errorf("runShow() JSON output has wrong passphrase. got=%q", connWithSecretData.Passphrase)
}
// Test case: network found, but not known (no secret)
buf.Reset()
if err := runShow(&buf, true, "GET off my LAN", mockBackend); err != nil {
// This should not fail, just return no secret.
t.Fatalf("runShow() with network without secret failed: %v", err)
}
// Re-initialize the struct to avoid carrying over the passphrase
connWithSecretData = connectionWithSecret{}
if err := json.Unmarshal(buf.Bytes(), &connWithSecretData); err != nil {
t.Fatalf("runShow() output is not valid JSON: %v. got=%q", err, buf.String())
}
if connWithSecretData.SSID != "GET off my LAN" {
t.Errorf("runShow() JSON output has wrong SSID. got=%q", connWithSecretData.SSID)
}
if connWithSecretData.Passphrase != "" {
t.Errorf("runShow() JSON output should have empty passphrase. got=%q", connWithSecretData.Passphrase)
}
}
func TestRunConnect(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
// Test case: connect to a new network with a passphrase
if err := runConnect(&buf, "new-network", "new-password", wifi.SecurityWPA, false, RetryConfig{Interval: time.Second}, mockBackend); err != nil {
t.Fatalf("runConnect() with passphrase failed: %v", err)
}
// Check if the network was added
networks, err := mockBackend.BuildNetworkList(false)
if err != nil {
t.Fatalf("failed to get network list: %v", err)
}
found := false
for _, n := range networks {
if n.SSID == "new-network" {
found = true
break
}
}
if !found {
t.Errorf("runConnect() did not add the new network")
}
// Test case: connect to a known network without a passphrase
buf.Reset()
if err := runConnect(&buf, "Password is password", "", wifi.SecurityWPA, false, RetryConfig{Interval: time.Second}, mockBackend); err != nil {
t.Fatalf("runConnect() without passphrase failed: %v", err)
}
// Check if the network is active
networks, err = mockBackend.BuildNetworkList(false)
if err != nil {
t.Fatalf("failed to get network list: %v", err)
}
found = false
for _, n := range networks {
if n.SSID == "Password is password" {
if n.IsActive {
found = true
}
break
}
}
if !found {
t.Errorf("runConnect() did not activate the existing network")
}
}
func TestRunRadio(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
if err := runRadio(&buf, "off", mockBackend); err != nil {
t.Fatalf("runRadio(off) failed: %v", err)
}
enabled, err := mockBackend.IsWirelessEnabled()
if err != nil {
t.Fatalf("IsWirelessEnabled() failed: %v", err)
}
if enabled {
t.Fatalf("expected wireless to be disabled")
}
buf.Reset()
if err := runRadio(&buf, "on", mockBackend); err != nil {
t.Fatalf("runRadio(on) failed: %v", err)
}
enabled, err = mockBackend.IsWirelessEnabled()
if err != nil {
t.Fatalf("IsWirelessEnabled() failed: %v", err)
}
if !enabled {
t.Fatalf("expected wireless to be enabled")
}
buf.Reset()
if err := runRadio(&buf, "toggle", mockBackend); err != nil {
t.Fatalf("runRadio(toggle) failed: %v", err)
}
enabled, err = mockBackend.IsWirelessEnabled()
if err != nil {
t.Fatalf("IsWirelessEnabled() failed: %v", err)
}
if enabled {
t.Fatalf("expected wireless to be disabled after toggle")
}
buf.Reset()
if err := runRadio(&buf, "", mockBackend); err != nil {
t.Fatalf("runRadio(default toggle) failed: %v", err)
}
enabled, err = mockBackend.IsWirelessEnabled()
if err != nil {
t.Fatalf("IsWirelessEnabled() failed: %v", err)
}
if !enabled {
t.Fatalf("expected wireless to be enabled after default toggle")
}
}
func TestRunRadioInvalidAction(t *testing.T) {
mockBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
var buf bytes.Buffer
err = runRadio(&buf, "wat", mockBackend)
if err == nil {
t.Fatal("expected invalid action error")
}
if !strings.Contains(err.Error(), "invalid radio action") {
t.Fatalf("unexpected error: %v", err)
}
}
type flakyBackend struct {
*mock.MockBackend
failCount int
maxFails int
scannedSSIDs []string // SSIDs that were scanned
}
func (f *flakyBackend) BuildNetworkList(shouldScan bool) ([]wifi.Connection, error) {
if shouldScan {
f.scannedSSIDs = append(f.scannedSSIDs, "any")
}
return f.MockBackend.BuildNetworkList(shouldScan)
}
func (f *flakyBackend) JoinNetwork(ssid, passphrase string, security wifi.SecurityType, isHidden bool) error {
if f.failCount < f.maxFails {
f.failCount++
return errors.New("transient failure")
}
return f.MockBackend.JoinNetwork(ssid, passphrase, security, isHidden)
}
func TestRunConnectRetry(t *testing.T) {
baseBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
mb, ok := baseBackend.(*mock.MockBackend)
if !ok {
t.Fatalf("expected *mock.MockBackend, got %T", baseBackend)
}
// fail count = 2.
// 1st try: fail (no scan).
// 2nd try: fail (scan, immediate).
// 3rd try: succeed (after sleep).
// Total time ~5s.
fb := &flakyBackend{
MockBackend: mb,
maxFails: 2,
}
var buf bytes.Buffer
retryInterval := longDuration
retryTotal := retryInterval * 3
// We set ActionSleep to 0 to make the mock actions fast, only our loop sleep matters.
fb.MockBackend.ActionSleep = 0
start := time.Now()
// Using passphrase triggers JoinNetwork which we overrode
if err := runConnect(&buf, "retry-network", "password", wifi.SecurityWPA, false, RetryConfig{Total: retryTotal, Interval: retryInterval}, fb); err != nil {
t.Fatalf("runConnect() with retry failed: %v", err)
}
duration := time.Since(start)
if duration < retryInterval {
t.Fatalf("expected at least one retry sleep (%v), got %v", retryInterval, duration)
}
if duration > retryInterval*4 {
t.Fatalf("retry path took unexpectedly long: %v", duration)
}
// Check if scan was performed
if len(fb.scannedSSIDs) == 0 {
t.Errorf("expected at least one scan attempt, got 0")
}
// Check output for retry messages
output := buf.String()
// We expect 2 failures:
// 1. "Quick connect failed..."
// 2. "Connection failed: ... Retrying in <interval>..."
if !strings.Contains(output, "Quick connect failed") {
t.Errorf("expected fast retry message, got:\n%s", output)
}
if !strings.Contains(output, "Connection failed: \"transient failure\"") {
t.Errorf("expected regular retry message, got:\n%s", output)
}
}
func TestRunConnectFastRetry(t *testing.T) {
baseBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
mb, ok := baseBackend.(*mock.MockBackend)
if !ok {
t.Fatalf("expected *mock.MockBackend, got %T", baseBackend)
}
// fail count = 1.
// 1st try: fail (no scan).
// 2nd try: succeed (scan, immediate).
// Total time < 1s.
fb := &flakyBackend{
MockBackend: mb,
maxFails: 1,
}
var buf bytes.Buffer
retryTotal := longDuration * 3
retryInterval := longDuration
// We set ActionSleep to 0 to make the mock actions fast.
fb.MockBackend.ActionSleep = 0
start := time.Now()
// Using passphrase triggers JoinNetwork which we overrode
if err := runConnect(&buf, "retry-network", "password", wifi.SecurityWPA, false, RetryConfig{Total: retryTotal, Interval: retryInterval}, fb); err != nil {
t.Fatalf("runConnect() with fast retry failed: %v", err)
}
duration := time.Since(start)
if duration > retryInterval {
t.Errorf("expected fast retry path to avoid sleeping, took %v", duration)
}
// Check if scan was performed
if len(fb.scannedSSIDs) == 0 {
t.Errorf("expected at least one scan attempt, got 0")
}
output := buf.String()
if !strings.Contains(output, "Quick connect failed") {
t.Errorf("expected fast retry message, got:\n%s", output)
}
}
func TestRunConnectCustomRetryInterval(t *testing.T) {
baseBackend, err := mock.New()
if err != nil {
t.Fatalf("failed to create mock backend: %v", err)
}
mb, ok := baseBackend.(*mock.MockBackend)
if !ok {
t.Fatalf("expected *mock.MockBackend, got %T", baseBackend)
}
// fail count = 2.
// 1st try: fail (no scan).
// 2nd try: fail (scan, immediate).
// 3rd try: succeed (after sleep).
fb := &flakyBackend{
MockBackend: mb,
maxFails: 2,
}
var buf bytes.Buffer
retryInterval := shortDuration
retryTotal := retryInterval * 3
fb.MockBackend.ActionSleep = 0
start := time.Now()
if err := runConnect(&buf, "retry-network", "password", wifi.SecurityWPA, false, RetryConfig{Total: retryTotal, Interval: retryInterval}, fb); err != nil {
t.Fatalf("runConnect() failed: %v", err)
}
duration := time.Since(start)
if duration < retryInterval {
t.Fatalf("expected at least one sleep of %v, got %v", retryInterval, duration)
}
if duration > retryInterval*4 {
t.Fatalf("custom retry interval path took unexpectedly long: %v", duration)
}
output := buf.String()
if !strings.Contains(output, fmt.Sprintf("Retrying in %v", retryInterval)) {
t.Errorf("expected retry message with configured interval, got:\n%s", output)
}
}
func init() {
mock.DefaultActionSleep = 0
}
func TestParseSecurityType(t *testing.T) {
tests := []struct {
input string
want wifi.SecurityType
wantErr bool
}{
{"open", wifi.SecurityOpen, false},
{"wep", wifi.SecurityWEP, false},
{"wpa", wifi.SecurityWPA, false},
{"", wifi.SecurityUnknown, true},
{"WPA", wifi.SecurityUnknown, true},
{"invalid", wifi.SecurityUnknown, true},
}
for _, tt := range tests {
got, err := parseSecurityType(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("parseSecurityType(%q) expected error, got nil", tt.input)
}
} else {
if err != nil {
t.Errorf("parseSecurityType(%q) unexpected error: %v", tt.input, err)
}
if got != tt.want {
t.Errorf("parseSecurityType(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}
}
func TestParseRetryConfig(t *testing.T) {
tests := []struct {
input string
wantTotal time.Duration
wantInterval time.Duration
wantErr bool
}{
{"", 0, defaultRetryInterval, false},
{"60s", 60 * time.Second, defaultRetryInterval, false},
{"2m", 2 * time.Minute, defaultRetryInterval, false},
{"2m:20s", 2 * time.Minute, 20 * time.Second, false},
{"invalid", 0, 0, true},
{"60s:invalid", 0, 0, true},
}
for _, tt := range tests {
got, err := parseRetryConfig(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("parseRetryConfig(%q) expected error, got nil", tt.input)
}
} else {
if err != nil {
t.Errorf("parseRetryConfig(%q) unexpected error: %v", tt.input, err)
}
if got.Total != tt.wantTotal {
t.Errorf("parseRetryConfig(%q).Total = %v, want %v", tt.input, got.Total, tt.wantTotal)
}
if got.Interval != tt.wantInterval {
t.Errorf("parseRetryConfig(%q).Interval = %v, want %v", tt.input, got.Interval, tt.wantInterval)
}
}
}
}
func TestFilterVisibleConnections(t *testing.T) {
connections := []wifi.Connection{
{SSID: "visible1", IsVisible: true},
{SSID: "hidden1", IsVisible: false},
{SSID: "visible2", IsVisible: true},
{SSID: "hidden2", IsVisible: false},
}
visible := filterVisibleConnections(connections)
if len(visible) != 2 {
t.Fatalf("filterVisibleConnections() returned %d connections, want 2", len(visible))
}
for _, c := range visible {
if !c.IsVisible {
t.Errorf("filterVisibleConnections() returned non-visible connection %q", c.SSID)
}
}
// Empty input
if got := filterVisibleConnections(nil); got != nil {
t.Errorf("filterVisibleConnections(nil) = %v, want nil", got)
}
}
func TestFindConnectionBySSID(t *testing.T) {
connections := []wifi.Connection{
{SSID: "NetworkA"},
{SSID: "NetworkB"},
{SSID: "NetworkC"},
}
c, found := findConnectionBySSID(connections, "NetworkB")
if !found {
t.Fatal("findConnectionBySSID() did not find existing network")
}
if c.SSID != "NetworkB" {
t.Errorf("findConnectionBySSID() returned wrong network: got %q, want %q", c.SSID, "NetworkB")
}
_, found = findConnectionBySSID(connections, "NotThere")
if found {
t.Error("findConnectionBySSID() returned true for missing network")
}
_, found = findConnectionBySSID(nil, "NetworkA")
if found {
t.Error("findConnectionBySSID() returned true for empty slice")
}
}