blob: 153542e99964f947fc98314600f4f8865dba83e3 [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file provides tests for individual messages. It tests
6// UserBoundNlMessageFactory's ability to create specific message types and it
7// tests the various UserBoundNlMessage types' ability to parse those
8// messages.
9
10// This file tests the public interface to Config80211.
11
12#include "shill/config80211.h"
13
14#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16#include <netlink/attr.h>
17#include <netlink/netlink.h>
18
19#include <string>
20#include <vector>
21
22#include <base/bind.h>
23
24#include "shill/mock_nl80211_socket.h"
25#include "shill/nl80211_socket.h"
26#include "shill/user_bound_nlmessage.h"
27
28using base::Bind;
29using base::Unretained;
30using std::string;
31using std::vector;
32using testing::_;
33using testing::Return;
34using testing::Test;
35
36namespace shill {
37
38namespace {
39
40// These data blocks have been collected by shill using Config80211 while,
41// simultaneously (and manually) comparing shill output with that of the 'iw'
42// code from which it was derived. The test strings represent the raw packet
43// data coming from the kernel. The comments above each of these strings is
44// the markup that "iw" outputs for ech of these packets.
45
46// These constants are consistent throughout the packets, below.
47
48const uint32_t kExpectedIfIndex = 4;
49const uint8_t kExpectedWifi = 0;
50const char kExpectedMacAddress[] = "c0:3f:0e:77:e8:7f";
51
52
53// wlan0 (phy #0): scan started
54
55const uint32_t kScanFrequencyTrigger[] = {
56 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
57 2452, 2457, 2462, 2467, 2472, 2484, 5180, 5200,
58 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520,
59 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
60 5700, 5745, 5765, 5785, 5805, 5825
61};
62
63const unsigned char kNL80211_CMD_TRIGGER_SCAN[] = {
64 0x68, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x21, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
68 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2d, 0x00,
69 0x04, 0x00, 0x00, 0x00, 0x34, 0x01, 0x2c, 0x00,
70 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
71 0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00,
72 0x08, 0x00, 0x02, 0x00, 0x76, 0x09, 0x00, 0x00,
73 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
74 0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00,
75 0x08, 0x00, 0x05, 0x00, 0x85, 0x09, 0x00, 0x00,
76 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
77 0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00,
78 0x08, 0x00, 0x08, 0x00, 0x94, 0x09, 0x00, 0x00,
79 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
80 0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00,
81 0x08, 0x00, 0x0b, 0x00, 0xa3, 0x09, 0x00, 0x00,
82 0x08, 0x00, 0x0c, 0x00, 0xa8, 0x09, 0x00, 0x00,
83 0x08, 0x00, 0x0d, 0x00, 0xb4, 0x09, 0x00, 0x00,
84 0x08, 0x00, 0x0e, 0x00, 0x3c, 0x14, 0x00, 0x00,
85 0x08, 0x00, 0x0f, 0x00, 0x50, 0x14, 0x00, 0x00,
86 0x08, 0x00, 0x10, 0x00, 0x64, 0x14, 0x00, 0x00,
87 0x08, 0x00, 0x11, 0x00, 0x78, 0x14, 0x00, 0x00,
88 0x08, 0x00, 0x12, 0x00, 0x8c, 0x14, 0x00, 0x00,
89 0x08, 0x00, 0x13, 0x00, 0xa0, 0x14, 0x00, 0x00,
90 0x08, 0x00, 0x14, 0x00, 0xb4, 0x14, 0x00, 0x00,
91 0x08, 0x00, 0x15, 0x00, 0xc8, 0x14, 0x00, 0x00,
92 0x08, 0x00, 0x16, 0x00, 0x7c, 0x15, 0x00, 0x00,
93 0x08, 0x00, 0x17, 0x00, 0x90, 0x15, 0x00, 0x00,
94 0x08, 0x00, 0x18, 0x00, 0xa4, 0x15, 0x00, 0x00,
95 0x08, 0x00, 0x19, 0x00, 0xb8, 0x15, 0x00, 0x00,
96 0x08, 0x00, 0x1a, 0x00, 0xcc, 0x15, 0x00, 0x00,
97 0x08, 0x00, 0x1b, 0x00, 0xe0, 0x15, 0x00, 0x00,
98 0x08, 0x00, 0x1c, 0x00, 0xf4, 0x15, 0x00, 0x00,
99 0x08, 0x00, 0x1d, 0x00, 0x08, 0x16, 0x00, 0x00,
100 0x08, 0x00, 0x1e, 0x00, 0x1c, 0x16, 0x00, 0x00,
101 0x08, 0x00, 0x1f, 0x00, 0x30, 0x16, 0x00, 0x00,
102 0x08, 0x00, 0x20, 0x00, 0x44, 0x16, 0x00, 0x00,
103 0x08, 0x00, 0x21, 0x00, 0x71, 0x16, 0x00, 0x00,
104 0x08, 0x00, 0x22, 0x00, 0x85, 0x16, 0x00, 0x00,
105 0x08, 0x00, 0x23, 0x00, 0x99, 0x16, 0x00, 0x00,
106 0x08, 0x00, 0x24, 0x00, 0xad, 0x16, 0x00, 0x00,
107 0x08, 0x00, 0x25, 0x00, 0xc1, 0x16, 0x00, 0x00,
108 0x08, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
109};
110
111
112// wlan0 (phy #0): scan finished: 2412 2417 2422 2427 2432 2437 2442 2447 2452
113// 2457 2462 2467 2472 2484 5180 5200 5220 5240 5260 5280 5300 5320 5500 5520
114// 5540 5560 5580 5600 5620 5640 5660 5680 5700 5745 5765 5785 5805 5825, ""
115
116const uint32_t kScanFrequencyResults[] = {
117 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
118 2452, 2457, 2462, 2467, 2472, 2484, 5180, 5200,
119 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520,
120 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
121 5700, 5745, 5765, 5785, 5805, 5825
122};
123
124const unsigned char kNL80211_CMD_NEW_SCAN_RESULTS[] = {
125 0x68, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x22, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
129 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2d, 0x00,
130 0x04, 0x00, 0x00, 0x00, 0x34, 0x01, 0x2c, 0x00,
131 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
132 0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00,
133 0x08, 0x00, 0x02, 0x00, 0x76, 0x09, 0x00, 0x00,
134 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
135 0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00,
136 0x08, 0x00, 0x05, 0x00, 0x85, 0x09, 0x00, 0x00,
137 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
138 0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00,
139 0x08, 0x00, 0x08, 0x00, 0x94, 0x09, 0x00, 0x00,
140 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
141 0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00,
142 0x08, 0x00, 0x0b, 0x00, 0xa3, 0x09, 0x00, 0x00,
143 0x08, 0x00, 0x0c, 0x00, 0xa8, 0x09, 0x00, 0x00,
144 0x08, 0x00, 0x0d, 0x00, 0xb4, 0x09, 0x00, 0x00,
145 0x08, 0x00, 0x0e, 0x00, 0x3c, 0x14, 0x00, 0x00,
146 0x08, 0x00, 0x0f, 0x00, 0x50, 0x14, 0x00, 0x00,
147 0x08, 0x00, 0x10, 0x00, 0x64, 0x14, 0x00, 0x00,
148 0x08, 0x00, 0x11, 0x00, 0x78, 0x14, 0x00, 0x00,
149 0x08, 0x00, 0x12, 0x00, 0x8c, 0x14, 0x00, 0x00,
150 0x08, 0x00, 0x13, 0x00, 0xa0, 0x14, 0x00, 0x00,
151 0x08, 0x00, 0x14, 0x00, 0xb4, 0x14, 0x00, 0x00,
152 0x08, 0x00, 0x15, 0x00, 0xc8, 0x14, 0x00, 0x00,
153 0x08, 0x00, 0x16, 0x00, 0x7c, 0x15, 0x00, 0x00,
154 0x08, 0x00, 0x17, 0x00, 0x90, 0x15, 0x00, 0x00,
155 0x08, 0x00, 0x18, 0x00, 0xa4, 0x15, 0x00, 0x00,
156 0x08, 0x00, 0x19, 0x00, 0xb8, 0x15, 0x00, 0x00,
157 0x08, 0x00, 0x1a, 0x00, 0xcc, 0x15, 0x00, 0x00,
158 0x08, 0x00, 0x1b, 0x00, 0xe0, 0x15, 0x00, 0x00,
159 0x08, 0x00, 0x1c, 0x00, 0xf4, 0x15, 0x00, 0x00,
160 0x08, 0x00, 0x1d, 0x00, 0x08, 0x16, 0x00, 0x00,
161 0x08, 0x00, 0x1e, 0x00, 0x1c, 0x16, 0x00, 0x00,
162 0x08, 0x00, 0x1f, 0x00, 0x30, 0x16, 0x00, 0x00,
163 0x08, 0x00, 0x20, 0x00, 0x44, 0x16, 0x00, 0x00,
164 0x08, 0x00, 0x21, 0x00, 0x71, 0x16, 0x00, 0x00,
165 0x08, 0x00, 0x22, 0x00, 0x85, 0x16, 0x00, 0x00,
166 0x08, 0x00, 0x23, 0x00, 0x99, 0x16, 0x00, 0x00,
167 0x08, 0x00, 0x24, 0x00, 0xad, 0x16, 0x00, 0x00,
168 0x08, 0x00, 0x25, 0x00, 0xc1, 0x16, 0x00, 0x00,
169 0x08, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
170};
171
172
173// wlan0: new station c0:3f:0e:77:e8:7f
174
175const uint32_t kNewStationExpectedGeneration = 275;
176
177const unsigned char kNL80211_CMD_NEW_STATION[] = {
178 0x34, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180 0x13, 0x01, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
181 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
182 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
183 0x08, 0x00, 0x2e, 0x00, 0x13, 0x01, 0x00, 0x00,
184 0x04, 0x00, 0x15, 0x00,
185};
186
187
188// wlan0 (phy #0): auth c0:3f:0e:77:e8:7f -> 48:5d:60:77:2d:cf status: 0:
189// Successful [frame: b0 00 3a 01 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f c0
190// 3f 0e 77 e8 7f 30 07 00 00 02 00 00 00]
191
192const unsigned char kAuthenticateFrame[] = {
193 0xb0, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
194 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
195 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x30, 0x07,
196 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
197};
198
199const unsigned char kNL80211_CMD_AUTHENTICATE[] = {
200 0x48, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 0x25, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
203 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
204 0x04, 0x00, 0x00, 0x00, 0x22, 0x00, 0x33, 0x00,
205 0xb0, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
206 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
207 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x30, 0x07,
208 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
209};
210
211
212// wlan0 (phy #0): assoc c0:3f:0e:77:e8:7f -> 48:5d:60:77:2d:cf status: 0:
213// Successful [frame: 10 00 3a 01 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f c0 3f 0e
214// 77 e8 7f 40 07 01 04 00 00 01 c0 01 08 82 84 8b 96 0c 12 18 24 32 04 30 48
215// 60 6c]
216
217const unsigned char kAssociateFrame[] = {
218 0x10, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
219 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
220 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x40, 0x07,
221 0x01, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x01, 0x08,
222 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24,
223 0x32, 0x04, 0x30, 0x48, 0x60, 0x6c
224};
225
226const unsigned char kNL80211_CMD_ASSOCIATE[] = {
227 0x58, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229 0x26, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
230 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
231 0x04, 0x00, 0x00, 0x00, 0x32, 0x00, 0x33, 0x00,
232 0x10, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
233 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
234 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x40, 0x07,
235 0x01, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x01, 0x08,
236 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24,
237 0x32, 0x04, 0x30, 0x48, 0x60, 0x6c, 0x00, 0x00,
238};
239
240
241// wlan0 (phy #0): connected to c0:3f:0e:77:e8:7f
242
243const uint16_t kExpectedConnectStatus = 0;
244
245const unsigned char kNL80211_CMD_CONNECT[] = {
246 0x4c, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 0x2e, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
249 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
250 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
251 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
252 0x06, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x14, 0x00, 0x4e, 0x00, 0x01, 0x08, 0x82, 0x84,
254 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x32, 0x04,
255 0x30, 0x48, 0x60, 0x6c,
256};
257
258
259// wlan0 (phy #0): deauth c0:3f:0e:77:e8:7f -> ff:ff:ff:ff:ff:ff reason 2:
260// Previous authentication no longer valid [frame: c0 00 00 00 ff ff ff ff
261// ff ff c0 3f 0e 77 e8 7f c0 3f 0e 77 e8 7f c0 0e 02 00]
262
263const unsigned char kDeauthenticateFrame[] = {
264 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
265 0xff, 0xff, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
266 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0xc0, 0x0e,
267 0x02, 0x00
268};
269
270const unsigned char kNL80211_CMD_DEAUTHENTICATE[] = {
271 0x44, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 0x27, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
274 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
275 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x33, 0x00,
276 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
277 0xff, 0xff, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
278 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0xc0, 0x0e,
279 0x02, 0x00, 0x00, 0x00,
280};
281
282
283// wlan0 (phy #0): disconnected (by AP) reason: 2: Previous authentication no
284// longer valid
285
286const uint16_t kExpectedDisconnectReason = 2;
287
288const unsigned char kNL80211_CMD_DISCONNECT[] = {
289 0x30, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x30, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
293 0x04, 0x00, 0x00, 0x00, 0x06, 0x00, 0x36, 0x00,
294 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x47, 0x00,
295};
296
297
298// wlan0 (phy #0): connection quality monitor event: peer c0:3f:0e:77:e8:7f
299// didn't ACK 50 packets
300
301const uint32_t kExpectedCqmNotAcked = 50;
302
303const unsigned char kNL80211_CMD_NOTIFY_CQM[] = {
304 0x3c, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x40, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
308 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
309 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
310 0x0c, 0x00, 0x5e, 0x00, 0x08, 0x00, 0x04, 0x00,
311 0x32, 0x00, 0x00, 0x00,
312};
313
314
315// wlan0 (phy #0): disassoc 48:5d:60:77:2d:cf -> c0:3f:0e:77:e8:7f reason 3:
316// Deauthenticated because sending station is [frame: a0 00 00 00 c0 3f 0e
317// 77 e8 7f 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f 00 00 03 00]
318
319const unsigned char kDisassociateFrame[] = {
320 0xa0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x0e, 0x77,
321 0xe8, 0x7f, 0x48, 0x5d, 0x60, 0x77, 0x2d, 0xcf,
322 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
323 0x03, 0x00
324};
325
326const unsigned char kNL80211_CMD_DISASSOCIATE[] = {
327 0x44, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329 0x28, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
330 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
331 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x33, 0x00,
332 0xa0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x0e, 0x77,
333 0xe8, 0x7f, 0x48, 0x5d, 0x60, 0x77, 0x2d, 0xcf,
334 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
335 0x03, 0x00, 0x00, 0x00,
336};
337
338} // namespace {}
339
340class Config80211Test : public Test {
341 public:
342 Config80211Test() : config80211_(Config80211::GetInstance()) {}
343 void SetupConfig80211Object() {
344 EXPECT_NE(config80211_, reinterpret_cast<Config80211 *>(NULL));
345 config80211_->sock_ = &socket_;
346 EXPECT_TRUE(config80211_->Init(reinterpret_cast<EventDispatcher *>(NULL)));
Wade Guthried6153612012-08-23 11:36:14 -0700347 config80211_->Reset();
Wade Guthrie0d438532012-05-18 14:18:50 -0700348 }
349
350 Config80211 *config80211_;
351 MockNl80211Socket socket_;
352};
353
354class TestCallbackObject {
355 public:
356 TestCallbackObject() : callback_(Bind(&TestCallbackObject::MessageHandler,
357 Unretained(this))) { }
358 void MessageHandler(const UserBoundNlMessage &msg) {
359 }
360 const Config80211::Callback &GetCallback() const { return callback_; }
361
362 private:
363 Config80211::Callback callback_;
364};
365
366MATCHER_P(IsEqualToCallback, callback, "") {
367 const Config80211::Callback *arg_cb =
368 reinterpret_cast<const Config80211::Callback *>(arg);
369 const Config80211::Callback *callback_cb =
370 reinterpret_cast<const Config80211::Callback *>(callback);
371 if (arg_cb == callback_cb)
372 return true;
373 if (arg_cb == reinterpret_cast<const Config80211::Callback *>(NULL))
374 return false;
375 if (callback_cb == reinterpret_cast<const Config80211::Callback *>(NULL))
376 return arg_cb->is_null();
377 return arg_cb->Equals(*callback_cb);
378}
379
380TEST_F(Config80211Test, AddLinkTest) {
381 SetupConfig80211Object();
382
383 // Create a default callback.
384 TestCallbackObject callback_object;
385
Wade Guthried6153612012-08-23 11:36:14 -0700386 // Install the callback and subscribe to events using it, wifi down
387 // (shouldn't actually send the subscription request).
388 EXPECT_CALL(socket_, AddGroupMembership(_)).Times(0);
389 EXPECT_CALL(socket_, DisableSequenceChecking()).Times(0);
390 EXPECT_CALL(socket_, SetNetlinkCallback(_,_)).Times(0);
391
Wade Guthrie0d438532012-05-18 14:18:50 -0700392 config80211_->SetDefaultCallback(callback_object.GetCallback());
Wade Guthried6153612012-08-23 11:36:14 -0700393 Config80211::EventType scan_event = Config80211::kEventTypeScan;
394 string scan_event_string;
395 EXPECT_TRUE(Config80211::GetEventTypeString(scan_event, &scan_event_string));
396 EXPECT_TRUE(config80211_->SubscribeToEvents(scan_event));
397
398 // Wifi up, should subscribe to events.
399 EXPECT_CALL(socket_, AddGroupMembership(scan_event_string))
Wade Guthrie0d438532012-05-18 14:18:50 -0700400 .WillOnce(Return(true));
401 EXPECT_CALL(socket_, DisableSequenceChecking())
402 .WillOnce(Return(true));
403 EXPECT_CALL(socket_, SetNetlinkCallback(
404 _, IsEqualToCallback(&callback_object.GetCallback())))
405 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700406 config80211_->SetWifiState(Config80211::kWifiUp);
Wade Guthrie0d438532012-05-18 14:18:50 -0700407
Wade Guthried6153612012-08-23 11:36:14 -0700408 // Second subscribe, same event (should do nothing).
409 EXPECT_CALL(socket_, AddGroupMembership(_)).Times(0);
410 EXPECT_CALL(socket_, DisableSequenceChecking()).Times(0);
411 EXPECT_CALL(socket_, SetNetlinkCallback(_,_)).Times(0);
412 EXPECT_TRUE(config80211_->SubscribeToEvents(scan_event));
Wade Guthrie0d438532012-05-18 14:18:50 -0700413
Wade Guthried6153612012-08-23 11:36:14 -0700414 // Bring the wifi back down.
415 config80211_->SetWifiState(Config80211::kWifiDown);
416
417 // Subscribe to a new event with the wifi down (should still do nothing).
418 Config80211::EventType mlme_event = Config80211::kEventTypeMlme;
419 string mlme_event_string;
420 EXPECT_TRUE(Config80211::GetEventTypeString(mlme_event, &mlme_event_string));
421 EXPECT_TRUE(config80211_->SubscribeToEvents(mlme_event));
422
423 // Wifi up (again), should subscribe to the original scan event and the new
424 // mlme event.
425 EXPECT_CALL(socket_, AddGroupMembership(scan_event_string))
Wade Guthrie0d438532012-05-18 14:18:50 -0700426 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700427 EXPECT_CALL(socket_, AddGroupMembership(mlme_event_string))
428 .WillOnce(Return(true));
429 EXPECT_CALL(socket_, DisableSequenceChecking())
430 .Times(2)
431 .WillRepeatedly(Return(true));
432 EXPECT_CALL(socket_, SetNetlinkCallback(
433 _, IsEqualToCallback(&callback_object.GetCallback())))
434 .Times(2)
435 .WillRepeatedly(Return(true));
436 config80211_->SetWifiState(Config80211::kWifiUp);
Wade Guthrie0d438532012-05-18 14:18:50 -0700437}
438
439TEST_F(Config80211Test, NL80211_CMD_TRIGGER_SCAN) {
440 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
441 const_cast<nlmsghdr *>(
442 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN)));
443
444 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
445 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_TRIGGER_SCAN);
446
447 {
448 uint8_t value;
449 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
450 EXPECT_EQ(value, kExpectedWifi);
451 }
452
453 {
454 uint32_t value;
455 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
456 EXPECT_EQ(value, kExpectedIfIndex);
457 }
458
459 // Make sure the scan frequencies in the attribute are the ones we expect.
460 {
461 vector<uint32_t>list;
462 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
463 NL80211_ATTR_SCAN_FREQUENCIES, &list));
464 EXPECT_EQ(arraysize(kScanFrequencyTrigger), list.size());
465 int i = 0;
466 vector<uint32_t>::const_iterator j = list.begin();
467 while (j != list.end()) {
468 EXPECT_EQ(kScanFrequencyTrigger[i], *j);
469 ++i;
470 ++j;
471 }
472 }
473
474 {
475 vector<string> ssids;
476 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
477 &ssids));
478 EXPECT_EQ(ssids.size(), 1);
479 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
480 }
481
482 // Significant only in its existence.
483 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_SUPPORT_MESH_AUTH));
484}
485
486TEST_F(Config80211Test, NL80211_CMD_NEW_SCAN_RESULTS) {
487 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
488 const_cast<nlmsghdr *>(
489 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS)));
490
491 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
492 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_NEW_SCAN_RESULTS);
493
494 {
495 uint8_t value;
496 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
497 EXPECT_EQ(value, kExpectedWifi);
498 }
499
500 {
501 uint32_t value;
502 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
503 EXPECT_EQ(value, kExpectedIfIndex);
504 }
505
506 // Make sure the scan frequencies in the attribute are the ones we expect.
507 {
508 vector<uint32_t>list;
509 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
510 NL80211_ATTR_SCAN_FREQUENCIES, &list));
511 EXPECT_EQ(arraysize(kScanFrequencyResults), list.size());
512 int i = 0;
513 vector<uint32_t>::const_iterator j = list.begin();
514 while (j != list.end()) {
515 EXPECT_EQ(kScanFrequencyResults[i], *j);
516 ++i;
517 ++j;
518 }
519 }
520
521 {
522 vector<string> ssids;
523 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
524 &ssids));
525 EXPECT_EQ(ssids.size(), 1);
526 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
527 }
528
529 // Significant only in its existence.
530 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_SUPPORT_MESH_AUTH));
531}
532
533TEST_F(Config80211Test, NL80211_CMD_NEW_STATION) {
534 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
535 const_cast<nlmsghdr *>(
536 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION)));
537
538 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
539 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_NEW_STATION);
540
541 {
542 uint32_t value;
543 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
544 EXPECT_EQ(value, kExpectedIfIndex);
545 }
546
547 {
548 string value;
549 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
550 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
551 }
552
553 // TODO(wdg): Make config80211 handle nested attributes so it can deal
554 // with things like NL80211_ATTR_STA_INFO (without just calling
555 // nla_parse_nested).
556 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_STA_INFO));
557
558 {
559 uint32_t value;
560 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_GENERATION, &value));
561 EXPECT_EQ(value, kNewStationExpectedGeneration);
562 }
563}
564
565TEST_F(Config80211Test, NL80211_CMD_AUTHENTICATE) {
566 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
567 const_cast<nlmsghdr *>(
568 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE)));
569
570 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
571 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_AUTHENTICATE);
572
573 {
574 uint8_t value;
575 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
576 EXPECT_EQ(value, kExpectedWifi);
577 }
578
579 {
580 uint32_t value;
581 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
582 EXPECT_EQ(value, kExpectedIfIndex);
583 }
584
585 {
586 void *rawdata = NULL;
587 int frame_byte_count = 0;
588 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
589 &frame_byte_count));
590 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
591 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
592
593 Nl80211Frame frame(frame_data, frame_byte_count);
594 Nl80211Frame expected_frame(kAuthenticateFrame, sizeof(kAuthenticateFrame));
595
596 EXPECT_TRUE(frame.IsEqual(expected_frame));
597 }
598}
599
600TEST_F(Config80211Test, NL80211_CMD_ASSOCIATE) {
601 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
602 const_cast<nlmsghdr *>(
603 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE)));
604
605 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
606 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_ASSOCIATE);
607
608 {
609 uint8_t value;
610 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
611 EXPECT_EQ(value, kExpectedWifi);
612 }
613
614 {
615 uint32_t value;
616 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
617 EXPECT_EQ(value, kExpectedIfIndex);
618 }
619
620 {
621 void *rawdata = NULL;
622 int frame_byte_count = 0;
623 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
624 &frame_byte_count));
625 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
626 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
627
628 Nl80211Frame frame(frame_data, frame_byte_count);
629 Nl80211Frame expected_frame(kAssociateFrame, sizeof(kAssociateFrame));
630
631 EXPECT_TRUE(frame.IsEqual(expected_frame));
632 }
633}
634
635TEST_F(Config80211Test, NL80211_CMD_CONNECT) {
636 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
637 const_cast<nlmsghdr *>(
638 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT)));
639
640 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
641 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_CONNECT);
642
643 {
644 uint8_t value;
645 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
646 EXPECT_EQ(value, kExpectedWifi);
647 }
648
649 {
650 uint32_t value;
651 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
652 EXPECT_EQ(value, kExpectedIfIndex);
653 }
654
655 {
656 string value;
657 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
658 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
659 }
660
661 {
662 uint16_t value;
663 EXPECT_TRUE(message->GetU16Attribute(NL80211_ATTR_STATUS_CODE, &value));
664 EXPECT_EQ(value, kExpectedConnectStatus);
665 }
666
667 // TODO(wdg): Need to check the value of this attribute.
668 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_RESP_IE));
669}
670
671TEST_F(Config80211Test, NL80211_CMD_DEAUTHENTICATE) {
672 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
673 const_cast<nlmsghdr *>(
674 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE)));
675
676 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
677 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_DEAUTHENTICATE);
678
679 {
680 uint8_t value;
681 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
682 EXPECT_EQ(value, kExpectedWifi);
683 }
684
685 {
686 uint32_t value;
687 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
688 EXPECT_EQ(value, kExpectedIfIndex);
689 }
690
691 {
692 void *rawdata = NULL;
693 int frame_byte_count = 0;
694 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
695 &frame_byte_count));
696 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
697 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
698
699 Nl80211Frame frame(frame_data, frame_byte_count);
700 Nl80211Frame expected_frame(kDeauthenticateFrame,
701 sizeof(kDeauthenticateFrame));
702
703 EXPECT_TRUE(frame.IsEqual(expected_frame));
704 }
705}
706
707TEST_F(Config80211Test, NL80211_CMD_DISCONNECT) {
708 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
709 const_cast<nlmsghdr *>(
710 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT)));
711
712 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
713 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_DISCONNECT);
714
715 {
716 uint8_t value;
717 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
718 EXPECT_EQ(value, kExpectedWifi);
719 }
720
721 {
722 uint32_t value;
723 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
724 EXPECT_EQ(value, kExpectedIfIndex);
725 }
726
727 {
728 uint16_t value;
729 EXPECT_TRUE(message->GetU16Attribute(NL80211_ATTR_REASON_CODE, &value));
730 EXPECT_EQ(value, kExpectedDisconnectReason);
731 }
732
733 // Significant only in its existence.
734 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP));
735}
736
737TEST_F(Config80211Test, NL80211_CMD_NOTIFY_CQM) {
738 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
739 const_cast<nlmsghdr *>(
740 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM)));
741
742 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
743 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_NOTIFY_CQM);
744
745
746 {
747 uint8_t value;
748 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
749 EXPECT_EQ(value, kExpectedWifi);
750 }
751
752 {
753 uint32_t value;
754 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
755 EXPECT_EQ(value, kExpectedIfIndex);
756 }
757
758 {
759 string value;
760 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
761 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
762 }
763
764 // TODO(wdg): Make config80211 handle nested attributes so it can deal
765 // with things like NL80211_ATTR_CQM (without just calling nla_parse_nested).
766 {
767 static const nla_policy kCqmPolicy[NL80211_ATTR_CQM_MAX + 1] = {
768 { NLA_U32, 0, 0 }, // Who Knows?
769 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
770 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
771 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
772 };
773
774 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_CQM));
775 const nlattr *const_data = message->GetAttribute(NL80211_ATTR_CQM);
776 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
777 EXPECT_NE(cqm_attr, reinterpret_cast<nlattr *>(NULL));
778
779 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
780 EXPECT_EQ(nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
781 const_cast<nla_policy *>(kCqmPolicy)), 0);
782
783 EXPECT_FALSE(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
784 EXPECT_TRUE(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
785 EXPECT_EQ(nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]),
786 kExpectedCqmNotAcked);
787 }
788}
789
790TEST_F(Config80211Test, NL80211_CMD_DISASSOCIATE) {
791 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
792 const_cast<nlmsghdr *>(
793 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE)));
794
795 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
796 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_DISASSOCIATE);
797
798
799 {
800 uint8_t value;
801 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
802 EXPECT_EQ(value, kExpectedWifi);
803 }
804
805 {
806 uint32_t value;
807 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
808 EXPECT_EQ(value, kExpectedIfIndex);
809 }
810
811 {
812 void *rawdata = NULL;
813 int frame_byte_count = 0;
814 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
815 &frame_byte_count));
816 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
817 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
818
819 Nl80211Frame frame(frame_data, frame_byte_count);
820 Nl80211Frame expected_frame(kDisassociateFrame, sizeof(kDisassociateFrame));
821
822 EXPECT_TRUE(frame.IsEqual(expected_frame));
823 }
824}
825
826} // namespace shill