blob: 4ba5876a9ce0e4560e2709d7b10f3186ac01ee0a [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";
264const int kDelRouteIPV6Metric = 0;
265
266const unsigned char kDelRouteIPV6[] = {
267 0x80, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269 0x0a, 0x80, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x01,
270 0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
271 0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
272 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 0x00, 0x00, 0x00, 0x01, 0xff, 0xa0, 0x06, 0x88,
274 0x14, 0x00, 0x05, 0x00, 0xff, 0x02, 0x00, 0x00,
275 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
276 0xff, 0xa0, 0x06, 0x88, 0x08, 0x00, 0x04, 0x00,
277 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
278 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0c, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x5f, 0x0c, 0x00, 0x00,
280 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283};
284
285// default via 192.168.17.254 dev if12 metric 9
286
287const int kAddRouteIPV4InterfaceIndex = 12;
288const char kAddRouteIPV4Address[] = "192.168.17.254";
289const int kAddRouteIPV4Metric = 9;
290
291const unsigned char kAddRouteIPV4[] = {
292 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
295 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
296 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
297 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00,
298 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
299 0x0c, 0x00, 0x00, 0x00,
300};
301
302
303// This is the same as kAddRouteIPV4 above, except manually corrupted
304// the second to last parameter
305const unsigned char kAddRouteBusted[] = {
306 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
309 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
310 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
311 0x09, 0x00, 0x00, 0x00, 0x28, 0x00, 0x05, 0x00,
312 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
313 0x0c, 0x00, 0x00, 0x00,
314};
315
316// 192.168.10.1 dev if8 lladdr 00:14:d1:cd:d5:2c REACHABLE
317const unsigned char kAddNeighborMessage[] = {
318 0x4c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
321 0x02, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x00,
322 0xc0, 0xa8, 0x0a, 0x01, 0x0a, 0x00, 0x02, 0x00,
323 0x00, 0x14, 0xd1, 0xcd, 0xd5, 0x2c, 0x00, 0x00,
324 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
325 0x14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x04, 0x00, 0x00, 0x00,
328};
329
330} // namespace {}
331
332class RTNLMessageTest : public Test {
333 protected:
334 void TestParseLink(const ByteString &packet,
335 RTNLMessage::MessageMode mode,
336 int interface_index,
337 unsigned int flags,
338 unsigned int change,
339 ByteString address,
340 ByteString name,
341 uint32 mtu,
342 ByteString qdisc,
343 int oper_state) {
344 RTNLMessage msg;
345 EXPECT_TRUE(msg.Decode(packet));
346
347 EXPECT_EQ(RTNLMessage::kMessageTypeLink, msg.type());
348 EXPECT_EQ(mode, msg.mode());
349 EXPECT_EQ(interface_index, msg.interface_index());
350
351 RTNLMessage::LinkStatus status = msg.link_status();
352 EXPECT_EQ(flags, status.flags);
353 EXPECT_EQ(change, status.change);
354
355 EXPECT_TRUE(msg.HasAttribute(IFLA_ADDRESS));
356 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFLA_ADDRESS).GetLength());
357 EXPECT_TRUE(msg.GetAttribute(IFLA_ADDRESS).Equals(address));
358
359 EXPECT_TRUE(msg.HasAttribute(IFLA_IFNAME));
360 EXPECT_EQ(name.GetLength(), msg.GetAttribute(IFLA_IFNAME).GetLength());
361 EXPECT_TRUE(msg.GetAttribute(IFLA_IFNAME).Equals(name));
362
363 EXPECT_TRUE(msg.HasAttribute(IFLA_MTU));
364 uint32 mtu_val;
365 EXPECT_TRUE(msg.GetAttribute(IFLA_MTU).ConvertToCPUUInt32(&mtu_val));
366 EXPECT_EQ(mtu, mtu_val);
367
368 EXPECT_TRUE(msg.HasAttribute(IFLA_QDISC));
369 EXPECT_EQ(qdisc.GetLength(), msg.GetAttribute(IFLA_QDISC).GetLength());
370 EXPECT_TRUE(msg.GetAttribute(IFLA_QDISC).Equals(qdisc));
371
372 EXPECT_TRUE(msg.HasAttribute(IFLA_OPERSTATE));
373 EXPECT_EQ(oper_state, msg.GetAttribute(IFLA_OPERSTATE).GetConstData()[0]);
374 }
375
376 void TestParseAddress(const ByteString &packet,
377 RTNLMessage::MessageMode mode,
378 int interface_index,
379 const IPAddress &address,
380 int prefix,
381 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();
391 EXPECT_EQ(prefix, status.prefix_len);
392 EXPECT_EQ(scope, status.scope);
393
394 EXPECT_TRUE(msg.HasAttribute(IFA_LOCAL));
395 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFA_LOCAL).GetLength());
396 EXPECT_TRUE(
397 IPAddress(address.family(),
398 msg.GetAttribute(IFA_LOCAL)).Equals(address));
399 }
400
401 void TestParseRoute(const ByteString &packet,
402 RTNLMessage::MessageMode mode,
403 IPAddress::Family family,
404 int interface_index,
405 const IPAddress &dst,
406 int dst_prefix,
407 const IPAddress &src,
408 int src_prefix,
409 const IPAddress &gateway,
410 unsigned char table,
411 int protocol,
412 unsigned char scope,
413 unsigned char type,
414 int metric) {
415 RTNLMessage msg;
416
417 EXPECT_TRUE(msg.Decode(packet));
418 EXPECT_EQ(RTNLMessage::kMessageTypeRoute, msg.type());
419 EXPECT_EQ(0, msg.interface_index());
420 EXPECT_EQ(family, msg.family());
421
422 RTNLMessage::RouteStatus status = msg.route_status();
423 EXPECT_EQ(table, status.table);
424 EXPECT_EQ(protocol, status.protocol);
425 EXPECT_EQ(scope, status.scope);
426 EXPECT_EQ(type, status.type);
427
428 if (!dst.IsDefault()) {
429 EXPECT_TRUE(msg.HasAttribute(RTA_DST));
430 EXPECT_TRUE(IPAddress(family, msg.GetAttribute(RTA_DST)).Equals(dst));
431 }
432
433 if (!src.IsDefault()) {
434 EXPECT_TRUE(msg.HasAttribute(RTA_SRC));
435 EXPECT_TRUE(IPAddress(family, msg.GetAttribute(RTA_SRC)).Equals(src));
436 }
437
438 if (!gateway.IsDefault()) {
439 EXPECT_TRUE(msg.HasAttribute(RTA_GATEWAY));
440 EXPECT_TRUE(IPAddress(family,
441 msg.GetAttribute(RTA_GATEWAY)).Equals(gateway));
442 }
443
444 if (interface_index >= 0) {
445 EXPECT_TRUE(msg.HasAttribute(RTA_OIF));
446 uint32 int_val;
447 EXPECT_TRUE(msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&int_val));
448 EXPECT_EQ(interface_index, int_val);
449 } else {
450 EXPECT_FALSE(msg.HasAttribute(RTA_OIF));
451 }
452 if (metric >= 0) {
453 EXPECT_TRUE(msg.HasAttribute(RTA_PRIORITY));
454 uint32 metric_val;
455 EXPECT_TRUE(
456 msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric_val));
457 EXPECT_EQ(metric, metric_val);
458 } else {
459 EXPECT_FALSE(msg.HasAttribute(RTA_PRIORITY));
460 }
461 }
462};
463
464TEST_F(RTNLMessageTest, NewLinkWlan0) {
465 TestParseLink(ByteString(kNewLinkMessageWlan0, sizeof(kNewLinkMessageWlan0)),
466 RTNLMessage::kMessageModeAdd,
467 kNewLinkMessageWlan0InterfaceIndex,
468 kNewLinkMessageWlan0InterfaceFlags,
469 kNewLinkMessageWlan0InterfaceFlagsChange,
470 ByteString(kNewLinkMessageWlan0MACAddress, false),
471 ByteString(kNewLinkMessageWlan0InterfaceName, true),
472 kNewLinkMessageWlan0MTU,
473 ByteString(kNewLinkMessageWlan0Qdisc, true),
474 kNewLinkMessageWlan0OperState);
475}
476
477TEST_F(RTNLMessageTest, DelLinkEth0) {
478 TestParseLink(ByteString(kDelLinkMessageEth0, sizeof(kDelLinkMessageEth0)),
479 RTNLMessage::kMessageModeDelete,
480 kDelLinkMessageEth0InterfaceIndex,
481 kDelLinkMessageEth0InterfaceFlags,
482 kDelLinkMessageEth0InterfaceFlagsChange,
483 ByteString(kDelLinkMessageEth0MACAddress, false),
484 ByteString(kDelLinkMessageEth0InterfacName, true),
485 kDelLinkMessageEth0MTU,
486 ByteString(kDelLinkMessageEth0Qdisc, true),
487 kDelLinkMessageEth0OperState);
488}
489
490TEST_F(RTNLMessageTest, NewAddrIPv4) {
491 IPAddress addr(IPAddress::kAddressFamilyIPv4);
492
493 EXPECT_TRUE(addr.SetAddressFromString(kNewAddrIPV4Address));
494 TestParseAddress(ByteString(kNewAddrIPV4, sizeof(kNewAddrIPV4)),
495 RTNLMessage::kMessageModeAdd,
496 kNewAddrIPV4InterfaceIndex,
497 addr,
498 kNewAddrIPV4AddressPrefix,
499 kNewAddrIPV4Scope);
500}
501
502TEST_F(RTNLMessageTest, DelRouteIPv6) {
503 IPAddress dst(IPAddress::kAddressFamilyIPv6);
504 IPAddress src(IPAddress::kAddressFamilyIPv6);
505 IPAddress gateway(IPAddress::kAddressFamilyIPv6);
506
507 EXPECT_TRUE(dst.SetAddressFromString(kDelRouteIPV6Address));
508 src.SetAddressToDefault();
509 EXPECT_TRUE(gateway.SetAddressFromString(kDelRouteIPV6Address));
510
511 TestParseRoute(ByteString(kDelRouteIPV6, sizeof(kDelRouteIPV6)),
512 RTNLMessage::kMessageModeDelete,
513 IPAddress::kAddressFamilyIPv6,
514 kDelRouteIPV6InterfaceIndex,
515 dst,
516 0,
517 src,
518 0,
519 gateway,
520 RT_TABLE_MAIN,
521 RTPROT_UNSPEC,
522 RT_SCOPE_UNIVERSE,
523 RTN_UNICAST,
524 kDelRouteIPV6Metric);
525}
526
527TEST_F(RTNLMessageTest, AddRouteIPv4) {
528 IPAddress dst(IPAddress::kAddressFamilyIPv4);
529 IPAddress src(IPAddress::kAddressFamilyIPv4);
530 IPAddress gateway(IPAddress::kAddressFamilyIPv4);
531
532 dst.SetAddressToDefault();
533 src.SetAddressToDefault();
534 EXPECT_TRUE(gateway.SetAddressFromString(kAddRouteIPV4Address));
535
536 TestParseRoute(ByteString(kAddRouteIPV4, sizeof(kAddRouteIPV4)),
537 RTNLMessage::kMessageModeAdd,
538 IPAddress::kAddressFamilyIPv4,
539 kAddRouteIPV4InterfaceIndex,
540 dst,
541 0,
542 src,
543 0,
544 gateway,
545 RT_TABLE_MAIN,
546 RTPROT_BOOT,
547 RT_SCOPE_UNIVERSE,
548 RTN_UNICAST,
549 kAddRouteIPV4Metric);
550}
551
552TEST_F(RTNLMessageTest, AddRouteBusted) {
553 // RTNLMessage should list parse errors as kMessageUnknown
554 RTNLMessage msg;
555 EXPECT_FALSE(msg.Decode(
556 ByteString(kAddRouteBusted, sizeof(kAddRouteBusted))));
557}
558
559TEST_F(RTNLMessageTest, AddNeighbor) {
560 // RTNLMessage doesn't parse Add-Neighbor messages -- ensure this fails
561 RTNLMessage msg;
562 EXPECT_FALSE(msg.Decode(
563 ByteString(kAddNeighborMessage, sizeof(kAddNeighborMessage))));
564}
565
566TEST_F(RTNLMessageTest, Encode) {
567 RTNLMessage msg(RTNLMessage::kMessageTypeRoute,
568 RTNLMessage::kMessageModeAdd,
569 0, 1, 2, 0,
570 IPAddress::kAddressFamilyIPv4);
571 IPAddress dst(IPAddress::kAddressFamilyIPv4);
572 IPAddress src(IPAddress::kAddressFamilyIPv4);
573 IPAddress gateway(IPAddress::kAddressFamilyIPv4);
574
575 dst.SetAddressToDefault();
576 src.SetAddressToDefault();
577 EXPECT_TRUE(gateway.SetAddressFromString("192.168.0.1"));
578
579 msg.set_route_status(RTNLMessage::RouteStatus(
580 0, 0, RT_TABLE_MAIN, RTPROT_BOOT, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0));
581 msg.SetAttribute(RTA_DST, dst.address());
582 msg.SetAttribute(RTA_SRC, src.address());
583 msg.SetAttribute(RTA_GATEWAY, gateway.address());
584 msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(12));
585 msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(13));
586
587
588 TestParseRoute(msg.Encode(),
589 RTNLMessage::kMessageModeAdd,
590 IPAddress::kAddressFamilyIPv4,
591 12,
592 dst,
593 0,
594 src,
595 0,
596 gateway,
597 RT_TABLE_MAIN,
598 RTPROT_BOOT,
599 RT_SCOPE_UNIVERSE,
600 RTN_UNICAST,
601 13);
602}
603
604} // namespace shill