blob: 39922e04de0107fce18c81554fa20559b4778fad [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)));
347 }
348
349 Config80211 *config80211_;
350 MockNl80211Socket socket_;
351};
352
353class TestCallbackObject {
354 public:
355 TestCallbackObject() : callback_(Bind(&TestCallbackObject::MessageHandler,
356 Unretained(this))) { }
357 void MessageHandler(const UserBoundNlMessage &msg) {
358 }
359 const Config80211::Callback &GetCallback() const { return callback_; }
360
361 private:
362 Config80211::Callback callback_;
363};
364
365MATCHER_P(IsEqualToCallback, callback, "") {
366 const Config80211::Callback *arg_cb =
367 reinterpret_cast<const Config80211::Callback *>(arg);
368 const Config80211::Callback *callback_cb =
369 reinterpret_cast<const Config80211::Callback *>(callback);
370 if (arg_cb == callback_cb)
371 return true;
372 if (arg_cb == reinterpret_cast<const Config80211::Callback *>(NULL))
373 return false;
374 if (callback_cb == reinterpret_cast<const Config80211::Callback *>(NULL))
375 return arg_cb->is_null();
376 return arg_cb->Equals(*callback_cb);
377}
378
379TEST_F(Config80211Test, AddLinkTest) {
380 SetupConfig80211Object();
381
382 // Create a default callback.
383 TestCallbackObject callback_object;
384
385 // Install the callback and subscribe to events using it.
386 config80211_->SetDefaultCallback(callback_object.GetCallback());
387 Config80211::EventType event_type = Config80211::kEventTypeScan;
388 string event_type_string;
389 EXPECT_TRUE(Config80211::GetEventTypeString(event_type, &event_type_string));
390 EXPECT_CALL(socket_, AddGroupMembership(event_type_string))
391 .WillOnce(Return(true));
392 EXPECT_CALL(socket_, DisableSequenceChecking())
393 .WillOnce(Return(true));
394 EXPECT_CALL(socket_, SetNetlinkCallback(
395 _, IsEqualToCallback(&callback_object.GetCallback())))
396 .WillOnce(Return(true));
397
398 EXPECT_TRUE(config80211_->SubscribeToEvents(event_type));
399
400 // Deinstall the callback.
401 config80211_->UnsetDefaultCallback();
402 EXPECT_TRUE(Config80211::GetEventTypeString(event_type, &event_type_string));
403 EXPECT_CALL(socket_, AddGroupMembership(event_type_string))
404 .WillOnce(Return(true));
405 EXPECT_CALL(socket_, DisableSequenceChecking()).WillOnce(Return(true));
406 EXPECT_CALL(socket_, SetNetlinkCallback(_,
407 IsEqualToCallback(NULL)
408 )).WillOnce(Return(true));
409 EXPECT_TRUE(config80211_->SubscribeToEvents(event_type));
410}
411
412TEST_F(Config80211Test, NL80211_CMD_TRIGGER_SCAN) {
413 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
414 const_cast<nlmsghdr *>(
415 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN)));
416
417 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
418 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_TRIGGER_SCAN);
419
420 {
421 uint8_t value;
422 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
423 EXPECT_EQ(value, kExpectedWifi);
424 }
425
426 {
427 uint32_t value;
428 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
429 EXPECT_EQ(value, kExpectedIfIndex);
430 }
431
432 // Make sure the scan frequencies in the attribute are the ones we expect.
433 {
434 vector<uint32_t>list;
435 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
436 NL80211_ATTR_SCAN_FREQUENCIES, &list));
437 EXPECT_EQ(arraysize(kScanFrequencyTrigger), list.size());
438 int i = 0;
439 vector<uint32_t>::const_iterator j = list.begin();
440 while (j != list.end()) {
441 EXPECT_EQ(kScanFrequencyTrigger[i], *j);
442 ++i;
443 ++j;
444 }
445 }
446
447 {
448 vector<string> ssids;
449 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
450 &ssids));
451 EXPECT_EQ(ssids.size(), 1);
452 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
453 }
454
455 // Significant only in its existence.
456 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_SUPPORT_MESH_AUTH));
457}
458
459TEST_F(Config80211Test, NL80211_CMD_NEW_SCAN_RESULTS) {
460 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
461 const_cast<nlmsghdr *>(
462 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS)));
463
464 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
465 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_NEW_SCAN_RESULTS);
466
467 {
468 uint8_t value;
469 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
470 EXPECT_EQ(value, kExpectedWifi);
471 }
472
473 {
474 uint32_t value;
475 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
476 EXPECT_EQ(value, kExpectedIfIndex);
477 }
478
479 // Make sure the scan frequencies in the attribute are the ones we expect.
480 {
481 vector<uint32_t>list;
482 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
483 NL80211_ATTR_SCAN_FREQUENCIES, &list));
484 EXPECT_EQ(arraysize(kScanFrequencyResults), list.size());
485 int i = 0;
486 vector<uint32_t>::const_iterator j = list.begin();
487 while (j != list.end()) {
488 EXPECT_EQ(kScanFrequencyResults[i], *j);
489 ++i;
490 ++j;
491 }
492 }
493
494 {
495 vector<string> ssids;
496 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
497 &ssids));
498 EXPECT_EQ(ssids.size(), 1);
499 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
500 }
501
502 // Significant only in its existence.
503 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_SUPPORT_MESH_AUTH));
504}
505
506TEST_F(Config80211Test, NL80211_CMD_NEW_STATION) {
507 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
508 const_cast<nlmsghdr *>(
509 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION)));
510
511 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
512 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_NEW_STATION);
513
514 {
515 uint32_t value;
516 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
517 EXPECT_EQ(value, kExpectedIfIndex);
518 }
519
520 {
521 string value;
522 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
523 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
524 }
525
526 // TODO(wdg): Make config80211 handle nested attributes so it can deal
527 // with things like NL80211_ATTR_STA_INFO (without just calling
528 // nla_parse_nested).
529 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_STA_INFO));
530
531 {
532 uint32_t value;
533 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_GENERATION, &value));
534 EXPECT_EQ(value, kNewStationExpectedGeneration);
535 }
536}
537
538TEST_F(Config80211Test, NL80211_CMD_AUTHENTICATE) {
539 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
540 const_cast<nlmsghdr *>(
541 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE)));
542
543 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
544 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_AUTHENTICATE);
545
546 {
547 uint8_t value;
548 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
549 EXPECT_EQ(value, kExpectedWifi);
550 }
551
552 {
553 uint32_t value;
554 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
555 EXPECT_EQ(value, kExpectedIfIndex);
556 }
557
558 {
559 void *rawdata = NULL;
560 int frame_byte_count = 0;
561 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
562 &frame_byte_count));
563 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
564 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
565
566 Nl80211Frame frame(frame_data, frame_byte_count);
567 Nl80211Frame expected_frame(kAuthenticateFrame, sizeof(kAuthenticateFrame));
568
569 EXPECT_TRUE(frame.IsEqual(expected_frame));
570 }
571}
572
573TEST_F(Config80211Test, NL80211_CMD_ASSOCIATE) {
574 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
575 const_cast<nlmsghdr *>(
576 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE)));
577
578 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
579 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_ASSOCIATE);
580
581 {
582 uint8_t value;
583 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
584 EXPECT_EQ(value, kExpectedWifi);
585 }
586
587 {
588 uint32_t value;
589 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
590 EXPECT_EQ(value, kExpectedIfIndex);
591 }
592
593 {
594 void *rawdata = NULL;
595 int frame_byte_count = 0;
596 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
597 &frame_byte_count));
598 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
599 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
600
601 Nl80211Frame frame(frame_data, frame_byte_count);
602 Nl80211Frame expected_frame(kAssociateFrame, sizeof(kAssociateFrame));
603
604 EXPECT_TRUE(frame.IsEqual(expected_frame));
605 }
606}
607
608TEST_F(Config80211Test, NL80211_CMD_CONNECT) {
609 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
610 const_cast<nlmsghdr *>(
611 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT)));
612
613 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
614 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_CONNECT);
615
616 {
617 uint8_t value;
618 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
619 EXPECT_EQ(value, kExpectedWifi);
620 }
621
622 {
623 uint32_t value;
624 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
625 EXPECT_EQ(value, kExpectedIfIndex);
626 }
627
628 {
629 string value;
630 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
631 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
632 }
633
634 {
635 uint16_t value;
636 EXPECT_TRUE(message->GetU16Attribute(NL80211_ATTR_STATUS_CODE, &value));
637 EXPECT_EQ(value, kExpectedConnectStatus);
638 }
639
640 // TODO(wdg): Need to check the value of this attribute.
641 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_RESP_IE));
642}
643
644TEST_F(Config80211Test, NL80211_CMD_DEAUTHENTICATE) {
645 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
646 const_cast<nlmsghdr *>(
647 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE)));
648
649 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
650 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_DEAUTHENTICATE);
651
652 {
653 uint8_t value;
654 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
655 EXPECT_EQ(value, kExpectedWifi);
656 }
657
658 {
659 uint32_t value;
660 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
661 EXPECT_EQ(value, kExpectedIfIndex);
662 }
663
664 {
665 void *rawdata = NULL;
666 int frame_byte_count = 0;
667 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
668 &frame_byte_count));
669 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
670 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
671
672 Nl80211Frame frame(frame_data, frame_byte_count);
673 Nl80211Frame expected_frame(kDeauthenticateFrame,
674 sizeof(kDeauthenticateFrame));
675
676 EXPECT_TRUE(frame.IsEqual(expected_frame));
677 }
678}
679
680TEST_F(Config80211Test, NL80211_CMD_DISCONNECT) {
681 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
682 const_cast<nlmsghdr *>(
683 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT)));
684
685 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
686 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_DISCONNECT);
687
688 {
689 uint8_t value;
690 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
691 EXPECT_EQ(value, kExpectedWifi);
692 }
693
694 {
695 uint32_t value;
696 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
697 EXPECT_EQ(value, kExpectedIfIndex);
698 }
699
700 {
701 uint16_t value;
702 EXPECT_TRUE(message->GetU16Attribute(NL80211_ATTR_REASON_CODE, &value));
703 EXPECT_EQ(value, kExpectedDisconnectReason);
704 }
705
706 // Significant only in its existence.
707 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP));
708}
709
710TEST_F(Config80211Test, NL80211_CMD_NOTIFY_CQM) {
711 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
712 const_cast<nlmsghdr *>(
713 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM)));
714
715 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
716 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_NOTIFY_CQM);
717
718
719 {
720 uint8_t value;
721 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
722 EXPECT_EQ(value, kExpectedWifi);
723 }
724
725 {
726 uint32_t value;
727 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
728 EXPECT_EQ(value, kExpectedIfIndex);
729 }
730
731 {
732 string value;
733 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
734 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
735 }
736
737 // TODO(wdg): Make config80211 handle nested attributes so it can deal
738 // with things like NL80211_ATTR_CQM (without just calling nla_parse_nested).
739 {
740 static const nla_policy kCqmPolicy[NL80211_ATTR_CQM_MAX + 1] = {
741 { NLA_U32, 0, 0 }, // Who Knows?
742 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
743 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
744 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
745 };
746
747 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_CQM));
748 const nlattr *const_data = message->GetAttribute(NL80211_ATTR_CQM);
749 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
750 EXPECT_NE(cqm_attr, reinterpret_cast<nlattr *>(NULL));
751
752 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
753 EXPECT_EQ(nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
754 const_cast<nla_policy *>(kCqmPolicy)), 0);
755
756 EXPECT_FALSE(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
757 EXPECT_TRUE(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
758 EXPECT_EQ(nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]),
759 kExpectedCqmNotAcked);
760 }
761}
762
763TEST_F(Config80211Test, NL80211_CMD_DISASSOCIATE) {
764 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
765 const_cast<nlmsghdr *>(
766 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE)));
767
768 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
769 EXPECT_EQ(message->GetMessageType(), NL80211_CMD_DISASSOCIATE);
770
771
772 {
773 uint8_t value;
774 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
775 EXPECT_EQ(value, kExpectedWifi);
776 }
777
778 {
779 uint32_t value;
780 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
781 EXPECT_EQ(value, kExpectedIfIndex);
782 }
783
784 {
785 void *rawdata = NULL;
786 int frame_byte_count = 0;
787 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
788 &frame_byte_count));
789 EXPECT_NE(rawdata, reinterpret_cast<void *>(NULL));
790 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
791
792 Nl80211Frame frame(frame_data, frame_byte_count);
793 Nl80211Frame expected_frame(kDisassociateFrame, sizeof(kDisassociateFrame));
794
795 EXPECT_TRUE(frame.IsEqual(expected_frame));
796 }
797}
798
799} // namespace shill