Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/socket.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 6 | #include <linux/if.h> // NOLINT(build/include_alpha) - needs sockaddr. |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 7 | #include <linux/netlink.h> |
| 8 | #include <linux/rtnetlink.h> |
| 9 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | #include "shill/byte_string.h" |
| 15 | #include "shill/ip_address.h" |
| 16 | #include "shill/rtnl_message.h" |
| 17 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 18 | using std::string; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 19 | using testing::Test; |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // These test strings were generated by cutting up the output of the |
| 26 | // "rtmon" utility into individual strings. This tool outputs the raw |
| 27 | // RTNL packet contents sent by the kernel. The comments above each |
| 28 | // of these strings is the markup that the "ip monitor" command |
| 29 | // outputs for each of these RTNL packets. |
| 30 | |
| 31 | |
| 32 | // 2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP |
| 33 | // link/ether c0:f8:da:05:03:0b brd ff:ff:ff:ff:ff:ff |
| 34 | |
| 35 | const int kNewLinkMessageWlan0InterfaceIndex = 2; |
| 36 | const unsigned int kNewLinkMessageWlan0InterfaceFlags = |
| 37 | IFF_BROADCAST | IFF_MULTICAST | IFF_UP | IFF_LOWER_UP | IFF_RUNNING; |
| 38 | const unsigned int kNewLinkMessageWlan0InterfaceFlagsChange = 0; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 39 | const uint32_t kNewLinkMessageWlan0MTU = 1500; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 40 | const char kNewLinkMessageWlan0MACAddress[] = "\xc0\xf8\xda\x05\x03\x0b"; |
| 41 | const char kNewLinkMessageWlan0InterfaceName[] = "wlan0"; |
| 42 | const char kNewLinkMessageWlan0Qdisc[] = "mq"; |
| 43 | const int kNewLinkMessageWlan0OperState = IF_OPER_UP; |
| 44 | |
| 45 | const unsigned char kNewLinkMessageWlan0[] = { |
| 46 | 0xe0, 0x03, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00, |
| 47 | 0x53, 0x18, 0x1f, 0x4e, 0xac, 0x77, 0x00, 0x00, |
| 48 | 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, |
| 49 | 0x43, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 50 | 0x0a, 0x00, 0x03, 0x00, 0x77, 0x6c, 0x61, 0x6e, |
| 51 | 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00, |
| 52 | 0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00, |
| 53 | 0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00, |
| 54 | 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, |
| 55 | 0xdc, 0x05, 0x00, 0x00, 0x07, 0x00, 0x06, 0x00, |
| 56 | 0x6d, 0x71, 0x00, 0x00, 0x20, 0x00, 0x0e, 0x00, |
| 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 59 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 60 | 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00, |
| 61 | 0xc0, 0xf8, 0xda, 0x05, 0x03, 0x0b, 0x00, 0x00, |
| 62 | 0x0a, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, |
| 63 | 0xff, 0xff, 0x00, 0x00, 0x60, 0x00, 0x07, 0x00, |
| 64 | 0xa6, 0x65, 0x0f, 0x00, 0xa6, 0x0b, 0x00, 0x00, |
| 65 | 0xd2, 0x29, 0x61, 0x06, 0xd7, 0x25, 0x07, 0x00, |
| 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 67 | 0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 68 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 69 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 70 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 71 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 72 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 73 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 74 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 75 | 0x00, 0x00, 0x00, 0x00, 0xbc, 0x00, 0x17, 0x00, |
| 76 | 0xa6, 0x65, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 77 | 0xa6, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 | 0xd2, 0x29, 0x61, 0x06, 0x00, 0x00, 0x00, 0x00, |
| 79 | 0xd7, 0x25, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 80 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 81 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 82 | 0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 84 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 86 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 87 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 88 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 89 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 90 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 91 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 92 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 93 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 94 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 95 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 96 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 97 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 98 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 99 | 0x08, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 100 | 0x30, 0x02, 0x1a, 0x00, 0x6c, 0x00, 0x02, 0x00, |
| 101 | 0x68, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 102 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 103 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 104 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 105 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 106 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 107 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 108 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 109 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 111 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 112 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 113 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 114 | 0xc0, 0x01, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00, |
| 115 | 0x10, 0x00, 0x00, 0x80, 0x14, 0x00, 0x05, 0x00, |
| 116 | 0xff, 0xff, 0x00, 0x00, 0x22, 0x05, 0x00, 0x00, |
| 117 | 0x4f, 0x53, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00, |
| 118 | 0x78, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 119 | 0x40, 0x00, 0x00, 0x00, 0xdc, 0x05, 0x00, 0x00, |
| 120 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 121 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 122 | 0x03, 0x00, 0x00, 0x00, 0xa0, 0x0f, 0x00, 0x00, |
| 123 | 0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 124 | 0x80, 0x3a, 0x09, 0x00, 0x80, 0x51, 0x01, 0x00, |
| 125 | 0x05, 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0x00, |
| 126 | 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 127 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 128 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 129 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 130 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 131 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 132 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 133 | 0xfc, 0x00, 0x03, 0x00, 0x1f, 0x00, 0x00, 0x00, |
| 134 | 0x00, 0x00, 0x00, 0x00, 0x15, 0xe6, 0x04, 0x00, |
| 135 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 136 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 137 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 138 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 139 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 140 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 141 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 142 | 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, |
| 143 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 144 | 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, |
| 145 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 146 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 147 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 148 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 149 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 150 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 151 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 152 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 153 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 154 | 0x00, 0x00, 0x00, 0x00, 0x15, 0xe6, 0x04, 0x00, |
| 155 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, |
| 156 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 157 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 158 | 0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01, |
| 159 | 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, |
| 160 | 0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01, |
| 161 | 0x00, 0x00, 0x00, 0x00, 0x18, 0x02, 0x00, 0x00, |
| 162 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 163 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 164 | 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00, |
| 165 | 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 166 | 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 167 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 168 | 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 169 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 170 | }; |
| 171 | |
| 172 | |
| 173 | // Deleted 8: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN |
| 174 | // link/ether 68:7f:74:ba:ef:c7 brd ff:ff:ff:ff:ff:ff |
| 175 | |
| 176 | const int kDelLinkMessageEth0InterfaceIndex = 8; |
| 177 | const unsigned int kDelLinkMessageEth0InterfaceFlags = |
| 178 | IFF_BROADCAST | IFF_MULTICAST; |
| 179 | const unsigned int kDelLinkMessageEth0InterfaceFlagsChange = 0xffffffff; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 180 | const uint32_t kDelLinkMessageEth0MTU = 1500; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 181 | const char kDelLinkMessageEth0MACAddress[] = "\x68\x7f\x74\xba\xef\xc7"; |
| 182 | const char kDelLinkMessageEth0InterfacName[] = "eth0"; |
| 183 | const char kDelLinkMessageEth0Qdisc[] = "noop"; |
| 184 | const int kDelLinkMessageEth0OperState = IF_OPER_DOWN; |
| 185 | |
| 186 | const unsigned char kDelLinkMessageEth0[] = { |
| 187 | 0xb8, 0x01, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, |
| 188 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 189 | 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, |
| 190 | 0x02, 0x10, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, |
| 191 | 0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30, |
| 192 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00, |
| 193 | 0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00, |
| 194 | 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00, |
| 195 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, |
| 196 | 0xdc, 0x05, 0x00, 0x00, 0x09, 0x00, 0x06, 0x00, |
| 197 | 0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00, |
| 198 | 0x20, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 199 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 200 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 201 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 202 | 0x0a, 0x00, 0x01, 0x00, 0x68, 0x7f, 0x74, 0xba, |
| 203 | 0xef, 0xc7, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00, |
| 204 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 205 | 0x60, 0x00, 0x07, 0x00, 0x09, 0x00, 0x00, 0x00, |
| 206 | 0x0e, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00, |
| 207 | 0x34, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 208 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 209 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 210 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 211 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 212 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 213 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 214 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 215 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 216 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 217 | 0xbc, 0x00, 0x17, 0x00, 0x09, 0x00, 0x00, 0x00, |
| 218 | 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, |
| 219 | 0x00, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00, |
| 220 | 0x00, 0x00, 0x00, 0x00, 0x34, 0x06, 0x00, 0x00, |
| 221 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 222 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 223 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 224 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 225 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 226 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 227 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 228 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 229 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 230 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 231 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 232 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 233 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 234 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 235 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 236 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 237 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 238 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 239 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 240 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x15, 0x00, |
| 241 | 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x1a, 0x00, |
| 242 | }; |
| 243 | |
| 244 | // 8: if8 inet 192.168.10.100/24 brd 192.168.10.255 scope global eth0 |
| 245 | |
| 246 | const int kNewAddrIPV4InterfaceIndex = 8; |
| 247 | const char kNewAddrIPV4Address[] = "192.168.10.100"; |
| 248 | const int kNewAddrIPV4AddressPrefix = 24; |
| 249 | const unsigned char kNewAddrIPV4Scope = RT_SCOPE_UNIVERSE; |
| 250 | |
| 251 | const unsigned char kNewAddrIPV4[] = { |
| 252 | 0x3c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, |
| 253 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 254 | 0x02, 0x18, 0x80, 0x00, 0x08, 0x00, 0x00, 0x00, |
| 255 | 0x08, 0x00, 0x01, 0x00, 0xc0, 0xa8, 0x0a, 0x64, |
| 256 | 0x08, 0x00, 0x02, 0x00, 0xc0, 0xa8, 0x0a, 0x64, |
| 257 | 0x08, 0x00, 0x04, 0x00, 0xc0, 0xa8, 0x0a, 0xff, |
| 258 | 0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30, |
| 259 | 0x00, 0x00, 0x00, 0x00, |
| 260 | }; |
| 261 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 262 | // Deleted 15: if15 inet6 fe80::6a7f:74ff:feba:efc7/64 scope link |
| 263 | // valid_lft forever preferred_lft forever |
| 264 | |
| 265 | const int kDelAddrIPV6InterfaceIndex = 15; |
| 266 | const char kDelAddrIPV6Address[] = "fe80::6a7f:74ff:feba:efc7"; |
| 267 | const int kDelAddrIPV6AddressPrefix = 64; |
| 268 | const unsigned char kDelAddrIPV6Scope = RT_SCOPE_LINK; |
| 269 | |
| 270 | const unsigned char kDelAddrIPV6[] = { |
| 271 | 0x40, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, |
| 272 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 273 | 0x0a, 0x40, 0x80, 0xfd, 0x0f, 0x00, 0x00, 0x00, |
| 274 | 0x14, 0x00, 0x01, 0x00, 0xfe, 0x80, 0x00, 0x00, |
| 275 | 0x00, 0x00, 0x00, 0x00, 0x6a, 0x7f, 0x74, 0xff, |
| 276 | 0xfe, 0xba, 0xef, 0xc7, 0x14, 0x00, 0x06, 0x00, |
| 277 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 278 | 0xbf, 0xdb, 0x02, 0x00, 0xbf, 0xdb, 0x02, 0x00, |
| 279 | }; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 280 | |
| 281 | // Deleted ff02::1:ffa0:688 via ff02::1:ffa0:688 dev if2 metric 0 |
| 282 | |
| 283 | const int kDelRouteIPV6InterfaceIndex = 2; |
| 284 | const char kDelRouteIPV6Address[] = "ff02::1:ffa0:688"; |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 285 | const int kDelRouteIPV6Prefix = 128; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 286 | const int kDelRouteIPV6Metric = 0; |
| 287 | |
| 288 | const unsigned char kDelRouteIPV6[] = { |
| 289 | 0x80, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, |
| 290 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 291 | 0x0a, 0x80, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x01, |
| 292 | 0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00, |
| 293 | 0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, |
| 294 | 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 295 | 0x00, 0x00, 0x00, 0x01, 0xff, 0xa0, 0x06, 0x88, |
| 296 | 0x14, 0x00, 0x05, 0x00, 0xff, 0x02, 0x00, 0x00, |
| 297 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 298 | 0xff, 0xa0, 0x06, 0x88, 0x08, 0x00, 0x04, 0x00, |
| 299 | 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, |
| 300 | 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0c, 0x00, |
| 301 | 0x00, 0x00, 0x00, 0x00, 0x5f, 0x0c, 0x00, 0x00, |
| 302 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 303 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 304 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 305 | }; |
| 306 | |
| 307 | // default via 192.168.17.254 dev if12 metric 9 |
| 308 | |
| 309 | const int kAddRouteIPV4InterfaceIndex = 12; |
| 310 | const char kAddRouteIPV4Address[] = "192.168.17.254"; |
| 311 | const int kAddRouteIPV4Metric = 9; |
| 312 | |
| 313 | const unsigned char kAddRouteIPV4[] = { |
| 314 | 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, |
| 315 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 316 | 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, |
| 317 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00, |
| 318 | 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, |
| 319 | 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00, |
| 320 | 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00, |
| 321 | 0x0c, 0x00, 0x00, 0x00, |
| 322 | }; |
| 323 | |
| 324 | |
| 325 | // This is the same as kAddRouteIPV4 above, except manually corrupted |
| 326 | // the second to last parameter |
| 327 | const unsigned char kAddRouteBusted[] = { |
| 328 | 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, |
| 329 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 330 | 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, |
| 331 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00, |
| 332 | 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, |
| 333 | 0x09, 0x00, 0x00, 0x00, 0x28, 0x00, 0x05, 0x00, |
| 334 | 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00, |
| 335 | 0x0c, 0x00, 0x00, 0x00, |
| 336 | }; |
| 337 | |
| 338 | // 192.168.10.1 dev if8 lladdr 00:14:d1:cd:d5:2c REACHABLE |
| 339 | const unsigned char kAddNeighborMessage[] = { |
| 340 | 0x4c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, |
| 341 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 342 | 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, |
| 343 | 0x02, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x00, |
| 344 | 0xc0, 0xa8, 0x0a, 0x01, 0x0a, 0x00, 0x02, 0x00, |
| 345 | 0x00, 0x14, 0xd1, 0xcd, 0xd5, 0x2c, 0x00, 0x00, |
| 346 | 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, |
| 347 | 0x14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 348 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 349 | 0x04, 0x00, 0x00, 0x00, |
| 350 | }; |
| 351 | |
Peter Qiu | 3ef4f53 | 2014-07-25 15:31:44 -0700 | [diff] [blame] | 352 | // RDNSS notification |
| 353 | // Lifetime: infinity (0xffffffff) |
| 354 | // Server addresses: 2001:db8:100:f101::1, 2001:db8:100:f101::2 |
| 355 | const unsigned char kNdRdnssMessage[] = { |
| 356 | 0x5c, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, |
| 357 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 358 | 0x0a, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 359 | 0x86, 0x00, 0x03, 0x00, 0x14, 0x00, 0x01, 0x00, |
| 360 | 0x19, 0x05, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, |
| 361 | 0x20, 0x01, 0x0d, 0xb8, 0x01, 0x00, 0xf1, 0x01, |
| 362 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 363 | 0x20, 0x01, 0x0d, 0xb8, 0x01, 0x00, 0xf1, 0x01, |
| 364 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, |
| 365 | 0x14, 0x00, 0x01, 0x00, 0xfe, 0x80, 0x00, 0x00, |
| 366 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 367 | 0x50, 0xf8, 0x86, 0xff, |
| 368 | }; |
| 369 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 370 | } // namespace |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 371 | |
| 372 | class RTNLMessageTest : public Test { |
| 373 | protected: |
| 374 | void TestParseLink(const ByteString &packet, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 375 | RTNLMessage::Mode mode, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 376 | int interface_index, |
| 377 | unsigned int flags, |
| 378 | unsigned int change, |
| 379 | ByteString address, |
| 380 | ByteString name, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 381 | uint32_t mtu, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 382 | ByteString qdisc, |
| 383 | int oper_state) { |
| 384 | RTNLMessage msg; |
| 385 | EXPECT_TRUE(msg.Decode(packet)); |
| 386 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 387 | EXPECT_EQ(RTNLMessage::kTypeLink, msg.type()); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 388 | EXPECT_EQ(mode, msg.mode()); |
| 389 | EXPECT_EQ(interface_index, msg.interface_index()); |
| 390 | |
| 391 | RTNLMessage::LinkStatus status = msg.link_status(); |
| 392 | EXPECT_EQ(flags, status.flags); |
| 393 | EXPECT_EQ(change, status.change); |
| 394 | |
| 395 | EXPECT_TRUE(msg.HasAttribute(IFLA_ADDRESS)); |
| 396 | EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFLA_ADDRESS).GetLength()); |
| 397 | EXPECT_TRUE(msg.GetAttribute(IFLA_ADDRESS).Equals(address)); |
| 398 | |
| 399 | EXPECT_TRUE(msg.HasAttribute(IFLA_IFNAME)); |
| 400 | EXPECT_EQ(name.GetLength(), msg.GetAttribute(IFLA_IFNAME).GetLength()); |
| 401 | EXPECT_TRUE(msg.GetAttribute(IFLA_IFNAME).Equals(name)); |
| 402 | |
| 403 | EXPECT_TRUE(msg.HasAttribute(IFLA_MTU)); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 404 | uint32_t mtu_val; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 405 | EXPECT_TRUE(msg.GetAttribute(IFLA_MTU).ConvertToCPUUInt32(&mtu_val)); |
| 406 | EXPECT_EQ(mtu, mtu_val); |
| 407 | |
| 408 | EXPECT_TRUE(msg.HasAttribute(IFLA_QDISC)); |
| 409 | EXPECT_EQ(qdisc.GetLength(), msg.GetAttribute(IFLA_QDISC).GetLength()); |
| 410 | EXPECT_TRUE(msg.GetAttribute(IFLA_QDISC).Equals(qdisc)); |
| 411 | |
| 412 | EXPECT_TRUE(msg.HasAttribute(IFLA_OPERSTATE)); |
| 413 | EXPECT_EQ(oper_state, msg.GetAttribute(IFLA_OPERSTATE).GetConstData()[0]); |
| 414 | } |
| 415 | |
| 416 | void TestParseAddress(const ByteString &packet, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 417 | RTNLMessage::Mode mode, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 418 | int interface_index, |
| 419 | const IPAddress &address, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 420 | unsigned char scope) { |
| 421 | RTNLMessage msg; |
| 422 | |
| 423 | EXPECT_TRUE(msg.Decode(packet)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 424 | EXPECT_EQ(RTNLMessage::kTypeAddress, msg.type()); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 425 | EXPECT_EQ(mode, msg.mode()); |
| 426 | EXPECT_EQ(interface_index, msg.interface_index()); |
| 427 | EXPECT_EQ(address.family(), msg.family()); |
| 428 | |
| 429 | RTNLMessage::AddressStatus status = msg.address_status(); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 430 | EXPECT_EQ(scope, status.scope); |
| 431 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 432 | EXPECT_TRUE(msg.HasAttribute(IFA_ADDRESS)); |
| 433 | EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFA_ADDRESS).GetLength()); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 434 | EXPECT_TRUE( |
| 435 | IPAddress(address.family(), |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 436 | msg.GetAttribute(IFA_ADDRESS), |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 437 | status.prefix_len).Equals(address)); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void TestParseRoute(const ByteString &packet, |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 441 | RTNLMessage::Mode /*mode*/, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 442 | IPAddress::Family family, |
| 443 | int interface_index, |
| 444 | const IPAddress &dst, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 445 | const IPAddress &src, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 446 | const IPAddress &gateway, |
| 447 | unsigned char table, |
| 448 | int protocol, |
| 449 | unsigned char scope, |
| 450 | unsigned char type, |
| 451 | int metric) { |
| 452 | RTNLMessage msg; |
| 453 | |
| 454 | EXPECT_TRUE(msg.Decode(packet)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 455 | EXPECT_EQ(RTNLMessage::kTypeRoute, msg.type()); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 456 | EXPECT_EQ(0, msg.interface_index()); |
| 457 | EXPECT_EQ(family, msg.family()); |
| 458 | |
| 459 | RTNLMessage::RouteStatus status = msg.route_status(); |
| 460 | EXPECT_EQ(table, status.table); |
| 461 | EXPECT_EQ(protocol, status.protocol); |
| 462 | EXPECT_EQ(scope, status.scope); |
| 463 | EXPECT_EQ(type, status.type); |
| 464 | |
| 465 | if (!dst.IsDefault()) { |
| 466 | EXPECT_TRUE(msg.HasAttribute(RTA_DST)); |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 467 | EXPECT_TRUE(IPAddress(family, |
| 468 | msg.GetAttribute(RTA_DST), |
| 469 | status.dst_prefix).Equals(dst)); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (!src.IsDefault()) { |
| 473 | EXPECT_TRUE(msg.HasAttribute(RTA_SRC)); |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 474 | EXPECT_TRUE(IPAddress(family, |
| 475 | msg.GetAttribute(RTA_SRC), |
| 476 | status.src_prefix).Equals(src)); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | if (!gateway.IsDefault()) { |
| 480 | EXPECT_TRUE(msg.HasAttribute(RTA_GATEWAY)); |
| 481 | EXPECT_TRUE(IPAddress(family, |
| 482 | msg.GetAttribute(RTA_GATEWAY)).Equals(gateway)); |
| 483 | } |
| 484 | |
| 485 | if (interface_index >= 0) { |
| 486 | EXPECT_TRUE(msg.HasAttribute(RTA_OIF)); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 487 | uint32_t int_val; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 488 | EXPECT_TRUE(msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&int_val)); |
| 489 | EXPECT_EQ(interface_index, int_val); |
| 490 | } else { |
| 491 | EXPECT_FALSE(msg.HasAttribute(RTA_OIF)); |
| 492 | } |
| 493 | if (metric >= 0) { |
| 494 | EXPECT_TRUE(msg.HasAttribute(RTA_PRIORITY)); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 495 | uint32_t metric_val; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 496 | EXPECT_TRUE( |
| 497 | msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric_val)); |
| 498 | EXPECT_EQ(metric, metric_val); |
| 499 | } else { |
| 500 | EXPECT_FALSE(msg.HasAttribute(RTA_PRIORITY)); |
| 501 | } |
| 502 | } |
Peter Qiu | 3ef4f53 | 2014-07-25 15:31:44 -0700 | [diff] [blame] | 503 | |
| 504 | void TestParseRdnss(const ByteString &packet, |
| 505 | RTNLMessage::Mode mode, |
| 506 | int interface_index, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 507 | uint32_t lifetime, |
Peter Qiu | 3ef4f53 | 2014-07-25 15:31:44 -0700 | [diff] [blame] | 508 | const std::string &dns_server_addresses) { |
| 509 | RTNLMessage msg; |
| 510 | |
| 511 | EXPECT_TRUE(msg.Decode(packet)); |
| 512 | EXPECT_EQ(RTNLMessage::kTypeRdnss, msg.type()); |
| 513 | EXPECT_EQ(mode, msg.mode()); |
| 514 | EXPECT_EQ(interface_index, msg.interface_index()); |
| 515 | |
| 516 | RTNLMessage::RdnssOption rdnss = msg.rdnss_option(); |
| 517 | |
| 518 | // Format addresses string for verification. |
| 519 | std::string addresses; |
| 520 | bool first = true; |
| 521 | for (auto &ip : rdnss.addresses) { |
| 522 | if (!first) { |
| 523 | addresses += ", "; |
| 524 | } else { |
| 525 | first = false; |
| 526 | } |
| 527 | addresses += ip.ToString(); |
| 528 | } |
| 529 | |
| 530 | // Verify life time and addresses. |
| 531 | EXPECT_EQ(lifetime, rdnss.lifetime); |
| 532 | EXPECT_EQ(dns_server_addresses, addresses); |
| 533 | } |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | TEST_F(RTNLMessageTest, NewLinkWlan0) { |
| 537 | TestParseLink(ByteString(kNewLinkMessageWlan0, sizeof(kNewLinkMessageWlan0)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 538 | RTNLMessage::kModeAdd, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 539 | kNewLinkMessageWlan0InterfaceIndex, |
| 540 | kNewLinkMessageWlan0InterfaceFlags, |
| 541 | kNewLinkMessageWlan0InterfaceFlagsChange, |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 542 | ByteString(string(kNewLinkMessageWlan0MACAddress), false), |
| 543 | ByteString(string(kNewLinkMessageWlan0InterfaceName), true), |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 544 | kNewLinkMessageWlan0MTU, |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 545 | ByteString(string(kNewLinkMessageWlan0Qdisc), true), |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 546 | kNewLinkMessageWlan0OperState); |
| 547 | } |
| 548 | |
| 549 | TEST_F(RTNLMessageTest, DelLinkEth0) { |
| 550 | TestParseLink(ByteString(kDelLinkMessageEth0, sizeof(kDelLinkMessageEth0)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 551 | RTNLMessage::kModeDelete, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 552 | kDelLinkMessageEth0InterfaceIndex, |
| 553 | kDelLinkMessageEth0InterfaceFlags, |
| 554 | kDelLinkMessageEth0InterfaceFlagsChange, |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 555 | ByteString(string(kDelLinkMessageEth0MACAddress), false), |
| 556 | ByteString(string(kDelLinkMessageEth0InterfacName), true), |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 557 | kDelLinkMessageEth0MTU, |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 558 | ByteString(string(kDelLinkMessageEth0Qdisc), true), |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 559 | kDelLinkMessageEth0OperState); |
| 560 | } |
| 561 | |
| 562 | TEST_F(RTNLMessageTest, NewAddrIPv4) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 563 | IPAddress addr(IPAddress::kFamilyIPv4); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 564 | |
| 565 | EXPECT_TRUE(addr.SetAddressFromString(kNewAddrIPV4Address)); |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 566 | addr.set_prefix(kNewAddrIPV4AddressPrefix); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 567 | TestParseAddress(ByteString(kNewAddrIPV4, sizeof(kNewAddrIPV4)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 568 | RTNLMessage::kModeAdd, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 569 | kNewAddrIPV4InterfaceIndex, |
| 570 | addr, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 571 | kNewAddrIPV4Scope); |
| 572 | } |
| 573 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 574 | TEST_F(RTNLMessageTest, DelAddrIPv6) { |
| 575 | IPAddress addr(IPAddress::kFamilyIPv6); |
| 576 | |
| 577 | EXPECT_TRUE(addr.SetAddressFromString(kDelAddrIPV6Address)); |
| 578 | addr.set_prefix(kDelAddrIPV6AddressPrefix); |
| 579 | TestParseAddress(ByteString(kDelAddrIPV6, sizeof(kDelAddrIPV6)), |
| 580 | RTNLMessage::kModeDelete, |
| 581 | kDelAddrIPV6InterfaceIndex, |
| 582 | addr, |
| 583 | kDelAddrIPV6Scope); |
| 584 | } |
| 585 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 586 | TEST_F(RTNLMessageTest, DelRouteIPv6) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 587 | IPAddress dst(IPAddress::kFamilyIPv6); |
| 588 | IPAddress src(IPAddress::kFamilyIPv6); |
| 589 | IPAddress gateway(IPAddress::kFamilyIPv6); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 590 | |
| 591 | EXPECT_TRUE(dst.SetAddressFromString(kDelRouteIPV6Address)); |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 592 | dst.set_prefix(kDelRouteIPV6Prefix); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 593 | src.SetAddressToDefault(); |
| 594 | EXPECT_TRUE(gateway.SetAddressFromString(kDelRouteIPV6Address)); |
| 595 | |
| 596 | TestParseRoute(ByteString(kDelRouteIPV6, sizeof(kDelRouteIPV6)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 597 | RTNLMessage::kModeDelete, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 598 | IPAddress::kFamilyIPv6, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 599 | kDelRouteIPV6InterfaceIndex, |
| 600 | dst, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 601 | src, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 602 | gateway, |
| 603 | RT_TABLE_MAIN, |
| 604 | RTPROT_UNSPEC, |
| 605 | RT_SCOPE_UNIVERSE, |
| 606 | RTN_UNICAST, |
| 607 | kDelRouteIPV6Metric); |
| 608 | } |
| 609 | |
| 610 | TEST_F(RTNLMessageTest, AddRouteIPv4) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 611 | IPAddress dst(IPAddress::kFamilyIPv4); |
| 612 | IPAddress src(IPAddress::kFamilyIPv4); |
| 613 | IPAddress gateway(IPAddress::kFamilyIPv4); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 614 | |
| 615 | dst.SetAddressToDefault(); |
| 616 | src.SetAddressToDefault(); |
| 617 | EXPECT_TRUE(gateway.SetAddressFromString(kAddRouteIPV4Address)); |
| 618 | |
| 619 | TestParseRoute(ByteString(kAddRouteIPV4, sizeof(kAddRouteIPV4)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 620 | RTNLMessage::kModeAdd, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 621 | IPAddress::kFamilyIPv4, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 622 | kAddRouteIPV4InterfaceIndex, |
| 623 | dst, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 624 | src, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 625 | gateway, |
| 626 | RT_TABLE_MAIN, |
| 627 | RTPROT_BOOT, |
| 628 | RT_SCOPE_UNIVERSE, |
| 629 | RTN_UNICAST, |
| 630 | kAddRouteIPV4Metric); |
| 631 | } |
| 632 | |
Peter Qiu | 3ef4f53 | 2014-07-25 15:31:44 -0700 | [diff] [blame] | 633 | TEST_F(RTNLMessageTest, NewRdnssOption) { |
| 634 | int interface_index = 1; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 635 | uint32_t lifetime = 0xffffffff; |
Peter Qiu | 3ef4f53 | 2014-07-25 15:31:44 -0700 | [diff] [blame] | 636 | std::string dns_server_addresses = |
| 637 | "2001:db8:100:f101::1, 2001:db8:100:f101::2"; |
| 638 | |
| 639 | TestParseRdnss(ByteString(kNdRdnssMessage, sizeof(kNdRdnssMessage)), |
| 640 | RTNLMessage::kModeAdd, |
| 641 | interface_index, |
| 642 | lifetime, |
| 643 | dns_server_addresses); |
| 644 | } |
| 645 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 646 | TEST_F(RTNLMessageTest, AddRouteBusted) { |
| 647 | // RTNLMessage should list parse errors as kMessageUnknown |
| 648 | RTNLMessage msg; |
| 649 | EXPECT_FALSE(msg.Decode( |
| 650 | ByteString(kAddRouteBusted, sizeof(kAddRouteBusted)))); |
| 651 | } |
| 652 | |
| 653 | TEST_F(RTNLMessageTest, AddNeighbor) { |
| 654 | // RTNLMessage doesn't parse Add-Neighbor messages -- ensure this fails |
| 655 | RTNLMessage msg; |
| 656 | EXPECT_FALSE(msg.Decode( |
| 657 | ByteString(kAddNeighborMessage, sizeof(kAddNeighborMessage)))); |
| 658 | } |
| 659 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 660 | TEST_F(RTNLMessageTest, EncodeRouteAdd) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 661 | RTNLMessage msg(RTNLMessage::kTypeRoute, |
| 662 | RTNLMessage::kModeAdd, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 663 | 0, 1, 2, 0, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 664 | IPAddress::kFamilyIPv4); |
| 665 | IPAddress dst(IPAddress::kFamilyIPv4); |
| 666 | IPAddress src(IPAddress::kFamilyIPv4); |
| 667 | IPAddress gateway(IPAddress::kFamilyIPv4); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 668 | |
| 669 | dst.SetAddressToDefault(); |
| 670 | src.SetAddressToDefault(); |
| 671 | EXPECT_TRUE(gateway.SetAddressFromString("192.168.0.1")); |
| 672 | |
| 673 | msg.set_route_status(RTNLMessage::RouteStatus( |
| 674 | 0, 0, RT_TABLE_MAIN, RTPROT_BOOT, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0)); |
| 675 | msg.SetAttribute(RTA_DST, dst.address()); |
| 676 | msg.SetAttribute(RTA_SRC, src.address()); |
| 677 | msg.SetAttribute(RTA_GATEWAY, gateway.address()); |
| 678 | msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(12)); |
| 679 | msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(13)); |
| 680 | |
| 681 | |
| 682 | TestParseRoute(msg.Encode(), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 683 | RTNLMessage::kModeAdd, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 684 | IPAddress::kFamilyIPv4, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 685 | 12, |
| 686 | dst, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 687 | src, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 688 | gateway, |
| 689 | RT_TABLE_MAIN, |
| 690 | RTPROT_BOOT, |
| 691 | RT_SCOPE_UNIVERSE, |
| 692 | RTN_UNICAST, |
| 693 | 13); |
| 694 | } |
| 695 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 696 | TEST_F(RTNLMessageTest, EncodeLinkDel) { |
| 697 | const int kInterfaceIndex = 0x1234; |
| 698 | RTNLMessage pmsg(RTNLMessage::kTypeLink, |
| 699 | RTNLMessage::kModeDelete, |
| 700 | NLM_F_REQUEST, |
| 701 | 0, |
| 702 | 0, |
| 703 | kInterfaceIndex, |
| 704 | IPAddress::kFamilyUnknown); |
| 705 | |
| 706 | |
| 707 | RTNLMessage msg; |
| 708 | EXPECT_TRUE(msg.Decode(pmsg.Encode())); |
| 709 | |
| 710 | EXPECT_EQ(RTNLMessage::kTypeLink, msg.type()); |
| 711 | EXPECT_EQ(RTNLMessage::kModeDelete, msg.mode()); |
| 712 | EXPECT_EQ(kInterfaceIndex, msg.interface_index()); |
| 713 | |
| 714 | RTNLMessage::LinkStatus status = msg.link_status(); |
| 715 | EXPECT_EQ(0, status.flags); |
| 716 | EXPECT_EQ(0, status.change); |
| 717 | |
| 718 | EXPECT_FALSE(msg.HasAttribute(IFLA_ADDRESS)); |
| 719 | EXPECT_FALSE(msg.HasAttribute(IFLA_IFNAME)); |
| 720 | EXPECT_FALSE(msg.HasAttribute(IFLA_MTU)); |
| 721 | EXPECT_FALSE(msg.HasAttribute(IFLA_QDISC)); |
| 722 | EXPECT_FALSE(msg.HasAttribute(IFLA_OPERSTATE)); |
| 723 | } |
| 724 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 725 | } // namespace shill |