Beispiel #1
0
TEST(dynmem, test_both_allocs_with_hole_usage) {
    uint16_t size = 112;
    mem_stat_t info;
    void *p[size];
    uint8_t *heap = (uint8_t*)malloc(size);
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());

    void *ptr = ns_dyn_mem_alloc(15);
    void *ptr2 = ns_dyn_mem_alloc(4);

    ns_dyn_mem_free(ptr);
    ns_dyn_mem_free(ptr2);
    CHECK(info.heap_sector_allocated_bytes == 0);

    void *ptr3 = ns_dyn_mem_temporary_alloc(15);
    void *ptr4 = ns_dyn_mem_temporary_alloc(5);

    ns_dyn_mem_free(ptr3);
    ns_dyn_mem_free(ptr4);


    CHECK(info.heap_sector_allocated_bytes == 0);

    free(heap);
}
Beispiel #2
0
uint8_t *sec_prot_lib_message_build(uint8_t *ptk, uint8_t *kde, uint16_t kde_len, eapol_pdu_t *eapol_pdu, uint16_t eapol_pdu_size, uint8_t header_size)
{
    uint8_t *eapol_pdu_frame = ns_dyn_mem_temporary_alloc(header_size + eapol_pdu_size);

    if (!eapol_pdu_frame) {
        return NULL;
    }

    uint8_t *eapol_kde = eapol_write_pdu_frame(eapol_pdu_frame + header_size, eapol_pdu);

    if (kde) {
        if (eapol_pdu->msg.key.key_information.encrypted_key_data) {
            size_t output_len = kde_len;
            if (nist_aes_key_wrap(1, &ptk[KEK_INDEX], 128, kde, kde_len - 8, eapol_kde, &output_len) < 0 || output_len != kde_len) {
                ns_dyn_mem_free(eapol_pdu_frame);
                return NULL;
            }
        } else {
            memcpy(eapol_kde, kde, kde_len);
        }
    }

    if (eapol_pdu->msg.key.key_information.key_mic) {
        uint8_t mic[EAPOL_KEY_MIC_LEN];
        if (hmac_sha1_calc(ptk, KCK_LEN, eapol_pdu_frame + header_size, eapol_pdu_size, mic) < 0) {
            ns_dyn_mem_free(eapol_pdu_frame);
            return NULL;
        }
        eapol_write_key_packet_mic(eapol_pdu_frame + header_size, mic);
    }

    return eapol_pdu_frame;
}
Beispiel #3
0
/**
 * Free allocated load balance class
 */
