// options_test.cc // Author: Allen Porter #include #include #include #include "options.h" #include "test_macros.h" // DHCPDISCOVER options uint8_t test1_opts[] = { 0x35, 0x01, 0x01, 0x37, 0x04, 0x01, 0x03, 0x06, 0x0f, 0x39, 0x02, 0x05, 0xdc, 0x3d, 0x07, 0x01, 0x00, 0x0a, 0x95, 0xbe, 0x88, 0xc6, 0x33, 0x04, 0x00, 0x76, 0xa7, 0x00, 0x0c, 0x07, 0x6c, 0x6f, 0x7a, 0x65, 0x6e, 0x67, 0x65, // pad 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t test1_output[] = { 0x35, 0x01, 0x01, 0x39, 0x02, 0x05, 0xdc, 0x0c, 0x07, 0x6c, 0x6f, 0x7a, 0x65, 0x6e, 0x67, 0x65, 0x33, 0x04, 0x00, 0x76, 0xa7, 0x00, 0x3d, 0x07, 0x01, 0x00, 0x0a, 0x95, 0xbe, 0x88, 0xc6, 0x37, 0x04, 0x01, 0x03, 0x06, 0x0f, 0xff }; // Tests DHCPDISCOVER options void test1() { struct options opts; memset(&opts, 0, sizeof(struct options)); parse_options(test1_opts, sizeof(test1_opts), &opts); ASSERT_EQ(MESSAGE_DHCPDISCOVER, opts.message_type); ASSERT_EQ(1500, opts.max_size); char hostname[] = { "lozenge" }; ASSERT_EQ(0, strcmp(hostname, opts.hostname)); ASSERT_EQ(7776000, opts.lease_time); ASSERT_EQ(1, opts.client_id_type); ASSERT_EQ(6, opts.client_id_length); uint8_t mac[] = { 0x00, 0x0a, 0x095, 0xbe, 0x88, 0xc6 }; ASSERT_EQ(0, memcmp(mac, opts.client_id, 6)); uint8_t params[] = { DHCP_SUBNET_MASK, DHCP_ROUTER, DHCP_DNS, DHCP_DOMAIN }; ASSERT_EQ(4, opts.param_size); ASSERT_EQ(0, memcmp(params, opts.param_list, 4)); ASSERT_EQ(0, opts.server_id); ASSERT_EQ(0, opts.subnet); ASSERT_EQ(0, opts.router_num); ASSERT_EQ(0, opts.dns_num); ASSERT_EQ(0, strlen(opts.domain)); uint8_t buf[1024]; uint16_t size = 1024; write_options(&opts, buf, &size); ASSERT_EQ(size, sizeof(test1_output)); ASSERT_ARRAY_EQ(test1_output, buf, size); } uint8_t test2_opts[] = { 0x35, 0x01, 0x02, 0x36, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, 0x33, 0x04, 0x00, 0x00, 0x38, 0x40, 0x34, 0x01, 0x03, 0x03, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x06, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x0f, 0x0c, 0x74, 0x68, 0x65, 0x62, 0x65, 0x6e, 0x64, 0x73, 0x2e, 0x6f, 0x72, 0x67, // pad 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0xa5, 0x45, 0xb0 }; uint8_t test2_output[] = { 0x35, 0x01, 0x02, 0x33, 0x04, 0x00, 0x00, 0x38, 0x40, 0x36, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, 0x03, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x06, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x0f, 0x0c, 0x74, 0x68, 0x65, 0x62, 0x65, 0x6e, 0x64, 0x73, 0x2e, 0x6f, 0x72, 0x67, // pad 0xff }; // Tests DHCPOFFER options void test2() { struct options opts; memset(&opts, 0, sizeof(struct options)); parse_options(test2_opts, sizeof(test2_opts), &opts); ASSERT_EQ(MESSAGE_DHCPOFFER, opts.message_type); ASSERT_EQ(14400, opts.lease_time); uint8_t id[] = { 10, 0, 1, 1 }; ASSERT_EQ(0, memcmp(id, &opts.server_id, 4)); uint8_t subnet[] = { 255, 255, 255, 0 }; ASSERT_EQ(0, memcmp(subnet, &opts.subnet, 4)); ASSERT_EQ(1, opts.router_num); ASSERT_EQ(0, memcmp(id, &opts.router, 4)); ASSERT_EQ(1, opts.dns_num); ASSERT_EQ(0, memcmp(id, &opts.dns, 4)); char domain[] = { "thebends.org" }; ASSERT_EQ(0, strcmp(domain, opts.domain)); // Options NOT in the response ASSERT_EQ(0, opts.max_size); ASSERT_EQ(0, strlen(opts.hostname)); ASSERT_EQ(0, opts.client_id_type); ASSERT_EQ(0, opts.client_id_length); ASSERT_EQ(0, opts.param_size); uint8_t buf[1024]; uint16_t size = 1024; write_options(&opts, buf, &size); ASSERT_EQ(size, sizeof(test2_output)); ASSERT_ARRAY_EQ(test2_output, buf, size); } uint8_t test3_opts[] = { 0x35, 0x01, 0x03, 0x37, 0x04, 0x01, 0x03, 0x06, 0x0f, 0x39, 0x02, 0x05, 0xdc, 0x3d, 0x07, 0x01, 0x00, 0x0a, 0x95, 0xbe, 0x88, 0xc6, 0x32, 0x04, 0x0a, 0x00, 0x01, 0x03, 0x36, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x0c, 0x07, 0x6c, 0x6f, 0x7a, 0x65, 0x6e, 0x67, 0x65, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t test3_output[] = { 0x35, 0x01, 0x03, 0x39, 0x02, 0x05, 0xdc, 0x0c, 0x07, 0x6c, 0x6f, 0x7a, 0x65, 0x6e, 0x67, 0x65, 0x32, 0x04, 0x0a, 0x00, 0x01, 0x03, 0x36, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x3d, 0x07, 0x01, 0x00, 0x0a, 0x95, 0xbe, 0x88, 0xc6, 0x37, 0x04, 0x01, 0x03, 0x06, 0x0f, 0xff }; // Tests DHCPREQUEST options void test3() { struct options opts; memset(&opts, 0, sizeof(struct options)); parse_options(test3_opts, sizeof(test3_opts), &opts); ASSERT_EQ(MESSAGE_DHCPREQUEST, opts.message_type); ASSERT_EQ(1500, opts.max_size); char hostname[] = { "lozenge" }; ASSERT_EQ(0, strcmp(hostname, opts.hostname)); uint8_t ip[] = { 10, 0, 1, 3 }; ASSERT_EQ(0, memcmp(ip, &opts.requested_ip, 4)); ASSERT_EQ(1, opts.client_id_type); ASSERT_EQ(6, opts.client_id_length); uint8_t mac[] = { 0x00, 0x0a, 0x095, 0xbe, 0x88, 0xc6 }; ASSERT_EQ(0, memcmp(mac, opts.client_id, 6)); uint8_t server_id[] = { 10, 0, 1, 1 }; ASSERT_EQ(0, memcmp(server_id, &opts.server_id, 4)); uint8_t params[] = { DHCP_SUBNET_MASK, DHCP_ROUTER, DHCP_DNS, DHCP_DOMAIN }; ASSERT_EQ(4, opts.param_size); ASSERT_EQ(0, memcmp(params, opts.param_list, 4)); // Not in the message ASSERT_EQ(0, opts.lease_time); ASSERT_EQ(0, opts.subnet); ASSERT_EQ(0, opts.router_num); ASSERT_EQ(0, opts.dns_num); ASSERT_EQ(0, strlen(opts.domain)); uint8_t buf[1024]; uint16_t size = 1024; write_options(&opts, buf, &size); ASSERT_ARRAY_EQ(test3_output, buf, size); ASSERT_EQ(size, sizeof(test3_output)); } uint8_t test4_opts[] = { 0x35, 0x01, 0x05, 0x36, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, 0x33, 0x04, 0x00, 0x00, 0x38, 0x40, 0x34, 0x01, 0x03, 0x03, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x06, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x0f, 0x0c, 0x74, 0x68, 0x65, 0x62, 0x65, 0x6e, 0x64, 0x73, 0x2e, 0x6f, 0x72, 0x67, // pad 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0x3e, 0x63, 0x5b }; uint8_t test4_output[] = { 0x35, 0x01, 0x05, 0x33, 0x04, 0x00, 0x00, 0x38, 0x40, 0x36, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, 0x03, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x06, 0x04, 0x0a, 0x00, 0x01, 0x01, 0x0f, 0x0c, 0x74, 0x68, 0x65, 0x62, 0x65, 0x6e, 0x64, 0x73, 0x2e, 0x6f, 0x72, 0x67, 0xff }; // Tests DHCPOFFER options void test4() { struct options opts; memset(&opts, 0, sizeof(struct options)); parse_options(test4_opts, sizeof(test4_opts), &opts); ASSERT_EQ(MESSAGE_DHCPACK, opts.message_type); uint8_t id[] = { 10, 0, 1, 1 }; ASSERT_EQ(0, memcmp(id, &opts.server_id, 4)); uint8_t subnet[] = { 255, 255, 255, 0 }; ASSERT_EQ(0, memcmp(subnet, &opts.subnet, 4)); ASSERT_EQ(14400, opts.lease_time); ASSERT_EQ(1, opts.router_num); ASSERT_EQ(0, memcmp(id, &opts.router, 4)); ASSERT_EQ(1, opts.dns_num); ASSERT_EQ(0, memcmp(id, &opts.dns, 4)); char domain[] = { "thebends.org" }; ASSERT_EQ(0, strcmp(domain, opts.domain)); // Options NOT in the response ASSERT_EQ(0, opts.max_size); ASSERT_EQ(0, strlen(opts.hostname)); ASSERT_EQ(0, opts.client_id_type); ASSERT_EQ(0, opts.client_id_length); ASSERT_EQ(0, opts.param_size); uint8_t buf[1024]; uint16_t size = 1024; write_options(&opts, buf, &size); ASSERT_ARRAY_EQ(test4_output, buf, size); ASSERT_EQ(size, sizeof(test4_output)); } int main(int argc, char* argv[]) { test1(); test2(); test3(); test4(); printf("PASS\n"); return 0; }