1package wifi 2 3import ( 4 "reflect" 5 "testing" 6) 7 8func TestInterfaceTypeString(t *testing.T) { 9 tests := []struct { 10 t InterfaceType 11 s string 12 }{ 13 { 14 t: InterfaceTypeUnspecified, 15 s: "unspecified", 16 }, 17 { 18 t: InterfaceTypeAdHoc, 19 s: "ad-hoc", 20 }, 21 { 22 t: InterfaceTypeStation, 23 s: "station", 24 }, 25 { 26 t: InterfaceTypeAP, 27 s: "access point", 28 }, 29 { 30 t: InterfaceTypeWDS, 31 s: "wireless distribution", 32 }, 33 { 34 t: InterfaceTypeMonitor, 35 s: "monitor", 36 }, 37 { 38 t: InterfaceTypeMeshPoint, 39 s: "mesh point", 40 }, 41 { 42 t: InterfaceTypeP2PClient, 43 s: "P2P client", 44 }, 45 { 46 t: InterfaceTypeP2PGroupOwner, 47 s: "P2P group owner", 48 }, 49 { 50 t: InterfaceTypeP2PDevice, 51 s: "P2P device", 52 }, 53 { 54 t: InterfaceTypeOCB, 55 s: "outside context of BSS", 56 }, 57 { 58 t: InterfaceTypeNAN, 59 s: "near-me area network", 60 }, 61 { 62 t: InterfaceTypeNAN + 1, 63 s: "unknown(13)", 64 }, 65 } 66 67 for _, tt := range tests { 68 t.Run(tt.s, func(t *testing.T) { 69 if want, got := tt.s, tt.t.String(); want != got { 70 t.Fatalf("unexpected interface type string:\n- want: %q\n- got: %q", 71 want, got) 72 } 73 }) 74 } 75} 76 77func TestBSSStatusString(t *testing.T) { 78 tests := []struct { 79 t BSSStatus 80 s string 81 }{ 82 { 83 t: BSSStatusAuthenticated, 84 s: "authenticated", 85 }, 86 { 87 t: BSSStatusAssociated, 88 s: "associated", 89 }, 90 { 91 t: BSSStatusIBSSJoined, 92 s: "IBSS joined", 93 }, 94 { 95 t: 3, 96 s: "unknown(3)", 97 }, 98 } 99 100 for _, tt := range tests { 101 t.Run(tt.s, func(t *testing.T) { 102 if want, got := tt.s, tt.t.String(); want != got { 103 t.Fatalf("unexpected BSS status string:\n- want: %q\n- got: %q", 104 want, got) 105 } 106 }) 107 } 108} 109 110func Test_parseIEs(t *testing.T) { 111 tests := []struct { 112 name string 113 b []byte 114 ies []ie 115 err error 116 }{ 117 { 118 name: "empty", 119 }, 120 { 121 name: "too short", 122 b: []byte{0x00}, 123 err: errInvalidIE, 124 }, 125 { 126 name: "length too long", 127 b: []byte{0x00, 0xff, 0x00}, 128 err: errInvalidIE, 129 }, 130 { 131 name: "OK one", 132 b: []byte{0x00, 0x03, 'f', 'o', 'o'}, 133 ies: []ie{{ 134 ID: 0, 135 Data: []byte("foo"), 136 }}, 137 }, 138 { 139 name: "OK three", 140 b: []byte{ 141 0x00, 0x03, 'f', 'o', 'o', 142 0x01, 0x00, 143 0x02, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 144 }, 145 ies: []ie{ 146 { 147 ID: 0, 148 Data: []byte("foo"), 149 }, 150 { 151 ID: 1, 152 Data: []byte{}, 153 }, 154 { 155 ID: 2, 156 Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}, 157 }, 158 }, 159 }, 160 } 161 162 for _, tt := range tests { 163 t.Run(tt.name, func(t *testing.T) { 164 ies, err := parseIEs(tt.b) 165 166 if want, got := tt.err, err; want != got { 167 t.Fatalf("unexpected error:\n- want: %v\n- got: %v", 168 want, got) 169 } 170 if err != nil { 171 t.Logf("err: %v", err) 172 return 173 } 174 175 if want, got := tt.ies, ies; !reflect.DeepEqual(want, got) { 176 t.Fatalf("unexpected ies:\n- want: %v\n- got: %v", 177 want, got) 178 } 179 }) 180 } 181} 182