static void load_balance_class_free(lb_internal_t *api)
{
    //Clean heared networks
    ns_dyn_mem_free(api->notified_network);
    lb_border_router_api_free(api);
    ns_dyn_mem_free(api);
}
Beispiel #4
0
TEST(dynmem, not_negative_stats)
{
    uint16_t size = 1000;
    mem_stat_t info;
    uint8_t *heap = (uint8_t*)malloc(size);
    void *p;
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    CHECK(info.heap_sector_allocated_bytes == 0);
    ns_dyn_mem_alloc(8);
    p = ns_dyn_mem_alloc(8);
    ns_dyn_mem_alloc(8);
    CHECK(info.heap_sector_allocated_bytes >= 24);
    int16_t last_value = info.heap_sector_allocated_bytes;
    ns_dyn_mem_free(p);
    CHECK(info.heap_sector_allocated_bytes >= 16);
    CHECK(info.heap_sector_allocated_bytes < last_value);
    last_value = info.heap_sector_allocated_bytes;
    for (int i=0; i<10; i++) {
        p = ns_dyn_mem_alloc(1);
        ns_dyn_mem_free(p);
    }
    CHECK(info.heap_sector_allocated_bytes == last_value);
    free(heap);
}
Beispiel #5
0
nwk_pan_descriptor_t *mac_helper_free_pan_descriptions(nwk_pan_descriptor_t *nwk_cur_active)
{
    if (nwk_cur_active) {
        ns_dyn_mem_free(nwk_cur_active->pan_descriptor);
        ns_dyn_mem_free(nwk_cur_active->beacon_payload);
        ns_dyn_mem_free(nwk_cur_active);
    }
    return NULL;
}
Beispiel #6
0
TEST(dynmem, ns_dyn_mem_alloc)
{
    uint16_t size = 1000;
    mem_stat_t info;
    void *p[size];
    uint8_t *heap = (uint8_t*)malloc(size);
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    int block = 1;

    int i;
    for (i=0; i<size; i++) {
        p[i] = ns_dyn_mem_alloc(block);
        if (!p[i])
            break;
    }
    CHECK(!heap_have_failed());
    CHECK(info.heap_alloc_fail_cnt == 1);
    CHECK(info.heap_sector_alloc_cnt == i);
    CHECK(info.heap_sector_allocated_bytes == info.heap_sector_allocated_bytes_max);

    for (; i>=0; i--) {
        ns_dyn_mem_free(p[i]);
    }
    CHECK(!heap_have_failed());
    CHECK(info.heap_sector_alloc_cnt == 0);
    free(heap);
}
void coap_security_destroy(coap_security_t *sec){
    if( sec ){
        coap_security_handler_reset(sec);
        ns_dyn_mem_free(sec);
        sec = NULL;
    }
}
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port, SecureConnectionMode mode,
                                          send_cb *send_cb,
                                          receive_cb *receive_cb,
                                          start_timer_cb *start_timer_cb,
                                          timer_status_cb *timer_status_cb)
{
    if( !address_ptr || send_cb == NULL || receive_cb == NULL || start_timer_cb == NULL || timer_status_cb == NULL){
        return NULL;
    }
    coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t));
    if( !this ){
        return NULL;
    }
    memset(this, 0, sizeof(coap_security_t));
    if( -1 == coap_security_handler_init(this) ){
        ns_dyn_mem_free(this);
        return NULL;
    }
    this->_remote_port = port;
    memcpy(this->_remote_address, address_ptr, 16);
    this->_conn_mode = mode;
    memset(this->_pw, 0, 64);
    this->_pw_len = 0;
    this->_socket_id = socket_id;
    this->_timer_id = timer_id;
    this->_send_cb = send_cb;
    this->_receive_cb = receive_cb;
    this->_start_timer_cb = start_timer_cb;
    this->_timer_status_cb = timer_status_cb;

    return this;
}
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, SecureConnectionMode mode,
                                          send_cb *socket_cb,
                                          receive_cb *receive_data_cb,
                                          start_timer_cb *timer_start_cb,
                                          timer_status_cb *timer_stat_cb)
{
    if (socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) {
        return NULL;
    }
    coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t));
    if( !this ){
        return NULL;
    }
    memset(this, 0, sizeof(coap_security_t));
    if (-1 == coap_security_handler_init(this)) {
        ns_dyn_mem_free(this);
        return NULL;
    }
    this->_handle = handle;
    this->_conn_mode = mode;
    memset(this->_pw, 0, 64);
    this->_pw_len = 0;
    this->_socket_id = socket_id;
    this->_timer_id = timer_id;
    this->_send_cb = socket_cb;
    this->_receive_cb = receive_data_cb;
    this->_start_timer_cb = timer_start_cb;
    this->_timer_status_cb = timer_stat_cb;

    return this;
}
Beispiel #10
0
uint8_t *sec_prot_lib_message_handle(uint8_t *ptk, uint16_t *kde_len, eapol_pdu_t *eapol_pdu)
{
    if (eapol_pdu->msg.key.key_data_length == 0 || eapol_pdu->msg.key.key_data == NULL) {
        return NULL;
    }

    uint8_t *key_data = eapol_pdu->msg.key.key_data;
    uint16_t key_data_len = eapol_pdu->msg.key.key_data_length;

    uint8_t *kde = ns_dyn_mem_temporary_alloc(key_data_len);
    *kde_len = key_data_len;

    if (eapol_pdu->msg.key.key_information.encrypted_key_data) {
        size_t output_len = eapol_pdu->msg.key.key_data_length;
        if (nist_aes_key_wrap(0, &ptk[KEK_INDEX], 128, key_data, key_data_len, kde, &output_len) < 0 || output_len != (size_t) key_data_len - 8) {
            ns_dyn_mem_free(kde);
            return NULL;
        }
        *kde_len = output_len;
    } else {
        memcpy(kde, key_data, *kde_len);
    }

    return kde;
}
Beispiel #11
0
// XXX: a ns_dyn_mem_realloc() would be nice to have
uint8_t *mac_helper_beacon_payload_reallocate(protocol_interface_info_entry_t *interface, uint8_t len)
{

    if (len == interface->mac_parameters->mac_beacon_payload_size) {
        // no change to size, return the existing buff
        //Set allways length to zero for safe beacon payload manipulate
        mac_helper_beacon_payload_length_set_to_mac(interface, 0);
        return interface->mac_parameters->mac_beacon_payload;
    }

    if (len == 0) {
        //SET MAC beacon payload to length to zero
        mac_helper_beacon_payload_length_set_to_mac(interface, 0);
        ns_dyn_mem_free(interface->mac_parameters->mac_beacon_payload);
        interface->mac_parameters->mac_beacon_payload = NULL;
        interface->mac_parameters->mac_beacon_payload_size = 0;
        return NULL;
    }

    tr_debug("mac_helper_beacon_payload_reallocate, old len: %d, new: %d", interface->mac_parameters->mac_beacon_payload_size, len);

    uint8_t *temp_buff = ns_dyn_mem_alloc(len);

    if (temp_buff == NULL) {
        // no need to proceed, could not allocate more space
        return NULL;
    }

    //SET MAC beacon payload to length to zero
    mac_helper_beacon_payload_length_set_to_mac(interface, 0);

    // copy data into new buffer before freeing old one
    if (interface->mac_parameters->mac_beacon_payload_size > 0) {
        const uint8_t min_len = MIN(len, interface->mac_parameters->mac_beacon_payload_size);

        memcpy(temp_buff, interface->mac_parameters->mac_beacon_payload, min_len);
        ns_dyn_mem_free(interface->mac_parameters->mac_beacon_payload);
    }

    //Set New Length and pointer to MAC

    interface->mac_parameters->mac_beacon_payload = temp_buff;
    interface->mac_parameters->mac_beacon_payload_size = len;

    return interface->mac_parameters->mac_beacon_payload;
}
Beispiel #12
0
/*
 * \brief Configure mesh network
 *
 */
