Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 1 | // 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 | |
Wade Guthrie | 40d992c | 2013-04-19 11:10:11 -0700 | [diff] [blame] | 17 | #include <base/stringprintf.h> |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Wade Guthrie | 40d992c | 2013-04-19 11:10:11 -0700 | [diff] [blame] | 21 | #include "shill/mock_log.h" |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 22 | #include "shill/mock_netlink_socket.h" |
| 23 | #include "shill/netlink_attribute.h" |
| 24 | #include "shill/refptr_types.h" |
| 25 | |
| 26 | using base::Bind; |
Wade Guthrie | 40d992c | 2013-04-19 11:10:11 -0700 | [diff] [blame] | 27 | using base::StringPrintf; |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 28 | using base::Unretained; |
| 29 | using std::string; |
| 30 | using std::vector; |
| 31 | using testing::_; |
Wade Guthrie | 40d992c | 2013-04-19 11:10:11 -0700 | [diff] [blame] | 32 | using testing::EndsWith; |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 33 | using testing::Invoke; |
| 34 | using testing::Return; |
| 35 | using testing::Test; |
| 36 | |
| 37 | namespace shill { |
| 38 | |
| 39 | namespace { |
| 40 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 41 | // These data blocks have been collected by shill using NetlinkManager while, |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 42 | // 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 |
| 45 | // the markup that "iw" outputs for ech of these packets. |
| 46 | |
| 47 | // These constants are consistent throughout the packets, below. |
| 48 | |
| 49 | const uint32_t kExpectedIfIndex = 4; |
| 50 | const uint32_t kWiPhy = 0; |
| 51 | const uint16_t kNl80211FamilyId = 0x13; |
| 52 | const char kExpectedMacAddress[] = "c0:3f:0e:77:e8:7f"; |
| 53 | |
| 54 | const uint8_t kMacAddressBytes[] = { |
| 55 | 0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f |
| 56 | }; |
| 57 | |
Wade Guthrie | 4dac028 | 2013-06-05 17:30:29 -0700 | [diff] [blame] | 58 | const uint8_t kRespIeBytes[] = { |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 59 | 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 | |
| 68 | const 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 | |
| 76 | const 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 | |
| 129 | const 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 | |
| 137 | const 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 | |
| 188 | const uint32_t kNewStationExpectedGeneration = 275; |
| 189 | |
| 190 | const 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 | |
| 205 | const 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 | |
| 212 | const 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 | |
| 230 | const 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 | |
| 239 | const 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 | |
| 256 | const uint16_t kExpectedConnectStatus = 0; |
| 257 | |
| 258 | const 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 | |
| 276 | const 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 | |
| 283 | const 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 | |
| 299 | const uint16_t kExpectedDisconnectReason = 2; |
| 300 | |
| 301 | const 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 | |
| 314 | const uint32_t kExpectedCqmNotAcked = 50; |
| 315 | |
| 316 | const 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 | |
| 332 | const 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 | |
| 339 | const 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 Guthrie | 40d992c | 2013-04-19 11:10:11 -0700 | [diff] [blame] | 351 | // This is just a NL80211_CMD_NEW_STATION message with the command changed to |
| 352 | // 0xfe (which is, intentionally, not a supported command). |
| 353 | |
| 354 | const unsigned char kCmdNL80211_CMD_UNKNOWN = 0xfe; |
| 355 | const 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 Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 365 | const char kGetFamilyCommandString[] = "CTRL_CMD_GETFAMILY"; |
| 366 | |
| 367 | } // namespace |
| 368 | |
| 369 | class NetlinkMessageTest : public Test { |
| 370 | public: |
| 371 | NetlinkMessageTest() { |
| 372 | message_factory_.AddFactoryMethod( |
| 373 | kNl80211FamilyId, Bind(&Nl80211Message::CreateMessage)); |
| 374 | Nl80211Message::SetMessageType(kNl80211FamilyId); |
| 375 | } |
| 376 | |
| 377 | protected: |
Wade Guthrie | 3e22e73 | 2013-06-05 17:26:56 -0700 | [diff] [blame] | 378 | // Helper function to provide an array of scan frequencies from a message's |
| 379 | // NL80211_ATTR_SCAN_FREQUENCIES attribute. |
| 380 | static bool GetScanFrequenciesFromMessage(const Nl80211Message &message, |
| 381 | vector<uint32_t> *value) { |
| 382 | if (!value) { |
| 383 | LOG(ERROR) << "Null |value| parameter"; |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | AttributeListConstRefPtr frequency_list; |
| 388 | if (!message.const_attributes()->ConstGetNestedAttributeList( |
| 389 | NL80211_ATTR_SCAN_FREQUENCIES, &frequency_list) || !frequency_list) { |
| 390 | LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_FREQUENCIES attribute"; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | AttributeIdIterator freq_iter(*frequency_list); |
| 395 | value->clear(); |
| 396 | for (; !freq_iter.AtEnd(); freq_iter.Advance()) { |
| 397 | uint32_t freq = 0; |
| 398 | if (frequency_list->GetU32AttributeValue(freq_iter.GetId(), &freq)) { |
| 399 | value->push_back(freq); |
| 400 | } |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | // Helper function to provide an array of SSIDs from a message's |
| 406 | // NL80211_ATTR_SCAN_SSIDS attribute. |
| 407 | static bool GetScanSsidsFromMessage(const Nl80211Message &message, |
| 408 | vector<string> *value) { |
| 409 | if (!value) { |
| 410 | LOG(ERROR) << "Null |value| parameter"; |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | AttributeListConstRefPtr ssid_list; |
| 415 | if (!message.const_attributes()->ConstGetNestedAttributeList( |
| 416 | NL80211_ATTR_SCAN_SSIDS, &ssid_list) || !ssid_list) { |
| 417 | LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_SSIDS attribute"; |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | AttributeIdIterator ssid_iter(*ssid_list); |
| 422 | value->clear(); |
| 423 | for (; !ssid_iter.AtEnd(); ssid_iter.Advance()) { |
| 424 | string ssid; |
| 425 | if (ssid_list->GetStringAttributeValue(ssid_iter.GetId(), &ssid)) { |
| 426 | value->push_back(ssid); |
| 427 | } |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 432 | NetlinkMessageFactory message_factory_; |
| 433 | }; |
| 434 | |
| 435 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_TRIGGER_SCAN) { |
| 436 | NetlinkMessage *netlink_message = |
| 437 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 438 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_TRIGGER_SCAN))); |
| 439 | |
| 440 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 441 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 442 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 443 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 444 | |
| 445 | EXPECT_EQ(NL80211_CMD_TRIGGER_SCAN, message->command()); |
| 446 | |
| 447 | { |
| 448 | uint32_t value; |
| 449 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 450 | NL80211_ATTR_WIPHY, &value)); |
| 451 | EXPECT_EQ(kWiPhy, value); |
| 452 | } |
| 453 | |
| 454 | { |
| 455 | uint32_t value; |
| 456 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 457 | NL80211_ATTR_IFINDEX, &value)); |
| 458 | EXPECT_EQ(kExpectedIfIndex, value); |
| 459 | } |
| 460 | |
| 461 | // Make sure the scan frequencies in the attribute are the ones we expect. |
| 462 | { |
| 463 | vector<uint32_t>list; |
Wade Guthrie | 3e22e73 | 2013-06-05 17:26:56 -0700 | [diff] [blame] | 464 | EXPECT_TRUE(GetScanFrequenciesFromMessage(*message, &list)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 465 | EXPECT_EQ(list.size(), arraysize(kScanFrequencyTrigger)); |
| 466 | int i = 0; |
| 467 | vector<uint32_t>::const_iterator j = list.begin(); |
| 468 | while (j != list.end()) { |
| 469 | EXPECT_EQ(kScanFrequencyTrigger[i], *j); |
| 470 | ++i; |
| 471 | ++j; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | { |
| 476 | vector<string> ssids; |
Wade Guthrie | 3e22e73 | 2013-06-05 17:26:56 -0700 | [diff] [blame] | 477 | EXPECT_TRUE(GetScanSsidsFromMessage(*message, &ssids)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 478 | EXPECT_EQ(1, ssids.size()); |
| 479 | EXPECT_EQ(0, ssids[0].compare("")); // Expect a single, empty SSID. |
| 480 | } |
| 481 | |
| 482 | EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue( |
| 483 | NL80211_ATTR_SUPPORT_MESH_AUTH)); |
| 484 | } |
| 485 | |
| 486 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NEW_SCAN_RESULTS) { |
| 487 | NetlinkMessage *netlink_message = |
| 488 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 489 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_SCAN_RESULTS))); |
| 490 | |
| 491 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 492 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 493 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 494 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 495 | |
| 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 Guthrie | 3e22e73 | 2013-06-05 17:26:56 -0700 | [diff] [blame] | 515 | EXPECT_TRUE(GetScanFrequenciesFromMessage(*message, &list)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 516 | 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 Guthrie | 3e22e73 | 2013-06-05 17:26:56 -0700 | [diff] [blame] | 528 | EXPECT_TRUE(GetScanSsidsFromMessage(*message, &ssids)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 529 | 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 | |
| 537 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NEW_STATION) { |
| 538 | NetlinkMessage *netlink_message = |
| 539 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 540 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NEW_STATION))); |
| 541 | |
| 542 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 543 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 544 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 545 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 546 | EXPECT_EQ(NL80211_CMD_NEW_STATION, message->command()); |
| 547 | |
| 548 | { |
| 549 | uint32_t value; |
| 550 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 551 | NL80211_ATTR_IFINDEX, &value)); |
| 552 | EXPECT_EQ(kExpectedIfIndex, value); |
| 553 | } |
| 554 | |
| 555 | { |
| 556 | string value; |
Wade Guthrie | 2193b23 | 2013-06-05 17:09:07 -0700 | [diff] [blame] | 557 | EXPECT_TRUE(message->const_attributes()->GetAttributeAsString( |
| 558 | NL80211_ATTR_MAC, &value)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 559 | EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length())); |
| 560 | } |
| 561 | |
| 562 | { |
| 563 | AttributeListConstRefPtr nested; |
| 564 | EXPECT_TRUE(message->const_attributes()->ConstGetNestedAttributeList( |
| 565 | NL80211_ATTR_STA_INFO, &nested)); |
| 566 | } |
| 567 | |
| 568 | { |
| 569 | uint32_t value; |
| 570 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 571 | NL80211_ATTR_GENERATION, &value)); |
| 572 | EXPECT_EQ(kNewStationExpectedGeneration, value); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_AUTHENTICATE) { |
| 577 | NetlinkMessage *netlink_message = |
| 578 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 579 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_AUTHENTICATE))); |
| 580 | |
| 581 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 582 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 583 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 584 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 585 | EXPECT_EQ(NL80211_CMD_AUTHENTICATE, message->command()); |
| 586 | |
| 587 | { |
| 588 | uint32_t value; |
| 589 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 590 | NL80211_ATTR_WIPHY, &value)); |
| 591 | EXPECT_EQ(kWiPhy, value); |
| 592 | } |
| 593 | |
| 594 | { |
| 595 | uint32_t value; |
| 596 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 597 | NL80211_ATTR_IFINDEX, &value)); |
| 598 | EXPECT_EQ(kExpectedIfIndex, value); |
| 599 | } |
| 600 | |
| 601 | { |
| 602 | ByteString rawdata; |
| 603 | EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue( |
| 604 | NL80211_ATTR_FRAME, &rawdata)); |
| 605 | EXPECT_FALSE(rawdata.IsEmpty()); |
| 606 | Nl80211Frame frame(rawdata); |
| 607 | Nl80211Frame expected_frame(ByteString(kAuthenticateFrame, |
| 608 | sizeof(kAuthenticateFrame))); |
| 609 | EXPECT_TRUE(frame.IsEqual(expected_frame)); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_ASSOCIATE) { |
| 614 | NetlinkMessage *netlink_message = |
| 615 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 616 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_ASSOCIATE))); |
| 617 | |
| 618 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 619 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 620 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 621 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 622 | EXPECT_EQ(NL80211_CMD_ASSOCIATE, message->command()); |
| 623 | |
| 624 | { |
| 625 | uint32_t value; |
| 626 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 627 | NL80211_ATTR_WIPHY, &value)); |
| 628 | EXPECT_EQ(kWiPhy, value); |
| 629 | } |
| 630 | |
| 631 | { |
| 632 | uint32_t value; |
| 633 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 634 | NL80211_ATTR_IFINDEX, &value)); |
| 635 | EXPECT_EQ(kExpectedIfIndex, value); |
| 636 | } |
| 637 | |
| 638 | { |
| 639 | ByteString rawdata; |
| 640 | EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue( |
| 641 | NL80211_ATTR_FRAME, &rawdata)); |
| 642 | EXPECT_FALSE(rawdata.IsEmpty()); |
| 643 | Nl80211Frame frame(rawdata); |
| 644 | Nl80211Frame expected_frame(ByteString(kAssociateFrame, |
| 645 | sizeof(kAssociateFrame))); |
| 646 | EXPECT_TRUE(frame.IsEqual(expected_frame)); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_CONNECT) { |
| 651 | NetlinkMessage *netlink_message = |
| 652 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 653 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_CONNECT))); |
| 654 | |
| 655 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 656 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 657 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 658 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 659 | EXPECT_EQ(NL80211_CMD_CONNECT, message->command()); |
| 660 | |
| 661 | { |
| 662 | uint32_t value; |
| 663 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 664 | NL80211_ATTR_WIPHY, &value)); |
| 665 | EXPECT_EQ(kWiPhy, value); |
| 666 | } |
| 667 | |
| 668 | { |
| 669 | uint32_t value; |
| 670 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 671 | NL80211_ATTR_IFINDEX, &value)); |
| 672 | EXPECT_EQ(kExpectedIfIndex, value); |
| 673 | } |
| 674 | |
| 675 | { |
| 676 | string value; |
Wade Guthrie | 2193b23 | 2013-06-05 17:09:07 -0700 | [diff] [blame] | 677 | EXPECT_TRUE(message->const_attributes()->GetAttributeAsString( |
| 678 | NL80211_ATTR_MAC, &value)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 679 | EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length())); |
| 680 | } |
| 681 | |
| 682 | { |
| 683 | uint16_t value; |
| 684 | EXPECT_TRUE(message->const_attributes()->GetU16AttributeValue( |
| 685 | NL80211_ATTR_STATUS_CODE, &value)); |
| 686 | EXPECT_EQ(kExpectedConnectStatus, value); |
| 687 | } |
| 688 | |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 689 | { |
| 690 | ByteString rawdata; |
| 691 | EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue( |
| 692 | NL80211_ATTR_RESP_IE, &rawdata)); |
Wade Guthrie | 4dac028 | 2013-06-05 17:30:29 -0700 | [diff] [blame] | 693 | EXPECT_TRUE(rawdata.Equals( |
| 694 | ByteString(kRespIeBytes, arraysize(kRespIeBytes)))); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | TEST_F(NetlinkMessageTest, Build_NL80211_CMD_CONNECT) { |
| 699 | // Build the message that is found in kNL80211_CMD_CONNECT. |
| 700 | ConnectMessage message; |
| 701 | EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_WIPHY, |
| 702 | Bind(&NetlinkAttribute::NewNl80211AttributeFromId))); |
| 703 | EXPECT_TRUE(message.attributes()->SetU32AttributeValue(NL80211_ATTR_WIPHY, |
| 704 | kWiPhy)); |
| 705 | |
| 706 | EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_IFINDEX, |
| 707 | Bind(&NetlinkAttribute::NewNl80211AttributeFromId))); |
| 708 | EXPECT_TRUE(message.attributes()->SetU32AttributeValue( |
| 709 | NL80211_ATTR_IFINDEX, kExpectedIfIndex)); |
| 710 | |
| 711 | EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_MAC, |
| 712 | Bind(&NetlinkAttribute::NewNl80211AttributeFromId))); |
| 713 | EXPECT_TRUE(message.attributes()->SetRawAttributeValue(NL80211_ATTR_MAC, |
| 714 | ByteString(kMacAddressBytes, arraysize(kMacAddressBytes)))); |
| 715 | |
Wade Guthrie | 35cb4b9 | 2013-04-19 10:46:11 -0700 | [diff] [blame] | 716 | // In the middle, let's try adding an attribute without populating it. |
| 717 | EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_REG_TYPE, |
| 718 | Bind(&NetlinkAttribute::NewNl80211AttributeFromId))); |
| 719 | |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 720 | EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_STATUS_CODE, |
| 721 | Bind(&NetlinkAttribute::NewNl80211AttributeFromId))); |
| 722 | EXPECT_TRUE(message.attributes()->SetU16AttributeValue( |
| 723 | NL80211_ATTR_STATUS_CODE, kExpectedConnectStatus)); |
| 724 | |
| 725 | EXPECT_TRUE(message.attributes()->CreateAttribute(NL80211_ATTR_RESP_IE, |
| 726 | Bind(&NetlinkAttribute::NewNl80211AttributeFromId))); |
| 727 | EXPECT_TRUE(message.attributes()->SetRawAttributeValue(NL80211_ATTR_RESP_IE, |
Wade Guthrie | 4dac028 | 2013-06-05 17:30:29 -0700 | [diff] [blame] | 728 | ByteString(kRespIeBytes, arraysize(kRespIeBytes)))); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 729 | |
| 730 | // Encode the message to a ByteString and remove all the run-specific |
| 731 | // values. |
| 732 | static const uint32_t kArbitrarySequenceNumber = 42; |
| 733 | ByteString message_bytes = message.Encode(kArbitrarySequenceNumber); |
| 734 | nlmsghdr *header = reinterpret_cast<nlmsghdr *>(message_bytes.GetData()); |
| 735 | header->nlmsg_flags = 0; // Overwrite with known values. |
| 736 | header->nlmsg_seq = 0; |
| 737 | header->nlmsg_pid = 0; |
| 738 | |
| 739 | // Verify that the messages are equal. |
| 740 | EXPECT_TRUE(message_bytes.Equals( |
| 741 | ByteString(kNL80211_CMD_CONNECT, arraysize(kNL80211_CMD_CONNECT)))); |
| 742 | } |
| 743 | |
| 744 | |
| 745 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DEAUTHENTICATE) { |
| 746 | NetlinkMessage *netlink_message = |
| 747 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 748 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DEAUTHENTICATE))); |
| 749 | |
| 750 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 751 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 752 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 753 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 754 | EXPECT_EQ(NL80211_CMD_DEAUTHENTICATE, message->command()); |
| 755 | |
| 756 | { |
| 757 | uint32_t value; |
| 758 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 759 | NL80211_ATTR_WIPHY, &value)); |
| 760 | EXPECT_EQ(kWiPhy, value); |
| 761 | } |
| 762 | |
| 763 | { |
| 764 | uint32_t value; |
| 765 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 766 | NL80211_ATTR_IFINDEX, &value)); |
| 767 | EXPECT_EQ(kExpectedIfIndex, value); |
| 768 | } |
| 769 | |
| 770 | { |
| 771 | ByteString rawdata; |
| 772 | EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue( |
| 773 | NL80211_ATTR_FRAME, &rawdata)); |
| 774 | EXPECT_FALSE(rawdata.IsEmpty()); |
| 775 | Nl80211Frame frame(rawdata); |
| 776 | Nl80211Frame expected_frame(ByteString(kDeauthenticateFrame, |
| 777 | sizeof(kDeauthenticateFrame))); |
| 778 | EXPECT_TRUE(frame.IsEqual(expected_frame)); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DISCONNECT) { |
| 783 | NetlinkMessage *netlink_message = |
| 784 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 785 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT))); |
| 786 | |
| 787 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 788 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 789 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 790 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 791 | EXPECT_EQ(NL80211_CMD_DISCONNECT, message->command()); |
| 792 | |
| 793 | { |
| 794 | uint32_t value; |
| 795 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 796 | NL80211_ATTR_WIPHY, &value)); |
| 797 | EXPECT_EQ(kWiPhy, value); |
| 798 | } |
| 799 | |
| 800 | { |
| 801 | uint32_t value; |
| 802 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 803 | NL80211_ATTR_IFINDEX, &value)); |
| 804 | EXPECT_EQ(kExpectedIfIndex, value); |
| 805 | } |
| 806 | |
| 807 | { |
| 808 | uint16_t value; |
| 809 | EXPECT_TRUE(message->const_attributes()->GetU16AttributeValue( |
| 810 | NL80211_ATTR_REASON_CODE, &value)); |
| 811 | EXPECT_EQ(kExpectedDisconnectReason, value); |
| 812 | } |
| 813 | |
| 814 | EXPECT_TRUE(message->const_attributes()->IsFlagAttributeTrue( |
| 815 | NL80211_ATTR_DISCONNECTED_BY_AP)); |
| 816 | } |
| 817 | |
| 818 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_NOTIFY_CQM) { |
| 819 | NetlinkMessage *netlink_message = |
| 820 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 821 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_NOTIFY_CQM))); |
| 822 | |
| 823 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 824 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 825 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 826 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 827 | EXPECT_EQ(NL80211_CMD_NOTIFY_CQM, message->command()); |
| 828 | |
| 829 | { |
| 830 | uint32_t value; |
| 831 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 832 | NL80211_ATTR_WIPHY, &value)); |
| 833 | EXPECT_EQ(kWiPhy, value); |
| 834 | } |
| 835 | |
| 836 | { |
| 837 | uint32_t value; |
| 838 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 839 | NL80211_ATTR_IFINDEX, &value)); |
| 840 | EXPECT_EQ(kExpectedIfIndex, value); |
| 841 | } |
| 842 | |
| 843 | { |
| 844 | string value; |
Wade Guthrie | 2193b23 | 2013-06-05 17:09:07 -0700 | [diff] [blame] | 845 | EXPECT_TRUE(message->const_attributes()->GetAttributeAsString( |
| 846 | NL80211_ATTR_MAC, &value)); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 847 | EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length())); |
| 848 | } |
| 849 | |
| 850 | { |
| 851 | AttributeListConstRefPtr nested; |
| 852 | EXPECT_TRUE(message->const_attributes()->ConstGetNestedAttributeList( |
| 853 | NL80211_ATTR_CQM, &nested)); |
| 854 | uint32_t threshold_event; |
| 855 | EXPECT_FALSE(nested->GetU32AttributeValue( |
| 856 | NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT, &threshold_event)); |
| 857 | uint32_t pkt_loss_event; |
| 858 | EXPECT_TRUE(nested->GetU32AttributeValue( |
| 859 | NL80211_ATTR_CQM_PKT_LOSS_EVENT, &pkt_loss_event)); |
| 860 | EXPECT_EQ(kExpectedCqmNotAcked, pkt_loss_event); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_DISASSOCIATE) { |
| 865 | NetlinkMessage *netlink_message = |
| 866 | message_factory_.CreateMessage(const_cast<nlmsghdr *>( |
| 867 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISASSOCIATE))); |
| 868 | |
| 869 | EXPECT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 870 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 871 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 872 | Nl80211Message *message = reinterpret_cast<Nl80211Message *>(netlink_message); |
| 873 | EXPECT_EQ(NL80211_CMD_DISASSOCIATE, message->command()); |
| 874 | |
| 875 | |
| 876 | { |
| 877 | uint32_t value; |
| 878 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 879 | NL80211_ATTR_WIPHY, &value)); |
| 880 | EXPECT_EQ(kWiPhy, value); |
| 881 | } |
| 882 | |
| 883 | { |
| 884 | uint32_t value; |
| 885 | EXPECT_TRUE(message->const_attributes()->GetU32AttributeValue( |
| 886 | NL80211_ATTR_IFINDEX, &value)); |
| 887 | EXPECT_EQ(kExpectedIfIndex, value); |
| 888 | } |
| 889 | |
| 890 | { |
| 891 | ByteString rawdata; |
| 892 | EXPECT_TRUE(message->const_attributes()->GetRawAttributeValue( |
| 893 | NL80211_ATTR_FRAME, &rawdata)); |
| 894 | EXPECT_FALSE(rawdata.IsEmpty()); |
| 895 | Nl80211Frame frame(rawdata); |
| 896 | Nl80211Frame expected_frame(ByteString(kDisassociateFrame, |
| 897 | sizeof(kDisassociateFrame))); |
| 898 | EXPECT_TRUE(frame.IsEqual(expected_frame)); |
| 899 | } |
| 900 | } |
| 901 | |
Wade Guthrie | 40d992c | 2013-04-19 11:10:11 -0700 | [diff] [blame] | 902 | // This test is to ensure that an unknown nl80211 message generates an |
| 903 | // Nl80211UnknownMessage with all Nl80211 parts. |
| 904 | TEST_F(NetlinkMessageTest, Parse_NL80211_CMD_UNKNOWN) { |
| 905 | ScopedMockLog log; |
| 906 | string logmessage = StringPrintf( |
| 907 | "Unknown/unhandled netlink nl80211 message 0x%02x", |
| 908 | kCmdNL80211_CMD_UNKNOWN); |
| 909 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, EndsWith(logmessage.c_str()))); |
| 910 | NetlinkMessage *netlink_message = |
| 911 | message_factory_.CreateMessage( |
| 912 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_UNKNOWN)); |
| 913 | ASSERT_NE(reinterpret_cast<NetlinkMessage *>(NULL), netlink_message); |
| 914 | EXPECT_EQ(kNl80211FamilyId, netlink_message->message_type()); |
| 915 | // The follwing is legal if the message_type is kNl80211FamilyId. |
| 916 | Nl80211Message *message = dynamic_cast<Nl80211Message *>(netlink_message); |
| 917 | EXPECT_EQ(kCmdNL80211_CMD_UNKNOWN, message->command()); |
| 918 | } |
| 919 | |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 920 | } // namespace shill |