Skip to content

Commit

Permalink
Add convenience method to determine if a MAC address is globally uniq…
Browse files Browse the repository at this point in the history
…ue or locally assigned.
  • Loading branch information
tsilia committed Feb 24, 2020
1 parent ce409db commit a68281c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/tins/hw_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,20 @@ class HWAddress {
return !is_broadcast() && !is_multicast();
}

/**
* \brief Indicates whether this is a globally unique address.
*/
bool is_globally_unique() const {
return !is_locally_assigned();
}

/**
* \brief Indicates whether this is a locally assigned address.
*/
bool is_locally_assigned() const {
return (*begin() & 0x02);
}

/**
* \brief Convert this address to a hex-notation std::string address.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/src/hw_address_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ TEST_F(HWAddressTest, IsMulticast) {
EXPECT_FALSE(HWAddress<6>("02:02:03:04:05:06").is_multicast());
}

TEST_F(HWAddressTest, IsGloballyUnique) {
EXPECT_FALSE(HWAddress<6>("ff:ff:ff:ff:ff:ff").is_globally_unique());
EXPECT_FALSE(HWAddress<6>("03:02:03:04:05:06").is_globally_unique());
EXPECT_FALSE(HWAddress<6>("de:ad:be:ef:00:00").is_globally_unique());
EXPECT_TRUE(HWAddress<6>("dc:ad:be:ef:00:00").is_globally_unique());
EXPECT_TRUE(HWAddress<6>("00:02:03:04:05:06").is_globally_unique());
EXPECT_TRUE(HWAddress<6>("01:02:03:04:05:06").is_globally_unique());
EXPECT_FALSE(HWAddress<6>("02:01:03:04:05:06").is_globally_unique());
EXPECT_TRUE(HWAddress<6>("04:05:06:07:08:09").is_globally_unique());
EXPECT_FALSE(HWAddress<6>("06:05:06:07:08:09").is_globally_unique());
EXPECT_TRUE(HWAddress<6>("08:05:06:07:08:09").is_globally_unique());
}

TEST_F(HWAddressTest, IsLocallyAssigned) {
EXPECT_TRUE(HWAddress<6>("ff:ff:ff:ff:ff:ff").is_locally_assigned());
EXPECT_FALSE(HWAddress<6>("00:00:00:00:00:00").is_locally_assigned());
EXPECT_TRUE(HWAddress<6>("de:ad:be:ef:00:00").is_locally_assigned());
EXPECT_TRUE(HWAddress<6>("12:13:14:15:16:17").is_locally_assigned());
EXPECT_FALSE(HWAddress<6>("14:13:12:15:16:17").is_locally_assigned());
EXPECT_TRUE(HWAddress<6>("03:00:00:00:00:00").is_locally_assigned());
EXPECT_FALSE(HWAddress<6>("05:00:00:00:00:00").is_locally_assigned());
}

TEST_F(HWAddressTest, CopyAssignmentOperator) {
HWAddress<6> addr1(byte_address), addr2;
addr2 = addr1;
Expand Down

0 comments on commit a68281c

Please sign in to comment.