dhcp: add test for dhcp_identifier_set_iaid()

This commit is contained in:
Thomas Haller 2018-11-01 14:46:27 +01:00
parent 7d7c8ea944
commit 43fc095532
1 changed files with 24 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <net/if.h>
#include "sd-dhcp-client.h"
#include "sd-event.h"
@ -16,6 +17,7 @@
#include "dhcp-internal.h"
#include "dhcp-protocol.h"
#include "fd-util.h"
#include "random-util.h"
#include "tests.h"
#include "util.h"
@ -153,6 +155,27 @@ static void test_checksum(void) {
assert_se(dhcp_packet_checksum((uint8_t*)&buf, 20) == be16toh(0x78ae));
}
static void test_dhcp_identifier_set_iaid(void) {
uint32_t iaid;
int ifindex;
for (;;) {
char ifname[IFNAMSIZ];
/* try to find an ifindex which does not exist. I causes dhcp_identifier_set_iaid()
* to hash the MAC address. */
pseudo_random_bytes(&ifindex, sizeof(ifindex));
if (ifindex > 0 && !if_indextoname(ifindex, ifname))
break;
}
assert_se(dhcp_identifier_set_iaid(ifindex, mac_addr, sizeof(mac_addr), &iaid) >= 0);
/* we expect, that the MAC address was hashed. Note that the value is in native
* endianness. */
assert_se(iaid == 0x8dde4ba8u);
}
static int check_options(uint8_t code, uint8_t len, const void *option, void *userdata) {
switch(code) {
case SD_DHCP_OPTION_CLIENT_IDENTIFIER:
@ -532,6 +555,7 @@ int main(int argc, char *argv[]) {
test_request_basic(e);
test_request_anonymize(e);
test_checksum();
test_dhcp_identifier_set_iaid();
test_discover_message(e);
test_addr_acq(e);