static int test_fd[2];
static test_callback_recv_t callback_recv;
static be32_t xid;
+static sd_event_source *test_hangcheck;
+
+static int test_dhcp_hangcheck(sd_event_source *s, uint64_t usec,
+ void *userdata)
+{
+ assert_not_reached("Test case should have completed in 2 seconds");
+
+ return 0;
+}
static void test_request_basic(sd_event *e)
{
assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
}
-static uint16_t client_checksum(void *buf, int len)
-{
- uint32_t sum;
- uint16_t *check;
- int i;
- uint8_t *odd;
-
- sum = 0;
- check = buf;
-
- for (i = 0; i < len / 2 ; i++)
- sum += check[i];
-
- if (len & 0x01) {
- odd = buf;
- sum += odd[len - 1];
- }
-
- while (sum >> 16)
- sum = (sum & 0xffff) + (sum >> 16);
-
- return ~sum;
-}
-
static void test_checksum(void)
{
uint8_t buf[20] = {
if (verbose)
printf("* %s\n", __FUNCTION__);
- assert_se(client_checksum(&buf, 20) == be16toh(0x78ae));
+ assert_se(dhcp_packet_checksum(&buf, 20) == be16toh(0x78ae));
}
static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
void *user_data)
{
+ switch(code) {
+ case DHCP_OPTION_CLIENT_IDENTIFIER:
+ assert_se(len == 7);
+ assert_se(option[0] == 0x01);
+ assert_se(memcmp(&option[1], &mac_addr, ETH_ALEN) == 0);
+ break;
+
+ default:
+ break;
+ }
+
return 0;
}
discover->ip.ttl = 0;
discover->ip.check = discover->udp.len;
- udp_check = ~client_checksum(&discover->ip.ttl, len - 8);
+ udp_check = ~dhcp_packet_checksum(&discover->ip.ttl, len - 8);
assert_se(udp_check == 0xffff);
discover->ip.ttl = IPDEFTTL;
discover->ip.check = ip_check;
- ip_check = ~client_checksum(&discover->ip, sizeof(discover->ip));
+ ip_check = ~dhcp_packet_checksum(&discover->ip, sizeof(discover->ip));
assert_se(ip_check == 0xffff);
assert_se(discover->dhcp.xid);
sd_dhcp_client_stop(client);
sd_dhcp_client_free(client);
- close(test_fd[0]);
- close(test_fd[1]);
+ test_fd[1] = safe_close(test_fd[1]);
callback_recv = NULL;
}
static int test_addr_acq_recv_request(size_t size, DHCPMessage *request)
{
uint16_t udp_check = 0;
+ uint8_t *msg_bytes = (uint8_t *)request;
int res;
res = dhcp_option_parse(request, size, check_options, NULL);
assert_se(res == DHCP_REQUEST);
assert_se(xid == request->xid);
+ assert_se(msg_bytes[size - 1] == DHCP_OPTION_END);
+
if (verbose)
printf(" recv DHCP Request 0x%08x\n", be32toh(xid));
static int test_addr_acq_recv_discover(size_t size, DHCPMessage *discover)
{
uint16_t udp_check = 0;
+ uint8_t *msg_bytes = (uint8_t *)discover;
int res;
res = dhcp_option_parse(discover, size, check_options, NULL);
assert_se(res == DHCP_DISCOVER);
+ assert_se(msg_bytes[size - 1] == DHCP_OPTION_END);
+
xid = discover->xid;
if (verbose)
static void test_addr_acq(sd_event *e)
{
+ usec_t time_now = now(CLOCK_MONOTONIC);
sd_dhcp_client *client;
int res, r;
callback_recv = test_addr_acq_recv_discover;
+ assert_se(sd_event_add_time(e, &test_hangcheck,
+ CLOCK_MONOTONIC,
+ time_now + 2 * USEC_PER_SEC, 0,
+ test_dhcp_hangcheck, NULL) >= 0);
+
res = sd_dhcp_client_start(client);
assert_se(res == 0 || res == -EINPROGRESS);
sd_event_loop(e);
+ test_hangcheck = sd_event_source_unref(test_hangcheck);
+
sd_dhcp_client_set_callback(client, NULL, NULL);
sd_dhcp_client_stop(client);
sd_dhcp_client_free(client);
- close(test_fd[0]);
- close(test_fd[1]);
+ test_fd[1] = safe_close(test_fd[1]);
callback_recv = NULL;
xid = 0;