/* Cesar project {{{ * * Copyright (C) 2008 Spidcom * * <<>> * * }}} */ /** * \file interface/test/src/test-interface.c * \brief Test the interface module. * \ingroup interface * */ #include "common/std.h" #include "lib/test.h" #include "hal/hle/ipmbox.h" #include "cl/cl.h" #include "mac/sar/sar.h" #include "interface/interface.h" #include "interface/sniffer/sniffer.h" #include "interface/inc/interface.h" #include "interface/inc/context.h" #include #include #include "mac/common/config.h" #include interface_t *interface; bool test_buffer_add; bool test_copy; bool test_mme_recv; bool test_beacon_add; void test_interface_mme_buffer_add (void *user_data, u8 *buffer); void test_interface_mme_recv (void *user_data, mfs_rx_t *mfs, u8 *buffer, uint length, void *mme_recv, bool encrypted); void test_interface_beacon_add(void *user_data, pb_beacon_t *beacon); void ipmbox_tx (ipmbox_t *ctx, u32 *first_msg, uint length); void test_configure_sniffer (test_t test) { u8 buffer[3]; test_case_begin (test, "Configure the sniffer"); buffer[0] = INTERFACE_MODULE_SNIFFER; buffer[1] = 1; buffer[2] = 0x1; interface_configure (interface, buffer); test_begin (test, "Activate the mme tx") { test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) != true, "Error sniffer mme TX shall be active"); } test_end; buffer[2] = 0x3; interface_configure (interface, buffer); test_begin (test, "Anactivate the mme tx/rx") { test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) != true, "Error sniffer mme TX shall be active"); test_fail_if (interface_sniffer_mme_status_rx (interface->sniffer) != true, "Error sniffer mme RX shall be active"); } test_end; buffer[2] = 0x7; interface_configure (interface, buffer); test_begin (test, "Anactivate the mme tx/rx, beacon Tx") { test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) != true, "Error sniffer mme TX shall be active"); test_fail_if (interface_sniffer_mme_status_rx (interface->sniffer) != true, "Error sniffer mme RX shall be active"); test_fail_if (interface_sniffer_beacon_status_tx (interface->sniffer) != true, "Error sniffer beacon TX shall be active"); } test_end; buffer[2] = 0xF; interface_configure (interface, buffer); test_begin (test, "Sniffer conf") { test_fail_if (interface_sniffer_beacon_status_rx (interface->sniffer) != true, "Error sniffer beacon RX shall be active"); test_fail_if (interface_sniffer_beacon_status_tx (interface->sniffer) != true, "Error sniffer beacon TX shall be active"); test_fail_if (interface_sniffer_mme_status_rx (interface->sniffer) != true, "Error sniffer MME RX shall be active"); test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) != true, "Error sniffer MME TX shall be active"); } test_end; } void test_adding_buffers (test_t test) { u8 *real_buffer; u8 *buffer_got; test_case_begin (test, "Adding buffers to Control Plane"); test_begin (test, "add buffer") { real_buffer = malloc (2048); test_buffer_add = false; interface_buffer_add (interface, real_buffer); test_fail_if (test_buffer_add != true, "Error the buffer shall be kept by the interface"); } test_end; test_case_begin (test, "Adding and getting buffers"); test_begin (test, "add buffer") { real_buffer = malloc (2048); test_buffer_add = false; interface_buffer_work_add (interface, real_buffer); buffer_got = interface_buffer_work_get (interface); test_fail_if (buffer_got != real_buffer, "Error, buffer shall be the same"); if (buffer_got) free (buffer_got); } test_end; } void test_copy_recv_mme (test_t test) { u8 mme [1000]; uint i; for (i = 0; i < 1000; i++) { mme[i] = i; } test_copy = false; test_mme_recv = false; /* Add a buffer to the interface. */ interface_buffer_work_add (interface, malloc (2048)); interface_mme_recv (interface, NULL ,mme, 1000, NULL, false); test_case_begin (test, "Sniff a received MME"); test_begin (test, "verifing copy") { test_fail_if (test_copy == false, "MME not copied"); test_fail_if (test_mme_recv == false, "MME not send to the CP"); } test_end; interface_mme_recv_done (interface, interface); } void test_copy_send_mme(test_t test) { u8 mme [1000]; uint i; for (i = 0; i < 1000; i++) { mme[i] = i; } /* Add a buffer to the interface. */ interface_buffer_work_add (interface, malloc (2048)); test_copy = false; test_mme_recv = false; interface_mme_send (interface, mme, 1000, NULL); test_case_begin (test, "Sniff a MME tx"); test_begin (test, "verifing copy") { test_fail_if (test_copy == false, "MME not copied"); } test_end; } void test_copy_send_beacon (test_t test) { pb_beacon_t *beacon; mfs_tx_t *mfs; beacon = (pb_beacon_t *) blk_alloc_desc (); mfs = blk_alloc (); /* Add a buffer to the interface. */ interface_buffer_work_add (interface, malloc (2048)); test_copy = false; interface_beacon_prepare (interface, beacon, 0x123456789abcull, mfs, &beacon->first_data_word); test_case_begin (test, "Sniff a beacon tx"); test_begin (test, "verifing copy") { test_fail_if (test_copy == false, "beacon not copied"); } test_end; blk_release_desc ((blk_t *) beacon); blk_release (mfs); } void test_copy_recv_beacon (test_t test) { pb_beacon_t *beacon; beacon = (pb_beacon_t *) blk_alloc_desc (); /* Add a buffer to the interface. */ interface_buffer_work_add (interface, malloc (2048)); test_copy = false; test_beacon_add = false; interface_beacon_add (interface, beacon, NULL); test_case_begin (test, "Sniff a beacon tx"); test_begin (test, "verifing copy") { test_fail_if (test_copy == false, "beacon not copied"); test_fail_if (test_beacon_add == false, "beacon not transmitted"); } test_end; blk_release_desc ((blk_t *) beacon); } int main (void) { test_t test; cl_t *cl; sar_t *sar; hle_t *hle; mac_config_t mac_config; test_init (test, 0, NULL); cl = cl_init(NULL, NULL, NULL); sar = sar_init (NULL, NULL, NULL); hle = blk_alloc (); mac_config.sta_mac_address = 0x123456789abcull; interface = interface_init (hle, cl, sar, &mac_config); interface_callback_init (interface, test_interface_mme_recv, test_interface_mme_buffer_add, test_interface_beacon_add, interface); test_configure_sniffer (test); test_adding_buffers (test); test_copy_recv_mme (test); test_copy_send_mme(test); test_copy_send_beacon(test); test_copy_recv_beacon (test); interface_uninit (interface); blk_release (hle); test_result (test); HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1); return test_nb_failed (test) == 0 ? 0 : 1; } /** * Initialise the interface to add an Interface buffer. * * \param ctx the hle context. * \param cb the function to call on interface buffer reception. * \param user_data the user_data to provide on function call. */ void hle_init_interface_buffer_add_cb (hle_t *ctx, hle_interface_buffer_add_cb_t cb, void *user_data) { } void test_interface_mme_buffer_add (void *user_data, u8 *buffer) { dbg_assert (buffer); free (buffer); test_buffer_add = true; } void test_interface_mme_recv (void *user_data, mfs_rx_t *mfs, u8 *buffer, uint length, void *mme_recv, bool encryption) { dbg_assert (buffer); test_mme_recv = true; } void test_interface_beacon_add(void *user_data, pb_beacon_t *beacon) { dbg_assert (beacon); test_beacon_add = true; } void hle_ipmbox_send (hle_t *ctx, u32 *msg, uint length) { test_copy = true; }