void thread_tasklet_configure_and_connect_to_network(void)
{
    int8_t status;
    link_configuration_s* temp_link_config=NULL;


    if (MBED_CONF_MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_MINIMAL_END_DEVICE) {
            thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_HOST;
    }
    else if (MBED_CONF_MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_SLEEPY_END_DEVICE) {
        thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_SLEEPY_HOST;
    } else {
        thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_ROUTER;
    }

    arm_nwk_interface_configure_6lowpan_bootstrap_set(
        thread_tasklet_data_ptr->nwk_if_id,
        thread_tasklet_data_ptr->operating_mode,
        NET_6LOWPAN_THREAD);
        
    thread_tasklet_data_ptr->channel_list.channel_page = (channel_page_e)MBED_CONF_MBED_MESH_API_THREAD_CONFIG_CHANNEL_PAGE;
    thread_tasklet_data_ptr->channel_list.channel_mask[0] = MBED_CONF_MBED_MESH_API_THREAD_CONFIG_CHANNEL_MASK;
    
    TRACE_DETAIL("channel page: %d", thread_tasklet_data_ptr->channel_list.channel_page);
    TRACE_DETAIL("channel mask: 0x%.8lx", thread_tasklet_data_ptr->channel_list.channel_mask[0]);

    // PSKd
    const char PSKd[] = MBED_CONF_MBED_MESH_API_THREAD_PSKD;
    MBED_ASSERT(sizeof(PSKd) > 5 && sizeof(PSKd) < 33);

    char *dyn_buf = ns_dyn_mem_alloc(sizeof(PSKd));
    strcpy(dyn_buf, PSKd);
    ns_dyn_mem_free(device_configuration.PSKd_ptr);
    device_configuration.PSKd_ptr = (uint8_t*)dyn_buf;
    device_configuration.PSKd_len = sizeof(PSKd) - 1;  
    
    if (true == MBED_CONF_MBED_MESH_API_THREAD_USE_STATIC_LINK_CONFIG) {
        read_link_configuration();
        temp_link_config = &thread_tasklet_data_ptr->link_config;
    }
    
    thread_management_node_init(thread_tasklet_data_ptr->nwk_if_id,
                           &thread_tasklet_data_ptr->channel_list,
                           &device_configuration,
                           temp_link_config);

    status = arm_nwk_interface_up(thread_tasklet_data_ptr->nwk_if_id);

    if (status >= 0) {
        thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED;
        tr_info("Start Thread bootstrap (%s mode)", thread_tasklet_data_ptr->operating_mode == NET_6LOWPAN_SLEEPY_HOST ? "SED" : "Router");
    } else {
        thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
        tr_err("Bootstrap start failed, %d", status);
        thread_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED);
    }
}
Beispiel #13
0
/**
 * Load balance border router class free
 */
