blob: 8791220e99bfb09221f535958e7c652cd51cf013 [file] [log] [blame]
Wade Guthrie12f113a2013-03-12 17:15:46 -07001// Copyright (c) 2013 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// NetlinkMessageFactory's ability to create specific message types and it
7// tests the various NetlinkMessage types' ability to parse those
8// messages.
9
10// This file tests the public interface to NetlinkMessage.
11
12#include "shill/nl80211_message.h"
13
14#include <string>
15#include <vector>
16
17#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19
20#include "shill/mock_netlink_socket.h"
21#include "shill/netlink_attribute.h"
22#include "shill/refptr_types.h"
23
24using base::Bind;
25using base::Unretained;
26using std::string;
27using std::vector;
28using testing::_;
29using testing::Invoke;
30using testing::Return;
31using testing::Test;
32
33namespace shill {
34
35namespace {
36
Wade Guthriebb9fca22013-04-10 17:21:42 -070037// These data blocks have been collected by shill using NetlinkManager while,
Wade Guthrie12f113a2013-03-12 17:15:46 -070038// simultaneously (and manually) comparing shill output with that of the 'iw'
39// code from which it was derived. The test strings represent the raw packet
40// data coming from the kernel. The comments above each of these strings is
41// the markup that "iw" outputs for ech of these packets.
42
43// These constants are consistent throughout the packets, below.
44
45const uint32_t kExpectedIfIndex = 4;
46const uint32_t kWiPhy = 0;
47const uint16_t kNl80211FamilyId = 0x13;
48const char kExpectedMacAddress[] = "c0:3f:0e:77:e8:7f";
49
50const uint8_t kMacAddressBytes[] = {
51 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f
52};
53
54const uint8_t kAssignedRespIeBytes[] = {
55 0x01, 0x08, 0x82, 0x84,
56 0x8b, 0x96, 0x0c, 0x12,
57 0x18, 0x24, 0x32, 0x04,
58 0x30, 0x48, 0x60, 0x6c
59};
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[] = {
273 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
277};
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
347const char kGetFamilyCommandString[] = "CTRL_CMD_GETFAMILY";
348
349} // namespace
350
351class NetlinkMessageTest : public Test {
352 public:
353 NetlinkMessageTest() {
354 message_factory_.AddFactoryMethod(
355 kNl80211FamilyId, Bind(&Nl80211Message::CreateMessage));
356 Nl80211Message::SetMessageType(kNl80211FamilyId);
357 }
358
359 protected:
360 NetlinkMessageFactory message_factory_;
361};
362
363TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_TRIGGER_SCAN) {
364 NetlinkMessage *netlink_message =
365 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
366 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN)));
367
368 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
369 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
370 // The follwing is legal if the message_type is kNl80211FamilyId.
371 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
372
373 EXPECT_EQ(NL80211_CMD_TRIGGER_SCAN, message->command());
374
375 {
376 uint32_t value;
377 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
378 NL80211_ATTR_WIPHY, &value));
379 EXPECT_EQ(kWiPhy, value);
380 }
381
382 {
383 uint32_t value;
384 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
385 NL80211_ATTR_IFINDEX, &value));
386 EXPECT_EQ(kExpectedIfIndex, value);
387 }
388
389 // Make sure the scan frequencies in the attribute are the ones we expect.
390 {
391 vector<uint32_t>list;
392 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
393 NL80211_ATTR_SCAN_FREQUENCIES, &list));
394 EXPECT_EQ(list.size(), arraysize(kScanFrequencyTrigger));
395 int i = 0;
396 vector<uint32_t>::const_iterator j = list.begin();
397 while (j != list.end()) {
398 EXPECT_EQ(kScanFrequencyTrigger[i], *j);
399 ++i;
400 ++j;
401 }
402 }
403
404 {
405 vector<string> ssids;
406 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
407 &ssids));
408 EXPECT_EQ(1, ssids.size());
409 EXPECT_EQ(0, ssids[0].compare("")); // Expect a single, empty SSID.
410 }
411
412 EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue(
413 NL80211_ATTR_SUPPORT_MESH_AUTH));
414}
415
416TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NEW_SCAN_RESULTS) {
417 NetlinkMessage *netlink_message =
418 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
419 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS)));
420
421 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
422 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
423 // The follwing is legal if the message_type is kNl80211FamilyId.
424 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
425
426 EXPECT_EQ(NL80211_CMD_NEW_SCAN_RESULTS, message->command());
427
428 {
429 uint32_t value;
430 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
431 NL80211_ATTR_WIPHY, &value));
432 EXPECT_EQ(kWiPhy, value);
433 }
434
435 {
436 uint32_t value;
437 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
438 NL80211_ATTR_IFINDEX, &value));
439 EXPECT_EQ(kExpectedIfIndex, value);
440 }
441
442 // Make sure the scan frequencies in the attribute are the ones we expect.
443 {
444 vector<uint32_t>list;
445 EXPECT_TRUE(message->GetScanFrequenciesAttribute(
446 NL80211_ATTR_SCAN_FREQUENCIES, &list));
447 EXPECT_EQ(arraysize(kScanFrequencyResults), list.size());
448 int i = 0;
449 vector<uint32_t>::const_iterator j = list.begin();
450 while (j != list.end()) {
451 EXPECT_EQ(kScanFrequencyResults[i], *j);
452 ++i;
453 ++j;
454 }
455 }
456
457 {
458 vector<string> ssids;
459 EXPECT_TRUE(message->GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS,
460 &ssids));
461 EXPECT_EQ(1, ssids.size());
462 EXPECT_EQ(0, ssids[0].compare("")); // Expect a single, empty SSID.
463 }
464
465 EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue(
466 NL80211_ATTR_SUPPORT_MESH_AUTH));
467}
468
469TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NEW_STATION) {
470 NetlinkMessage *netlink_message =
471 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
472 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION)));
473
474 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
475 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
476 // The follwing is legal if the message_type is kNl80211FamilyId.
477 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
478 EXPECT_EQ(NL80211_CMD_NEW_STATION, message->command());
479
480 {
481 uint32_t value;
482 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
483 NL80211_ATTR_IFINDEX, &value));
484 EXPECT_EQ(kExpectedIfIndex, value);
485 }
486
487 {
488 string value;
489 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
490 EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
491 }
492
493 {
494 AttributeListConstRefPtr nested;
495 EXPECT_TRUE(message->const_attributes()->ConstGetNestedAttributeList(
496 NL80211_ATTR_STA_INFO, &nested));
497 }
498
499 {
500 uint32_t value;
501 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
502 NL80211_ATTR_GENERATION, &value));
503 EXPECT_EQ(kNewStationExpectedGeneration, value);
504 }
505}
506
507TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_AUTHENTICATE) {
508 NetlinkMessage *netlink_message =
509 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
510 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE)));
511
512 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
513 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
514 // The follwing is legal if the message_type is kNl80211FamilyId.
515 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
516 EXPECT_EQ(NL80211_CMD_AUTHENTICATE, message->command());
517
518 {
519 uint32_t value;
520 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
521 NL80211_ATTR_WIPHY, &value));
522 EXPECT_EQ(kWiPhy, value);
523 }
524
525 {
526 uint32_t value;
527 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
528 NL80211_ATTR_IFINDEX, &value));
529 EXPECT_EQ(kExpectedIfIndex, value);
530 }
531
532 {
533 ByteString rawdata;
534 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
535 NL80211_ATTR_FRAME, &rawdata));
536 EXPECT_FALSE(rawdata.IsEmpty());
537 Nl80211Frame frame(rawdata);
538 Nl80211Frame expected_frame(ByteString(kAuthenticateFrame,
539 sizeof(kAuthenticateFrame)));
540 EXPECT_TRUE(frame.IsEqual(expected_frame));
541 }
542}
543
544TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_ASSOCIATE) {
545 NetlinkMessage *netlink_message =
546 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
547 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE)));
548
549 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
550 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
551 // The follwing is legal if the message_type is kNl80211FamilyId.
552 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
553 EXPECT_EQ(NL80211_CMD_ASSOCIATE, message->command());
554
555 {
556 uint32_t value;
557 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
558 NL80211_ATTR_WIPHY, &value));
559 EXPECT_EQ(kWiPhy, value);
560 }
561
562 {
563 uint32_t value;
564 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
565 NL80211_ATTR_IFINDEX, &value));
566 EXPECT_EQ(kExpectedIfIndex, value);
567 }
568
569 {
570 ByteString rawdata;
571 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
572 NL80211_ATTR_FRAME, &rawdata));
573 EXPECT_FALSE(rawdata.IsEmpty());
574 Nl80211Frame frame(rawdata);
575 Nl80211Frame expected_frame(ByteString(kAssociateFrame,
576 sizeof(kAssociateFrame)));
577 EXPECT_TRUE(frame.IsEqual(expected_frame));
578 }
579}
580
581TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_CONNECT) {
582 NetlinkMessage *netlink_message =
583 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
584 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT)));
585
586 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
587 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
588 // The follwing is legal if the message_type is kNl80211FamilyId.
589 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
590 EXPECT_EQ(NL80211_CMD_CONNECT, message->command());
591
592 {
593 uint32_t value;
594 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
595 NL80211_ATTR_WIPHY, &value));
596 EXPECT_EQ(kWiPhy, value);
597 }
598
599 {
600 uint32_t value;
601 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
602 NL80211_ATTR_IFINDEX, &value));
603 EXPECT_EQ(kExpectedIfIndex, value);
604 }
605
606 {
607 string value;
608 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
609 EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
610 }
611
612 {
613 uint16_t value;
614 EXPECT_TRUE(message->const_attributes()->GetU16AttributeValue(
615 NL80211_ATTR_STATUS_CODE, &value));
616 EXPECT_EQ(kExpectedConnectStatus, value);
617 }
618
619 // TODO(wdg): Need to check the value of this attribute.
620 {
621 ByteString rawdata;
622 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
623 NL80211_ATTR_RESP_IE, &rawdata));
624 }
625}
626
627TEST_F(NetlinkMessageTest, Build_NL80211_CMD_CONNECT) {
628 // Build the message that is found in kNL80211_CMD_CONNECT.
629 ConnectMessage message;
630 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_WIPHY,
631 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
632 EXPECT_TRUE(message.attributes()->SetU32AttributeValue(NL80211_ATTR_WIPHY,
633 kWiPhy));
634
635 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_IFINDEX,
636 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
637 EXPECT_TRUE(message.attributes()->SetU32AttributeValue(
638 NL80211_ATTR_IFINDEX, kExpectedIfIndex));
639
640 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_MAC,
641 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
642 EXPECT_TRUE(message.attributes()->SetRawAttributeValue(NL80211_ATTR_MAC,
643 ByteString(kMacAddressBytes, arraysize(kMacAddressBytes))));
644
Wade Guthrie35cb4b92013-04-19 10:46:11 -0700645 // In the middle, let's try adding an attribute without populating it.
646 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_REG_TYPE,
647 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
648
Wade Guthrie12f113a2013-03-12 17:15:46 -0700649 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_STATUS_CODE,
650 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
651 EXPECT_TRUE(message.attributes()->SetU16AttributeValue(
652 NL80211_ATTR_STATUS_CODE, kExpectedConnectStatus));
653
654 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_RESP_IE,
655 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
656 EXPECT_TRUE(message.attributes()->SetRawAttributeValue(NL80211_ATTR_RESP_IE,
657 ByteString(kAssignedRespIeBytes, arraysize(kAssignedRespIeBytes))));
658
659 // Encode the message to a ByteString and remove all the run-specific
660 // values.
661 static const uint32_t kArbitrarySequenceNumber = 42;
662 ByteString message_bytes = message.Encode(kArbitrarySequenceNumber);
663 nlmsghdr *header = reinterpret_cast<nlmsghdr *>(message_bytes.GetData());
664 header->nlmsg_flags = 0; // Overwrite with known values.
665 header->nlmsg_seq = 0;
666 header->nlmsg_pid = 0;
667
668 // Verify that the messages are equal.
669 EXPECT_TRUE(message_bytes.Equals(
670 ByteString(kNL80211_CMD_CONNECT, arraysize(kNL80211_CMD_CONNECT))));
671}
672
673
674TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DEAUTHENTICATE) {
675 NetlinkMessage *netlink_message =
676 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
677 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE)));
678
679 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
680 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
681 // The follwing is legal if the message_type is kNl80211FamilyId.
682 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
683 EXPECT_EQ(NL80211_CMD_DEAUTHENTICATE, message->command());
684
685 {
686 uint32_t value;
687 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
688 NL80211_ATTR_WIPHY, &value));
689 EXPECT_EQ(kWiPhy, value);
690 }
691
692 {
693 uint32_t value;
694 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
695 NL80211_ATTR_IFINDEX, &value));
696 EXPECT_EQ(kExpectedIfIndex, value);
697 }
698
699 {
700 ByteString rawdata;
701 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
702 NL80211_ATTR_FRAME, &rawdata));
703 EXPECT_FALSE(rawdata.IsEmpty());
704 Nl80211Frame frame(rawdata);
705 Nl80211Frame expected_frame(ByteString(kDeauthenticateFrame,
706 sizeof(kDeauthenticateFrame)));
707 EXPECT_TRUE(frame.IsEqual(expected_frame));
708 }
709}
710
711TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DISCONNECT) {
712 NetlinkMessage *netlink_message =
713 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
714 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT)));
715
716 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
717 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
718 // The follwing is legal if the message_type is kNl80211FamilyId.
719 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
720 EXPECT_EQ(NL80211_CMD_DISCONNECT, message->command());
721
722 {
723 uint32_t value;
724 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
725 NL80211_ATTR_WIPHY, &value));
726 EXPECT_EQ(kWiPhy, value);
727 }
728
729 {
730 uint32_t value;
731 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
732 NL80211_ATTR_IFINDEX, &value));
733 EXPECT_EQ(kExpectedIfIndex, value);
734 }
735
736 {
737 uint16_t value;
738 EXPECT_TRUE(message->const_attributes()->GetU16AttributeValue(
739 NL80211_ATTR_REASON_CODE, &value));
740 EXPECT_EQ(kExpectedDisconnectReason, value);
741 }
742
743 EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue(
744 NL80211_ATTR_DISCONNECTED_BY_AP));
745}
746
747TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NOTIFY_CQM) {
748 NetlinkMessage *netlink_message =
749 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
750 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM)));
751
752 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
753 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
754 // The follwing is legal if the message_type is kNl80211FamilyId.
755 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
756 EXPECT_EQ(NL80211_CMD_NOTIFY_CQM, message->command());
757
758 {
759 uint32_t value;
760 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
761 NL80211_ATTR_WIPHY, &value));
762 EXPECT_EQ(kWiPhy, value);
763 }
764
765 {
766 uint32_t value;
767 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
768 NL80211_ATTR_IFINDEX, &value));
769 EXPECT_EQ(kExpectedIfIndex, value);
770 }
771
772 {
773 string value;
774 EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
775 EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
776 }
777
778 {
779 AttributeListConstRefPtr nested;
780 EXPECT_TRUE(message->const_attributes()->ConstGetNestedAttributeList(
781 NL80211_ATTR_CQM, &nested));
782 uint32_t threshold_event;
783 EXPECT_FALSE(nested->GetU32AttributeValue(
784 NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT, &threshold_event));
785 uint32_t pkt_loss_event;
786 EXPECT_TRUE(nested->GetU32AttributeValue(
787 NL80211_ATTR_CQM_PKT_LOSS_EVENT, &pkt_loss_event));
788 EXPECT_EQ(kExpectedCqmNotAcked, pkt_loss_event);
789 }
790}
791
792TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DISASSOCIATE) {
793 NetlinkMessage *netlink_message =
794 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
795 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE)));
796
797 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
798 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
799 // The follwing is legal if the message_type is kNl80211FamilyId.
800 Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message);
801 EXPECT_EQ(NL80211_CMD_DISASSOCIATE, message->command());
802
803
804 {
805 uint32_t value;
806 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
807 NL80211_ATTR_WIPHY, &value));
808 EXPECT_EQ(kWiPhy, value);
809 }
810
811 {
812 uint32_t value;
813 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
814 NL80211_ATTR_IFINDEX, &value));
815 EXPECT_EQ(kExpectedIfIndex, value);
816 }
817
818 {
819 ByteString rawdata;
820 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
821 NL80211_ATTR_FRAME, &rawdata));
822 EXPECT_FALSE(rawdata.IsEmpty());
823 Nl80211Frame frame(rawdata);
824 Nl80211Frame expected_frame(ByteString(kDisassociateFrame,
825 sizeof(kDisassociateFrame)));
826 EXPECT_TRUE(frame.IsEqual(expected_frame));
827 }
828}
829
830} // namespace shill