source: mainline/uspace/lib/inet/test/eth_addr.c

Last change on this file was 3e6bca8, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Represent Ethernet address as a number instead of an array

Carefully document the design since this breaks the principle of least
surprise. Also add unit tests.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2021 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <inet/eth_addr.h>
31#include <pcut/pcut.h>
32
33PCUT_INIT;
34
35PCUT_TEST_SUITE(eth_addr);
36
37/** Test ETH_ADDR_INITIALIZER constructs correct Ethernet address */
38PCUT_TEST(initializer)
39{
40 eth_addr_t addr = ETH_ADDR_INITIALIZER(0x11, 0x22, 0x33, 0x44, 0x55,
41 0x66);
42 PCUT_ASSERT_INT_EQUALS(0x112233445566, addr.a);
43}
44
45/** Test eth_addr_decode() correctly decodes from array of bytes */
46PCUT_TEST(decode)
47{
48 eth_addr_t addr;
49 uint8_t b[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
50
51 eth_addr_decode(b, &addr);
52 PCUT_ASSERT_INT_EQUALS(0x112233445566, addr.a);
53}
54
55/** Test eth_addr_encode() correctly encodes to array of bytes */
56PCUT_TEST(encode)
57{
58 eth_addr_t addr;
59 uint8_t b[7] = { 0, 0, 0, 0, 0, 0, 0 };
60
61 addr.a = 0x112233445566;
62 eth_addr_encode(&addr, b);
63
64 PCUT_ASSERT_INT_EQUALS(0x11, b[0]);
65 PCUT_ASSERT_INT_EQUALS(0x22, b[1]);
66 PCUT_ASSERT_INT_EQUALS(0x33, b[2]);
67 PCUT_ASSERT_INT_EQUALS(0x44, b[3]);
68 PCUT_ASSERT_INT_EQUALS(0x55, b[4]);
69 PCUT_ASSERT_INT_EQUALS(0x66, b[5]);
70}
71
72/** Test eth_addr_compare() correctly compares two Ethernet addresses */
73PCUT_TEST(compare)
74{
75 eth_addr_t a;
76 eth_addr_t b;
77
78 a.a = 1;
79 b.a = 2;
80 PCUT_ASSERT_INT_EQUALS(-1, eth_addr_compare(&a, &b));
81
82 a.a = 2;
83 b.a = 2;
84 PCUT_ASSERT_INT_EQUALS(0, eth_addr_compare(&a, &b));
85
86 a.a = 2;
87 b.a = 1;
88 PCUT_ASSERT_INT_EQUALS(1, eth_addr_compare(&a, &b));
89}
90
91/** Tets eth_addr_format() correctly formats Ethernet address to string */
92PCUT_TEST(format)
93{
94 eth_addr_t addr1 = ETH_ADDR_INITIALIZER(0x11, 0x22, 0x33, 0x44, 0x55, 0x66);
95 eth_addr_t addr2 = ETH_ADDR_INITIALIZER(0x01, 0x02, 0x03, 0x04, 0x05, 0x06);
96 eth_addr_t addr3 = ETH_ADDR_INITIALIZER(0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
97 eth_addr_str_t saddr;
98
99 eth_addr_format(&addr1, &saddr);
100 PCUT_ASSERT_STR_EQUALS("11:22:33:44:55:66", saddr.str);
101
102 eth_addr_format(&addr2, &saddr);
103 PCUT_ASSERT_STR_EQUALS("01:02:03:04:05:06", saddr.str);
104
105 eth_addr_format(&addr3, &saddr);
106 PCUT_ASSERT_STR_EQUALS("aa:bb:cc:dd:ee:ff", saddr.str);
107}
108
109PCUT_EXPORT(eth_addr);
Note: See TracBrowser for help on using the repository browser.