Skip to content

Commit

Permalink
tests:ethernets: Add comments about MAC->EUI64 transformation and mov…
Browse files Browse the repository at this point in the history
…e method to base.py
  • Loading branch information
slyon committed Aug 28, 2024
1 parent 30f05de commit c5a3e12
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
17 changes: 17 additions & 0 deletions tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ def resolved_in_use():
return os.path.isfile('/run/systemd/resolve/resolv.conf')


def mac_to_eui64(hwaddr: str) -> ipaddress.IPv6Address:
'''
Calculate a EUI-64 IPv6 address from a MAC, according to RFC-4291,
e.g.: https://www.geeksforgeeks.org/ipv6-eui-64-extended-unique-identifier
'''
# convert MAC address bytes to a list of base10 decimals
mac_addr = [int(h, base=16) for h in hwaddr.split(':')]
mac_addr[0] ^= 2 # flip 7th bit of the 1st byte, e.g. XOR 0b00000010 (=2)
# inject 0xFFFE in the middle, increasing 48bit MAC to 64bit IPv6 Interface ID
mac_addr[3:3] = [0xff, 0xfe]
# apply 64bit IPv6 Interface ID on generic IPv6 carrier address (2600::)
eui_addr_int = 0x26000000000000000000000000000000
for i, h in enumerate(reversed(mac_addr)):
eui_addr_int |= h << 8 * i
return ipaddress.IPv6Address(eui_addr_int)


class IntegrationTestsBase(unittest.TestCase):
'''Common functionality for network test cases
Expand Down
25 changes: 8 additions & 17 deletions tests/integration/ethernets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import subprocess
import sys
import unittest
import ipaddress

from base import IntegrationTestsBase, nm_uses_dnsmasq, resolved_in_use, test_backends
from base import (IntegrationTestsBase, mac_to_eui64, nm_uses_dnsmasq,
resolved_in_use, test_backends)


class _CommonTests():
Expand Down Expand Up @@ -234,13 +234,9 @@ def test_ip6_stable_privacy(self):
accept-ra: yes
ipv6-address-generation: stable-privacy''' % {'r': self.backend, 'ec': self.dev_e_client})
self.generate_and_settle([self.state_dhcp6(self.dev_e_client)])
mac_addr = [int(h, base=16) for h in self.dev_e_client_mac.split(':')]
mac_addr[0] ^= 2
mac_addr[3:3] = [0xff, 0xfe]
eui_addr_int = 0x26000000000000000000000000000000
for i, h in enumerate(reversed(mac_addr)):
eui_addr_int |= h << 8 * i
eui_addr = ipaddress.IPv6Address(eui_addr_int)
# Compare to EUI-64 address, to make sure it will NOT match the
# (random) stable-privacy address generated.
eui_addr = mac_to_eui64(self.dev_e_client_mac)
self.assert_iface_up(self.dev_e_client, ['inet6 2600::'], [f'inet6 {eui_addr.compressed}/64'])

def test_ip6_eui64(self):
Expand All @@ -255,13 +251,8 @@ def test_ip6_eui64(self):
accept-ra: yes
ipv6-address-generation: eui64''' % {'r': self.backend, 'ec': self.dev_e_client})
self.generate_and_settle([self.state_dhcp6(self.dev_e_client)])
mac_addr = [int(h, base=16) for h in self.dev_e_client_mac.split(':')]
mac_addr[0] ^= 2
mac_addr[3:3] = [0xff, 0xfe]
eui_addr_int = 0x26000000000000000000000000000000
for i, h in enumerate(reversed(mac_addr)):
eui_addr_int |= h << 8 * i
eui_addr = ipaddress.IPv6Address(eui_addr_int)
# Compare to EUI-64 address, to make sure it matches the one generated.
eui_addr = mac_to_eui64(self.dev_e_client_mac)
self.assert_iface_up(self.dev_e_client, [f'inet6 {eui_addr.compressed}/64'])

def test_link_local_all(self):
Expand Down

0 comments on commit c5a3e12

Please sign in to comment.