gopkg.in/hugelgupf/u-root.v2@v2.0.0-20180831055005-3f8fdb0ce09d/cmds/wifi/wifi_test.go (about) 1 // Copyright 2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 func TestIwconfigRE(t *testing.T) { 13 testcases := []struct { 14 s string 15 exp bool 16 }{ 17 {"blahblahblah\nlo no wireless extensions.\n", false}, 18 {"blahblahblah\nwlp4s0 IEEE 802.11 ESSID:\"stub\"", true}, 19 {"blahblahblah\n Mode:Managed Frequency:5.58 GHz Access Point: 00:00:00:00:00:00\n", false}, 20 } 21 for _, test := range testcases { 22 if out := iwconfigRE.MatchString(test.s); out != test.exp { 23 t.Errorf("%s\ngot:%v\nwant:%v", test.s, out, test.exp) 24 } 25 } 26 } 27 28 func TestParseIwconfig(t *testing.T) { 29 testcases := []struct { 30 name string 31 o []byte 32 exp []string 33 }{ 34 { 35 name: "nil input", 36 o: nil, 37 exp: nil, 38 }, 39 { 40 name: "empty string input", 41 o: []byte(""), 42 exp: nil, 43 }, 44 { 45 name: "No Wireless in input", 46 o: []byte(` 47 lo no wireless extensions. 48 49 eno1 no wireless extensions. 50 51 `), 52 exp: nil, 53 }, 54 { 55 name: "One (1) Wireless extension", 56 o: []byte(` 57 wlp4s0 IEEE 802.11 ESSID:"stub" 58 Mode:Managed Frequency:5.58 GHz Access Point: 00:00:00:00:00:00 59 Bit Rate=22 Mb/s Tx-Power=22 dBm 60 Retry short limit:7 RTS thr:off Fragment thr:off 61 Power Management:on 62 Link Quality=27/70 Signal level=-53 dBm 63 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 64 Tx excessive retries:0 Invalid misc:0 Missed beacon:0 65 66 lo no wireless extensions. 67 68 enp0s31f6 no wireless extensions. 69 70 `), 71 exp: []string{"wlp4s0"}, 72 }, 73 { 74 name: "Two (2) Wireless extensions", 75 o: []byte(` 76 wlp4s0 IEEE 802.11 ESSID:"stub" 77 Mode:Managed Frequency:5.58 GHz Access Point: 00:00:00:00:00:00 78 Bit Rate=22 Mb/s Tx-Power=22 dBm 79 Retry short limit:7 RTS thr:off Fragment thr:off 80 Power Management:on 81 Link Quality=27/70 Signal level=-53 dBm 82 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 83 Tx excessive retries:0 Invalid misc:0 Missed beacon:0 84 85 wlp4s1 IEEE 802.11 ESSID:"stub" 86 Mode:Managed Frequency:5.58 GHz Access Point: 00:00:00:00:00:00 87 Bit Rate=22 Mb/s Tx-Power=22 dBm 88 Retry short limit:7 RTS thr:off Fragment thr:off 89 Power Management:on 90 Link Quality=27/70 Signal level=-53 dBm 91 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 92 Tx excessive retries:0 Invalid misc:0 Missed beacon:0 93 94 lo no wireless extensions. 95 96 enp0s31f6 no wireless extensions. 97 98 `), 99 exp: []string{"wlp4s0", "wlp4s1"}, 100 }, 101 } 102 103 for _, test := range testcases { 104 out := parseIwconfig(test.o) 105 if !reflect.DeepEqual(out, test.exp) { 106 t.Errorf("%v\ngot:%v\nwant:%v", test.name, out, test.exp) 107 } 108 } 109 }