diff options
author | Tom Gundersen <teg@jklm.no> | 2014-05-24 21:04:27 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-06-13 16:53:13 +0200 |
commit | be077570f779664ed87b50f60608df9fbe258821 (patch) | |
tree | d522c30be1b95b7cafe4ada621a0114920b915a9 /src/libsystemd-network/test-dhcp-server.c | |
parent | 3a864fe4a894745ac61f1ecabd7cadf04139a284 (diff) |
sd-dhcp-server: add basic message handling and verification
Diffstat (limited to 'src/libsystemd-network/test-dhcp-server.c')
-rw-r--r-- | src/libsystemd-network/test-dhcp-server.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index 2feb124036..dd0f29a04c 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -20,6 +20,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ +#include <netinet/if_ether.h> #include <assert.h> #include <errno.h> @@ -54,6 +55,61 @@ static void test_basic(sd_event *event) { assert_se(sd_dhcp_server_start(server) >= 0); } +static void test_message_handler(void) { + _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL; + struct { + DHCPMessage message; + struct { + uint8_t code; + uint8_t length; + uint8_t type; + } _packed_ option_type; + uint8_t end; + } _packed_ test = { + .message.op = BOOTREQUEST, + .message.htype = ARPHRD_ETHER, + .message.hlen = ETHER_ADDR_LEN, + .option_type.code = DHCP_OPTION_MESSAGE_TYPE, + .option_type.length = 1, + .option_type.type = DHCP_DISCOVER, + .end = DHCP_OPTION_END, + }; + + assert_se(sd_dhcp_server_new(&server, 1) >= 0); + + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); + + test.end = 0; + /* TODO, shouldn't this fail? */ + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); + test.end = DHCP_OPTION_END; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); + + test.option_type.code = 0; + test.option_type.length = 0; + test.option_type.type = 0; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0); + test.option_type.code = DHCP_OPTION_MESSAGE_TYPE; + test.option_type.length = 1; + test.option_type.type = DHCP_DISCOVER; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); + + test.message.op = 0; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0); + test.message.op = BOOTREQUEST; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); + + test.message.htype = 0; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0); + test.message.htype = ARPHRD_ETHER; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); + + test.message.hlen = 0; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0); + test.message.hlen = ETHER_ADDR_LEN; + assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1); +} + int main(int argc, char *argv[]) { _cleanup_event_unref_ sd_event *e; @@ -64,6 +120,7 @@ int main(int argc, char *argv[]) { assert_se(sd_event_new(&e) >= 0); test_basic(e); + test_message_handler(); return 0; } |