1 #include <gtest/gtest.h> 2 #include <netdb.h> 3 #include <netinet/ether.h> 4 using namespace testing::ext; 5 6 class NetinetEtherTest : public testing::Test { SetUp()7 void SetUp() override {} TearDown()8 void TearDown() override {} 9 }; 10 11 /** 12 * @tc.name: ether_aton_001 13 * @tc.desc: Verify that the ether_aton function can correctly convert Ethernet MAC addresses from string to binary 14 * representations without errors and that the resulting binary representation matches the expected values. 15 * @tc.type: FUNC 16 **/ 17 HWTEST_F(NetinetEtherTest, ether_aton_001, TestSize.Level1) 18 { 19 ether_addr* a = ether_aton("ef:cd:ab:89:67:45"); 20 ASSERT_NE(nullptr, a); 21 EXPECT_EQ(0xef, a->ether_addr_octet[0]); 22 EXPECT_EQ(0xcd, a->ether_addr_octet[1]); 23 EXPECT_EQ(0xab, a->ether_addr_octet[2]); 24 EXPECT_EQ(0x89, a->ether_addr_octet[3]); 25 EXPECT_EQ(0x67, a->ether_addr_octet[4]); 26 EXPECT_EQ(0x45, a->ether_addr_octet[5]); 27 } 28 29 /** 30 * @tc.name: ether_ntoa_001 31 * @tc.desc: Verify that the ether_ntoa and ether_aton functions can correctly convert Ethernet MAC addresses between 32 * string and binary representations without errors. 33 * @tc.type: FUNC 34 **/ 35 HWTEST_F(NetinetEtherTest, ether_ntoa_001, TestSize.Level1) 36 { 37 ether_addr* a = ether_aton("ef:cd:ab:89:67:45"); 38 ASSERT_NE(nullptr, a); 39 EXPECT_STREQ("EF:CD:AB:89:67:45", ether_ntoa(a)); 40 } 41 42 /** 43 * @tc.name: ether_aton_r_001 44 * @tc.desc: Verify that the ether_aton_r function can correctly convert Ethernet MAC addresses from string to binary 45 * representations without errors and that the resulting binary representation matches the expected values. 46 * @tc.type: FUNC 47 **/ 48 HWTEST_F(NetinetEtherTest, ether_aton_r_001, TestSize.Level1) 49 { 50 ether_addr addr; 51 memset(&addr, 0, sizeof(addr)); 52 ether_addr* a = ether_aton_r("ef:cd:ab:89:67:45", &addr); 53 EXPECT_EQ(&addr, a); 54 EXPECT_EQ(0xef, addr.ether_addr_octet[0]); 55 EXPECT_EQ(0xcd, addr.ether_addr_octet[1]); 56 EXPECT_EQ(0xab, addr.ether_addr_octet[2]); 57 EXPECT_EQ(0x89, addr.ether_addr_octet[3]); 58 EXPECT_EQ(0x67, addr.ether_addr_octet[4]); 59 EXPECT_EQ(0x45, addr.ether_addr_octet[5]); 60 } 61 62 /** 63 * @tc.name: ether_ntoa_r_001 64 * @tc.desc: Verify that the ether_ntoa_r and ether_aton_r functions can correctly convert Ethernet MAC addresses 65 * between string and binary representations without errors. 66 * @tc.type: FUNC 67 **/ 68 HWTEST_F(NetinetEtherTest, ether_ntoa_r_001, TestSize.Level1) 69 { 70 ether_addr pa; 71 memset(&pa, 0, sizeof(pa)); 72 ether_addr* a = ether_aton_r("ef:cd:ab:89:67:45", &pa); 73 EXPECT_EQ(&pa, a); 74 75 char x[BUFSIZ]; 76 77 memset(&x, 0, sizeof(x)); 78 char* p = ether_ntoa_r(&pa, x); 79 EXPECT_EQ(x, p); 80 EXPECT_STREQ("EF:CD:AB:89:67:45", x); 81 }