blob: ffb5d309f821e23c845e2db1aebf23924867335b [file] [log] [blame]
Gary Morain41780232012-07-31 15:08:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartdd7df792011-07-15 11:09:50 -07002// 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>
6#include <linux/if.h>
7#include <linux/netlink.h>
8#include <linux/rtnetlink.h>
9
10#include <gtest/gtest.h>
11
12#include "shill/byte_string.h"
13#include "shill/ip_address.h"
14#include "shill/rtnl_message.h"
15
Gary Morain41780232012-07-31 15:08:31 -070016using std::string;
Paul Stewartdd7df792011-07-15 11:09:50 -070017using testing::Test;
18
19namespace shill {
20
21namespace {
22
23// These test strings were generated by cutting up the output of the
24// "rtmon" utility into individual strings. This tool outputs the raw
25// RTNL packet contents sent by the kernel. The comments above each
26// of these strings is the markup that the "ip monitor" command
27// outputs for each of these RTNL packets.
28
29
30// 2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP
31// link/ether c0:f8:da:05:03:0b brd ff:ff:ff:ff:ff:ff
32
33const int kNewLinkMessageWlan0InterfaceIndex = 2;
34const unsigned int kNewLinkMessageWlan0InterfaceFlags =
35 IFF_BROADCAST | IFF_MULTICAST | IFF_UP | IFF_LOWER_UP | IFF_RUNNING;
36const unsigned int kNewLinkMessageWlan0InterfaceFlagsChange = 0;
37const uint32 kNewLinkMessageWlan0MTU = 1500;
38const char kNewLinkMessageWlan0MACAddress[] = "\xc0\xf8\xda\x05\x03\x0b";
39const char kNewLinkMessageWlan0InterfaceName[] = "wlan0";
40const char kNewLinkMessageWlan0Qdisc[] = "mq";
41const int kNewLinkMessageWlan0OperState = IF_OPER_UP;
42
43const unsigned char kNewLinkMessageWlan0[] = {
44 0xe0, 0x03, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00,
45 0x53, 0x18, 0x1f, 0x4e, 0xac, 0x77, 0x00, 0x00,
46 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
47 0x43, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x0a, 0x00, 0x03, 0x00, 0x77, 0x6c, 0x61, 0x6e,
49 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
50 0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
51 0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
52 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
53 0xdc, 0x05, 0x00, 0x00, 0x07, 0x00, 0x06, 0x00,
54 0x6d, 0x71, 0x00, 0x00, 0x20, 0x00, 0x0e, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00,
59 0xc0, 0xf8, 0xda, 0x05, 0x03, 0x0b, 0x00, 0x00,
60 0x0a, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0x00, 0x00, 0x60, 0x00, 0x07, 0x00,
62 0xa6, 0x65, 0x0f, 0x00, 0xa6, 0x0b, 0x00, 0x00,
63 0xd2, 0x29, 0x61, 0x06, 0xd7, 0x25, 0x07, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 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, 0xbc, 0x00, 0x17, 0x00,
74 0xa6, 0x65, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0xa6, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0xd2, 0x29, 0x61, 0x06, 0x00, 0x00, 0x00, 0x00,
77 0xd7, 0x25, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 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 0x08, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x30, 0x02, 0x1a, 0x00, 0x6c, 0x00, 0x02, 0x00,
99 0x68, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
102 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
103 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00, 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 0xc0, 0x01, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00,
113 0x10, 0x00, 0x00, 0x80, 0x14, 0x00, 0x05, 0x00,
114 0xff, 0xff, 0x00, 0x00, 0x22, 0x05, 0x00, 0x00,
115 0x4f, 0x53, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00,
116 0x78, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x40, 0x00, 0x00, 0x00, 0xdc, 0x05, 0x00, 0x00,
118 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
119 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
120 0x03, 0x00, 0x00, 0x00, 0xa0, 0x0f, 0x00, 0x00,
121 0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 0x80, 0x3a, 0x09, 0x00, 0x80, 0x51, 0x01, 0x00,
123 0x05, 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0x00,
124 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0xfc, 0x00, 0x03, 0x00, 0x1f, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x15, 0xe6, 0x04, 0x00,
133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x05, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x15, 0xe6, 0x04, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01,
157 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00,
158 0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01,
159 0x00, 0x00, 0x00, 0x00, 0x18, 0x02, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00,
163 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168};
169
170
171// Deleted 8: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
172// link/ether 68:7f:74:ba:ef:c7 brd ff:ff:ff:ff:ff:ff
173
174const int kDelLinkMessageEth0InterfaceIndex = 8;
175const unsigned int kDelLinkMessageEth0InterfaceFlags =
176 IFF_BROADCAST | IFF_MULTICAST;
177const unsigned int kDelLinkMessageEth0InterfaceFlagsChange = 0xffffffff;
178const uint32 kDelLinkMessageEth0MTU = 1500;
179const char kDelLinkMessageEth0MACAddress[] = "\x68\x7f\x74\xba\xef\xc7";
180const char kDelLinkMessageEth0InterfacName[] = "eth0";
181const char kDelLinkMessageEth0Qdisc[] = "noop";
182const int kDelLinkMessageEth0OperState = IF_OPER_DOWN;
183
184const unsigned char kDelLinkMessageEth0[] = {
185 0xb8, 0x01, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
188 0x02, 0x10, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
189 0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30,
190 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
191 0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
192 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
194 0xdc, 0x05, 0x00, 0x00, 0x09, 0x00, 0x06, 0x00,
195 0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00,
196 0x20, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 0x0a, 0x00, 0x01, 0x00, 0x68, 0x7f, 0x74, 0xba,
201 0xef, 0xc7, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00,
202 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
203 0x60, 0x00, 0x07, 0x00, 0x09, 0x00, 0x00, 0x00,
204 0x0e, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00,
205 0x34, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 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 0xbc, 0x00, 0x17, 0x00, 0x09, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00,
218 0x00, 0x00, 0x00, 0x00, 0x34, 0x06, 0x00, 0x00,
219 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x08, 0x00, 0x15, 0x00,
239 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x1a, 0x00,
240};
241
242// 8: if8 inet 192.168.10.100/24 brd 192.168.10.255 scope global eth0
243
244const int kNewAddrIPV4InterfaceIndex = 8;
245const char kNewAddrIPV4Address[] = "192.168.10.100";
246const int kNewAddrIPV4AddressPrefix = 24;
247const unsigned char kNewAddrIPV4Scope = RT_SCOPE_UNIVERSE;
248
249const unsigned char kNewAddrIPV4[] = {
250 0x3c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 0x02, 0x18, 0x80, 0x00, 0x08, 0x00, 0x00, 0x00,
253 0x08, 0x00, 0x01, 0x00, 0xc0, 0xa8, 0x0a, 0x64,
254 0x08, 0x00, 0x02, 0x00, 0xc0, 0xa8, 0x0a, 0x64,
255 0x08, 0x00, 0x04, 0x00, 0xc0, 0xa8, 0x0a, 0xff,
256 0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30,
257 0x00, 0x00, 0x00, 0x00,
258};
259
Paul Stewart7355ce12011-09-02 10:47:01 -0700260// Deleted 15: if15 inet6 fe80::6a7f:74ff:feba:efc7/64 scope link
261// valid_lft forever preferred_lft forever
262
263const int kDelAddrIPV6InterfaceIndex = 15;
264const char kDelAddrIPV6Address[] = "fe80::6a7f:74ff:feba:efc7";
265const int kDelAddrIPV6AddressPrefix = 64;
266const unsigned char kDelAddrIPV6Scope = RT_SCOPE_LINK;
267
268const unsigned char kDelAddrIPV6[] = {
269 0x40, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
270 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271 0x0a, 0x40, 0x80, 0xfd, 0x0f, 0x00, 0x00, 0x00,
272 0x14, 0x00, 0x01, 0x00, 0xfe, 0x80, 0x00, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x6a, 0x7f, 0x74, 0xff,
274 0xfe, 0xba, 0xef, 0xc7, 0x14, 0x00, 0x06, 0x00,
275 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
276 0xbf, 0xdb, 0x02, 0x00, 0xbf, 0xdb, 0x02, 0x00,
277};
Paul Stewartdd7df792011-07-15 11:09:50 -0700278
279// Deleted ff02::1:ffa0:688 via ff02::1:ffa0:688 dev if2 metric 0
280
281const int kDelRouteIPV6InterfaceIndex = 2;
282const char kDelRouteIPV6Address[] = "ff02::1:ffa0:688";
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700283const int kDelRouteIPV6Prefix = 128;
Paul Stewartdd7df792011-07-15 11:09:50 -0700284const int kDelRouteIPV6Metric = 0;
285
286const unsigned char kDelRouteIPV6[] = {
287 0x80, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
288 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x0a, 0x80, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x01,
290 0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
291 0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
292 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x01, 0xff, 0xa0, 0x06, 0x88,
294 0x14, 0x00, 0x05, 0x00, 0xff, 0x02, 0x00, 0x00,
295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
296 0xff, 0xa0, 0x06, 0x88, 0x08, 0x00, 0x04, 0x00,
297 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
298 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0c, 0x00,
299 0x00, 0x00, 0x00, 0x00, 0x5f, 0x0c, 0x00, 0x00,
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
303};
304
305// default via 192.168.17.254 dev if12 metric 9
306
307const int kAddRouteIPV4InterfaceIndex = 12;
308const char kAddRouteIPV4Address[] = "192.168.17.254";
309const int kAddRouteIPV4Metric = 9;
310
311const unsigned char kAddRouteIPV4[] = {
312 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
315 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
316 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
317 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00,
318 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
319 0x0c, 0x00, 0x00, 0x00,
320};
321
322
323// This is the same as kAddRouteIPV4 above, except manually corrupted
324// the second to last parameter
325const unsigned char kAddRouteBusted[] = {
326 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
329 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
330 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
331 0x09, 0x00, 0x00, 0x00, 0x28, 0x00, 0x05, 0x00,
332 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
333 0x0c, 0x00, 0x00, 0x00,
334};
335
336// 192.168.10.1 dev if8 lladdr 00:14:d1:cd:d5:2c REACHABLE
337const unsigned char kAddNeighborMessage[] = {
338 0x4c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
339 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
341 0x02, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x00,
342 0xc0, 0xa8, 0x0a, 0x01, 0x0a, 0x00, 0x02, 0x00,
343 0x00, 0x14, 0xd1, 0xcd, 0xd5, 0x2c, 0x00, 0x00,
344 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
345 0x14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 0x04, 0x00, 0x00, 0x00,
348};
349
350} // namespace {}
351
352class RTNLMessageTest : public Test {
353 protected:
354 void TestParseLink(const ByteString &packet,
Paul Stewart9a908082011-08-31 12:18:48 -0700355 RTNLMessage::Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700356 int interface_index,
357 unsigned int flags,
358 unsigned int change,
359 ByteString address,
360 ByteString name,
361 uint32 mtu,
362 ByteString qdisc,
363 int oper_state) {
364 RTNLMessage msg;
365 EXPECT_TRUE(msg.Decode(packet));
366
Paul Stewart9a908082011-08-31 12:18:48 -0700367 EXPECT_EQ(RTNLMessage::kTypeLink, msg.type());
Paul Stewartdd7df792011-07-15 11:09:50 -0700368 EXPECT_EQ(mode, msg.mode());
369 EXPECT_EQ(interface_index, msg.interface_index());
370
371 RTNLMessage::LinkStatus status = msg.link_status();
372 EXPECT_EQ(flags, status.flags);
373 EXPECT_EQ(change, status.change);
374
375 EXPECT_TRUE(msg.HasAttribute(IFLA_ADDRESS));
376 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFLA_ADDRESS).GetLength());
377 EXPECT_TRUE(msg.GetAttribute(IFLA_ADDRESS).Equals(address));
378
379 EXPECT_TRUE(msg.HasAttribute(IFLA_IFNAME));
380 EXPECT_EQ(name.GetLength(), msg.GetAttribute(IFLA_IFNAME).GetLength());
381 EXPECT_TRUE(msg.GetAttribute(IFLA_IFNAME).Equals(name));
382
383 EXPECT_TRUE(msg.HasAttribute(IFLA_MTU));
384 uint32 mtu_val;
385 EXPECT_TRUE(msg.GetAttribute(IFLA_MTU).ConvertToCPUUInt32(&mtu_val));
386 EXPECT_EQ(mtu, mtu_val);
387
388 EXPECT_TRUE(msg.HasAttribute(IFLA_QDISC));
389 EXPECT_EQ(qdisc.GetLength(), msg.GetAttribute(IFLA_QDISC).GetLength());
390 EXPECT_TRUE(msg.GetAttribute(IFLA_QDISC).Equals(qdisc));
391
392 EXPECT_TRUE(msg.HasAttribute(IFLA_OPERSTATE));
393 EXPECT_EQ(oper_state, msg.GetAttribute(IFLA_OPERSTATE).GetConstData()[0]);
394 }
395
396 void TestParseAddress(const ByteString &packet,
Paul Stewart9a908082011-08-31 12:18:48 -0700397 RTNLMessage::Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700398 int interface_index,
399 const IPAddress &address,
Paul Stewartdd7df792011-07-15 11:09:50 -0700400 unsigned char scope) {
401 RTNLMessage msg;
402
403 EXPECT_TRUE(msg.Decode(packet));
Paul Stewart9a908082011-08-31 12:18:48 -0700404 EXPECT_EQ(RTNLMessage::kTypeAddress, msg.type());
Paul Stewartdd7df792011-07-15 11:09:50 -0700405 EXPECT_EQ(mode, msg.mode());
406 EXPECT_EQ(interface_index, msg.interface_index());
407 EXPECT_EQ(address.family(), msg.family());
408
409 RTNLMessage::AddressStatus status = msg.address_status();
Paul Stewartdd7df792011-07-15 11:09:50 -0700410 EXPECT_EQ(scope, status.scope);
411
Paul Stewart7355ce12011-09-02 10:47:01 -0700412 EXPECT_TRUE(msg.HasAttribute(IFA_ADDRESS));
413 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFA_ADDRESS).GetLength());
Paul Stewartdd7df792011-07-15 11:09:50 -0700414 EXPECT_TRUE(
415 IPAddress(address.family(),
Paul Stewart7355ce12011-09-02 10:47:01 -0700416 msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700417 status.prefix_len).Equals(address));
Paul Stewartdd7df792011-07-15 11:09:50 -0700418 }
419
420 void TestParseRoute(const ByteString &packet,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700421 RTNLMessage::Mode /*mode*/,
Paul Stewartdd7df792011-07-15 11:09:50 -0700422 IPAddress::Family family,
423 int interface_index,
424 const IPAddress &dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700425 const IPAddress &src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700426 const IPAddress &gateway,
427 unsigned char table,
428 int protocol,
429 unsigned char scope,
430 unsigned char type,
431 int metric) {
432 RTNLMessage msg;
433
434 EXPECT_TRUE(msg.Decode(packet));
Paul Stewart9a908082011-08-31 12:18:48 -0700435 EXPECT_EQ(RTNLMessage::kTypeRoute, msg.type());
Paul Stewartdd7df792011-07-15 11:09:50 -0700436 EXPECT_EQ(0, msg.interface_index());
437 EXPECT_EQ(family, msg.family());
438
439 RTNLMessage::RouteStatus status = msg.route_status();
440 EXPECT_EQ(table, status.table);
441 EXPECT_EQ(protocol, status.protocol);
442 EXPECT_EQ(scope, status.scope);
443 EXPECT_EQ(type, status.type);
444
445 if (!dst.IsDefault()) {
446 EXPECT_TRUE(msg.HasAttribute(RTA_DST));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700447 EXPECT_TRUE(IPAddress(family,
448 msg.GetAttribute(RTA_DST),
449 status.dst_prefix).Equals(dst));
Paul Stewartdd7df792011-07-15 11:09:50 -0700450 }
451
452 if (!src.IsDefault()) {
453 EXPECT_TRUE(msg.HasAttribute(RTA_SRC));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700454 EXPECT_TRUE(IPAddress(family,
455 msg.GetAttribute(RTA_SRC),
456 status.src_prefix).Equals(src));
Paul Stewartdd7df792011-07-15 11:09:50 -0700457 }
458
459 if (!gateway.IsDefault()) {
460 EXPECT_TRUE(msg.HasAttribute(RTA_GATEWAY));
461 EXPECT_TRUE(IPAddress(family,
462 msg.GetAttribute(RTA_GATEWAY)).Equals(gateway));
463 }
464
465 if (interface_index >= 0) {
466 EXPECT_TRUE(msg.HasAttribute(RTA_OIF));
467 uint32 int_val;
468 EXPECT_TRUE(msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&int_val));
469 EXPECT_EQ(interface_index, int_val);
470 } else {
471 EXPECT_FALSE(msg.HasAttribute(RTA_OIF));
472 }
473 if (metric >= 0) {
474 EXPECT_TRUE(msg.HasAttribute(RTA_PRIORITY));
475 uint32 metric_val;
476 EXPECT_TRUE(
477 msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric_val));
478 EXPECT_EQ(metric, metric_val);
479 } else {
480 EXPECT_FALSE(msg.HasAttribute(RTA_PRIORITY));
481 }
482 }
483};
484
485TEST_F(RTNLMessageTest, NewLinkWlan0) {
486 TestParseLink(ByteString(kNewLinkMessageWlan0, sizeof(kNewLinkMessageWlan0)),
Paul Stewart9a908082011-08-31 12:18:48 -0700487 RTNLMessage::kModeAdd,
Paul Stewartdd7df792011-07-15 11:09:50 -0700488 kNewLinkMessageWlan0InterfaceIndex,
489 kNewLinkMessageWlan0InterfaceFlags,
490 kNewLinkMessageWlan0InterfaceFlagsChange,
Gary Morain41780232012-07-31 15:08:31 -0700491 ByteString(string(kNewLinkMessageWlan0MACAddress), false),
492 ByteString(string(kNewLinkMessageWlan0InterfaceName), true),
Paul Stewartdd7df792011-07-15 11:09:50 -0700493 kNewLinkMessageWlan0MTU,
Gary Morain41780232012-07-31 15:08:31 -0700494 ByteString(string(kNewLinkMessageWlan0Qdisc), true),
Paul Stewartdd7df792011-07-15 11:09:50 -0700495 kNewLinkMessageWlan0OperState);
496}
497
498TEST_F(RTNLMessageTest, DelLinkEth0) {
499 TestParseLink(ByteString(kDelLinkMessageEth0, sizeof(kDelLinkMessageEth0)),
Paul Stewart9a908082011-08-31 12:18:48 -0700500 RTNLMessage::kModeDelete,
Paul Stewartdd7df792011-07-15 11:09:50 -0700501 kDelLinkMessageEth0InterfaceIndex,
502 kDelLinkMessageEth0InterfaceFlags,
503 kDelLinkMessageEth0InterfaceFlagsChange,
Gary Morain41780232012-07-31 15:08:31 -0700504 ByteString(string(kDelLinkMessageEth0MACAddress), false),
505 ByteString(string(kDelLinkMessageEth0InterfacName), true),
Paul Stewartdd7df792011-07-15 11:09:50 -0700506 kDelLinkMessageEth0MTU,
Gary Morain41780232012-07-31 15:08:31 -0700507 ByteString(string(kDelLinkMessageEth0Qdisc), true),
Paul Stewartdd7df792011-07-15 11:09:50 -0700508 kDelLinkMessageEth0OperState);
509}
510
511TEST_F(RTNLMessageTest, NewAddrIPv4) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700512 IPAddress addr(IPAddress::kFamilyIPv4);
Paul Stewartdd7df792011-07-15 11:09:50 -0700513
514 EXPECT_TRUE(addr.SetAddressFromString(kNewAddrIPV4Address));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700515 addr.set_prefix(kNewAddrIPV4AddressPrefix);
Paul Stewartdd7df792011-07-15 11:09:50 -0700516 TestParseAddress(ByteString(kNewAddrIPV4, sizeof(kNewAddrIPV4)),
Paul Stewart9a908082011-08-31 12:18:48 -0700517 RTNLMessage::kModeAdd,
Paul Stewartdd7df792011-07-15 11:09:50 -0700518 kNewAddrIPV4InterfaceIndex,
519 addr,
Paul Stewartdd7df792011-07-15 11:09:50 -0700520 kNewAddrIPV4Scope);
521}
522
Paul Stewart7355ce12011-09-02 10:47:01 -0700523TEST_F(RTNLMessageTest, DelAddrIPv6) {
524 IPAddress addr(IPAddress::kFamilyIPv6);
525
526 EXPECT_TRUE(addr.SetAddressFromString(kDelAddrIPV6Address));
527 addr.set_prefix(kDelAddrIPV6AddressPrefix);
528 TestParseAddress(ByteString(kDelAddrIPV6, sizeof(kDelAddrIPV6)),
529 RTNLMessage::kModeDelete,
530 kDelAddrIPV6InterfaceIndex,
531 addr,
532 kDelAddrIPV6Scope);
533}
534
Paul Stewartdd7df792011-07-15 11:09:50 -0700535TEST_F(RTNLMessageTest, DelRouteIPv6) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700536 IPAddress dst(IPAddress::kFamilyIPv6);
537 IPAddress src(IPAddress::kFamilyIPv6);
538 IPAddress gateway(IPAddress::kFamilyIPv6);
Paul Stewartdd7df792011-07-15 11:09:50 -0700539
540 EXPECT_TRUE(dst.SetAddressFromString(kDelRouteIPV6Address));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700541 dst.set_prefix(kDelRouteIPV6Prefix);
Paul Stewartdd7df792011-07-15 11:09:50 -0700542 src.SetAddressToDefault();
543 EXPECT_TRUE(gateway.SetAddressFromString(kDelRouteIPV6Address));
544
545 TestParseRoute(ByteString(kDelRouteIPV6, sizeof(kDelRouteIPV6)),
Paul Stewart9a908082011-08-31 12:18:48 -0700546 RTNLMessage::kModeDelete,
Paul Stewart7355ce12011-09-02 10:47:01 -0700547 IPAddress::kFamilyIPv6,
Paul Stewartdd7df792011-07-15 11:09:50 -0700548 kDelRouteIPV6InterfaceIndex,
549 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700550 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700551 gateway,
552 RT_TABLE_MAIN,
553 RTPROT_UNSPEC,
554 RT_SCOPE_UNIVERSE,
555 RTN_UNICAST,
556 kDelRouteIPV6Metric);
557}
558
559TEST_F(RTNLMessageTest, AddRouteIPv4) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700560 IPAddress dst(IPAddress::kFamilyIPv4);
561 IPAddress src(IPAddress::kFamilyIPv4);
562 IPAddress gateway(IPAddress::kFamilyIPv4);
Paul Stewartdd7df792011-07-15 11:09:50 -0700563
564 dst.SetAddressToDefault();
565 src.SetAddressToDefault();
566 EXPECT_TRUE(gateway.SetAddressFromString(kAddRouteIPV4Address));
567
568 TestParseRoute(ByteString(kAddRouteIPV4, sizeof(kAddRouteIPV4)),
Paul Stewart9a908082011-08-31 12:18:48 -0700569 RTNLMessage::kModeAdd,
Paul Stewart7355ce12011-09-02 10:47:01 -0700570 IPAddress::kFamilyIPv4,
Paul Stewartdd7df792011-07-15 11:09:50 -0700571 kAddRouteIPV4InterfaceIndex,
572 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700573 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700574 gateway,
575 RT_TABLE_MAIN,
576 RTPROT_BOOT,
577 RT_SCOPE_UNIVERSE,
578 RTN_UNICAST,
579 kAddRouteIPV4Metric);
580}
581
582TEST_F(RTNLMessageTest, AddRouteBusted) {
583 // RTNLMessage should list parse errors as kMessageUnknown
584 RTNLMessage msg;
585 EXPECT_FALSE(msg.Decode(
586 ByteString(kAddRouteBusted, sizeof(kAddRouteBusted))));
587}
588
589TEST_F(RTNLMessageTest, AddNeighbor) {
590 // RTNLMessage doesn't parse Add-Neighbor messages -- ensure this fails
591 RTNLMessage msg;
592 EXPECT_FALSE(msg.Decode(
593 ByteString(kAddNeighborMessage, sizeof(kAddNeighborMessage))));
594}
595
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800596TEST_F(RTNLMessageTest, EncodeRouteAdd) {
Paul Stewart9a908082011-08-31 12:18:48 -0700597 RTNLMessage msg(RTNLMessage::kTypeRoute,
598 RTNLMessage::kModeAdd,
Paul Stewartdd7df792011-07-15 11:09:50 -0700599 0, 1, 2, 0,
Paul Stewart7355ce12011-09-02 10:47:01 -0700600 IPAddress::kFamilyIPv4);
601 IPAddress dst(IPAddress::kFamilyIPv4);
602 IPAddress src(IPAddress::kFamilyIPv4);
603 IPAddress gateway(IPAddress::kFamilyIPv4);
Paul Stewartdd7df792011-07-15 11:09:50 -0700604
605 dst.SetAddressToDefault();
606 src.SetAddressToDefault();
607 EXPECT_TRUE(gateway.SetAddressFromString("192.168.0.1"));
608
609 msg.set_route_status(RTNLMessage::RouteStatus(
610 0, 0, RT_TABLE_MAIN, RTPROT_BOOT, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0));
611 msg.SetAttribute(RTA_DST, dst.address());
612 msg.SetAttribute(RTA_SRC, src.address());
613 msg.SetAttribute(RTA_GATEWAY, gateway.address());
614 msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(12));
615 msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(13));
616
617
618 TestParseRoute(msg.Encode(),
Paul Stewart9a908082011-08-31 12:18:48 -0700619 RTNLMessage::kModeAdd,
Paul Stewart7355ce12011-09-02 10:47:01 -0700620 IPAddress::kFamilyIPv4,
Paul Stewartdd7df792011-07-15 11:09:50 -0700621 12,
622 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700623 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700624 gateway,
625 RT_TABLE_MAIN,
626 RTPROT_BOOT,
627 RT_SCOPE_UNIVERSE,
628 RTN_UNICAST,
629 13);
630}
631
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800632TEST_F(RTNLMessageTest, EncodeLinkDel) {
633 const int kInterfaceIndex = 0x1234;
634 RTNLMessage pmsg(RTNLMessage::kTypeLink,
635 RTNLMessage::kModeDelete,
636 NLM_F_REQUEST,
637 0,
638 0,
639 kInterfaceIndex,
640 IPAddress::kFamilyUnknown);
641
642
643 RTNLMessage msg;
644 EXPECT_TRUE(msg.Decode(pmsg.Encode()));
645
646 EXPECT_EQ(RTNLMessage::kTypeLink, msg.type());
647 EXPECT_EQ(RTNLMessage::kModeDelete, msg.mode());
648 EXPECT_EQ(kInterfaceIndex, msg.interface_index());
649
650 RTNLMessage::LinkStatus status = msg.link_status();
651 EXPECT_EQ(0, status.flags);
652 EXPECT_EQ(0, status.change);
653
654 EXPECT_FALSE(msg.HasAttribute(IFLA_ADDRESS));
655 EXPECT_FALSE(msg.HasAttribute(IFLA_IFNAME));
656 EXPECT_FALSE(msg.HasAttribute(IFLA_MTU));
657 EXPECT_FALSE(msg.HasAttribute(IFLA_QDISC));
658 EXPECT_FALSE(msg.HasAttribute(IFLA_OPERSTATE));
659}
660
Paul Stewartdd7df792011-07-15 11:09:50 -0700661} // namespace shill