blob: 461458f5a7d43ce2043dd66a2d4fedf0de2176a6 [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
Paul Stewart7355ce12011-09-02 10:47:01 -0700259// Deleted 15: if15 inet6 fe80::6a7f:74ff:feba:efc7/64 scope link
260// valid_lft forever preferred_lft forever
261
262const int kDelAddrIPV6InterfaceIndex = 15;
263const char kDelAddrIPV6Address[] = "fe80::6a7f:74ff:feba:efc7";
264const int kDelAddrIPV6AddressPrefix = 64;
265const unsigned char kDelAddrIPV6Scope = RT_SCOPE_LINK;
266
267const unsigned char kDelAddrIPV6[] = {
268 0x40, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
269 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
270 0x0a, 0x40, 0x80, 0xfd, 0x0f, 0x00, 0x00, 0x00,
271 0x14, 0x00, 0x01, 0x00, 0xfe, 0x80, 0x00, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x6a, 0x7f, 0x74, 0xff,
273 0xfe, 0xba, 0xef, 0xc7, 0x14, 0x00, 0x06, 0x00,
274 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
275 0xbf, 0xdb, 0x02, 0x00, 0xbf, 0xdb, 0x02, 0x00,
276};
Paul Stewartdd7df792011-07-15 11:09:50 -0700277
278// Deleted ff02::1:ffa0:688 via ff02::1:ffa0:688 dev if2 metric 0
279
280const int kDelRouteIPV6InterfaceIndex = 2;
281const char kDelRouteIPV6Address[] = "ff02::1:ffa0:688";
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700282const int kDelRouteIPV6Prefix = 128;
Paul Stewartdd7df792011-07-15 11:09:50 -0700283const int kDelRouteIPV6Metric = 0;
284
285const unsigned char kDelRouteIPV6[] = {
286 0x80, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
287 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
288 0x0a, 0x80, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x01,
289 0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
290 0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
291 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x01, 0xff, 0xa0, 0x06, 0x88,
293 0x14, 0x00, 0x05, 0x00, 0xff, 0x02, 0x00, 0x00,
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
295 0xff, 0xa0, 0x06, 0x88, 0x08, 0x00, 0x04, 0x00,
296 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
297 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0c, 0x00,
298 0x00, 0x00, 0x00, 0x00, 0x5f, 0x0c, 0x00, 0x00,
299 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302};
303
304// default via 192.168.17.254 dev if12 metric 9
305
306const int kAddRouteIPV4InterfaceIndex = 12;
307const char kAddRouteIPV4Address[] = "192.168.17.254";
308const int kAddRouteIPV4Metric = 9;
309
310const unsigned char kAddRouteIPV4[] = {
311 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
313 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
314 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
315 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
316 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00,
317 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
318 0x0c, 0x00, 0x00, 0x00,
319};
320
321
322// This is the same as kAddRouteIPV4 above, except manually corrupted
323// the second to last parameter
324const unsigned char kAddRouteBusted[] = {
325 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
328 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
329 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
330 0x09, 0x00, 0x00, 0x00, 0x28, 0x00, 0x05, 0x00,
331 0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
332 0x0c, 0x00, 0x00, 0x00,
333};
334
335// 192.168.10.1 dev if8 lladdr 00:14:d1:cd:d5:2c REACHABLE
336const unsigned char kAddNeighborMessage[] = {
337 0x4c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
340 0x02, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x00,
341 0xc0, 0xa8, 0x0a, 0x01, 0x0a, 0x00, 0x02, 0x00,
342 0x00, 0x14, 0xd1, 0xcd, 0xd5, 0x2c, 0x00, 0x00,
343 0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
344 0x14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 0x04, 0x00, 0x00, 0x00,
347};
348
349} // namespace {}
350
351class RTNLMessageTest : public Test {
352 protected:
353 void TestParseLink(const ByteString &packet,
Paul Stewart9a908082011-08-31 12:18:48 -0700354 RTNLMessage::Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700355 int interface_index,
356 unsigned int flags,
357 unsigned int change,
358 ByteString address,
359 ByteString name,
360 uint32 mtu,
361 ByteString qdisc,
362 int oper_state) {
363 RTNLMessage msg;
364 EXPECT_TRUE(msg.Decode(packet));
365
Paul Stewart9a908082011-08-31 12:18:48 -0700366 EXPECT_EQ(RTNLMessage::kTypeLink, msg.type());
Paul Stewartdd7df792011-07-15 11:09:50 -0700367 EXPECT_EQ(mode, msg.mode());
368 EXPECT_EQ(interface_index, msg.interface_index());
369
370 RTNLMessage::LinkStatus status = msg.link_status();
371 EXPECT_EQ(flags, status.flags);
372 EXPECT_EQ(change, status.change);
373
374 EXPECT_TRUE(msg.HasAttribute(IFLA_ADDRESS));
375 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFLA_ADDRESS).GetLength());
376 EXPECT_TRUE(msg.GetAttribute(IFLA_ADDRESS).Equals(address));
377
378 EXPECT_TRUE(msg.HasAttribute(IFLA_IFNAME));
379 EXPECT_EQ(name.GetLength(), msg.GetAttribute(IFLA_IFNAME).GetLength());
380 EXPECT_TRUE(msg.GetAttribute(IFLA_IFNAME).Equals(name));
381
382 EXPECT_TRUE(msg.HasAttribute(IFLA_MTU));
383 uint32 mtu_val;
384 EXPECT_TRUE(msg.GetAttribute(IFLA_MTU).ConvertToCPUUInt32(&mtu_val));
385 EXPECT_EQ(mtu, mtu_val);
386
387 EXPECT_TRUE(msg.HasAttribute(IFLA_QDISC));
388 EXPECT_EQ(qdisc.GetLength(), msg.GetAttribute(IFLA_QDISC).GetLength());
389 EXPECT_TRUE(msg.GetAttribute(IFLA_QDISC).Equals(qdisc));
390
391 EXPECT_TRUE(msg.HasAttribute(IFLA_OPERSTATE));
392 EXPECT_EQ(oper_state, msg.GetAttribute(IFLA_OPERSTATE).GetConstData()[0]);
393 }
394
395 void TestParseAddress(const ByteString &packet,
Paul Stewart9a908082011-08-31 12:18:48 -0700396 RTNLMessage::Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700397 int interface_index,
398 const IPAddress &address,
Paul Stewartdd7df792011-07-15 11:09:50 -0700399 unsigned char scope) {
400 RTNLMessage msg;
401
402 EXPECT_TRUE(msg.Decode(packet));
Paul Stewart9a908082011-08-31 12:18:48 -0700403 EXPECT_EQ(RTNLMessage::kTypeAddress, msg.type());
Paul Stewartdd7df792011-07-15 11:09:50 -0700404 EXPECT_EQ(mode, msg.mode());
405 EXPECT_EQ(interface_index, msg.interface_index());
406 EXPECT_EQ(address.family(), msg.family());
407
408 RTNLMessage::AddressStatus status = msg.address_status();
Paul Stewartdd7df792011-07-15 11:09:50 -0700409 EXPECT_EQ(scope, status.scope);
410
Paul Stewart7355ce12011-09-02 10:47:01 -0700411 EXPECT_TRUE(msg.HasAttribute(IFA_ADDRESS));
412 EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFA_ADDRESS).GetLength());
Paul Stewartdd7df792011-07-15 11:09:50 -0700413 EXPECT_TRUE(
414 IPAddress(address.family(),
Paul Stewart7355ce12011-09-02 10:47:01 -0700415 msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700416 status.prefix_len).Equals(address));
Paul Stewartdd7df792011-07-15 11:09:50 -0700417 }
418
419 void TestParseRoute(const ByteString &packet,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700420 RTNLMessage::Mode /*mode*/,
Paul Stewartdd7df792011-07-15 11:09:50 -0700421 IPAddress::Family family,
422 int interface_index,
423 const IPAddress &dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700424 const IPAddress &src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700425 const IPAddress &gateway,
426 unsigned char table,
427 int protocol,
428 unsigned char scope,
429 unsigned char type,
430 int metric) {
431 RTNLMessage msg;
432
433 EXPECT_TRUE(msg.Decode(packet));
Paul Stewart9a908082011-08-31 12:18:48 -0700434 EXPECT_EQ(RTNLMessage::kTypeRoute, msg.type());
Paul Stewartdd7df792011-07-15 11:09:50 -0700435 EXPECT_EQ(0, msg.interface_index());
436 EXPECT_EQ(family, msg.family());
437
438 RTNLMessage::RouteStatus status = msg.route_status();
439 EXPECT_EQ(table, status.table);
440 EXPECT_EQ(protocol, status.protocol);
441 EXPECT_EQ(scope, status.scope);
442 EXPECT_EQ(type, status.type);
443
444 if (!dst.IsDefault()) {
445 EXPECT_TRUE(msg.HasAttribute(RTA_DST));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700446 EXPECT_TRUE(IPAddress(family,
447 msg.GetAttribute(RTA_DST),
448 status.dst_prefix).Equals(dst));
Paul Stewartdd7df792011-07-15 11:09:50 -0700449 }
450
451 if (!src.IsDefault()) {
452 EXPECT_TRUE(msg.HasAttribute(RTA_SRC));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700453 EXPECT_TRUE(IPAddress(family,
454 msg.GetAttribute(RTA_SRC),
455 status.src_prefix).Equals(src));
Paul Stewartdd7df792011-07-15 11:09:50 -0700456 }
457
458 if (!gateway.IsDefault()) {
459 EXPECT_TRUE(msg.HasAttribute(RTA_GATEWAY));
460 EXPECT_TRUE(IPAddress(family,
461 msg.GetAttribute(RTA_GATEWAY)).Equals(gateway));
462 }
463
464 if (interface_index >= 0) {
465 EXPECT_TRUE(msg.HasAttribute(RTA_OIF));
466 uint32 int_val;
467 EXPECT_TRUE(msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&int_val));
468 EXPECT_EQ(interface_index, int_val);
469 } else {
470 EXPECT_FALSE(msg.HasAttribute(RTA_OIF));
471 }
472 if (metric >= 0) {
473 EXPECT_TRUE(msg.HasAttribute(RTA_PRIORITY));
474 uint32 metric_val;
475 EXPECT_TRUE(
476 msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric_val));
477 EXPECT_EQ(metric, metric_val);
478 } else {
479 EXPECT_FALSE(msg.HasAttribute(RTA_PRIORITY));
480 }
481 }
482};
483
484TEST_F(RTNLMessageTest, NewLinkWlan0) {
485 TestParseLink(ByteString(kNewLinkMessageWlan0, sizeof(kNewLinkMessageWlan0)),
Paul Stewart9a908082011-08-31 12:18:48 -0700486 RTNLMessage::kModeAdd,
Paul Stewartdd7df792011-07-15 11:09:50 -0700487 kNewLinkMessageWlan0InterfaceIndex,
488 kNewLinkMessageWlan0InterfaceFlags,
489 kNewLinkMessageWlan0InterfaceFlagsChange,
490 ByteString(kNewLinkMessageWlan0MACAddress, false),
491 ByteString(kNewLinkMessageWlan0InterfaceName, true),
492 kNewLinkMessageWlan0MTU,
493 ByteString(kNewLinkMessageWlan0Qdisc, true),
494 kNewLinkMessageWlan0OperState);
495}
496
497TEST_F(RTNLMessageTest, DelLinkEth0) {
498 TestParseLink(ByteString(kDelLinkMessageEth0, sizeof(kDelLinkMessageEth0)),
Paul Stewart9a908082011-08-31 12:18:48 -0700499 RTNLMessage::kModeDelete,
Paul Stewartdd7df792011-07-15 11:09:50 -0700500 kDelLinkMessageEth0InterfaceIndex,
501 kDelLinkMessageEth0InterfaceFlags,
502 kDelLinkMessageEth0InterfaceFlagsChange,
503 ByteString(kDelLinkMessageEth0MACAddress, false),
504 ByteString(kDelLinkMessageEth0InterfacName, true),
505 kDelLinkMessageEth0MTU,
506 ByteString(kDelLinkMessageEth0Qdisc, true),
507 kDelLinkMessageEth0OperState);
508}
509
510TEST_F(RTNLMessageTest, NewAddrIPv4) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700511 IPAddress addr(IPAddress::kFamilyIPv4);
Paul Stewartdd7df792011-07-15 11:09:50 -0700512
513 EXPECT_TRUE(addr.SetAddressFromString(kNewAddrIPV4Address));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700514 addr.set_prefix(kNewAddrIPV4AddressPrefix);
Paul Stewartdd7df792011-07-15 11:09:50 -0700515 TestParseAddress(ByteString(kNewAddrIPV4, sizeof(kNewAddrIPV4)),
Paul Stewart9a908082011-08-31 12:18:48 -0700516 RTNLMessage::kModeAdd,
Paul Stewartdd7df792011-07-15 11:09:50 -0700517 kNewAddrIPV4InterfaceIndex,
518 addr,
Paul Stewartdd7df792011-07-15 11:09:50 -0700519 kNewAddrIPV4Scope);
520}
521
Paul Stewart7355ce12011-09-02 10:47:01 -0700522TEST_F(RTNLMessageTest, DelAddrIPv6) {
523 IPAddress addr(IPAddress::kFamilyIPv6);
524
525 EXPECT_TRUE(addr.SetAddressFromString(kDelAddrIPV6Address));
526 addr.set_prefix(kDelAddrIPV6AddressPrefix);
527 TestParseAddress(ByteString(kDelAddrIPV6, sizeof(kDelAddrIPV6)),
528 RTNLMessage::kModeDelete,
529 kDelAddrIPV6InterfaceIndex,
530 addr,
531 kDelAddrIPV6Scope);
532}
533
Paul Stewartdd7df792011-07-15 11:09:50 -0700534TEST_F(RTNLMessageTest, DelRouteIPv6) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700535 IPAddress dst(IPAddress::kFamilyIPv6);
536 IPAddress src(IPAddress::kFamilyIPv6);
537 IPAddress gateway(IPAddress::kFamilyIPv6);
Paul Stewartdd7df792011-07-15 11:09:50 -0700538
539 EXPECT_TRUE(dst.SetAddressFromString(kDelRouteIPV6Address));
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700540 dst.set_prefix(kDelRouteIPV6Prefix);
Paul Stewartdd7df792011-07-15 11:09:50 -0700541 src.SetAddressToDefault();
542 EXPECT_TRUE(gateway.SetAddressFromString(kDelRouteIPV6Address));
543
544 TestParseRoute(ByteString(kDelRouteIPV6, sizeof(kDelRouteIPV6)),
Paul Stewart9a908082011-08-31 12:18:48 -0700545 RTNLMessage::kModeDelete,
Paul Stewart7355ce12011-09-02 10:47:01 -0700546 IPAddress::kFamilyIPv6,
Paul Stewartdd7df792011-07-15 11:09:50 -0700547 kDelRouteIPV6InterfaceIndex,
548 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700549 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700550 gateway,
551 RT_TABLE_MAIN,
552 RTPROT_UNSPEC,
553 RT_SCOPE_UNIVERSE,
554 RTN_UNICAST,
555 kDelRouteIPV6Metric);
556}
557
558TEST_F(RTNLMessageTest, AddRouteIPv4) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700559 IPAddress dst(IPAddress::kFamilyIPv4);
560 IPAddress src(IPAddress::kFamilyIPv4);
561 IPAddress gateway(IPAddress::kFamilyIPv4);
Paul Stewartdd7df792011-07-15 11:09:50 -0700562
563 dst.SetAddressToDefault();
564 src.SetAddressToDefault();
565 EXPECT_TRUE(gateway.SetAddressFromString(kAddRouteIPV4Address));
566
567 TestParseRoute(ByteString(kAddRouteIPV4, sizeof(kAddRouteIPV4)),
Paul Stewart9a908082011-08-31 12:18:48 -0700568 RTNLMessage::kModeAdd,
Paul Stewart7355ce12011-09-02 10:47:01 -0700569 IPAddress::kFamilyIPv4,
Paul Stewartdd7df792011-07-15 11:09:50 -0700570 kAddRouteIPV4InterfaceIndex,
571 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700572 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700573 gateway,
574 RT_TABLE_MAIN,
575 RTPROT_BOOT,
576 RT_SCOPE_UNIVERSE,
577 RTN_UNICAST,
578 kAddRouteIPV4Metric);
579}
580
581TEST_F(RTNLMessageTest, AddRouteBusted) {
582 // RTNLMessage should list parse errors as kMessageUnknown
583 RTNLMessage msg;
584 EXPECT_FALSE(msg.Decode(
585 ByteString(kAddRouteBusted, sizeof(kAddRouteBusted))));
586}
587
588TEST_F(RTNLMessageTest, AddNeighbor) {
589 // RTNLMessage doesn't parse Add-Neighbor messages -- ensure this fails
590 RTNLMessage msg;
591 EXPECT_FALSE(msg.Decode(
592 ByteString(kAddNeighborMessage, sizeof(kAddNeighborMessage))));
593}
594
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800595TEST_F(RTNLMessageTest, EncodeRouteAdd) {
Paul Stewart9a908082011-08-31 12:18:48 -0700596 RTNLMessage msg(RTNLMessage::kTypeRoute,
597 RTNLMessage::kModeAdd,
Paul Stewartdd7df792011-07-15 11:09:50 -0700598 0, 1, 2, 0,
Paul Stewart7355ce12011-09-02 10:47:01 -0700599 IPAddress::kFamilyIPv4);
600 IPAddress dst(IPAddress::kFamilyIPv4);
601 IPAddress src(IPAddress::kFamilyIPv4);
602 IPAddress gateway(IPAddress::kFamilyIPv4);
Paul Stewartdd7df792011-07-15 11:09:50 -0700603
604 dst.SetAddressToDefault();
605 src.SetAddressToDefault();
606 EXPECT_TRUE(gateway.SetAddressFromString("192.168.0.1"));
607
608 msg.set_route_status(RTNLMessage::RouteStatus(
609 0, 0, RT_TABLE_MAIN, RTPROT_BOOT, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0));
610 msg.SetAttribute(RTA_DST, dst.address());
611 msg.SetAttribute(RTA_SRC, src.address());
612 msg.SetAttribute(RTA_GATEWAY, gateway.address());
613 msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(12));
614 msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(13));
615
616
617 TestParseRoute(msg.Encode(),
Paul Stewart9a908082011-08-31 12:18:48 -0700618 RTNLMessage::kModeAdd,
Paul Stewart7355ce12011-09-02 10:47:01 -0700619 IPAddress::kFamilyIPv4,
Paul Stewartdd7df792011-07-15 11:09:50 -0700620 12,
621 dst,
Paul Stewartdd7df792011-07-15 11:09:50 -0700622 src,
Paul Stewartdd7df792011-07-15 11:09:50 -0700623 gateway,
624 RT_TABLE_MAIN,
625 RTPROT_BOOT,
626 RT_SCOPE_UNIVERSE,
627 RTN_UNICAST,
628 13);
629}
630
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800631TEST_F(RTNLMessageTest, EncodeLinkDel) {
632 const int kInterfaceIndex = 0x1234;
633 RTNLMessage pmsg(RTNLMessage::kTypeLink,
634 RTNLMessage::kModeDelete,
635 NLM_F_REQUEST,
636 0,
637 0,
638 kInterfaceIndex,
639 IPAddress::kFamilyUnknown);
640
641
642 RTNLMessage msg;
643 EXPECT_TRUE(msg.Decode(pmsg.Encode()));
644
645 EXPECT_EQ(RTNLMessage::kTypeLink, msg.type());
646 EXPECT_EQ(RTNLMessage::kModeDelete, msg.mode());
647 EXPECT_EQ(kInterfaceIndex, msg.interface_index());
648
649 RTNLMessage::LinkStatus status = msg.link_status();
650 EXPECT_EQ(0, status.flags);
651 EXPECT_EQ(0, status.change);
652
653 EXPECT_FALSE(msg.HasAttribute(IFLA_ADDRESS));
654 EXPECT_FALSE(msg.HasAttribute(IFLA_IFNAME));
655 EXPECT_FALSE(msg.HasAttribute(IFLA_MTU));
656 EXPECT_FALSE(msg.HasAttribute(IFLA_QDISC));
657 EXPECT_FALSE(msg.HasAttribute(IFLA_OPERSTATE));
658}
659
Paul Stewartdd7df792011-07-15 11:09:50 -0700660} // namespace shill