static int8_t lb_border_router_api_free(lb_internal_t *api)
{
    if (api->lb_border_router) {
        ns_dyn_mem_free(api->lb_border_router);
        api->lb_border_router = NULL;
        return 0;
    }
    return -1;
}
Beispiel #14
0
TEST(dynmem, double_free)
{
    uint16_t size = 1000;
    mem_stat_t info;
    uint8_t *heap = (uint8_t*)malloc(size);
    void *p;
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    p = ns_dyn_mem_alloc(100);
    CHECK(p);
    ns_dyn_mem_free(p);
    CHECK(!heap_have_failed());
    ns_dyn_mem_free(p);
    CHECK(heap_have_failed());
    CHECK(NS_DYN_MEM_DOUBLE_FREE == current_heap_error);
    free(heap);
}
Beispiel #15
0
TEST(dynmem, test_free_corrupted_next_block) {
    uint16_t size = 1000;
    uint8_t *heap = (uint8_t*)malloc(size);
    uint8_t *p;
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, NULL);
    CHECK(!heap_have_failed());

    int *ptr = (int *)ns_dyn_mem_temporary_alloc(4);
    int *ptr2 = (int *)ns_dyn_mem_temporary_alloc(4);
    ns_dyn_mem_free(ptr);
    ptr = ptr2 + 2;
    *ptr = -2;
    ns_dyn_mem_free(ptr2);

    CHECK(NS_DYN_MEM_HEAP_SECTOR_CORRUPTED == current_heap_error);

    free(heap);
}
Beispiel #16
0
int8_t net_nvm_wpan_params_storage_disable(int8_t interface_id)
{
    protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
    if (!cur || !cur->nwk_wpan_nvm_api) {
        return -1;
    }
    ns_dyn_mem_free(cur->nwk_wpan_nvm_api);
    cur->nwk_wpan_nvm_api = NULL;
    return 0;

}
Beispiel #17
0
TEST(dynmem, not_from_this_heap)
{
    uint16_t size = 1000;
    mem_stat_t info;
    uint8_t *heap = (uint8_t*)malloc(size);
    uint8_t *p;
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    p = (uint8_t *)ns_dyn_mem_alloc(100);
    CHECK(p);
    ns_dyn_mem_free(&heap[-1]);
    CHECK(heap_have_failed());
    CHECK(NS_DYN_MEM_POINTER_NOT_VALID == current_heap_error);
    reset_heap_error();
    ns_dyn_mem_free(&heap[1001]);
    CHECK(heap_have_failed());
    CHECK(NS_DYN_MEM_POINTER_NOT_VALID == current_heap_error);
    free(heap);
}
Beispiel #18
0
TEST(dynmem, middle_free)
{
    uint16_t size = 1000;
    mem_stat_t info;
    uint8_t *heap = (uint8_t*)malloc(size);
    void *p[3];
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    for (int i=0; i<3; i++) {
        p[i] = ns_dyn_mem_temporary_alloc(100);
        CHECK(p);
    }
    ns_dyn_mem_free(p[1]);
    CHECK(!heap_have_failed());
    ns_dyn_mem_free(p[0]);
    CHECK(!heap_have_failed());
    ns_dyn_mem_free(p[2]);
    CHECK(!heap_have_failed());
    free(heap);
}
Beispiel #19
0
int ns_file_system_set_root_path(const char *root_path)
{
    char *new_root_path;

    if (root_path == NULL) {
        // File system usage disabled
        ns_dyn_mem_free(file_system_root);
        file_system_root = NULL;
        return 0;
    }

    new_root_path = ns_dyn_mem_alloc(strlen(root_path) + 1);
    if (!new_root_path) {
        // mem alloc failed
        return -2;
    }

    ns_dyn_mem_free(file_system_root);
    file_system_root = new_root_path;
    strcpy(file_system_root, root_path);

    return 0;
}
Beispiel #20
0
TEST(dynmem, test_invalid_pointer_freed) {
    uint16_t size = 92;
    uint8_t *heap = (uint8_t*)malloc(size);
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, NULL);
    int *ptr = (int *)ns_dyn_mem_alloc(4);
    ptr--;
    *ptr = 16;
    ptr++;
    ns_dyn_mem_free(ptr);
    CHECK(NS_DYN_MEM_POINTER_NOT_VALID == current_heap_error);

    free(heap);
}
Beispiel #21
0
uint8_t thread_tasklet_device_pskd_set(const char *pskd)
{
    int len = strlen(pskd);
    if(len < 6 || len > 32) {
        return MESH_ERROR_PARAM;
    }
    char *dyn_buf = ns_dyn_mem_alloc(strlen(pskd)+1);
    if (!dyn_buf) {
        return MESH_ERROR_MEMORY;
    }
    strcpy(dyn_buf, pskd);
    ns_dyn_mem_free(device_configuration.PSKd_ptr);
    device_configuration.PSKd_ptr = (uint8_t*)dyn_buf;
    device_configuration.PSKd_len = strlen(pskd);
    return 0;
}
Beispiel #22
0
/**
 * Allocate notified network class
 */
