blob: 3db52e3f1c43891102164bb6bc8340128cefb6fd [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
Wade Guthrie8343f7f2012-12-04 13:52:32 -080014#include <net/if.h>
15#include <netlink/attr.h>
16#include <netlink/genl/genl.h>
17#include <netlink/msg.h>
18#include <netlink/netlink.h>
19
Wade Guthrieb1ec8602012-10-18 17:26:14 -070020#include <list>
21#include <string>
22#include <vector>
23
24#include <base/bind.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070025#include <gmock/gmock.h>
26#include <gtest/gtest.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070027
Wade Guthrie5d3d6de2012-11-02 11:08:34 -070028#include "shill/kernel_bound_nlmessage.h"
Wade Guthrieb1ec8602012-10-18 17:26:14 -070029#include "shill/mock_callback80211_object.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070030#include "shill/mock_nl80211_socket.h"
Wade Guthrie8343f7f2012-12-04 13:52:32 -080031#include "shill/nl80211_attribute.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070032#include "shill/nl80211_socket.h"
33#include "shill/user_bound_nlmessage.h"
34
35using base::Bind;
36using base::Unretained;
Wade Guthrieb1ec8602012-10-18 17:26:14 -070037using std::list;
Wade Guthrie0d438532012-05-18 14:18:50 -070038using std::string;
39using std::vector;
40using testing::_;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -070041using testing::Invoke;
Wade Guthrie0d438532012-05-18 14:18:50 -070042using testing::Return;
43using testing::Test;
44
45namespace shill {
46
47namespace {
48
49// These data blocks have been collected by shill using Config80211 while,
50// simultaneously (and manually) comparing shill output with that of the 'iw'
51// code from which it was derived. The test strings represent the raw packet
52// data coming from the kernel. The comments above each of these strings is
53// the markup that "iw" outputs for ech of these packets.
54
55// These constants are consistent throughout the packets, below.
56
57const uint32_t kExpectedIfIndex = 4;
58const uint8_t kExpectedWifi = 0;
59const char kExpectedMacAddress[] = "c0:3f:0e:77:e8:7f";
60
61
62// wlan0 (phy #0): scan started
63
64const uint32_t kScanFrequencyTrigger[] = {
65 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
66 2452, 2457, 2462, 2467, 2472, 2484, 5180, 5200,
67 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520,
68 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
69 5700, 5745, 5765, 5785, 5805, 5825
70};
71
72const unsigned char kNL80211_CMD_TRIGGER_SCAN[] = {
73 0x68, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x21, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
77 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2d, 0x00,
78 0x04, 0x00, 0x00, 0x00, 0x34, 0x01, 0x2c, 0x00,
79 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
80 0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00,
81 0x08, 0x00, 0x02, 0x00, 0x76, 0x09, 0x00, 0x00,
82 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
83 0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00,
84 0x08, 0x00, 0x05, 0x00, 0x85, 0x09, 0x00, 0x00,
85 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
86 0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00,
87 0x08, 0x00, 0x08, 0x00, 0x94, 0x09, 0x00, 0x00,
88 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
89 0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00,
90 0x08, 0x00, 0x0b, 0x00, 0xa3, 0x09, 0x00, 0x00,
91 0x08, 0x00, 0x0c, 0x00, 0xa8, 0x09, 0x00, 0x00,
92 0x08, 0x00, 0x0d, 0x00, 0xb4, 0x09, 0x00, 0x00,
93 0x08, 0x00, 0x0e, 0x00, 0x3c, 0x14, 0x00, 0x00,
94 0x08, 0x00, 0x0f, 0x00, 0x50, 0x14, 0x00, 0x00,
95 0x08, 0x00, 0x10, 0x00, 0x64, 0x14, 0x00, 0x00,
96 0x08, 0x00, 0x11, 0x00, 0x78, 0x14, 0x00, 0x00,
97 0x08, 0x00, 0x12, 0x00, 0x8c, 0x14, 0x00, 0x00,
98 0x08, 0x00, 0x13, 0x00, 0xa0, 0x14, 0x00, 0x00,
99 0x08, 0x00, 0x14, 0x00, 0xb4, 0x14, 0x00, 0x00,
100 0x08, 0x00, 0x15, 0x00, 0xc8, 0x14, 0x00, 0x00,
101 0x08, 0x00, 0x16, 0x00, 0x7c, 0x15, 0x00, 0x00,
102 0x08, 0x00, 0x17, 0x00, 0x90, 0x15, 0x00, 0x00,
103 0x08, 0x00, 0x18, 0x00, 0xa4, 0x15, 0x00, 0x00,
104 0x08, 0x00, 0x19, 0x00, 0xb8, 0x15, 0x00, 0x00,
105 0x08, 0x00, 0x1a, 0x00, 0xcc, 0x15, 0x00, 0x00,
106 0x08, 0x00, 0x1b, 0x00, 0xe0, 0x15, 0x00, 0x00,
107 0x08, 0x00, 0x1c, 0x00, 0xf4, 0x15, 0x00, 0x00,
108 0x08, 0x00, 0x1d, 0x00, 0x08, 0x16, 0x00, 0x00,
109 0x08, 0x00, 0x1e, 0x00, 0x1c, 0x16, 0x00, 0x00,
110 0x08, 0x00, 0x1f, 0x00, 0x30, 0x16, 0x00, 0x00,
111 0x08, 0x00, 0x20, 0x00, 0x44, 0x16, 0x00, 0x00,
112 0x08, 0x00, 0x21, 0x00, 0x71, 0x16, 0x00, 0x00,
113 0x08, 0x00, 0x22, 0x00, 0x85, 0x16, 0x00, 0x00,
114 0x08, 0x00, 0x23, 0x00, 0x99, 0x16, 0x00, 0x00,
115 0x08, 0x00, 0x24, 0x00, 0xad, 0x16, 0x00, 0x00,
116 0x08, 0x00, 0x25, 0x00, 0xc1, 0x16, 0x00, 0x00,
117 0x08, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
118};
119
120
121// wlan0 (phy #0): scan finished: 2412 2417 2422 2427 2432 2437 2442 2447 2452
122// 2457 2462 2467 2472 2484 5180 5200 5220 5240 5260 5280 5300 5320 5500 5520
123// 5540 5560 5580 5600 5620 5640 5660 5680 5700 5745 5765 5785 5805 5825, ""
124
125const uint32_t kScanFrequencyResults[] = {
126 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
127 2452, 2457, 2462, 2467, 2472, 2484, 5180, 5200,
128 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520,
129 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
130 5700, 5745, 5765, 5785, 5805, 5825
131};
132
133const unsigned char kNL80211_CMD_NEW_SCAN_RESULTS[] = {
134 0x68, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x22, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
137 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
138 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2d, 0x00,
139 0x04, 0x00, 0x00, 0x00, 0x34, 0x01, 0x2c, 0x00,
140 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
141 0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00,
142 0x08, 0x00, 0x02, 0x00, 0x76, 0x09, 0x00, 0x00,
143 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
144 0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00,
145 0x08, 0x00, 0x05, 0x00, 0x85, 0x09, 0x00, 0x00,
146 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
147 0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00,
148 0x08, 0x00, 0x08, 0x00, 0x94, 0x09, 0x00, 0x00,
149 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
150 0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00,
151 0x08, 0x00, 0x0b, 0x00, 0xa3, 0x09, 0x00, 0x00,
152 0x08, 0x00, 0x0c, 0x00, 0xa8, 0x09, 0x00, 0x00,
153 0x08, 0x00, 0x0d, 0x00, 0xb4, 0x09, 0x00, 0x00,
154 0x08, 0x00, 0x0e, 0x00, 0x3c, 0x14, 0x00, 0x00,
155 0x08, 0x00, 0x0f, 0x00, 0x50, 0x14, 0x00, 0x00,
156 0x08, 0x00, 0x10, 0x00, 0x64, 0x14, 0x00, 0x00,
157 0x08, 0x00, 0x11, 0x00, 0x78, 0x14, 0x00, 0x00,
158 0x08, 0x00, 0x12, 0x00, 0x8c, 0x14, 0x00, 0x00,
159 0x08, 0x00, 0x13, 0x00, 0xa0, 0x14, 0x00, 0x00,
160 0x08, 0x00, 0x14, 0x00, 0xb4, 0x14, 0x00, 0x00,
161 0x08, 0x00, 0x15, 0x00, 0xc8, 0x14, 0x00, 0x00,
162 0x08, 0x00, 0x16, 0x00, 0x7c, 0x15, 0x00, 0x00,
163 0x08, 0x00, 0x17, 0x00, 0x90, 0x15, 0x00, 0x00,
164 0x08, 0x00, 0x18, 0x00, 0xa4, 0x15, 0x00, 0x00,
165 0x08, 0x00, 0x19, 0x00, 0xb8, 0x15, 0x00, 0x00,
166 0x08, 0x00, 0x1a, 0x00, 0xcc, 0x15, 0x00, 0x00,
167 0x08, 0x00, 0x1b, 0x00, 0xe0, 0x15, 0x00, 0x00,
168 0x08, 0x00, 0x1c, 0x00, 0xf4, 0x15, 0x00, 0x00,
169 0x08, 0x00, 0x1d, 0x00, 0x08, 0x16, 0x00, 0x00,
170 0x08, 0x00, 0x1e, 0x00, 0x1c, 0x16, 0x00, 0x00,
171 0x08, 0x00, 0x1f, 0x00, 0x30, 0x16, 0x00, 0x00,
172 0x08, 0x00, 0x20, 0x00, 0x44, 0x16, 0x00, 0x00,
173 0x08, 0x00, 0x21, 0x00, 0x71, 0x16, 0x00, 0x00,
174 0x08, 0x00, 0x22, 0x00, 0x85, 0x16, 0x00, 0x00,
175 0x08, 0x00, 0x23, 0x00, 0x99, 0x16, 0x00, 0x00,
176 0x08, 0x00, 0x24, 0x00, 0xad, 0x16, 0x00, 0x00,
177 0x08, 0x00, 0x25, 0x00, 0xc1, 0x16, 0x00, 0x00,
178 0x08, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
179};
180
181
182// wlan0: new station c0:3f:0e:77:e8:7f
183
184const uint32_t kNewStationExpectedGeneration = 275;
185
186const unsigned char kNL80211_CMD_NEW_STATION[] = {
187 0x34, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
188 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x13, 0x01, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
190 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
191 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
192 0x08, 0x00, 0x2e, 0x00, 0x13, 0x01, 0x00, 0x00,
193 0x04, 0x00, 0x15, 0x00,
194};
195
196
197// wlan0 (phy #0): auth c0:3f:0e:77:e8:7f -> 48:5d:60:77:2d:cf status: 0:
198// Successful [frame: b0 00 3a 01 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f c0
199// 3f 0e 77 e8 7f 30 07 00 00 02 00 00 00]
200
201const unsigned char kAuthenticateFrame[] = {
202 0xb0, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
203 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
204 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x30, 0x07,
205 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
206};
207
208const unsigned char kNL80211_CMD_AUTHENTICATE[] = {
209 0x48, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x25, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
212 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
213 0x04, 0x00, 0x00, 0x00, 0x22, 0x00, 0x33, 0x00,
214 0xb0, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
215 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
216 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x30, 0x07,
217 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
218};
219
220
221// wlan0 (phy #0): assoc c0:3f:0e:77:e8:7f -> 48:5d:60:77:2d:cf status: 0:
222// Successful [frame: 10 00 3a 01 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f c0 3f 0e
223// 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
224// 60 6c]
225
226const unsigned char kAssociateFrame[] = {
227 0x10, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
228 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
229 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x40, 0x07,
230 0x01, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x01, 0x08,
231 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24,
232 0x32, 0x04, 0x30, 0x48, 0x60, 0x6c
233};
234
235const unsigned char kNL80211_CMD_ASSOCIATE[] = {
236 0x58, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x26, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
239 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
240 0x04, 0x00, 0x00, 0x00, 0x32, 0x00, 0x33, 0x00,
241 0x10, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
242 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
243 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x40, 0x07,
244 0x01, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x01, 0x08,
245 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24,
246 0x32, 0x04, 0x30, 0x48, 0x60, 0x6c, 0x00, 0x00,
247};
248
249
250// wlan0 (phy #0): connected to c0:3f:0e:77:e8:7f
251
252const uint16_t kExpectedConnectStatus = 0;
253
254const unsigned char kNL80211_CMD_CONNECT[] = {
255 0x4c, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257 0x2e, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
259 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
260 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
261 0x06, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00,
262 0x14, 0x00, 0x4e, 0x00, 0x01, 0x08, 0x82, 0x84,
263 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x32, 0x04,
264 0x30, 0x48, 0x60, 0x6c,
265};
266
267
268// wlan0 (phy #0): deauth c0:3f:0e:77:e8:7f -> ff:ff:ff:ff:ff:ff reason 2:
269// Previous authentication no longer valid [frame: c0 00 00 00 ff ff ff ff
270// ff ff c0 3f 0e 77 e8 7f c0 3f 0e 77 e8 7f c0 0e 02 00]
271
272const unsigned char kDeauthenticateFrame[] = {
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700273 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
274 0xff, 0xff, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
275 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0xc0, 0x0e,
276 0x02, 0x00
Wade Guthrie0d438532012-05-18 14:18:50 -0700277};
278
279const unsigned char kNL80211_CMD_DEAUTHENTICATE[] = {
280 0x44, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
281 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
282 0x27, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
283 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
284 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x33, 0x00,
285 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
286 0xff, 0xff, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
287 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0xc0, 0x0e,
288 0x02, 0x00, 0x00, 0x00,
289};
290
291
292// wlan0 (phy #0): disconnected (by AP) reason: 2: Previous authentication no
293// longer valid
294
295const uint16_t kExpectedDisconnectReason = 2;
296
297const unsigned char kNL80211_CMD_DISCONNECT[] = {
298 0x30, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
299 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x30, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
302 0x04, 0x00, 0x00, 0x00, 0x06, 0x00, 0x36, 0x00,
303 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x47, 0x00,
304};
305
306
307// wlan0 (phy #0): connection quality monitor event: peer c0:3f:0e:77:e8:7f
308// didn't ACK 50 packets
309
310const uint32_t kExpectedCqmNotAcked = 50;
311
312const unsigned char kNL80211_CMD_NOTIFY_CQM[] = {
313 0x3c, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 0x40, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
316 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
317 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
318 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
319 0x0c, 0x00, 0x5e, 0x00, 0x08, 0x00, 0x04, 0x00,
320 0x32, 0x00, 0x00, 0x00,
321};
322
323
324// wlan0 (phy #0): disassoc 48:5d:60:77:2d:cf -> c0:3f:0e:77:e8:7f reason 3:
325// Deauthenticated because sending station is [frame: a0 00 00 00 c0 3f 0e
326// 77 e8 7f 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f 00 00 03 00]
327
328const unsigned char kDisassociateFrame[] = {
329 0xa0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x0e, 0x77,
330 0xe8, 0x7f, 0x48, 0x5d, 0x60, 0x77, 0x2d, 0xcf,
331 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
332 0x03, 0x00
333};
334
335const unsigned char kNL80211_CMD_DISASSOCIATE[] = {
336 0x44, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
337 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
338 0x28, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
339 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
340 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x33, 0x00,
341 0xa0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x0e, 0x77,
342 0xe8, 0x7f, 0x48, 0x5d, 0x60, 0x77, 0x2d, 0xcf,
343 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
344 0x03, 0x00, 0x00, 0x00,
345};
346
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700347} // namespace
Christopher Wiley393b93f2012-11-08 17:30:58 -0800348uint32 MockNl80211Socket::Send(KernelBoundNlMessage *message) {
349 // Don't need a real family id; this is never sent.
350 const uint32 family_id = 0;
351 uint32 sequence_number = ++sequence_number_;
352 if (genlmsg_put(message->message(), NL_AUTO_PID, sequence_number, family_id,
353 0, 0, message->command(), 0) == NULL) {
354 LOG(ERROR) << "genlmsg_put returned a NULL pointer.";
355 return 0;
356 }
357 return sequence_number;
358}
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700359
Wade Guthrie0d438532012-05-18 14:18:50 -0700360class Config80211Test : public Test {
361 public:
362 Config80211Test() : config80211_(Config80211::GetInstance()) {}
Han Shen4afba202012-12-17 08:48:35 -0800363 ~Config80211Test() {
364 // Config80211 is a singleton, the sock_ field *MUST* be cleared
365 // before "Config80211Test::socket_" gets invalidated, otherwise
366 // later tests will refer to a corrupted memory.
367 config80211_->sock_ = NULL;
368 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700369 void SetupConfig80211Object() {
370 EXPECT_NE(config80211_, reinterpret_cast<Config80211 *>(NULL));
371 config80211_->sock_ = &socket_;
372 EXPECT_TRUE(config80211_->Init(reinterpret_cast<EventDispatcher *>(NULL)));
Wade Guthried6153612012-08-23 11:36:14 -0700373 config80211_->Reset();
Wade Guthrie0d438532012-05-18 14:18:50 -0700374 }
375
376 Config80211 *config80211_;
377 MockNl80211Socket socket_;
378};
379
380class TestCallbackObject {
381 public:
382 TestCallbackObject() : callback_(Bind(&TestCallbackObject::MessageHandler,
383 Unretained(this))) { }
384 void MessageHandler(const UserBoundNlMessage &msg) {
385 }
386 const Config80211::Callback &GetCallback() const { return callback_; }
387
388 private:
389 Config80211::Callback callback_;
390};
391
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700392// Checks a config80211 parameter to make sure it contains |callback_arg|
393// in its list of broadcast callbacks.
394MATCHER_P(ContainsCallback, callback_arg, "") {
395 if (arg == reinterpret_cast<void *>(NULL)) {
396 LOG(WARNING) << "NULL parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700397 return false;
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700398 }
399 const Config80211 *config80211 = static_cast<Config80211 *>(arg);
400 const Config80211::Callback callback =
401 static_cast<const Config80211::Callback>(callback_arg);
402
403 return config80211->FindBroadcastCallback(callback);
Wade Guthrie0d438532012-05-18 14:18:50 -0700404}
405
406TEST_F(Config80211Test, AddLinkTest) {
407 SetupConfig80211Object();
408
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700409 // Create a broadcast callback.
Wade Guthrie0d438532012-05-18 14:18:50 -0700410 TestCallbackObject callback_object;
411
Wade Guthried6153612012-08-23 11:36:14 -0700412 // Install the callback and subscribe to events using it, wifi down
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700413 // (shouldn't actually send the subscription request until wifi comes up).
Wade Guthried6153612012-08-23 11:36:14 -0700414 EXPECT_CALL(socket_, AddGroupMembership(_)).Times(0);
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700415 EXPECT_CALL(socket_, SetNetlinkCallback(_, _)).Times(0);
Wade Guthried6153612012-08-23 11:36:14 -0700416
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700417 EXPECT_TRUE(config80211_->AddBroadcastCallback(
418 callback_object.GetCallback()));
Wade Guthried6153612012-08-23 11:36:14 -0700419 Config80211::EventType scan_event = Config80211::kEventTypeScan;
420 string scan_event_string;
421 EXPECT_TRUE(Config80211::GetEventTypeString(scan_event, &scan_event_string));
422 EXPECT_TRUE(config80211_->SubscribeToEvents(scan_event));
423
424 // Wifi up, should subscribe to events.
425 EXPECT_CALL(socket_, AddGroupMembership(scan_event_string))
Wade Guthrie0d438532012-05-18 14:18:50 -0700426 .WillOnce(Return(true));
Wade Guthrie0d438532012-05-18 14:18:50 -0700427 EXPECT_CALL(socket_, SetNetlinkCallback(
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700428 _, ContainsCallback(callback_object.GetCallback())))
Wade Guthrie0d438532012-05-18 14:18:50 -0700429 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700430 config80211_->SetWifiState(Config80211::kWifiUp);
Wade Guthrie0d438532012-05-18 14:18:50 -0700431
Wade Guthried6153612012-08-23 11:36:14 -0700432 // Second subscribe, same event (should do nothing).
433 EXPECT_CALL(socket_, AddGroupMembership(_)).Times(0);
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700434 EXPECT_CALL(socket_, SetNetlinkCallback(_, _)).Times(0);
Wade Guthried6153612012-08-23 11:36:14 -0700435 EXPECT_TRUE(config80211_->SubscribeToEvents(scan_event));
Wade Guthrie0d438532012-05-18 14:18:50 -0700436
Wade Guthried6153612012-08-23 11:36:14 -0700437 // Bring the wifi back down.
438 config80211_->SetWifiState(Config80211::kWifiDown);
439
440 // Subscribe to a new event with the wifi down (should still do nothing).
441 Config80211::EventType mlme_event = Config80211::kEventTypeMlme;
442 string mlme_event_string;
443 EXPECT_TRUE(Config80211::GetEventTypeString(mlme_event, &mlme_event_string));
444 EXPECT_TRUE(config80211_->SubscribeToEvents(mlme_event));
445
446 // Wifi up (again), should subscribe to the original scan event and the new
447 // mlme event.
448 EXPECT_CALL(socket_, AddGroupMembership(scan_event_string))
Wade Guthrie0d438532012-05-18 14:18:50 -0700449 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700450 EXPECT_CALL(socket_, AddGroupMembership(mlme_event_string))
451 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700452 EXPECT_CALL(socket_, SetNetlinkCallback(
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700453 _, ContainsCallback(callback_object.GetCallback())))
Wade Guthrie5d53d492012-11-07 09:53:31 -0800454 .Times(1)
Wade Guthried6153612012-08-23 11:36:14 -0700455 .WillRepeatedly(Return(true));
456 config80211_->SetWifiState(Config80211::kWifiUp);
Wade Guthrie0d438532012-05-18 14:18:50 -0700457}
458
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700459TEST_F(Config80211Test, BroadcastCallbackTest) {
460 SetupConfig80211Object();
461
462 nlmsghdr *message = const_cast<nlmsghdr *>(
463 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT));
464
Christopher Wileyfe34be02012-11-12 16:02:46 -0800465 MockCallback80211 callback1;
466 MockCallback80211 callback2;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700467
468 // Simple, 1 callback, case.
469 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
Christopher Wileyfe34be02012-11-12 16:02:46 -0800470 callback1.InstallAsBroadcastCallback();
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700471 config80211_->OnNlMessageReceived(message);
472
473 // Add a second callback.
474 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
475 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
476 EXPECT_TRUE(callback2.InstallAsBroadcastCallback());
477 config80211_->OnNlMessageReceived(message);
478
479 // Verify that a callback can't be added twice.
480 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
481 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
482 EXPECT_FALSE(callback1.InstallAsBroadcastCallback());
483 config80211_->OnNlMessageReceived(message);
484
485 // Check that we can remove a callback.
486 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(0);
487 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
488 EXPECT_TRUE(callback1.DeinstallAsCallback());
489 config80211_->OnNlMessageReceived(message);
490
491 // Check that re-adding the callback goes smoothly.
492 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
493 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
494 EXPECT_TRUE(callback1.InstallAsBroadcastCallback());
495 config80211_->OnNlMessageReceived(message);
496
497 // Check that ClearBroadcastCallbacks works.
498 config80211_->ClearBroadcastCallbacks();
499 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(0);
500 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(0);
501 config80211_->OnNlMessageReceived(message);
502}
503
504TEST_F(Config80211Test, MessageCallbackTest) {
505 // Setup.
506 SetupConfig80211Object();
507
Christopher Wileyfe34be02012-11-12 16:02:46 -0800508 MockCallback80211 callback_broadcast;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700509 EXPECT_TRUE(callback_broadcast.InstallAsBroadcastCallback());
510
Christopher Wiley393b93f2012-11-08 17:30:58 -0800511 KernelBoundNlMessage sent_message_1(CTRL_CMD_GETFAMILY);
Christopher Wileyfe34be02012-11-12 16:02:46 -0800512 MockCallback80211 callback_sent_1;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700513 EXPECT_TRUE(sent_message_1.Init());
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700514
Christopher Wiley393b93f2012-11-08 17:30:58 -0800515 KernelBoundNlMessage sent_message_2(CTRL_CMD_GETFAMILY);
Christopher Wileyfe34be02012-11-12 16:02:46 -0800516 MockCallback80211 callback_sent_2;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700517 EXPECT_TRUE(sent_message_2.Init());
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700518
519 // Set up the received message as a response to sent_message_1.
520 scoped_array<unsigned char> message_memory(
521 new unsigned char[sizeof(kNL80211_CMD_DISCONNECT)]);
522 memcpy(message_memory.get(), kNL80211_CMD_DISCONNECT,
523 sizeof(kNL80211_CMD_DISCONNECT));
524 nlmsghdr *received_message =
525 reinterpret_cast<nlmsghdr *>(message_memory.get());
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700526
527 // Now, we can start the actual test...
528
529 // Verify that generic callback gets called for a message when no
530 // message-specific callback has been installed.
531 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
532 config80211_->OnNlMessageReceived(received_message);
533
Christopher Wiley393b93f2012-11-08 17:30:58 -0800534 // Send the message and give our callback. Verify that we get called back.
535 EXPECT_TRUE(config80211_->SendMessage(&sent_message_1,
Christopher Wileyfe34be02012-11-12 16:02:46 -0800536 callback_sent_1.callback()));
Christopher Wiley393b93f2012-11-08 17:30:58 -0800537 // Make it appear that this message is in response to our sent message.
538 received_message->nlmsg_seq = socket_.GetLastSequenceNumber();
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700539 EXPECT_CALL(callback_sent_1, Config80211MessageCallback(_)).Times(1);
540 config80211_->OnNlMessageReceived(received_message);
541
542 // Verify that broadcast callback is called for the message after the
543 // message-specific callback is called once.
544 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
545 config80211_->OnNlMessageReceived(received_message);
546
547 // Install and then uninstall message-specific callback; verify broadcast
548 // callback is called on message receipt.
Christopher Wiley393b93f2012-11-08 17:30:58 -0800549 EXPECT_TRUE(config80211_->SendMessage(&sent_message_1,
Christopher Wileyfe34be02012-11-12 16:02:46 -0800550 callback_sent_1.callback()));
Christopher Wiley393b93f2012-11-08 17:30:58 -0800551 received_message->nlmsg_seq = socket_.GetLastSequenceNumber();
552 EXPECT_TRUE(config80211_->RemoveMessageCallback(sent_message_1));
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700553 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
554 config80211_->OnNlMessageReceived(received_message);
555
556 // Install callback for different message; verify that broadcast callback is
557 // called for _this_ message.
Christopher Wiley393b93f2012-11-08 17:30:58 -0800558 EXPECT_TRUE(config80211_->SendMessage(&sent_message_2,
Christopher Wileyfe34be02012-11-12 16:02:46 -0800559 callback_sent_2.callback()));
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700560 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
561 config80211_->OnNlMessageReceived(received_message);
562
563 // Change the ID for the message to that of the second callback; verify that
564 // the appropriate callback is called for _that_ message.
Christopher Wiley393b93f2012-11-08 17:30:58 -0800565 received_message->nlmsg_seq = socket_.GetLastSequenceNumber();
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700566 EXPECT_CALL(callback_sent_2, Config80211MessageCallback(_)).Times(1);
567 config80211_->OnNlMessageReceived(received_message);
568}
569
Wade Guthrie0d438532012-05-18 14:18:50 -0700570TEST_F(Config80211Test, NL80211_CMD_TRIGGER_SCAN) {
571 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
572 const_cast<nlmsghdr *>(
573 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN)));
574
575 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800576 EXPECT_EQ(message->message_type(), NL80211_CMD_TRIGGER_SCAN);
Wade Guthrie0d438532012-05-18 14:18:50 -0700577
578 {
579 uint8_t value;
580 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
581 EXPECT_EQ(value, kExpectedWifi);
582 }
583
584 {
585 uint32_t value;
586 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
587 EXPECT_EQ(value, kExpectedIfIndex);
588 }
589
590 // Make sure the scan frequencies in the attribute are the ones we expect.
591 {
592 vector<uint32_t>list;
593 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
594 NL80211_ATTR_SCAN_FREQUENCIES, &list));
595 EXPECT_EQ(arraysize(kScanFrequencyTrigger), list.size());
596 int i = 0;
597 vector<uint32_t>::const_iterator j = list.begin();
598 while (j != list.end()) {
599 EXPECT_EQ(kScanFrequencyTrigger[i], *j);
600 ++i;
601 ++j;
602 }
603 }
604
605 {
606 vector<string> ssids;
607 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
608 &ssids));
609 EXPECT_EQ(ssids.size(), 1);
610 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
611 }
612
613 // Significant only in its existence.
614 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_SUPPORT_MESH_AUTH));
615}
616
617TEST_F(Config80211Test, NL80211_CMD_NEW_SCAN_RESULTS) {
618 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
619 const_cast<nlmsghdr *>(
620 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS)));
621
622 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800623 EXPECT_EQ(message->message_type(), NL80211_CMD_NEW_SCAN_RESULTS);
Wade Guthrie0d438532012-05-18 14:18:50 -0700624
625 {
626 uint8_t value;
627 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
628 EXPECT_EQ(value, kExpectedWifi);
629 }
630
631 {
632 uint32_t value;
633 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
634 EXPECT_EQ(value, kExpectedIfIndex);
635 }
636
637 // Make sure the scan frequencies in the attribute are the ones we expect.
638 {
639 vector<uint32_t>list;
640 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
641 NL80211_ATTR_SCAN_FREQUENCIES, &list));
642 EXPECT_EQ(arraysize(kScanFrequencyResults), list.size());
643 int i = 0;
644 vector<uint32_t>::const_iterator j = list.begin();
645 while (j != list.end()) {
646 EXPECT_EQ(kScanFrequencyResults[i], *j);
647 ++i;
648 ++j;
649 }
650 }
651
652 {
653 vector<string> ssids;
654 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
655 &ssids));
656 EXPECT_EQ(ssids.size(), 1);
657 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
658 }
659
660 // Significant only in its existence.
661 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_SUPPORT_MESH_AUTH));
662}
663
664TEST_F(Config80211Test, NL80211_CMD_NEW_STATION) {
665 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
666 const_cast<nlmsghdr *>(
667 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION)));
668
669 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800670 EXPECT_EQ(message->message_type(), NL80211_CMD_NEW_STATION);
Wade Guthrie0d438532012-05-18 14:18:50 -0700671
672 {
673 uint32_t value;
674 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
675 EXPECT_EQ(value, kExpectedIfIndex);
676 }
677
678 {
679 string value;
680 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
681 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
682 }
683
684 // TODO(wdg): Make config80211 handle nested attributes so it can deal
685 // with things like NL80211_ATTR_STA_INFO (without just calling
686 // nla_parse_nested).
687 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_STA_INFO));
688
689 {
690 uint32_t value;
691 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_GENERATION, &value));
692 EXPECT_EQ(value, kNewStationExpectedGeneration);
693 }
694}
695
696TEST_F(Config80211Test, NL80211_CMD_AUTHENTICATE) {
697 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
698 const_cast<nlmsghdr *>(
699 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE)));
700
701 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800702 EXPECT_EQ(message->message_type(), NL80211_CMD_AUTHENTICATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700703
704 {
705 uint8_t value;
706 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
707 EXPECT_EQ(value, kExpectedWifi);
708 }
709
710 {
711 uint32_t value;
712 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
713 EXPECT_EQ(value, kExpectedIfIndex);
714 }
715
716 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800717 ByteString rawdata;
718 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata));
719 EXPECT_FALSE(rawdata.IsEmpty());
720 Nl80211Frame frame(rawdata);
721 Nl80211Frame expected_frame(ByteString(kAuthenticateFrame,
722 sizeof(kAuthenticateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700723 EXPECT_TRUE(frame.IsEqual(expected_frame));
724 }
725}
726
727TEST_F(Config80211Test, NL80211_CMD_ASSOCIATE) {
728 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
729 const_cast<nlmsghdr *>(
730 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE)));
731
732 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800733 EXPECT_EQ(message->message_type(), NL80211_CMD_ASSOCIATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700734
735 {
736 uint8_t value;
737 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
738 EXPECT_EQ(value, kExpectedWifi);
739 }
740
741 {
742 uint32_t value;
743 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
744 EXPECT_EQ(value, kExpectedIfIndex);
745 }
746
747 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800748 ByteString rawdata;
749 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata));
750 EXPECT_FALSE(rawdata.IsEmpty());
751 Nl80211Frame frame(rawdata);
752 Nl80211Frame expected_frame(ByteString(kAssociateFrame,
753 sizeof(kAssociateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700754 EXPECT_TRUE(frame.IsEqual(expected_frame));
755 }
756}
757
758TEST_F(Config80211Test, NL80211_CMD_CONNECT) {
759 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
760 const_cast<nlmsghdr *>(
761 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT)));
762
763 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800764 EXPECT_EQ(message->message_type(), NL80211_CMD_CONNECT);
Wade Guthrie0d438532012-05-18 14:18:50 -0700765
766 {
767 uint8_t value;
768 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
769 EXPECT_EQ(value, kExpectedWifi);
770 }
771
772 {
773 uint32_t value;
774 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
775 EXPECT_EQ(value, kExpectedIfIndex);
776 }
777
778 {
779 string value;
780 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
781 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
782 }
783
784 {
785 uint16_t value;
786 EXPECT_TRUE(message->GetU16Attribute(NL80211_ATTR_STATUS_CODE, &value));
787 EXPECT_EQ(value, kExpectedConnectStatus);
788 }
789
790 // TODO(wdg): Need to check the value of this attribute.
791 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_RESP_IE));
792}
793
794TEST_F(Config80211Test, NL80211_CMD_DEAUTHENTICATE) {
795 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
796 const_cast<nlmsghdr *>(
797 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE)));
798
799 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800800 EXPECT_EQ(message->message_type(), NL80211_CMD_DEAUTHENTICATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700801
802 {
803 uint8_t value;
804 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
805 EXPECT_EQ(value, kExpectedWifi);
806 }
807
808 {
809 uint32_t value;
810 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
811 EXPECT_EQ(value, kExpectedIfIndex);
812 }
813
814 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800815 ByteString rawdata;
816 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata));
817 EXPECT_FALSE(rawdata.IsEmpty());
818 Nl80211Frame frame(rawdata);
819 Nl80211Frame expected_frame(ByteString(kDeauthenticateFrame,
820 sizeof(kDeauthenticateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700821 EXPECT_TRUE(frame.IsEqual(expected_frame));
822 }
823}
824
825TEST_F(Config80211Test, NL80211_CMD_DISCONNECT) {
826 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
827 const_cast<nlmsghdr *>(
828 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT)));
829
830 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800831 EXPECT_EQ(message->message_type(), NL80211_CMD_DISCONNECT);
Wade Guthrie0d438532012-05-18 14:18:50 -0700832
833 {
834 uint8_t value;
835 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
836 EXPECT_EQ(value, kExpectedWifi);
837 }
838
839 {
840 uint32_t value;
841 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
842 EXPECT_EQ(value, kExpectedIfIndex);
843 }
844
845 {
846 uint16_t value;
847 EXPECT_TRUE(message->GetU16Attribute(NL80211_ATTR_REASON_CODE, &value));
848 EXPECT_EQ(value, kExpectedDisconnectReason);
849 }
850
851 // Significant only in its existence.
852 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP));
853}
854
855TEST_F(Config80211Test, NL80211_CMD_NOTIFY_CQM) {
856 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
857 const_cast<nlmsghdr *>(
858 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM)));
859
860 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800861 EXPECT_EQ(message->message_type(), NL80211_CMD_NOTIFY_CQM);
Wade Guthrie0d438532012-05-18 14:18:50 -0700862
863
864 {
865 uint8_t value;
866 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
867 EXPECT_EQ(value, kExpectedWifi);
868 }
869
870 {
871 uint32_t value;
872 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
873 EXPECT_EQ(value, kExpectedIfIndex);
874 }
875
876 {
877 string value;
878 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
879 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
880 }
881
882 // TODO(wdg): Make config80211 handle nested attributes so it can deal
883 // with things like NL80211_ATTR_CQM (without just calling nla_parse_nested).
884 {
885 static const nla_policy kCqmPolicy[NL80211_ATTR_CQM_MAX + 1] = {
886 { NLA_U32, 0, 0 }, // Who Knows?
887 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
888 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
889 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
890 };
891
892 EXPECT_TRUE(message->AttributeExists(NL80211_ATTR_CQM));
893 const nlattr *const_data = message->GetAttribute(NL80211_ATTR_CQM);
894 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
895 EXPECT_NE(cqm_attr, reinterpret_cast<nlattr *>(NULL));
896
897 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
898 EXPECT_EQ(nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
899 const_cast<nla_policy *>(kCqmPolicy)), 0);
900
901 EXPECT_FALSE(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
902 EXPECT_TRUE(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800903 EXPECT_EQ(Nl80211Attribute::NlaGetU32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]),
Wade Guthrie0d438532012-05-18 14:18:50 -0700904 kExpectedCqmNotAcked);
905 }
906}
907
908TEST_F(Config80211Test, NL80211_CMD_DISASSOCIATE) {
909 UserBoundNlMessage *message = UserBoundNlMessageFactory::CreateMessage(
910 const_cast<nlmsghdr *>(
911 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE)));
912
913 EXPECT_NE(message, reinterpret_cast<UserBoundNlMessage *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800914 EXPECT_EQ(message->message_type(), NL80211_CMD_DISASSOCIATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700915
916
917 {
918 uint8_t value;
919 EXPECT_TRUE(message->GetU8Attribute(NL80211_ATTR_WIPHY, &value));
920 EXPECT_EQ(value, kExpectedWifi);
921 }
922
923 {
924 uint32_t value;
925 EXPECT_TRUE(message->GetU32Attribute(NL80211_ATTR_IFINDEX, &value));
926 EXPECT_EQ(value, kExpectedIfIndex);
927 }
928
929 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800930 ByteString rawdata;
931 EXPECT_TRUE(message->GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata));
932 EXPECT_FALSE(rawdata.IsEmpty());
933 Nl80211Frame frame(rawdata);
934 Nl80211Frame expected_frame(ByteString(kDisassociateFrame,
935 sizeof(kDisassociateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700936 EXPECT_TRUE(frame.IsEqual(expected_frame));
937 }
938}
939
940} // namespace shill