diff options
author | Patrik Flykt <patrik.flykt@linux.intel.com> | 2013-12-09 23:43:16 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-12-12 11:43:34 -0500 |
commit | 39b7f5960044511b72b40853d1c6c64e5d618b1b (patch) | |
tree | 19d9b5ca39a5bc654c103c576467798dc1cf352d /src/libsystemd-dhcp | |
parent | d8b61a1dc9153c6f22c923b303b6235ff55122a3 (diff) |
dhcp: Add test function for computing checksum
Diffstat (limited to 'src/libsystemd-dhcp')
-rw-r--r-- | src/libsystemd-dhcp/test-dhcp-client.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libsystemd-dhcp/test-dhcp-client.c b/src/libsystemd-dhcp/test-dhcp-client.c index 863f1966cf..d097c7d43c 100644 --- a/src/libsystemd-dhcp/test-dhcp-client.c +++ b/src/libsystemd-dhcp/test-dhcp-client.c @@ -22,6 +22,7 @@ #include <stdlib.h> #include <assert.h> #include <errno.h> +#include <stdio.h> #include "dhcp-protocol.h" #include "sd-dhcp-client.h" @@ -73,9 +74,48 @@ static void test_request_basic(void) assert(sd_dhcp_client_set_request_option(client, 44) == 0); } +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]; + } + + return ~((sum & 0xffff) + (sum >> 16)); +} + +static void test_checksum(void) +{ + uint8_t buf[20] = { + 0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, + 0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xff, 0xff + }; + + uint8_t check[2] = { + 0x78, 0xae + }; + + uint16_t *val = (uint16_t *)check; + + assert(client_checksum(&buf, 20) == *val); +} + int main(int argc, char *argv[]) { test_request_basic(); + test_checksum(); return 0; } |