blob: cae6ae1ceef74c43758b35e4daa115c1bf3456d6 [file] [log] [blame]
Paul Stewartdd7df792011-07-15 11:09:50 -07001// Copyright (c) 2011 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#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
16using testing::Test;
17
18namespace shill {
19
20namespace {
21
22// These test strings were generated by cutting up the output of the
23// "rtmon" utility into individual strings. This tool outputs the raw
24// RTNL packet contents sent by the kernel. The comments above each
25// of these strings is the markup that the "ip monitor" command
26// outputs for each of these RTNL packets.
27
28
29// 2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP
30// link/ether c0:f8:da:05:03:0b brd ff:ff:ff:ff:ff:ff
31
32const int kNewLinkMessageWlan0InterfaceIndex = 2;
33const unsigned int kNewLinkMessageWlan0InterfaceFlags =
34 IFF_BROADCAST | IFF_MULTICAST | IFF_UP | IFF_LOWER_UP | IFF_RUNNING;
35const unsigned int kNewLinkMessageWlan0InterfaceFlagsChange = 0;
36const uint32 kNewLinkMessageWlan0MTU = 1500;
37const char kNewLinkMessageWlan0MACAddress[] = "\xc0\xf8\xda\x05\x03\x0b";
38const char kNewLinkMessageWlan0InterfaceName[] = "wlan0";
39const char kNewLinkMessageWlan0Qdisc[] = "mq";
40const int kNewLinkMessageWlan0OperState = IF_OPER_UP;
41
42const unsigned char kNewLinkMessageWlan0[] = {
43 0xe0, 0x03, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00,
44 0x53, 0x18, 0x1f, 0x4e, 0xac, 0x77, 0x00, 0x00,
45 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
46 0x43, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x0a, 0x00, 0x03, 0x00, 0x77, 0x6c, 0x61, 0x6e,
48 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
49 0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
50 0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
51 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
52 0xdc, 0x05, 0x00, 0x00, 0x07, 0x00, 0x06, 0x00,
53 0x6d, 0x71, 0x00, 0x00, 0x20, 0x00, 0x0e, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00,
58 0xc0, 0xf8, 0xda, 0x05, 0x03, 0x0b, 0x00, 0x00,
59 0x0a, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0x00, 0x00, 0x60, 0x00, 0x07, 0x00,
61 0xa6, 0x65, 0x0f, 0x00, 0xa6, 0x0b, 0x00, 0x00,
62 0xd2, 0x29, 0x61, 0x06, 0xd7, 0x25, 0x07, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 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, 0xbc, 0x00, 0x17, 0x00,
73 0xa6, 0x65, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0xa6, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0xd2, 0x29, 0x61, 0x06, 0x00, 0x00, 0x00, 0x00,
76 0xd7, 0x25, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 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 0x08, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x30, 0x02, 0x1a, 0x00, 0x6c, 0x00, 0x02, 0x00,
98 0x68, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
101 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
102 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00, 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 0xc0, 0x01, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00,
112 0x10, 0x00, 0x00, 0x80, 0x14, 0x00, 0x05, 0x00,
113 0xff, 0xff, 0x00, 0x00, 0x22, 0x05, 0x00, 0x00,
114 0x4f, 0x53, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00,
115 0x78, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
116 0x40, 0x00, 0x00, 0x00, 0xdc, 0x05, 0x00, 0x00,
117 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
118 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
119 0x03, 0x00, 0x00, 0x00, 0xa0, 0x0f, 0x00, 0x00,
120 0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 0x80, 0x3a, 0x09, 0x00, 0x80, 0x51, 0x01, 0x00,
122 0x05, 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0x00,
123 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 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 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0xfc, 0x00, 0x03, 0x00, 0x1f, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x15, 0xe6, 0x04, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x05, 0x00, 0x00, 0x00,
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
142 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x15, 0xe6, 0x04, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01,
156 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01,
158 0x00, 0x00, 0x00, 0x00, 0x18, 0x02, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00,
162 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167};
168
169
170// Deleted 8: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
171// link/ether 68:7f:74:ba:ef:c7 brd ff:ff:ff:ff:ff:ff
172
173const int kDelLinkMessageEth0InterfaceIndex = 8;
174const unsigned int kDelLinkMessageEth0InterfaceFlags =
175 IFF_BROADCAST | IFF_MULTICAST;
176const unsigned int kDelLinkMessageEth0InterfaceFlagsChange = 0xffffffff;
177const uint32 kDelLinkMessageEth0MTU = 1500;
178const char kDelLinkMessageEth0MACAddress[] = "\x68\x7f\x74\xba\xef\xc7";
179const char kDelLinkMessageEth0InterfacName[] = "eth0";
180const char kDelLinkMessageEth0Qdisc[] = "noop";
181const int kDelLinkMessageEth0OperState = IF_OPER_DOWN;
182
183const unsigned char kDelLinkMessageEth0[] = {
184 0xb8, 0x01, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
185 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
187 0x02, 0x10, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
188 0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30,
189 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
190 0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
191 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
193 0xdc, 0x05, 0x00, 0x00, 0x09, 0x00, 0x06, 0x00,
194 0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00,
195 0x20, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
196 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 0x0a, 0x00, 0x01, 0x00, 0x68, 0x7f, 0x74, 0xba,
200 0xef, 0xc7, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00,
201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
202 0x60, 0x00, 0x07, 0x00, 0x09, 0x00, 0x00, 0x00,
203 0x0e, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00,
204 0x34, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 0x00, 0x00, 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 0xbc, 0x00, 0x17, 0x00, 0x09, 0x00, 0x00, 0x00,
215 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x34, 0x06, 0x00, 0x00,
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x08, 0x00, 0x15, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x1a, 0x00,
239};
240
241// 8: if8 inet 192.168.10.100/24 brd 192.168.10.255 scope global eth0
242
243const int kNewAddrIPV4InterfaceIndex = 8;
244const char kNewAddrIPV4Address[] = "192.168.10.100";
245const int kNewAddrIPV4AddressPrefix = 24;
246const unsigned char kNewAddrIPV4Scope = RT_SCOPE_UNIVERSE;
247
248const unsigned char kNewAddrIPV4[] = {
249 0x3c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
250 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x02, 0x18, 0x80, 0x00, 0x08, 0x00, 0x00, 0x00,
252 0x08, 0x00, 0x01, 0x00, 0xc0, 0xa8, 0x0a, 0x64,
253 0x08, 0x00, 0x02, 0x00, 0xc0, 0xa8, 0x0a, 0x64,
254 0x08, 0x00, 0x04, 0x00, 0xc0, 0xa8, 0x0a, 0xff,
255 0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30,
256 0x00, 0x00, 0x00, 0x00,
257};
258
259
260// Deleted ff02::1:ffa0:688 via ff02::1:ffa0:688 dev if2 metric 0
261
262const int kDelRouteIPV6InterfaceIndex = 2;
263const char kDelRouteIPV6Address[] = "ff02::1:ffa0:688";
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700264const int kDelRouteIPV6Prefix = 128;
Paul Stewartdd7df792011-07-15 11:09:50 -0700265const int kDelRouteIPV6Metric = 0;
266
267const unsigned char kDelRouteIPV6[] = {
268 0x80, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
269 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
270 0x0a, 0x80, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x01,
271 0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
272 0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
273 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
274 0x00, 0x00, 0x00, 0x01, 0xff, 0xa0, 0x06, 0x88,
275 0x14, 0x00, 0x05, 0x00, 0xff, 0x02, 0x00, 0x00,
276 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
277 0xff, 0xa0, 0x06, 0x88, 0x08, 0x00, 0x04, 0x00,
278 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0c, 0x00,
280 0x00, 0x00, 0x00, 0x00, 0x5f, 0x0c, 0x00, 0x00,
281 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
282 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284};
285
286// default via 192.168.17.254 dev if12 metric 9
287
288const int kAddRouteIPV4InterfaceIndex = 12;
289const char kAddRouteIPV4Address[] = "192.168.17.254";
290const int kAddRouteIPV4Metric = 9;
291
292const unsigned char kAddRouteIPV4[] = {
293 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
296 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
297 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
298 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00,
299 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
300 0x0c, 0x00, 0x00, 0x00,
301};
302
303
304// This is the same as kAddRouteIPV4 above, except manually corrupted
305// the second to last parameter
306const unsigned char kAddRouteBusted[] = {
307 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
310 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
311 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
312 0x09, 0x00, 0x00, 0x00, 0x28, 0x00, 0x05, 0x00,
313 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
314 0x0c, 0x00, 0x00, 0x00,
315};
316
317// 192.168.10.1 dev if8 lladdr 00:14:d1:cd:d5:2c REACHABLE
318const unsigned char kAddNeighborMessage[] = {
319 0x4c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
321 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
322 0x02, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x00,
323 0xc0, 0xa8, 0x0a, 0x01, 0x0a, 0x00, 0x02, 0x00,
324 0x00, 0x14, 0xd1, 0xcd, 0xd5, 0x2c, 0x00, 0x00,
325 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
326 0x14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328 0x04, 0x00, 0x00, 0x00,
329};
330
331} // namespace {}
332
333class RTNLMessageTest : public Test {
334 protected:
335 void TestParseLink(const ByteString &packet,
336 RTNLMessage::MessageMode mode,
337 int interface_index,
338 unsigned int flags,
339 unsigned int change,
340 ByteString address,
341 ByteString name,
342 uint32 mtu,
343 ByteString qdisc,
344 int oper_state) {
345 RTNLMessage msg;
346 EXPECT_TRUE(msg.Decode(packet));
347
348 EXPECT_EQ(RTNLMessage::kMessageTypeLink, msg.type());
349 EXPECT_EQ(mode, msg.mode());
350 EXPECT_EQ(interface_index, msg.interface_index());
351
352 RTNLMessage::LinkStatus status = msg.link_status();
353 EXPECT_EQ(flags, status.flags);
354 EXPECT_EQ(change, status.change);
355
356 EXPECT_TRUE(msg.HasAttribute(IFLA_ADDRESS));
357 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFLA_ADDRESS).GetLength());
358 EXPECT_TRUE(msg.GetAttribute(IFLA_ADDRESS).Equals(address));
359
360 EXPECT_TRUE(msg.HasAttribute(IFLA_IFNAME));
361 EXPECT_EQ(name.GetLength(), msg.GetAttribute(IFLA_IFNAME).GetLength());
362 EXPECT_TRUE(msg.GetAttribute(IFLA_IFNAME).Equals(name));
363
364 EXPECT_TRUE(msg.HasAttribute(IFLA_MTU));
365 uint32 mtu_val;
366 EXPECT_TRUE(msg.GetAttribute(IFLA_MTU).ConvertToCPUUInt32(&mtu_val));
367 EXPECT_EQ(mtu, mtu_val);
368
369 EXPECT_TRUE(msg.HasAttribute(IFLA_QDISC));
370 EXPECT_EQ(qdisc.GetLength(), msg.GetAttribute(IFLA_QDISC).GetLength());
371 EXPECT_TRUE(msg.GetAttribute(IFLA_QDISC).Equals(qdisc));
372
373 EXPECT_TRUE(msg.HasAttribute(IFLA_OPERSTATE));
374 EXPECT_EQ(oper_state, msg.GetAttribute(IFLA_OPERSTATE).GetConstData()[0]);
375 }
376
377 void TestParseAddress(const ByteString &packet,
378 RTNLMessage::MessageMode mode,
379 int interface_index,
380 const IPAddress &address,
Paul Stewartdd7df792011-07-15 11:09:50 -0700381 unsigned char scope) {
382 RTNLMessage msg;
383
384 EXPECT_TRUE(msg.Decode(packet));
385 EXPECT_EQ(RTNLMessage::kMessageTypeAddress, msg.type());
386 EXPECT_EQ(mode, msg.mode());
387 EXPECT_EQ(interface_index, msg.interface_index());
388 EXPECT_EQ(address.family(), msg.family());
389
390 RTNLMessage::AddressStatus status = msg.address_status();
Paul Stewartdd7df792011-07-15 11:09:50 -0700391 EXPECT_EQ(scope, status.scope);
392
393 EXPECT_TRUE(msg.HasAttribute(IFA_LOCAL));
394 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFA_LOCAL).GetLength());
395 EXPECT_TRUE(
396 IPAddress(address.family(),
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700397 msg.GetAttribute(IFA_LOCAL),
398 status.prefix_len).Equals(address));
Paul Stewartdd7df792011-07-15 11:09:50 -0700399 }
400
401 void TestParseRoute(const ByteString &packet,
402 RTNLMessage::MessageMode mode,
403 IPAddress::Family family,
404 int interface_index,
405 const IPAddress &dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700406 const IPAddress &src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700407 const IPAddress &gateway,
408 unsigned char table,
409 int protocol,
410 unsigned char scope,
411 unsigned char type,
412 int metric) {
413 RTNLMessage msg;
414
415 EXPECT_TRUE(msg.Decode(packet));
416 EXPECT_EQ(RTNLMessage::kMessageTypeRoute, msg.type());
417 EXPECT_EQ(0, msg.interface_index());
418 EXPECT_EQ(family, msg.family());
419
420 RTNLMessage::RouteStatus status = msg.route_status();
421 EXPECT_EQ(table, status.table);
422 EXPECT_EQ(protocol, status.protocol);
423 EXPECT_EQ(scope, status.scope);
424 EXPECT_EQ(type, status.type);
425
426 if (!dst.IsDefault()) {
427 EXPECT_TRUE(msg.HasAttribute(RTA_DST));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700428 EXPECT_TRUE(IPAddress(family,
429 msg.GetAttribute(RTA_DST),
430 status.dst_prefix).Equals(dst));
Paul Stewartdd7df792011-07-15 11:09:50 -0700431 }
432
433 if (!src.IsDefault()) {
434 EXPECT_TRUE(msg.HasAttribute(RTA_SRC));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700435 EXPECT_TRUE(IPAddress(family,
436 msg.GetAttribute(RTA_SRC),
437 status.src_prefix).Equals(src));
Paul Stewartdd7df792011-07-15 11:09:50 -0700438 }
439
440 if (!gateway.IsDefault()) {
441 EXPECT_TRUE(msg.HasAttribute(RTA_GATEWAY));
442 EXPECT_TRUE(IPAddress(family,
443 msg.GetAttribute(RTA_GATEWAY)).Equals(gateway));
444 }
445
446 if (interface_index >= 0) {
447 EXPECT_TRUE(msg.HasAttribute(RTA_OIF));
448 uint32 int_val;
449 EXPECT_TRUE(msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&int_val));
450 EXPECT_EQ(interface_index, int_val);
451 } else {
452 EXPECT_FALSE(msg.HasAttribute(RTA_OIF));
453 }
454 if (metric >= 0) {
455 EXPECT_TRUE(msg.HasAttribute(RTA_PRIORITY));
456 uint32 metric_val;
457 EXPECT_TRUE(
458 msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric_val));
459 EXPECT_EQ(metric, metric_val);
460 } else {
461 EXPECT_FALSE(msg.HasAttribute(RTA_PRIORITY));
462 }
463 }
464};
465
466TEST_F(RTNLMessageTest, NewLinkWlan0) {
467 TestParseLink(ByteString(kNewLinkMessageWlan0, sizeof(kNewLinkMessageWlan0)),
468 RTNLMessage::kMessageModeAdd,
469 kNewLinkMessageWlan0InterfaceIndex,
470 kNewLinkMessageWlan0InterfaceFlags,
471 kNewLinkMessageWlan0InterfaceFlagsChange,
472 ByteString(kNewLinkMessageWlan0MACAddress, false),
473 ByteString(kNewLinkMessageWlan0InterfaceName, true),
474 kNewLinkMessageWlan0MTU,
475 ByteString(kNewLinkMessageWlan0Qdisc, true),
476 kNewLinkMessageWlan0OperState);
477}
478
479TEST_F(RTNLMessageTest, DelLinkEth0) {
480 TestParseLink(ByteString(kDelLinkMessageEth0, sizeof(kDelLinkMessageEth0)),
481 RTNLMessage::kMessageModeDelete,
482 kDelLinkMessageEth0InterfaceIndex,
483 kDelLinkMessageEth0InterfaceFlags,
484 kDelLinkMessageEth0InterfaceFlagsChange,
485 ByteString(kDelLinkMessageEth0MACAddress, false),
486 ByteString(kDelLinkMessageEth0InterfacName, true),
487 kDelLinkMessageEth0MTU,
488 ByteString(kDelLinkMessageEth0Qdisc, true),
489 kDelLinkMessageEth0OperState);
490}
491
492TEST_F(RTNLMessageTest, NewAddrIPv4) {
493 IPAddress addr(IPAddress::kAddressFamilyIPv4);
494
495 EXPECT_TRUE(addr.SetAddressFromString(kNewAddrIPV4Address));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700496 addr.set_prefix(kNewAddrIPV4AddressPrefix);
Paul Stewartdd7df792011-07-15 11:09:50 -0700497 TestParseAddress(ByteString(kNewAddrIPV4, sizeof(kNewAddrIPV4)),
498 RTNLMessage::kMessageModeAdd,
499 kNewAddrIPV4InterfaceIndex,
500 addr,
Paul Stewartdd7df792011-07-15 11:09:50 -0700501 kNewAddrIPV4Scope);
502}
503
504TEST_F(RTNLMessageTest, DelRouteIPv6) {
505 IPAddress dst(IPAddress::kAddressFamilyIPv6);
506 IPAddress src(IPAddress::kAddressFamilyIPv6);
507 IPAddress gateway(IPAddress::kAddressFamilyIPv6);
508
509 EXPECT_TRUE(dst.SetAddressFromString(kDelRouteIPV6Address));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700510 dst.set_prefix(kDelRouteIPV6Prefix);
Paul Stewartdd7df792011-07-15 11:09:50 -0700511 src.SetAddressToDefault();
512 EXPECT_TRUE(gateway.SetAddressFromString(kDelRouteIPV6Address));
513
514 TestParseRoute(ByteString(kDelRouteIPV6, sizeof(kDelRouteIPV6)),
515 RTNLMessage::kMessageModeDelete,
516 IPAddress::kAddressFamilyIPv6,
517 kDelRouteIPV6InterfaceIndex,
518 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700519 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700520 gateway,
521 RT_TABLE_MAIN,
522 RTPROT_UNSPEC,
523 RT_SCOPE_UNIVERSE,
524 RTN_UNICAST,
525 kDelRouteIPV6Metric);
526}
527
528TEST_F(RTNLMessageTest, AddRouteIPv4) {
529 IPAddress dst(IPAddress::kAddressFamilyIPv4);
530 IPAddress src(IPAddress::kAddressFamilyIPv4);
531 IPAddress gateway(IPAddress::kAddressFamilyIPv4);
532
533 dst.SetAddressToDefault();
534 src.SetAddressToDefault();
535 EXPECT_TRUE(gateway.SetAddressFromString(kAddRouteIPV4Address));
536
537 TestParseRoute(ByteString(kAddRouteIPV4, sizeof(kAddRouteIPV4)),
538 RTNLMessage::kMessageModeAdd,
539 IPAddress::kAddressFamilyIPv4,
540 kAddRouteIPV4InterfaceIndex,
541 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700542 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700543 gateway,
544 RT_TABLE_MAIN,
545 RTPROT_BOOT,
546 RT_SCOPE_UNIVERSE,
547 RTN_UNICAST,
548 kAddRouteIPV4Metric);
549}
550
551TEST_F(RTNLMessageTest, AddRouteBusted) {
552 // RTNLMessage should list parse errors as kMessageUnknown
553 RTNLMessage msg;
554 EXPECT_FALSE(msg.Decode(
555 ByteString(kAddRouteBusted, sizeof(kAddRouteBusted))));
556}
557
558TEST_F(RTNLMessageTest, AddNeighbor) {
559 // RTNLMessage doesn't parse Add-Neighbor messages -- ensure this fails
560 RTNLMessage msg;
561 EXPECT_FALSE(msg.Decode(
562 ByteString(kAddNeighborMessage, sizeof(kAddNeighborMessage))));
563}
564
565TEST_F(RTNLMessageTest, Encode) {
566 RTNLMessage msg(RTNLMessage::kMessageTypeRoute,
567 RTNLMessage::kMessageModeAdd,
568 0, 1, 2, 0,
569 IPAddress::kAddressFamilyIPv4);
570 IPAddress dst(IPAddress::kAddressFamilyIPv4);
571 IPAddress src(IPAddress::kAddressFamilyIPv4);
572 IPAddress gateway(IPAddress::kAddressFamilyIPv4);
573
574 dst.SetAddressToDefault();
575 src.SetAddressToDefault();
576 EXPECT_TRUE(gateway.SetAddressFromString("192.168.0.1"));
577
578 msg.set_route_status(RTNLMessage::RouteStatus(
579 0, 0, RT_TABLE_MAIN, RTPROT_BOOT, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0));
580 msg.SetAttribute(RTA_DST, dst.address());
581 msg.SetAttribute(RTA_SRC, src.address());
582 msg.SetAttribute(RTA_GATEWAY, gateway.address());
583 msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(12));
584 msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(13));
585
586
587 TestParseRoute(msg.Encode(),
588 RTNLMessage::kMessageModeAdd,
589 IPAddress::kAddressFamilyIPv4,
590 12,
591 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700592 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700593 gateway,
594 RT_TABLE_MAIN,
595 RTPROT_BOOT,
596 RT_SCOPE_UNIVERSE,
597 RTN_UNICAST,
598 13);
599}
600
601} // namespace shill