blob: 6a81c1ec30e11f61350dc0e7482aefef4733b5ab [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
repo syncdc085c82012-12-28 08:54:41 -08006// Nl80211MessageFactory's ability to create specific message types and it
7// tests the various Nl80211Message types' ability to parse those
Wade Guthrie0d438532012-05-18 14:18:50 -07008// 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 Guthrieb1ec8602012-10-18 17:26:14 -070028#include "shill/mock_callback80211_object.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070029#include "shill/mock_nl80211_socket.h"
Wade Guthrie8343f7f2012-12-04 13:52:32 -080030#include "shill/nl80211_attribute.h"
repo syncdc085c82012-12-28 08:54:41 -080031#include "shill/nl80211_message.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070032#include "shill/nl80211_socket.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070033
34using base::Bind;
35using base::Unretained;
repo sync12cca802012-12-19 17:34:22 -080036using base::WeakPtr;
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;
repo sync1538d442012-12-20 15:24:35 -080058const uint32_t kExpectedWifi = 0;
Wade Guthrie0d438532012-05-18 14:18:50 -070059const 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
repo syncdc085c82012-12-28 08:54:41 -0800347const char kGetFamilyCommandString[] = "CTRL_CMD_GETFAMILY";
348
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700349} // namespace
repo syncdc085c82012-12-28 08:54:41 -0800350
351bool MockNl80211Socket::SendMessage(Nl80211Message *message) {
352 if (!message) {
353 LOG(ERROR) << "Null |message|";
354 return false;
Christopher Wiley393b93f2012-11-08 17:30:58 -0800355 }
repo syncdc085c82012-12-28 08:54:41 -0800356 return true;
357}
358
359uint32_t MockNl80211Socket::GetSequenceNumber() {
360 // Sequence number 0 is reserved for broadcast messages from the kernel.
361 if (++sequence_number_ == 0)
362 ++sequence_number_;
363 return sequence_number_;
Christopher Wiley393b93f2012-11-08 17:30:58 -0800364}
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700365
Wade Guthrie0d438532012-05-18 14:18:50 -0700366class Config80211Test : public Test {
367 public:
368 Config80211Test() : config80211_(Config80211::GetInstance()) {}
Han Shen4afba202012-12-17 08:48:35 -0800369 ~Config80211Test() {
370 // Config80211 is a singleton, the sock_ field *MUST* be cleared
371 // before "Config80211Test::socket_" gets invalidated, otherwise
372 // later tests will refer to a corrupted memory.
373 config80211_->sock_ = NULL;
374 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700375 void SetupConfig80211Object() {
376 EXPECT_NE(config80211_, reinterpret_cast<Config80211 *>(NULL));
377 config80211_->sock_ = &socket_;
378 EXPECT_TRUE(config80211_->Init(reinterpret_cast<EventDispatcher *>(NULL)));
Wade Guthried6153612012-08-23 11:36:14 -0700379 config80211_->Reset();
Wade Guthrie0d438532012-05-18 14:18:50 -0700380 }
381
382 Config80211 *config80211_;
383 MockNl80211Socket socket_;
384};
385
386class TestCallbackObject {
387 public:
388 TestCallbackObject() : callback_(Bind(&TestCallbackObject::MessageHandler,
389 Unretained(this))) { }
repo syncdc085c82012-12-28 08:54:41 -0800390 void MessageHandler(const Nl80211Message &msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700391 }
392 const Config80211::Callback &GetCallback() const { return callback_; }
393
394 private:
395 Config80211::Callback callback_;
396};
397
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700398// Checks a config80211 parameter to make sure it contains |callback_arg|
399// in its list of broadcast callbacks.
400MATCHER_P(ContainsCallback, callback_arg, "") {
401 if (arg == reinterpret_cast<void *>(NULL)) {
402 LOG(WARNING) << "NULL parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700403 return false;
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700404 }
405 const Config80211 *config80211 = static_cast<Config80211 *>(arg);
406 const Config80211::Callback callback =
407 static_cast<const Config80211::Callback>(callback_arg);
408
409 return config80211->FindBroadcastCallback(callback);
Wade Guthrie0d438532012-05-18 14:18:50 -0700410}
411
412TEST_F(Config80211Test, AddLinkTest) {
413 SetupConfig80211Object();
414
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700415 // Create a broadcast callback.
Wade Guthrie0d438532012-05-18 14:18:50 -0700416 TestCallbackObject callback_object;
417
Wade Guthried6153612012-08-23 11:36:14 -0700418 // Install the callback and subscribe to events using it, wifi down
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700419 // (shouldn't actually send the subscription request until wifi comes up).
Wade Guthried6153612012-08-23 11:36:14 -0700420 EXPECT_CALL(socket_, AddGroupMembership(_)).Times(0);
Wade Guthried6153612012-08-23 11:36:14 -0700421
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700422 EXPECT_TRUE(config80211_->AddBroadcastCallback(
423 callback_object.GetCallback()));
Wade Guthried6153612012-08-23 11:36:14 -0700424 Config80211::EventType scan_event = Config80211::kEventTypeScan;
425 string scan_event_string;
426 EXPECT_TRUE(Config80211::GetEventTypeString(scan_event, &scan_event_string));
427 EXPECT_TRUE(config80211_->SubscribeToEvents(scan_event));
428
429 // Wifi up, should subscribe to events.
430 EXPECT_CALL(socket_, AddGroupMembership(scan_event_string))
Wade Guthrie0d438532012-05-18 14:18:50 -0700431 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700432 config80211_->SetWifiState(Config80211::kWifiUp);
Wade Guthrie0d438532012-05-18 14:18:50 -0700433
Wade Guthried6153612012-08-23 11:36:14 -0700434 // Second subscribe, same event (should do nothing).
435 EXPECT_CALL(socket_, AddGroupMembership(_)).Times(0);
Wade Guthried6153612012-08-23 11:36:14 -0700436 EXPECT_TRUE(config80211_->SubscribeToEvents(scan_event));
Wade Guthrie0d438532012-05-18 14:18:50 -0700437
Wade Guthried6153612012-08-23 11:36:14 -0700438 // Bring the wifi back down.
439 config80211_->SetWifiState(Config80211::kWifiDown);
440
441 // Subscribe to a new event with the wifi down (should still do nothing).
442 Config80211::EventType mlme_event = Config80211::kEventTypeMlme;
443 string mlme_event_string;
444 EXPECT_TRUE(Config80211::GetEventTypeString(mlme_event, &mlme_event_string));
445 EXPECT_TRUE(config80211_->SubscribeToEvents(mlme_event));
446
447 // Wifi up (again), should subscribe to the original scan event and the new
448 // mlme event.
449 EXPECT_CALL(socket_, AddGroupMembership(scan_event_string))
Wade Guthrie0d438532012-05-18 14:18:50 -0700450 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700451 EXPECT_CALL(socket_, AddGroupMembership(mlme_event_string))
452 .WillOnce(Return(true));
Wade Guthried6153612012-08-23 11:36:14 -0700453 config80211_->SetWifiState(Config80211::kWifiUp);
Wade Guthrie0d438532012-05-18 14:18:50 -0700454}
455
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700456TEST_F(Config80211Test, BroadcastCallbackTest) {
457 SetupConfig80211Object();
458
459 nlmsghdr *message = const_cast<nlmsghdr *>(
460 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT));
461
Christopher Wileyfe34be02012-11-12 16:02:46 -0800462 MockCallback80211 callback1;
463 MockCallback80211 callback2;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700464
465 // Simple, 1 callback, case.
466 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
Christopher Wileyfe34be02012-11-12 16:02:46 -0800467 callback1.InstallAsBroadcastCallback();
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700468 config80211_->OnNlMessageReceived(message);
469
470 // Add a second callback.
471 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
472 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
473 EXPECT_TRUE(callback2.InstallAsBroadcastCallback());
474 config80211_->OnNlMessageReceived(message);
475
476 // Verify that a callback can't be added twice.
477 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
478 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
479 EXPECT_FALSE(callback1.InstallAsBroadcastCallback());
480 config80211_->OnNlMessageReceived(message);
481
482 // Check that we can remove a callback.
483 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(0);
484 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
485 EXPECT_TRUE(callback1.DeinstallAsCallback());
486 config80211_->OnNlMessageReceived(message);
487
488 // Check that re-adding the callback goes smoothly.
489 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(1);
490 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(1);
491 EXPECT_TRUE(callback1.InstallAsBroadcastCallback());
492 config80211_->OnNlMessageReceived(message);
493
494 // Check that ClearBroadcastCallbacks works.
495 config80211_->ClearBroadcastCallbacks();
496 EXPECT_CALL(callback1, Config80211MessageCallback(_)).Times(0);
497 EXPECT_CALL(callback2, Config80211MessageCallback(_)).Times(0);
498 config80211_->OnNlMessageReceived(message);
499}
500
501TEST_F(Config80211Test, MessageCallbackTest) {
502 // Setup.
503 SetupConfig80211Object();
504
Christopher Wileyfe34be02012-11-12 16:02:46 -0800505 MockCallback80211 callback_broadcast;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700506 EXPECT_TRUE(callback_broadcast.InstallAsBroadcastCallback());
507
repo syncdc085c82012-12-28 08:54:41 -0800508 Nl80211Message sent_message_1(CTRL_CMD_GETFAMILY, kGetFamilyCommandString);
Christopher Wileyfe34be02012-11-12 16:02:46 -0800509 MockCallback80211 callback_sent_1;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700510
repo syncdc085c82012-12-28 08:54:41 -0800511 Nl80211Message sent_message_2(CTRL_CMD_GETFAMILY, kGetFamilyCommandString);
Christopher Wileyfe34be02012-11-12 16:02:46 -0800512 MockCallback80211 callback_sent_2;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700513
514 // Set up the received message as a response to sent_message_1.
515 scoped_array<unsigned char> message_memory(
516 new unsigned char[sizeof(kNL80211_CMD_DISCONNECT)]);
517 memcpy(message_memory.get(), kNL80211_CMD_DISCONNECT,
518 sizeof(kNL80211_CMD_DISCONNECT));
519 nlmsghdr *received_message =
520 reinterpret_cast<nlmsghdr *>(message_memory.get());
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700521
522 // Now, we can start the actual test...
523
524 // Verify that generic callback gets called for a message when no
525 // message-specific callback has been installed.
526 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
527 config80211_->OnNlMessageReceived(received_message);
528
Christopher Wiley393b93f2012-11-08 17:30:58 -0800529 // Send the message and give our callback. Verify that we get called back.
530 EXPECT_TRUE(config80211_->SendMessage(&sent_message_1,
Christopher Wileyfe34be02012-11-12 16:02:46 -0800531 callback_sent_1.callback()));
Christopher Wiley393b93f2012-11-08 17:30:58 -0800532 // Make it appear that this message is in response to our sent message.
533 received_message->nlmsg_seq = socket_.GetLastSequenceNumber();
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700534 EXPECT_CALL(callback_sent_1, Config80211MessageCallback(_)).Times(1);
535 config80211_->OnNlMessageReceived(received_message);
536
537 // Verify that broadcast callback is called for the message after the
538 // message-specific callback is called once.
539 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
540 config80211_->OnNlMessageReceived(received_message);
541
542 // Install and then uninstall message-specific callback; verify broadcast
543 // callback is called on message receipt.
Christopher Wiley393b93f2012-11-08 17:30:58 -0800544 EXPECT_TRUE(config80211_->SendMessage(&sent_message_1,
Christopher Wileyfe34be02012-11-12 16:02:46 -0800545 callback_sent_1.callback()));
Christopher Wiley393b93f2012-11-08 17:30:58 -0800546 received_message->nlmsg_seq = socket_.GetLastSequenceNumber();
547 EXPECT_TRUE(config80211_->RemoveMessageCallback(sent_message_1));
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700548 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
549 config80211_->OnNlMessageReceived(received_message);
550
551 // Install callback for different message; verify that broadcast callback is
552 // called for _this_ message.
Christopher Wiley393b93f2012-11-08 17:30:58 -0800553 EXPECT_TRUE(config80211_->SendMessage(&sent_message_2,
Christopher Wileyfe34be02012-11-12 16:02:46 -0800554 callback_sent_2.callback()));
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700555 EXPECT_CALL(callback_broadcast, Config80211MessageCallback(_)).Times(1);
556 config80211_->OnNlMessageReceived(received_message);
557
558 // Change the ID for the message to that of the second callback; verify that
559 // the appropriate callback is called for _that_ message.
Christopher Wiley393b93f2012-11-08 17:30:58 -0800560 received_message->nlmsg_seq = socket_.GetLastSequenceNumber();
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700561 EXPECT_CALL(callback_sent_2, Config80211MessageCallback(_)).Times(1);
562 config80211_->OnNlMessageReceived(received_message);
563}
564
Wade Guthrie0d438532012-05-18 14:18:50 -0700565TEST_F(Config80211Test, NL80211_CMD_TRIGGER_SCAN) {
repo syncdc085c82012-12-28 08:54:41 -0800566 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700567 const_cast<nlmsghdr *>(
568 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN)));
569
repo syncdc085c82012-12-28 08:54:41 -0800570 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800571 EXPECT_EQ(message->message_type(), NL80211_CMD_TRIGGER_SCAN);
Wade Guthrie0d438532012-05-18 14:18:50 -0700572
573 {
repo syncd316eb72012-12-10 15:48:47 -0800574 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800575 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
576 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700577 EXPECT_EQ(value, kExpectedWifi);
578 }
579
580 {
581 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800582 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
583 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700584 EXPECT_EQ(value, kExpectedIfIndex);
585 }
586
587 // Make sure the scan frequencies in the attribute are the ones we expect.
588 {
589 vector<uint32_t>list;
590 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
591 NL80211_ATTR_SCAN_FREQUENCIES, &list));
592 EXPECT_EQ(arraysize(kScanFrequencyTrigger), list.size());
593 int i = 0;
594 vector<uint32_t>::const_iterator j = list.begin();
595 while (j != list.end()) {
596 EXPECT_EQ(kScanFrequencyTrigger[i], *j);
597 ++i;
598 ++j;
599 }
600 }
601
602 {
603 vector<string> ssids;
604 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
605 &ssids));
606 EXPECT_EQ(ssids.size(), 1);
607 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
608 }
609
repo sync90ee0fa2012-12-18 10:08:08 -0800610 EXPECT_TRUE(message->attributes().IsFlagAttributeTrue(
611 NL80211_ATTR_SUPPORT_MESH_AUTH));
Wade Guthrie0d438532012-05-18 14:18:50 -0700612}
613
614TEST_F(Config80211Test, NL80211_CMD_NEW_SCAN_RESULTS) {
repo syncdc085c82012-12-28 08:54:41 -0800615 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700616 const_cast<nlmsghdr *>(
617 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS)));
618
repo syncdc085c82012-12-28 08:54:41 -0800619 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800620 EXPECT_EQ(message->message_type(), NL80211_CMD_NEW_SCAN_RESULTS);
Wade Guthrie0d438532012-05-18 14:18:50 -0700621
622 {
repo syncd316eb72012-12-10 15:48:47 -0800623 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800624 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
625 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700626 EXPECT_EQ(value, kExpectedWifi);
627 }
628
629 {
630 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800631 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
632 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700633 EXPECT_EQ(value, kExpectedIfIndex);
634 }
635
636 // Make sure the scan frequencies in the attribute are the ones we expect.
637 {
638 vector<uint32_t>list;
639 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
640 NL80211_ATTR_SCAN_FREQUENCIES, &list));
641 EXPECT_EQ(arraysize(kScanFrequencyResults), list.size());
642 int i = 0;
643 vector<uint32_t>::const_iterator j = list.begin();
644 while (j != list.end()) {
645 EXPECT_EQ(kScanFrequencyResults[i], *j);
646 ++i;
647 ++j;
648 }
649 }
650
651 {
652 vector<string> ssids;
653 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
654 &ssids));
655 EXPECT_EQ(ssids.size(), 1);
656 EXPECT_EQ(ssids[0].compare(""), 0); // Expect a single, empty SSID.
657 }
658
repo sync90ee0fa2012-12-18 10:08:08 -0800659 EXPECT_TRUE(message->attributes().IsFlagAttributeTrue(
660 NL80211_ATTR_SUPPORT_MESH_AUTH));
Wade Guthrie0d438532012-05-18 14:18:50 -0700661}
662
663TEST_F(Config80211Test, NL80211_CMD_NEW_STATION) {
repo syncdc085c82012-12-28 08:54:41 -0800664 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700665 const_cast<nlmsghdr *>(
666 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION)));
667
repo syncdc085c82012-12-28 08:54:41 -0800668 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800669 EXPECT_EQ(message->message_type(), NL80211_CMD_NEW_STATION);
Wade Guthrie0d438532012-05-18 14:18:50 -0700670
671 {
672 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800673 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
674 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700675 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
repo sync90ee0fa2012-12-18 10:08:08 -0800684 // TODO(wdg): Look at nested values of NL80211_ATTR_STA_INFO.
685 {
repo sync12cca802012-12-19 17:34:22 -0800686 WeakPtr<AttributeList> nested;
687 EXPECT_TRUE(message->attributes().GetNestedAttributeValue(
repo sync90ee0fa2012-12-18 10:08:08 -0800688 NL80211_ATTR_STA_INFO, &nested));
689 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700690
691 {
692 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800693 EXPECT_TRUE(message->attributes().GetU32AttributeValue(
694 NL80211_ATTR_GENERATION, &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700695 EXPECT_EQ(value, kNewStationExpectedGeneration);
696 }
697}
698
699TEST_F(Config80211Test, NL80211_CMD_AUTHENTICATE) {
repo syncdc085c82012-12-28 08:54:41 -0800700 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700701 const_cast<nlmsghdr *>(
702 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE)));
703
repo syncdc085c82012-12-28 08:54:41 -0800704 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800705 EXPECT_EQ(message->message_type(), NL80211_CMD_AUTHENTICATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700706
707 {
repo syncd316eb72012-12-10 15:48:47 -0800708 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800709 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
710 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700711 EXPECT_EQ(value, kExpectedWifi);
712 }
713
714 {
715 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800716 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
717 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700718 EXPECT_EQ(value, kExpectedIfIndex);
719 }
720
721 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800722 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800723 EXPECT_TRUE(message->attributes().GetRawAttributeValue(NL80211_ATTR_FRAME,
724 &rawdata));
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800725 EXPECT_FALSE(rawdata.IsEmpty());
726 Nl80211Frame frame(rawdata);
727 Nl80211Frame expected_frame(ByteString(kAuthenticateFrame,
728 sizeof(kAuthenticateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700729 EXPECT_TRUE(frame.IsEqual(expected_frame));
730 }
731}
732
733TEST_F(Config80211Test, NL80211_CMD_ASSOCIATE) {
repo syncdc085c82012-12-28 08:54:41 -0800734 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700735 const_cast<nlmsghdr *>(
736 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE)));
737
repo syncdc085c82012-12-28 08:54:41 -0800738 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800739 EXPECT_EQ(message->message_type(), NL80211_CMD_ASSOCIATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700740
741 {
repo syncd316eb72012-12-10 15:48:47 -0800742 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800743 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
744 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700745 EXPECT_EQ(value, kExpectedWifi);
746 }
747
748 {
749 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800750 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
751 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700752 EXPECT_EQ(value, kExpectedIfIndex);
753 }
754
755 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800756 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800757 EXPECT_TRUE(message->attributes().GetRawAttributeValue(NL80211_ATTR_FRAME,
758 &rawdata));
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800759 EXPECT_FALSE(rawdata.IsEmpty());
760 Nl80211Frame frame(rawdata);
761 Nl80211Frame expected_frame(ByteString(kAssociateFrame,
762 sizeof(kAssociateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700763 EXPECT_TRUE(frame.IsEqual(expected_frame));
764 }
765}
766
767TEST_F(Config80211Test, NL80211_CMD_CONNECT) {
repo syncdc085c82012-12-28 08:54:41 -0800768 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700769 const_cast<nlmsghdr *>(
770 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT)));
771
repo syncdc085c82012-12-28 08:54:41 -0800772 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800773 EXPECT_EQ(message->message_type(), NL80211_CMD_CONNECT);
Wade Guthrie0d438532012-05-18 14:18:50 -0700774
775 {
repo syncd316eb72012-12-10 15:48:47 -0800776 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800777 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
778 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700779 EXPECT_EQ(value, kExpectedWifi);
780 }
781
782 {
783 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800784 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
785 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700786 EXPECT_EQ(value, kExpectedIfIndex);
787 }
788
789 {
790 string value;
791 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
792 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
793 }
794
795 {
796 uint16_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800797 EXPECT_TRUE(message->attributes().GetU16AttributeValue(
798 NL80211_ATTR_STATUS_CODE, &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700799 EXPECT_EQ(value, kExpectedConnectStatus);
800 }
801
802 // TODO(wdg): Need to check the value of this attribute.
repo sync90ee0fa2012-12-18 10:08:08 -0800803 {
804 ByteString rawdata;
805 EXPECT_TRUE(message->attributes().GetRawAttributeValue(NL80211_ATTR_RESP_IE,
806 &rawdata));
807 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700808}
809
810TEST_F(Config80211Test, NL80211_CMD_DEAUTHENTICATE) {
repo syncdc085c82012-12-28 08:54:41 -0800811 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700812 const_cast<nlmsghdr *>(
813 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE)));
814
repo syncdc085c82012-12-28 08:54:41 -0800815 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800816 EXPECT_EQ(message->message_type(), NL80211_CMD_DEAUTHENTICATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700817
818 {
repo syncd316eb72012-12-10 15:48:47 -0800819 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800820 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
821 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700822 EXPECT_EQ(value, kExpectedWifi);
823 }
824
825 {
826 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800827 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
828 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700829 EXPECT_EQ(value, kExpectedIfIndex);
830 }
831
832 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800833 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800834 EXPECT_TRUE(message->attributes().GetRawAttributeValue(NL80211_ATTR_FRAME,
835 &rawdata));
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800836 EXPECT_FALSE(rawdata.IsEmpty());
837 Nl80211Frame frame(rawdata);
838 Nl80211Frame expected_frame(ByteString(kDeauthenticateFrame,
839 sizeof(kDeauthenticateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700840 EXPECT_TRUE(frame.IsEqual(expected_frame));
841 }
842}
843
844TEST_F(Config80211Test, NL80211_CMD_DISCONNECT) {
repo syncdc085c82012-12-28 08:54:41 -0800845 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700846 const_cast<nlmsghdr *>(
847 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT)));
848
repo syncdc085c82012-12-28 08:54:41 -0800849 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800850 EXPECT_EQ(message->message_type(), NL80211_CMD_DISCONNECT);
Wade Guthrie0d438532012-05-18 14:18:50 -0700851
852 {
repo syncd316eb72012-12-10 15:48:47 -0800853 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800854 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
855 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700856 EXPECT_EQ(value, kExpectedWifi);
857 }
858
859 {
860 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800861 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
862 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700863 EXPECT_EQ(value, kExpectedIfIndex);
864 }
865
866 {
867 uint16_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800868 EXPECT_TRUE(message->attributes().GetU16AttributeValue(
869 NL80211_ATTR_REASON_CODE, &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700870 EXPECT_EQ(value, kExpectedDisconnectReason);
871 }
872
repo sync90ee0fa2012-12-18 10:08:08 -0800873 EXPECT_TRUE(message->attributes().IsFlagAttributeTrue(
874 NL80211_ATTR_DISCONNECTED_BY_AP));
Wade Guthrie0d438532012-05-18 14:18:50 -0700875}
876
877TEST_F(Config80211Test, NL80211_CMD_NOTIFY_CQM) {
repo syncdc085c82012-12-28 08:54:41 -0800878 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700879 const_cast<nlmsghdr *>(
880 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM)));
881
repo syncdc085c82012-12-28 08:54:41 -0800882 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800883 EXPECT_EQ(message->message_type(), NL80211_CMD_NOTIFY_CQM);
Wade Guthrie0d438532012-05-18 14:18:50 -0700884
885
886 {
repo syncd316eb72012-12-10 15:48:47 -0800887 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800888 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
889 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700890 EXPECT_EQ(value, kExpectedWifi);
891 }
892
893 {
894 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800895 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
896 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700897 EXPECT_EQ(value, kExpectedIfIndex);
898 }
899
900 {
901 string value;
902 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
903 EXPECT_EQ(strncmp(value.c_str(), kExpectedMacAddress, value.length()), 0);
904 }
905
Wade Guthrie0d438532012-05-18 14:18:50 -0700906 {
repo sync12cca802012-12-19 17:34:22 -0800907 WeakPtr<AttributeList> nested;
908 EXPECT_TRUE(message->attributes().GetNestedAttributeValue(NL80211_ATTR_CQM,
909 &nested));
Wade Guthrie3af8b4d2013-01-10 08:39:30 -0800910 uint32_t threshold_event;
911 EXPECT_FALSE(nested->GetU32AttributeValue(
912 NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT, &threshold_event));
repo sync12cca802012-12-19 17:34:22 -0800913 uint32_t pkt_loss_event;
914 EXPECT_TRUE(nested->GetU32AttributeValue(
915 NL80211_ATTR_CQM_PKT_LOSS_EVENT, &pkt_loss_event));
916 EXPECT_EQ(pkt_loss_event, kExpectedCqmNotAcked);
Wade Guthrie0d438532012-05-18 14:18:50 -0700917 }
918}
919
920TEST_F(Config80211Test, NL80211_CMD_DISASSOCIATE) {
repo syncdc085c82012-12-28 08:54:41 -0800921 Nl80211Message *message = Nl80211MessageFactory::CreateMessage(
Wade Guthrie0d438532012-05-18 14:18:50 -0700922 const_cast<nlmsghdr *>(
923 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE)));
924
repo syncdc085c82012-12-28 08:54:41 -0800925 EXPECT_NE(message, reinterpret_cast<Nl80211Message *>(NULL));
Christopher Wiley764538d2012-11-09 10:58:23 -0800926 EXPECT_EQ(message->message_type(), NL80211_CMD_DISASSOCIATE);
Wade Guthrie0d438532012-05-18 14:18:50 -0700927
928
929 {
repo syncd316eb72012-12-10 15:48:47 -0800930 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800931 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
932 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700933 EXPECT_EQ(value, kExpectedWifi);
934 }
935
936 {
937 uint32_t value;
repo sync90ee0fa2012-12-18 10:08:08 -0800938 EXPECT_TRUE(message->attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
939 &value));
Wade Guthrie0d438532012-05-18 14:18:50 -0700940 EXPECT_EQ(value, kExpectedIfIndex);
941 }
942
943 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800944 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800945 EXPECT_TRUE(message->attributes().GetRawAttributeValue(NL80211_ATTR_FRAME,
946 &rawdata));
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800947 EXPECT_FALSE(rawdata.IsEmpty());
948 Nl80211Frame frame(rawdata);
949 Nl80211Frame expected_frame(ByteString(kDisassociateFrame,
950 sizeof(kDisassociateFrame)));
Wade Guthrie0d438532012-05-18 14:18:50 -0700951 EXPECT_TRUE(frame.IsEqual(expected_frame));
952 }
953}
954
955} // namespace shill