summaryrefslogtreecommitdiff
path: root/net/arp_packet_unittest.cc
blob: ad66fedbb9891cf0e7617e34747c7632e099faf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "shill/net/arp_packet.h"

#include <gtest/gtest.h>

#include "shill/mock_log.h"

using testing::_;
using testing::HasSubstr;
using testing::Test;

namespace shill {

namespace {
const uint8_t kArpRequestV4[] =
    { 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01 };
const uint8_t kArpRequestV6[] =
    { 0x00, 0x01, 0x86, 0xdd, 0x06, 0x10, 0x00, 0x01 };
const uint8_t kArpReplyV4[] =
    { 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x02 };
const uint8_t kArpReplyV6[] =
    { 0x00, 0x01, 0x86, 0xdd, 0x06, 0x10, 0x00, 0x02 };
const char kIPv4Address0[] = "192.168.0.1";
const char kIPv4Address1[] = "10.0.12.13";
const char kIPv6Address0[] = "fe80::1aa9:5ff:7ebf:14c5";
const char kIPv6Address1[] = "1980:0:0:1000:1b02:1aa9:5ff:7ebf";
const uint8_t kMACAddress0[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
const uint8_t kMACAddress1[] = { 0x88, 0x87, 0x86, 0x85, 0x84, 0x83 };
const uint8_t kInsertedByte[] = { 0x00 };
const size_t kArpPaddingSizeV4 = 18;
const size_t kArpPaddingSizeV6 = 0;
}  // namespace

class ArpPacketTest : public Test {
 public:
  ArpPacketTest()
      : ipv4_address0_(IPAddress::kFamilyIPv4),
        ipv4_address1_(IPAddress::kFamilyIPv4),
        ipv6_address0_(IPAddress::kFamilyIPv6),
        ipv6_address1_(IPAddress::kFamilyIPv6),
        mac_address0_(kMACAddress0, arraysize(kMACAddress0)),
        mac_address1_(kMACAddress1, arraysize(kMACAddress1)),
        inserted_byte_(kInsertedByte, arraysize(kInsertedByte)) {}
  virtual ~ArpPacketTest() {}

  virtual void SetUp() {
    EXPECT_TRUE(ipv4_address0_.SetAddressFromString(kIPv4Address0));
    EXPECT_TRUE(ipv4_address1_.SetAddressFromString(kIPv4Address1));
    EXPECT_TRUE(ipv6_address0_.SetAddressFromString(kIPv6Address0));
    EXPECT_TRUE(ipv6_address1_.SetAddressFromString(kIPv6Address1));
  }

 protected:
  IPAddress ipv4_address0_;
  IPAddress ipv4_address1_;
  IPAddress ipv6_address0_;
  IPAddress ipv6_address1_;
  ByteString mac_address0_;
  ByteString mac_address1_;
  ByteString inserted_byte_;
  ArpPacket packet_;
};

TEST_F(ArpPacketTest, Constructor) {
  EXPECT_FALSE(packet_.local_ip_address().IsValid());
  EXPECT_FALSE(packet_.remote_ip_address().IsValid());
  EXPECT_TRUE(packet_.local_mac_address().IsEmpty());
  EXPECT_TRUE(packet_.remote_mac_address().IsEmpty());
}

TEST_F(ArpPacketTest, GettersAndSetters) {
  packet_.set_local_ip_address(ipv4_address0_);
  packet_.set_remote_ip_address(ipv6_address1_);
  packet_.set_local_mac_address(mac_address0_);
  packet_.set_remote_mac_address(mac_address1_);
  EXPECT_TRUE(ipv4_address0_.Equals(packet_.local_ip_address()));
  EXPECT_TRUE(ipv6_address1_.Equals(packet_.remote_ip_address()));
  EXPECT_TRUE(mac_address0_.Equals(packet_.local_mac_address()));
  EXPECT_TRUE(mac_address1_.Equals(packet_.remote_mac_address()));
}

TEST_F(ArpPacketTest, ParseTinyPacket) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("too short to contain ARP header."))).Times(1);

  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.Resize(arp_bytes.GetLength() - 1);
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseBadHRDType) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("Packet is of unknown ARPHRD type 257"))).Times(1);

  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.GetData()[0] = 0x1;
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseBadProtocol) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("Packet has unknown protocol 2049"))).Times(1);

  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.GetData()[3] = 0x1;
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseBadHardwareLength) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("Packet has unexpected hardware address length"))).Times(1);

  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.GetData()[4] = 0x1;
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseBadProtocolLength) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("Packet has unexpected protocol address length"))).Times(1);

  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.GetData()[5] = 0x1;
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseBadOpCode) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("Packet is not an ARP reply or request but of type 258")))
              .Times(1);

  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.GetData()[6] = 0x1;
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseShortPacket) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("is too small to contain entire ARP payload"))).Times(1);

  ByteString arp_bytes(kArpReplyV6, arraysize(kArpReplyV6));
  arp_bytes.Append(mac_address1_);
  arp_bytes.Append(ipv6_address0_.address());
  arp_bytes.Append(mac_address0_);
  arp_bytes.Append(ipv6_address1_.address());
  arp_bytes.Resize(arp_bytes.GetLength() - 1);
  EXPECT_FALSE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseIPv4) {
  ByteString arp_bytes(kArpReplyV4, arraysize(kArpReplyV4));
  arp_bytes.Append(mac_address0_);
  arp_bytes.Append(ipv4_address0_.address());
  arp_bytes.Append(mac_address1_);
  arp_bytes.Append(ipv4_address1_.address());
  EXPECT_TRUE(packet_.Parse(arp_bytes));
  EXPECT_TRUE(packet_.IsReply());
  EXPECT_TRUE(ipv4_address0_.Equals(packet_.local_ip_address()));
  EXPECT_TRUE(ipv4_address1_.Equals(packet_.remote_ip_address()));
  EXPECT_TRUE(mac_address0_.Equals(packet_.local_mac_address()));
  EXPECT_TRUE(mac_address1_.Equals(packet_.remote_mac_address()));

  // Parse should succeed with arbitrary trailing padding.
  arp_bytes.Append(ByteString(1000));
  EXPECT_TRUE(packet_.Parse(arp_bytes));
}

