blob: d7b81eb8d9533089c1bbd556ce7d5f561f9f5ea5 [file] [log] [blame]
Elliott Hughesc6190a92016-05-16 13:24:31 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <netinet/ether.h>
20
21TEST(netinet_ether, ether_aton__ether_ntoa) {
Elliott Hughes09e97e62016-05-16 16:21:37 -070022 ether_addr* a = ether_aton("12:34:56:78:9a:bc");
23 ASSERT_NE(nullptr, a);
Elliott Hughesc6190a92016-05-16 13:24:31 -070024 ASSERT_EQ(0x12, a->ether_addr_octet[0]);
25 ASSERT_EQ(0x34, a->ether_addr_octet[1]);
26 ASSERT_EQ(0x56, a->ether_addr_octet[2]);
27 ASSERT_EQ(0x78, a->ether_addr_octet[3]);
28 ASSERT_EQ(0x9a, a->ether_addr_octet[4]);
29 ASSERT_EQ(0xbc, a->ether_addr_octet[5]);
30
Elliott Hughes09e97e62016-05-16 16:21:37 -070031 ASSERT_STREQ("12:34:56:78:9a:bc", ether_ntoa(a));
Elliott Hughesc6190a92016-05-16 13:24:31 -070032}
33
34TEST(netinet_ether, ether_aton_r__ether_ntoa_r) {
35 ether_addr addr;
36 memset(&addr, 0, sizeof(addr));
Elliott Hughes7cebf832020-08-12 14:25:41 -070037 ether_addr* a = ether_aton_r("12:34:56:78:9a:Bc", &addr);
Elliott Hughesc6190a92016-05-16 13:24:31 -070038 ASSERT_EQ(&addr, a);
39 ASSERT_EQ(0x12, addr.ether_addr_octet[0]);
40 ASSERT_EQ(0x34, addr.ether_addr_octet[1]);
41 ASSERT_EQ(0x56, addr.ether_addr_octet[2]);
42 ASSERT_EQ(0x78, addr.ether_addr_octet[3]);
43 ASSERT_EQ(0x9a, addr.ether_addr_octet[4]);
44 ASSERT_EQ(0xbc, addr.ether_addr_octet[5]);
45
46 char buf[32];
47 memset(buf, 0, sizeof(buf));
48 char* p = ether_ntoa_r(&addr, buf);
49 ASSERT_EQ(buf, p);
Elliott Hughes09e97e62016-05-16 16:21:37 -070050 ASSERT_STREQ("12:34:56:78:9a:bc", buf);
Elliott Hughesc6190a92016-05-16 13:24:31 -070051}
Elliott Hughes7cebf832020-08-12 14:25:41 -070052
53TEST(netinet_ether, ether_aton_r_failures) {
54 ether_addr addr;
55 ASSERT_TRUE(ether_aton_r("12:34:56:78:9a;bc", &addr) == nullptr);
56 ASSERT_TRUE(ether_aton_r("12:34:56:78:9a:bc ", &addr) == nullptr);
57 ASSERT_TRUE(ether_aton_r("g2:34:56:78:9a:bc ", &addr) == nullptr);
58 ASSERT_TRUE(ether_aton_r("1G:34:56:78:9a:bc ", &addr) == nullptr);
Elliott Hughes3ed6e722024-02-16 01:18:01 +000059 ASSERT_TRUE(ether_aton_r("123:34:56:78:9a:bc ", &addr) == nullptr);
Elliott Hughes7cebf832020-08-12 14:25:41 -070060}