static bool load_balance_network_class_allocate(lb_internal_t *lb_store_ptr, uint16_t beacon_max_payload_length)
{
    ns_dyn_mem_free(lb_store_ptr->notified_network);
    lb_store_ptr->beacon_max_payload_length = 0;
    if (beacon_max_payload_length) {
        lb_store_ptr->notified_network = ns_dyn_mem_alloc(sizeof(lb_network_t) + beacon_max_payload_length);
        if (!lb_store_ptr->notified_network) {
            return false;
        }
        lb_store_ptr->notified_network->network_switch_accepted = false;
        lb_store_ptr->notified_network->priority = 0xff;
        lb_store_ptr->notified_network->state = LB_NWK_SWITCH_IDLE;
        lb_store_ptr->notified_network->state_timer = 0;
        lb_store_ptr->beacon_max_payload_length = beacon_max_payload_length;

    }
    return true;
}
Beispiel #23
0
TEST(dynmem, over_by_one)
{
    uint16_t size = 1000;
    mem_stat_t info;
    uint8_t *heap = (uint8_t*)malloc(size);
    uint8_t *p;
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    p = (uint8_t *)ns_dyn_mem_alloc(100);
    CHECK(p);
    p[100] = 0xff;
    ns_dyn_mem_free(p);
    CHECK(heap_have_failed());
    CHECK(NS_DYN_MEM_HEAP_SECTOR_CORRUPTED == current_heap_error);
    free(heap);
}
int entropy_poll( void *ctx, unsigned char *output, size_t len,
                           size_t *olen )
{
    (void)ctx;
    //TODO: change to more secure random
    randLIB_seed_random();
    char *c = (char*)ns_dyn_mem_temporary_alloc(len);
    if( !c ){
        return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
    }
    memset(c, 0, len);
    for(uint16_t i=0; i < len; i++){
        *(c + i) = (char)randLIB_get_8bit();
    }
    memmove(output, c, len);
    *olen = len;

    ns_dyn_mem_free(c);
    return( 0 );
}
Beispiel #25
0
TEST(dynmem, diff_sizes)
{
    uint16_t size = 1000;
    mem_stat_t info;
    void *p;
    uint8_t *heap = (uint8_t*)malloc(size);
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    int i;
    for (i=1; i<(size-72); i++) {
        p = ns_dyn_mem_temporary_alloc(i);
        CHECK(p);
        ns_dyn_mem_free(p);
        CHECK(!heap_have_failed());
    }
    CHECK(!heap_have_failed());
    CHECK(info.heap_sector_alloc_cnt == 0);
    free(heap);
}
Beispiel #26
0
int8_t mac_helper_nwk_id_filter_set(const uint8_t *nw_id, nwk_filter_params_s *filter)
{
    if (!filter) {
        return -1;
    }
    int8_t ret_val = 0;
    if (nw_id) {
        if (filter->beacon_nwk_id_filter == 0) {
            filter->beacon_nwk_id_filter = ns_dyn_mem_alloc(16);
        }
        if (filter->beacon_nwk_id_filter) {
            memcpy(filter->beacon_nwk_id_filter, nw_id, 16);
        } else {
            ret_val = -1;
        }
    } else {
        ns_dyn_mem_free(filter->beacon_nwk_id_filter);
        filter->beacon_nwk_id_filter = 0;
    }
    return ret_val;
}
Beispiel #27
0
TEST(dynmem, no_big_enough_sector) {
    uint16_t size = 112;
    mem_stat_t info;
    uint8_t *heap = (uint8_t*)malloc(size);
    uint8_t *ptr = heap;
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());
    int *pt = (int *)ns_dyn_mem_alloc(8);
    pt = (int *)ns_dyn_mem_alloc(8);
    ns_dyn_mem_alloc(8);
    ns_dyn_mem_temporary_alloc(8);
    ns_dyn_mem_temporary_alloc(8);

    ns_dyn_mem_free(pt);

    pt = (int *)ns_dyn_mem_temporary_alloc(32);
    CHECK(NULL == pt);
    free(heap);
}
Beispiel #28
0
int8_t ws_pae_controller_delete(protocol_interface_info_entry_t *interface_ptr)
{
    if (!interface_ptr) {
        return -1;
    }

    ws_pae_controller_stop(interface_ptr);

    pae_controller_t *controller = ws_pae_controller_get(interface_ptr);
    if (!controller) {
        return -1;
    }

    ns_list_remove(&pae_controller_list, controller);

    sec_prot_certs_delete(&controller->certs);

    ns_dyn_mem_free(controller);

    return 0;
}
Beispiel #29
0
TEST(dynmem, ns_dyn_mem_temporary_alloc_with_heap_threshold)
{
    uint16_t size = 1000;
    mem_stat_t info;
    void *p1, *p2;
    int ret_val;
    uint8_t *heap = (uint8_t*)malloc(size);
    CHECK(NULL != heap);
    reset_heap_error();
    ns_dyn_mem_init(heap, size, &heap_fail_callback, &info);
    CHECK(!heap_have_failed());

    // test1: temporary alloc will fail if there is less than 5% heap free
    p1 = ns_dyn_mem_temporary_alloc((size-72)*0.96);
    CHECK(!heap_have_failed());
    CHECK(p1);
    p2 = ns_dyn_mem_temporary_alloc((size-72)*0.02);
    CHECK(p2 == NULL);
    CHECK(!heap_have_failed());
    CHECK(info.heap_alloc_fail_cnt == 1);

    // Test2, disable threshold feature and try p2 allocation again
    ns_dyn_mem_set_temporary_alloc_free_heap_threshold(0, 0);
    p2 = ns_dyn_mem_temporary_alloc((size-72)*0.02);
    CHECK(!heap_have_failed());
    CHECK(p2);
    ns_dyn_mem_free(p1);
    ns_dyn_mem_free(p2);
    CHECK(info.heap_alloc_fail_cnt == 1);
    CHECK(info.heap_sector_alloc_cnt == 0);

    // Test3, enable feature by free heap percentage
    ns_dyn_mem_set_temporary_alloc_free_heap_threshold(40, 0);
    p1 = ns_dyn_mem_temporary_alloc((size-72)*0.65);
    CHECK(p1);
    p2 = ns_dyn_mem_temporary_alloc((size-72)*0.10);
    CHECK(p2==NULL);
    ns_dyn_mem_free(p1);
    CHECK(!heap_have_failed());
    CHECK(info.heap_alloc_fail_cnt == 2);
    CHECK(info.heap_sector_alloc_cnt == 0);

    // Test4, enable feature by free heap amount
    ns_dyn_mem_set_temporary_alloc_free_heap_threshold(0, 200);
    p1 = ns_dyn_mem_temporary_alloc(size-72-100 /*828 bytes */);
    CHECK(p1);
    p2 = ns_dyn_mem_temporary_alloc(1);
    CHECK(p2==NULL);
    ns_dyn_mem_free(p1);

    // Test5, illegal API parameters
    ret_val = ns_dyn_mem_set_temporary_alloc_free_heap_threshold(0, size/2);
    CHECK(ret_val==-2);
    ret_val = ns_dyn_mem_set_temporary_alloc_free_heap_threshold(0, size*2);
    CHECK(ret_val==-2);
    ret_val = ns_dyn_mem_set_temporary_alloc_free_heap_threshold(51, 0);
    CHECK(ret_val==-2);
    ret_val = ns_dyn_mem_set_temporary_alloc_free_heap_threshold(255, 0);
    CHECK(ret_val==-2);

    CHECK(!heap_have_failed());
    CHECK(info.heap_alloc_fail_cnt == 3);
    CHECK(info.heap_sector_alloc_cnt == 0);
    free(heap);

    // Test6, feature is disabled if info is not set
    heap = (uint8_t*)malloc(size);
    CHECK(NULL != heap);
    ns_dyn_mem_init(heap, size, &heap_fail_callback, NULL);
    ret_val = ns_dyn_mem_set_temporary_alloc_free_heap_threshold(0, 0);
    CHECK(ret_val==-1);
    CHECK(!heap_have_failed());
    free(heap);
}
Beispiel #30
0
//NOTE! This test must be last!
TEST(dynmem, uninitialized_test){
    void *p = ns_dyn_mem_alloc(4);
    ns_dyn_mem_free(p);
    CHECK(p == NULL);
}