TEST_F(ArpPacketTest, ParseIPv6) {
  ByteString arp_bytes(kArpReplyV6, arraysize(kArpReplyV6));
  arp_bytes.Append(mac_address1_);
  arp_bytes.Append(ipv6_address0_.address());
  arp_bytes.Append(mac_address0_);
  arp_bytes.Append(ipv6_address1_.address());
  EXPECT_TRUE(packet_.Parse(arp_bytes));
  EXPECT_TRUE(packet_.IsReply());
  EXPECT_TRUE(ipv6_address0_.Equals(packet_.local_ip_address()));
  EXPECT_TRUE(ipv6_address1_.Equals(packet_.remote_ip_address()));
  EXPECT_TRUE(mac_address1_.Equals(packet_.local_mac_address()));
  EXPECT_TRUE(mac_address0_.Equals(packet_.remote_mac_address()));
}

TEST_F(ArpPacketTest, ParseRequest) {
  ByteString arp_bytes(kArpRequestV4, arraysize(kArpRequestV4));
  arp_bytes.Append(mac_address0_);
  arp_bytes.Append(ipv4_address0_.address());
  arp_bytes.Append(mac_address1_);
  arp_bytes.Append(ipv4_address1_.address());
  EXPECT_TRUE(packet_.Parse(arp_bytes));
  EXPECT_FALSE(packet_.IsReply());
  EXPECT_TRUE(ipv4_address0_.Equals(packet_.local_ip_address()));
  EXPECT_TRUE(ipv4_address1_.Equals(packet_.remote_ip_address()));
  EXPECT_TRUE(mac_address0_.Equals(packet_.local_mac_address()));
  EXPECT_TRUE(mac_address1_.Equals(packet_.remote_mac_address()));
}

TEST_F(ArpPacketTest, FormatRequestInvalidAddress) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("Local or remote IP address is not valid"))).Times(3);

  ByteString arp_bytes;
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
  packet_.set_local_ip_address(ipv4_address0_);
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
  packet_.set_local_ip_address(IPAddress(IPAddress::kFamilyUnknown));
  packet_.set_remote_ip_address(ipv4_address0_);
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
}

TEST_F(ArpPacketTest, FormatRequestMismatchedAddresses) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("IP address families do not match"))).Times(1);

  ByteString arp_bytes;
  packet_.set_local_ip_address(ipv4_address0_);
  packet_.set_remote_ip_address(ipv6_address1_);
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
}

TEST_F(ArpPacketTest, FormatRequestBadMACAddressLength) {
  ScopedMockLog log;
  EXPECT_CALL(log,
      Log(logging::LOG_ERROR, _,
          HasSubstr("MAC address length is incorrect"))).Times(3);

  ByteString arp_bytes;
  packet_.set_local_ip_address(ipv4_address0_);
  packet_.set_remote_ip_address(ipv4_address1_);
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
  packet_.set_local_mac_address(mac_address0_);
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
  packet_.set_local_mac_address(ByteString());
  packet_.set_remote_mac_address(mac_address0_);
  EXPECT_FALSE(packet_.FormatRequest(&arp_bytes));
}

TEST_F(ArpPacketTest, FormatRequestIPv4) {
  ByteString arp_bytes;
  packet_.set_local_ip_address(ipv4_address0_);
  packet_.set_remote_ip_address(ipv4_address1_);
  packet_.set_local_mac_address(mac_address0_);
  packet_.set_remote_mac_address(mac_address1_);
  EXPECT_TRUE(packet_.FormatRequest(&arp_bytes));

  ByteString expected_bytes(kArpRequestV4, arraysize(kArpRequestV4));
  expected_bytes.Append(mac_address0_);
  expected_bytes.Append(ipv4_address0_.address());
  expected_bytes.Append(mac_address1_);
  expected_bytes.Append(ipv4_address1_.address());
  expected_bytes.Append(ByteString(kArpPaddingSizeV4));
  EXPECT_TRUE(expected_bytes.Equals(arp_bytes));
}

TEST_F(ArpPacketTest, FormatRequestIPv6) {
  ByteString arp_bytes;
  packet_.set_local_ip_address(ipv6_address0_);
  packet_.set_remote_ip_address(ipv6_address1_);
  packet_.set_local_mac_address(mac_address1_);
  packet_.set_remote_mac_address(mac_address0_);
  EXPECT_TRUE(packet_.FormatRequest(&arp_bytes));

  ByteString expected_bytes(kArpRequestV6, arraysize(kArpRequestV6));
  expected_bytes.Append(mac_address1_);
  expected_bytes.Append(ipv6_address0_.address());
  expected_bytes.Append(mac_address0_);
  expected_bytes.Append(ipv6_address1_.address());
  expected_bytes.Append(ByteString(kArpPaddingSizeV6));
  EXPECT_TRUE(expected_bytes.Equals(arp_bytes));
}

}  // namespace shill