blob: 52235365f154291b785ae6ea583a3e0545e67742 [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
Ben Chana0ddf462014-02-06 11:32:42 -080017#include <base/strings/stringprintf.h>
Wade Guthrie12f113a2013-03-12 17:15:46 -070018#include <gmock/gmock.h>
19#include <gtest/gtest.h>
20
Wade Guthrie40d992c2013-04-19 11:10:11 -070021#include "shill/mock_log.h"
Wade Guthrie12f113a2013-03-12 17:15:46 -070022#include "shill/mock_netlink_socket.h"
23#include "shill/netlink_attribute.h"
24#include "shill/refptr_types.h"
25
26using base::Bind;
Wade Guthrie40d992c2013-04-19 11:10:11 -070027using base::StringPrintf;
Wade Guthrie12f113a2013-03-12 17:15:46 -070028using base::Unretained;
29using std::string;
30using std::vector;
31using testing::_;
Wade Guthrie40d992c2013-04-19 11:10:11 -070032using testing::EndsWith;
Wade Guthrie12f113a2013-03-12 17:15:46 -070033using testing::Invoke;
34using testing::Return;
35using testing::Test;
36
37namespace shill {
38
39namespace {
40
Wade Guthriebb9fca22013-04-10 17:21:42 -070041// These data blocks have been collected by shill using NetlinkManager while,
Wade Guthrie12f113a2013-03-12 17:15:46 -070042// simultaneously (and manually) comparing shill output with that of the 'iw'
43// code from which it was derived. The test strings represent the raw packet
44// data coming from the kernel. The comments above each of these strings is
Wade Guthrieffdcbea2013-09-27 10:58:49 -070045// the markup that 'iw' outputs for each of these packets.
Wade Guthrie12f113a2013-03-12 17:15:46 -070046
47// These constants are consistent throughout the packets, below.
48
49const uint32_t kExpectedIfIndex = 4;
50const uint32_t kWiPhy = 0;
51const uint16_t kNl80211FamilyId = 0x13;
52const char kExpectedMacAddress[] = "c0:3f:0e:77:e8:7f";
53
54const uint8_t kMacAddressBytes[] = {
55 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f
56};
57
Wade Guthrie4dac0282013-06-05 17:30:29 -070058const uint8_t kRespIeBytes[] = {
Wade Guthrie12f113a2013-03-12 17:15:46 -070059 0x01, 0x08, 0x82, 0x84,
60 0x8b, 0x96, 0x0c, 0x12,
61 0x18, 0x24, 0x32, 0x04,
62 0x30, 0x48, 0x60, 0x6c
63};
64
65
66// wlan0 (phy #0): scan started
67
68const uint32_t kScanFrequencyTrigger[] = {
69 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
70 2452, 2457, 2462, 2467, 2472, 2484, 5180, 5200,
71 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520,
72 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
73 5700, 5745, 5765, 5785, 5805, 5825
74};
75
76const unsigned char kNL80211_CMD_TRIGGER_SCAN[] = {
77 0x68, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x21, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
81 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2d, 0x00,
82 0x04, 0x00, 0x00, 0x00, 0x34, 0x01, 0x2c, 0x00,
83 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
84 0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00,
85 0x08, 0x00, 0x02, 0x00, 0x76, 0x09, 0x00, 0x00,
86 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
87 0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00,
88 0x08, 0x00, 0x05, 0x00, 0x85, 0x09, 0x00, 0x00,
89 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
90 0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00,
91 0x08, 0x00, 0x08, 0x00, 0x94, 0x09, 0x00, 0x00,
92 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
93 0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00,
94 0x08, 0x00, 0x0b, 0x00, 0xa3, 0x09, 0x00, 0x00,
95 0x08, 0x00, 0x0c, 0x00, 0xa8, 0x09, 0x00, 0x00,
96 0x08, 0x00, 0x0d, 0x00, 0xb4, 0x09, 0x00, 0x00,
97 0x08, 0x00, 0x0e, 0x00, 0x3c, 0x14, 0x00, 0x00,
98 0x08, 0x00, 0x0f, 0x00, 0x50, 0x14, 0x00, 0x00,
99 0x08, 0x00, 0x10, 0x00, 0x64, 0x14, 0x00, 0x00,
100 0x08, 0x00, 0x11, 0x00, 0x78, 0x14, 0x00, 0x00,
101 0x08, 0x00, 0x12, 0x00, 0x8c, 0x14, 0x00, 0x00,
102 0x08, 0x00, 0x13, 0x00, 0xa0, 0x14, 0x00, 0x00,
103 0x08, 0x00, 0x14, 0x00, 0xb4, 0x14, 0x00, 0x00,
104 0x08, 0x00, 0x15, 0x00, 0xc8, 0x14, 0x00, 0x00,
105 0x08, 0x00, 0x16, 0x00, 0x7c, 0x15, 0x00, 0x00,
106 0x08, 0x00, 0x17, 0x00, 0x90, 0x15, 0x00, 0x00,
107 0x08, 0x00, 0x18, 0x00, 0xa4, 0x15, 0x00, 0x00,
108 0x08, 0x00, 0x19, 0x00, 0xb8, 0x15, 0x00, 0x00,
109 0x08, 0x00, 0x1a, 0x00, 0xcc, 0x15, 0x00, 0x00,
110 0x08, 0x00, 0x1b, 0x00, 0xe0, 0x15, 0x00, 0x00,
111 0x08, 0x00, 0x1c, 0x00, 0xf4, 0x15, 0x00, 0x00,
112 0x08, 0x00, 0x1d, 0x00, 0x08, 0x16, 0x00, 0x00,
113 0x08, 0x00, 0x1e, 0x00, 0x1c, 0x16, 0x00, 0x00,
114 0x08, 0x00, 0x1f, 0x00, 0x30, 0x16, 0x00, 0x00,
115 0x08, 0x00, 0x20, 0x00, 0x44, 0x16, 0x00, 0x00,
116 0x08, 0x00, 0x21, 0x00, 0x71, 0x16, 0x00, 0x00,
117 0x08, 0x00, 0x22, 0x00, 0x85, 0x16, 0x00, 0x00,
118 0x08, 0x00, 0x23, 0x00, 0x99, 0x16, 0x00, 0x00,
119 0x08, 0x00, 0x24, 0x00, 0xad, 0x16, 0x00, 0x00,
120 0x08, 0x00, 0x25, 0x00, 0xc1, 0x16, 0x00, 0x00,
121 0x08, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
122};
123
124
125// wlan0 (phy #0): scan finished: 2412 2417 2422 2427 2432 2437 2442 2447 2452
126// 2457 2462 2467 2472 2484 5180 5200 5220 5240 5260 5280 5300 5320 5500 5520
127// 5540 5560 5580 5600 5620 5640 5660 5680 5700 5745 5765 5785 5805 5825, ""
128
129const uint32_t kScanFrequencyResults[] = {
130 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
131 2452, 2457, 2462, 2467, 2472, 2484, 5180, 5200,
132 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520,
133 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
134 5700, 5745, 5765, 5785, 5805, 5825
135};
136
137const unsigned char kNL80211_CMD_NEW_SCAN_RESULTS[] = {
138 0x68, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 0x22, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
142 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2d, 0x00,
143 0x04, 0x00, 0x00, 0x00, 0x34, 0x01, 0x2c, 0x00,
144 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
145 0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00,
146 0x08, 0x00, 0x02, 0x00, 0x76, 0x09, 0x00, 0x00,
147 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
148 0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00,
149 0x08, 0x00, 0x05, 0x00, 0x85, 0x09, 0x00, 0x00,
150 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
151 0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00,
152 0x08, 0x00, 0x08, 0x00, 0x94, 0x09, 0x00, 0x00,
153 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
154 0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00,
155 0x08, 0x00, 0x0b, 0x00, 0xa3, 0x09, 0x00, 0x00,
156 0x08, 0x00, 0x0c, 0x00, 0xa8, 0x09, 0x00, 0x00,
157 0x08, 0x00, 0x0d, 0x00, 0xb4, 0x09, 0x00, 0x00,
158 0x08, 0x00, 0x0e, 0x00, 0x3c, 0x14, 0x00, 0x00,
159 0x08, 0x00, 0x0f, 0x00, 0x50, 0x14, 0x00, 0x00,
160 0x08, 0x00, 0x10, 0x00, 0x64, 0x14, 0x00, 0x00,
161 0x08, 0x00, 0x11, 0x00, 0x78, 0x14, 0x00, 0x00,
162 0x08, 0x00, 0x12, 0x00, 0x8c, 0x14, 0x00, 0x00,
163 0x08, 0x00, 0x13, 0x00, 0xa0, 0x14, 0x00, 0x00,
164 0x08, 0x00, 0x14, 0x00, 0xb4, 0x14, 0x00, 0x00,
165 0x08, 0x00, 0x15, 0x00, 0xc8, 0x14, 0x00, 0x00,
166 0x08, 0x00, 0x16, 0x00, 0x7c, 0x15, 0x00, 0x00,
167 0x08, 0x00, 0x17, 0x00, 0x90, 0x15, 0x00, 0x00,
168 0x08, 0x00, 0x18, 0x00, 0xa4, 0x15, 0x00, 0x00,
169 0x08, 0x00, 0x19, 0x00, 0xb8, 0x15, 0x00, 0x00,
170 0x08, 0x00, 0x1a, 0x00, 0xcc, 0x15, 0x00, 0x00,
171 0x08, 0x00, 0x1b, 0x00, 0xe0, 0x15, 0x00, 0x00,
172 0x08, 0x00, 0x1c, 0x00, 0xf4, 0x15, 0x00, 0x00,
173 0x08, 0x00, 0x1d, 0x00, 0x08, 0x16, 0x00, 0x00,
174 0x08, 0x00, 0x1e, 0x00, 0x1c, 0x16, 0x00, 0x00,
175 0x08, 0x00, 0x1f, 0x00, 0x30, 0x16, 0x00, 0x00,
176 0x08, 0x00, 0x20, 0x00, 0x44, 0x16, 0x00, 0x00,
177 0x08, 0x00, 0x21, 0x00, 0x71, 0x16, 0x00, 0x00,
178 0x08, 0x00, 0x22, 0x00, 0x85, 0x16, 0x00, 0x00,
179 0x08, 0x00, 0x23, 0x00, 0x99, 0x16, 0x00, 0x00,
180 0x08, 0x00, 0x24, 0x00, 0xad, 0x16, 0x00, 0x00,
181 0x08, 0x00, 0x25, 0x00, 0xc1, 0x16, 0x00, 0x00,
182 0x08, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
183};
184
185
186// wlan0: new station c0:3f:0e:77:e8:7f
187
188const uint32_t kNewStationExpectedGeneration = 275;
189
190const unsigned char kNL80211_CMD_NEW_STATION[] = {
191 0x34, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x13, 0x01, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
194 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
195 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
196 0x08, 0x00, 0x2e, 0x00, 0x13, 0x01, 0x00, 0x00,
197 0x04, 0x00, 0x15, 0x00,
198};
199
200
201// wlan0 (phy #0): auth c0:3f:0e:77:e8:7f -> 48:5d:60:77:2d:cf status: 0:
202// Successful [frame: b0 00 3a 01 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f c0
203// 3f 0e 77 e8 7f 30 07 00 00 02 00 00 00]
204
205const unsigned char kAuthenticateFrame[] = {
206 0xb0, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
207 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
208 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x30, 0x07,
209 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
210};
211
212const unsigned char kNL80211_CMD_AUTHENTICATE[] = {
213 0x48, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215 0x25, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
217 0x04, 0x00, 0x00, 0x00, 0x22, 0x00, 0x33, 0x00,
218 0xb0, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
219 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
220 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x30, 0x07,
221 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
222};
223
224
225// wlan0 (phy #0): assoc c0:3f:0e:77:e8:7f -> 48:5d:60:77:2d:cf status: 0:
226// Successful [frame: 10 00 3a 01 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f c0 3f 0e
227// 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
228// 60 6c]
229
230const unsigned char kAssociateFrame[] = {
231 0x10, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
232 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
233 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x40, 0x07,
234 0x01, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x01, 0x08,
235 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24,
236 0x32, 0x04, 0x30, 0x48, 0x60, 0x6c
237};
238
239const unsigned char kNL80211_CMD_ASSOCIATE[] = {
240 0x58, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 0x26, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
244 0x04, 0x00, 0x00, 0x00, 0x32, 0x00, 0x33, 0x00,
245 0x10, 0x00, 0x3a, 0x01, 0x48, 0x5d, 0x60, 0x77,
246 0x2d, 0xcf, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
247 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x40, 0x07,
248 0x01, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x01, 0x08,
249 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24,
250 0x32, 0x04, 0x30, 0x48, 0x60, 0x6c, 0x00, 0x00,
251};
252
253
254// wlan0 (phy #0): connected to c0:3f:0e:77:e8:7f
255
256const uint16_t kExpectedConnectStatus = 0;
257
258const unsigned char kNL80211_CMD_CONNECT[] = {
259 0x4c, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261 0x2e, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
263 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
264 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
265 0x06, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00,
266 0x14, 0x00, 0x4e, 0x00, 0x01, 0x08, 0x82, 0x84,
267 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x32, 0x04,
268 0x30, 0x48, 0x60, 0x6c,
269};
270
271
272// wlan0 (phy #0): deauth c0:3f:0e:77:e8:7f -> ff:ff:ff:ff:ff:ff reason 2:
273// Previous authentication no longer valid [frame: c0 00 00 00 ff ff ff ff
274// ff ff c0 3f 0e 77 e8 7f c0 3f 0e 77 e8 7f c0 0e 02 00]
275
276const unsigned char kDeauthenticateFrame[] = {
277 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
278 0xff, 0xff, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
279 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0xc0, 0x0e,
280 0x02, 0x00
281};
282
283const unsigned char kNL80211_CMD_DEAUTHENTICATE[] = {
284 0x44, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
285 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286 0x27, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
287 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
288 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x33, 0x00,
289 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
290 0xff, 0xff, 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f,
291 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0xc0, 0x0e,
292 0x02, 0x00, 0x00, 0x00,
293};
294
295
296// wlan0 (phy #0): disconnected (by AP) reason: 2: Previous authentication no
297// longer valid
298
299const uint16_t kExpectedDisconnectReason = 2;
300
301const unsigned char kNL80211_CMD_DISCONNECT[] = {
302 0x30, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 0x30, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
305 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
306 0x04, 0x00, 0x00, 0x00, 0x06, 0x00, 0x36, 0x00,
307 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x47, 0x00,
308};
309
310
311// wlan0 (phy #0): connection quality monitor event: peer c0:3f:0e:77:e8:7f
312// didn't ACK 50 packets
313
314const uint32_t kExpectedCqmNotAcked = 50;
315
316const unsigned char kNL80211_CMD_NOTIFY_CQM[] = {
317 0x3c, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
319 0x40, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
321 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
322 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
323 0x0c, 0x00, 0x5e, 0x00, 0x08, 0x00, 0x04, 0x00,
324 0x32, 0x00, 0x00, 0x00,
325};
326
327
328// wlan0 (phy #0): disassoc 48:5d:60:77:2d:cf -> c0:3f:0e:77:e8:7f reason 3:
329// Deauthenticated because sending station is [frame: a0 00 00 00 c0 3f 0e
330// 77 e8 7f 48 5d 60 77 2d cf c0 3f 0e 77 e8 7f 00 00 03 00]
331
332const unsigned char kDisassociateFrame[] = {
333 0xa0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x0e, 0x77,
334 0xe8, 0x7f, 0x48, 0x5d, 0x60, 0x77, 0x2d, 0xcf,
335 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
336 0x03, 0x00
337};
338
339const unsigned char kNL80211_CMD_DISASSOCIATE[] = {
340 0x44, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342 0x28, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
343 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
344 0x04, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x33, 0x00,
345 0xa0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x0e, 0x77,
346 0xe8, 0x7f, 0x48, 0x5d, 0x60, 0x77, 0x2d, 0xcf,
347 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
348 0x03, 0x00, 0x00, 0x00,
349};
350
Wade Guthrie40d992c2013-04-19 11:10:11 -0700351// This is just a NL80211_CMD_NEW_STATION message with the command changed to
352// 0xfe (which is, intentionally, not a supported command).
353
354const unsigned char kCmdNL80211_CMD_UNKNOWN = 0xfe;
355const unsigned char kNL80211_CMD_UNKNOWN[] = {
356 0x34, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0xfe, 0x01, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
359 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x00,
360 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f, 0x00, 0x00,
361 0x08, 0x00, 0x2e, 0x00, 0x13, 0x01, 0x00, 0x00,
362 0x04, 0x00, 0x15, 0x00,
363};
364
Wade Guthrie12f113a2013-03-12 17:15:46 -0700365} // namespace
366
367class NetlinkMessageTest : public Test {
368 public:
369 NetlinkMessageTest() {
370 message_factory_.AddFactoryMethod(
371 kNl80211FamilyId, Bind(&Nl80211Message::CreateMessage));
372 Nl80211Message::SetMessageType(kNl80211FamilyId);
373 }
374
375 protected:
Wade Guthrie3e22e732013-06-05 17:26:56 -0700376 // Helper function to provide an array of scan frequencies from a message's
377 // NL80211_ATTR_SCAN_FREQUENCIES attribute.
378 static bool GetScanFrequenciesFromMessage(const Nl80211Message &message,
379 vector<uint32_t> *value) {
380 if (!value) {
381 LOG(ERROR) << "Null |value| parameter";
382 return false;
383 }
384
385 AttributeListConstRefPtr frequency_list;
386 if (!message.const_attributes()->ConstGetNestedAttributeList(
387 NL80211_ATTR_SCAN_FREQUENCIES, &frequency_list) || !frequency_list) {
388 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_FREQUENCIES attribute";
389 return false;
390 }
391
392 AttributeIdIterator freq_iter(*frequency_list);
393 value->clear();
394 for (; !freq_iter.AtEnd(); freq_iter.Advance()) {
395 uint32_t freq = 0;
396 if (frequency_list->GetU32AttributeValue(freq_iter.GetId(), &freq)) {
397 value->push_back(freq);
398 }
399 }
400 return true;
401 }
402
403 // Helper function to provide an array of SSIDs from a message's
404 // NL80211_ATTR_SCAN_SSIDS attribute.
405 static bool GetScanSsidsFromMessage(const Nl80211Message &message,
406 vector<string> *value) {
407 if (!value) {
408 LOG(ERROR) << "Null |value| parameter";
409 return false;
410 }
411
412 AttributeListConstRefPtr ssid_list;
413 if (!message.const_attributes()->ConstGetNestedAttributeList(
414 NL80211_ATTR_SCAN_SSIDS, &ssid_list) || !ssid_list) {
415 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_SSIDS attribute";
416 return false;
417 }
418
419 AttributeIdIterator ssid_iter(*ssid_list);
420 value->clear();
421 for (; !ssid_iter.AtEnd(); ssid_iter.Advance()) {
422 string ssid;
423 if (ssid_list->GetStringAttributeValue(ssid_iter.GetId(), &ssid)) {
424 value->push_back(ssid);
425 }
426 }
427 return true;
428 }
429
Wade Guthrie12f113a2013-03-12 17:15:46 -0700430 NetlinkMessageFactory message_factory_;
431};
432
433TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_TRIGGER_SCAN) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700434 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700435 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700436 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700437
438 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
439 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700440 // The following is legal if the message_type is kNl80211FamilyId.
441 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
442 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700443
444 EXPECT_EQ(NL80211_CMD_TRIGGER_SCAN, message->command());
445
446 {
447 uint32_t value;
448 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
449 NL80211_ATTR_WIPHY, &value));
450 EXPECT_EQ(kWiPhy, value);
451 }
452
453 {
454 uint32_t value;
455 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
456 NL80211_ATTR_IFINDEX, &value));
457 EXPECT_EQ(kExpectedIfIndex, value);
458 }
459
460 // Make sure the scan frequencies in the attribute are the ones we expect.
461 {
462 vector<uint32_t>list;
Wade Guthrie3e22e732013-06-05 17:26:56 -0700463 EXPECT_TRUE(GetScanFrequenciesFromMessage(*message, &list));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700464 EXPECT_EQ(list.size(), arraysize(kScanFrequencyTrigger));
465 int i = 0;
466 vector<uint32_t>::const_iterator j = list.begin();
467 while (j != list.end()) {
468 EXPECT_EQ(kScanFrequencyTrigger[i], *j);
469 ++i;
470 ++j;
471 }
472 }
473
474 {
475 vector<string> ssids;
Wade Guthrie3e22e732013-06-05 17:26:56 -0700476 EXPECT_TRUE(GetScanSsidsFromMessage(*message, &ssids));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700477 EXPECT_EQ(1, ssids.size());
478 EXPECT_EQ(0, ssids[0].compare("")); // Expect a single, empty SSID.
479 }
480
481 EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue(
482 NL80211_ATTR_SUPPORT_MESH_AUTH));
483}
484
485TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NEW_SCAN_RESULTS) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700486 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700487 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700488 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700489
490 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
491 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700492 // The following is legal if the message_type is kNl80211FamilyId.
493 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
494 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700495
496 EXPECT_EQ(NL80211_CMD_NEW_SCAN_RESULTS, message->command());
497
498 {
499 uint32_t value;
500 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
501 NL80211_ATTR_WIPHY, &value));
502 EXPECT_EQ(kWiPhy, value);
503 }
504
505 {
506 uint32_t value;
507 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
508 NL80211_ATTR_IFINDEX, &value));
509 EXPECT_EQ(kExpectedIfIndex, value);
510 }
511
512 // Make sure the scan frequencies in the attribute are the ones we expect.
513 {
514 vector<uint32_t>list;
Wade Guthrie3e22e732013-06-05 17:26:56 -0700515 EXPECT_TRUE(GetScanFrequenciesFromMessage(*message, &list));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700516 EXPECT_EQ(arraysize(kScanFrequencyResults), list.size());
517 int i = 0;
518 vector<uint32_t>::const_iterator j = list.begin();
519 while (j != list.end()) {
520 EXPECT_EQ(kScanFrequencyResults[i], *j);
521 ++i;
522 ++j;
523 }
524 }
525
526 {
527 vector<string> ssids;
Wade Guthrie3e22e732013-06-05 17:26:56 -0700528 EXPECT_TRUE(GetScanSsidsFromMessage(*message, &ssids));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700529 EXPECT_EQ(1, ssids.size());
530 EXPECT_EQ(0, ssids[0].compare("")); // Expect a single, empty SSID.
531 }
532
533 EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue(
534 NL80211_ATTR_SUPPORT_MESH_AUTH));
535}
536
537TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NEW_STATION) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700538 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700539 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700540 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700541
542 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
543 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700544 // The following is legal if the message_type is kNl80211FamilyId.
545 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
546 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700547 EXPECT_EQ(NL80211_CMD_NEW_STATION, message->command());
548
549 {
550 uint32_t value;
551 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
552 NL80211_ATTR_IFINDEX, &value));
553 EXPECT_EQ(kExpectedIfIndex, value);
554 }
555
556 {
557 string value;
Wade Guthrie2193b232013-06-05 17:09:07 -0700558 EXPECT_TRUE(message->const_attributes()->GetAttributeAsString(
559 NL80211_ATTR_MAC, &value));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700560 EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
561 }
562
563 {
564 AttributeListConstRefPtr nested;
565 EXPECT_TRUE(message->const_attributes()->ConstGetNestedAttributeList(
566 NL80211_ATTR_STA_INFO, &nested));
567 }
568
569 {
570 uint32_t value;
571 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
572 NL80211_ATTR_GENERATION, &value));
573 EXPECT_EQ(kNewStationExpectedGeneration, value);
574 }
575}
576
577TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_AUTHENTICATE) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700578 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700579 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700580 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700581
582 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
583 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700584 // The following is legal if the message_type is kNl80211FamilyId.
585 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
586 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700587 EXPECT_EQ(NL80211_CMD_AUTHENTICATE, message->command());
588
589 {
590 uint32_t value;
591 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
592 NL80211_ATTR_WIPHY, &value));
593 EXPECT_EQ(kWiPhy, value);
594 }
595
596 {
597 uint32_t value;
598 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
599 NL80211_ATTR_IFINDEX, &value));
600 EXPECT_EQ(kExpectedIfIndex, value);
601 }
602
603 {
604 ByteString rawdata;
605 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
606 NL80211_ATTR_FRAME, &rawdata));
607 EXPECT_FALSE(rawdata.IsEmpty());
608 Nl80211Frame frame(rawdata);
609 Nl80211Frame expected_frame(ByteString(kAuthenticateFrame,
610 sizeof(kAuthenticateFrame)));
611 EXPECT_TRUE(frame.IsEqual(expected_frame));
612 }
613}
614
615TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_ASSOCIATE) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700616 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700617 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700618 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700619
620 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
621 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700622 // The following is legal if the message_type is kNl80211FamilyId.
623 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
624 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700625 EXPECT_EQ(NL80211_CMD_ASSOCIATE, message->command());
626
627 {
628 uint32_t value;
629 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
630 NL80211_ATTR_WIPHY, &value));
631 EXPECT_EQ(kWiPhy, value);
632 }
633
634 {
635 uint32_t value;
636 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
637 NL80211_ATTR_IFINDEX, &value));
638 EXPECT_EQ(kExpectedIfIndex, value);
639 }
640
641 {
642 ByteString rawdata;
643 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
644 NL80211_ATTR_FRAME, &rawdata));
645 EXPECT_FALSE(rawdata.IsEmpty());
646 Nl80211Frame frame(rawdata);
647 Nl80211Frame expected_frame(ByteString(kAssociateFrame,
648 sizeof(kAssociateFrame)));
649 EXPECT_TRUE(frame.IsEqual(expected_frame));
650 }
651}
652
653TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_CONNECT) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700654 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700655 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700656 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700657
658 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
659 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700660 // The following is legal if the message_type is kNl80211FamilyId.
661 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
662 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700663 EXPECT_EQ(NL80211_CMD_CONNECT, message->command());
664
665 {
666 uint32_t value;
667 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
668 NL80211_ATTR_WIPHY, &value));
669 EXPECT_EQ(kWiPhy, value);
670 }
671
672 {
673 uint32_t value;
674 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
675 NL80211_ATTR_IFINDEX, &value));
676 EXPECT_EQ(kExpectedIfIndex, value);
677 }
678
679 {
680 string value;
Wade Guthrie2193b232013-06-05 17:09:07 -0700681 EXPECT_TRUE(message->const_attributes()->GetAttributeAsString(
682 NL80211_ATTR_MAC, &value));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700683 EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
684 }
685
686 {
687 uint16_t value;
688 EXPECT_TRUE(message->const_attributes()->GetU16AttributeValue(
689 NL80211_ATTR_STATUS_CODE, &value));
690 EXPECT_EQ(kExpectedConnectStatus, value);
691 }
692
Wade Guthrie12f113a2013-03-12 17:15:46 -0700693 {
694 ByteString rawdata;
695 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
696 NL80211_ATTR_RESP_IE, &rawdata));
Wade Guthrie4dac0282013-06-05 17:30:29 -0700697 EXPECT_TRUE(rawdata.Equals(
698 ByteString(kRespIeBytes, arraysize(kRespIeBytes))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700699 }
700}
701
702TEST_F(NetlinkMessageTest, Build_NL80211_CMD_CONNECT) {
703 // Build the message that is found in kNL80211_CMD_CONNECT.
704 ConnectMessage message;
705 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_WIPHY,
706 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
707 EXPECT_TRUE(message.attributes()->SetU32AttributeValue(NL80211_ATTR_WIPHY,
708 kWiPhy));
709
710 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_IFINDEX,
711 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
712 EXPECT_TRUE(message.attributes()->SetU32AttributeValue(
713 NL80211_ATTR_IFINDEX, kExpectedIfIndex));
714
715 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_MAC,
716 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
717 EXPECT_TRUE(message.attributes()->SetRawAttributeValue(NL80211_ATTR_MAC,
718 ByteString(kMacAddressBytes, arraysize(kMacAddressBytes))));
719
Wade Guthrie35cb4b92013-04-19 10:46:11 -0700720 // In the middle, let's try adding an attribute without populating it.
721 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_REG_TYPE,
722 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
723
Wade Guthrie12f113a2013-03-12 17:15:46 -0700724 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_STATUS_CODE,
725 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
726 EXPECT_TRUE(message.attributes()->SetU16AttributeValue(
727 NL80211_ATTR_STATUS_CODE, kExpectedConnectStatus));
728
729 EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_RESP_IE,
730 Bind(&NetlinkAttribute::NewNl80211AttributeFromId)));
731 EXPECT_TRUE(message.attributes()->SetRawAttributeValue(NL80211_ATTR_RESP_IE,
Wade Guthrie4dac0282013-06-05 17:30:29 -0700732 ByteString(kRespIeBytes, arraysize(kRespIeBytes))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700733
734 // Encode the message to a ByteString and remove all the run-specific
735 // values.
736 static const uint32_t kArbitrarySequenceNumber = 42;
737 ByteString message_bytes = message.Encode(kArbitrarySequenceNumber);
738 nlmsghdr *header = reinterpret_cast<nlmsghdr *>(message_bytes.GetData());
739 header->nlmsg_flags = 0; // Overwrite with known values.
740 header->nlmsg_seq = 0;
741 header->nlmsg_pid = 0;
742
743 // Verify that the messages are equal.
744 EXPECT_TRUE(message_bytes.Equals(
745 ByteString(kNL80211_CMD_CONNECT, arraysize(kNL80211_CMD_CONNECT))));
746}
747
748
749TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DEAUTHENTICATE) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700750 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700751 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700752 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700753
754 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
755 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700756 // The following is legal if the message_type is kNl80211FamilyId.
757 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
758 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700759 EXPECT_EQ(NL80211_CMD_DEAUTHENTICATE, message->command());
760
761 {
762 uint32_t value;
763 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
764 NL80211_ATTR_WIPHY, &value));
765 EXPECT_EQ(kWiPhy, value);
766 }
767
768 {
769 uint32_t value;
770 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
771 NL80211_ATTR_IFINDEX, &value));
772 EXPECT_EQ(kExpectedIfIndex, value);
773 }
774
775 {
776 ByteString rawdata;
777 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
778 NL80211_ATTR_FRAME, &rawdata));
779 EXPECT_FALSE(rawdata.IsEmpty());
780 Nl80211Frame frame(rawdata);
781 Nl80211Frame expected_frame(ByteString(kDeauthenticateFrame,
782 sizeof(kDeauthenticateFrame)));
783 EXPECT_TRUE(frame.IsEqual(expected_frame));
784 }
785}
786
787TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DISCONNECT) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700788 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700789 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700790 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700791
792 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
793 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700794 // The following is legal if the message_type is kNl80211FamilyId.
795 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
796 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700797 EXPECT_EQ(NL80211_CMD_DISCONNECT, message->command());
798
799 {
800 uint32_t value;
801 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
802 NL80211_ATTR_WIPHY, &value));
803 EXPECT_EQ(kWiPhy, value);
804 }
805
806 {
807 uint32_t value;
808 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
809 NL80211_ATTR_IFINDEX, &value));
810 EXPECT_EQ(kExpectedIfIndex, value);
811 }
812
813 {
814 uint16_t value;
815 EXPECT_TRUE(message->const_attributes()->GetU16AttributeValue(
816 NL80211_ATTR_REASON_CODE, &value));
817 EXPECT_EQ(kExpectedDisconnectReason, value);
818 }
819
820 EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue(
821 NL80211_ATTR_DISCONNECTED_BY_AP));
822}
823
824TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NOTIFY_CQM) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700825 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700826 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700827 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700828
829 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
830 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700831 // The following is legal if the message_type is kNl80211FamilyId.
832 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
833 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700834 EXPECT_EQ(NL80211_CMD_NOTIFY_CQM, message->command());
835
836 {
837 uint32_t value;
838 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
839 NL80211_ATTR_WIPHY, &value));
840 EXPECT_EQ(kWiPhy, value);
841 }
842
843 {
844 uint32_t value;
845 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
846 NL80211_ATTR_IFINDEX, &value));
847 EXPECT_EQ(kExpectedIfIndex, value);
848 }
849
850 {
851 string value;
Wade Guthrie2193b232013-06-05 17:09:07 -0700852 EXPECT_TRUE(message->const_attributes()->GetAttributeAsString(
853 NL80211_ATTR_MAC, &value));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700854 EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
855 }
856
857 {
858 AttributeListConstRefPtr nested;
859 EXPECT_TRUE(message->const_attributes()->ConstGetNestedAttributeList(
860 NL80211_ATTR_CQM, &nested));
861 uint32_t threshold_event;
862 EXPECT_FALSE(nested->GetU32AttributeValue(
863 NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT, &threshold_event));
864 uint32_t pkt_loss_event;
865 EXPECT_TRUE(nested->GetU32AttributeValue(
866 NL80211_ATTR_CQM_PKT_LOSS_EVENT, &pkt_loss_event));
867 EXPECT_EQ(kExpectedCqmNotAcked, pkt_loss_event);
868 }
869}
870
871TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DISASSOCIATE) {
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700872 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie12f113a2013-03-12 17:15:46 -0700873 message_factory_.CreateMessage(const_cast<nlmsghdr *>(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700874 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE))));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700875
876 EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message);
877 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700878 // The following is legal if the message_type is kNl80211FamilyId.
879 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
880 netlink_message.release()));
Wade Guthrie12f113a2013-03-12 17:15:46 -0700881 EXPECT_EQ(NL80211_CMD_DISASSOCIATE, message->command());
882
883
884 {
885 uint32_t value;
886 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
887 NL80211_ATTR_WIPHY, &value));
888 EXPECT_EQ(kWiPhy, value);
889 }
890
891 {
892 uint32_t value;
893 EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue(
894 NL80211_ATTR_IFINDEX, &value));
895 EXPECT_EQ(kExpectedIfIndex, value);
896 }
897
898 {
899 ByteString rawdata;
900 EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue(
901 NL80211_ATTR_FRAME, &rawdata));
902 EXPECT_FALSE(rawdata.IsEmpty());
903 Nl80211Frame frame(rawdata);
904 Nl80211Frame expected_frame(ByteString(kDisassociateFrame,
905 sizeof(kDisassociateFrame)));
906 EXPECT_TRUE(frame.IsEqual(expected_frame));
907 }
908}
909
Wade Guthrie40d992c2013-04-19 11:10:11 -0700910// This test is to ensure that an unknown nl80211 message generates an
911// Nl80211UnknownMessage with all Nl80211 parts.
912TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_UNKNOWN) {
913 ScopedMockLog log;
914 string logmessage = StringPrintf(
915 "Unknown/unhandled netlink nl80211 message 0x%02x",
916 kCmdNL80211_CMD_UNKNOWN);
917 EXPECT_CALL(log, Log(logging::LOG_WARNING, _, EndsWith(logmessage.c_str())));
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700918 scoped_ptr<NetlinkMessage> netlink_message(
Wade Guthrie40d992c2013-04-19 11:10:11 -0700919 message_factory_.CreateMessage(
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700920 reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_UNKNOWN)));
921 ASSERT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message.get());
Wade Guthrie40d992c2013-04-19 11:10:11 -0700922 EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type());
Wade Guthrieffdcbea2013-09-27 10:58:49 -0700923 // The following is legal if the message_type is kNl80211FamilyId.
924 scoped_ptr<Nl80211Message> message(dynamic_cast<Nl80211Message *>(
925 netlink_message.release()));
Wade Guthrie40d992c2013-04-19 11:10:11 -0700926 EXPECT_EQ(kCmdNL80211_CMD_UNKNOWN, message->command());
927}
928
Wade Guthrie12f113a2013-03-12 17:15:46 -0700929} // namespace shill