END_TEST START_TEST(tc_tftp_send_tx_req) { char filename[14] = "some filename"; example_session.socket = &example_socket; called_user_cb = 0; called_pico_socket_close = 0; called_sendto = 0; #ifdef PICO_FAULTY example_session.callback = tftp_user_cb; /* send_req must call error cb when out of memory */ pico_set_mm_failure(1); tftp_send_tx_req(&example_session, NULL, 0, filename); fail_if(called_user_cb < 1); fail_if(called_sendto > 0); #endif expected_opcode = PICO_TFTP_WRQ; tftp_send_tx_req(&example_session, NULL, 0, NULL); fail_if(called_sendto > 0); /* Calling with filename = NULL: not good */ tftp_send_tx_req(&example_session, NULL, 0, filename); fail_if(called_sendto < 0); }
END_TEST START_TEST(tc_pico_frame_copy) { struct pico_frame *f = pico_frame_alloc(FRAME_SIZE); struct pico_frame *c1, *c2, *c3; (void)c3; fail_if(!f); fail_if(!f->buffer); fail_if(*f->usage_count != 1); /* First copy */ c1 = pico_frame_copy(f); fail_if(!c1); fail_if(!c1->buffer); fail_if(!c1->usage_count); fail_if (c1->buffer != f->buffer); fail_if(c1->usage_count != f->usage_count); fail_if(*c1->usage_count != 2); fail_if(*f->usage_count != 2); fail_if(c1->start != c1->buffer); fail_if(c1->len != c1->buffer_len); fail_if(c1->len != FRAME_SIZE); /* Second copy */ c2 = pico_frame_copy(f); fail_if (c2->buffer != f->buffer); fail_if(c2->usage_count != f->usage_count); fail_if(*c2->usage_count != 3); fail_if(*f->usage_count != 3); fail_if(c2->start != c2->buffer); fail_if(c2->len != c2->buffer_len); fail_if(c2->len != FRAME_SIZE); #ifdef PICO_FAULTY printf("Testing with faulty memory in frame_copy (1)\n"); pico_set_mm_failure(1); c3 = pico_frame_copy(f); fail_if(c3); fail_if(!f); #endif /* Discard 1 */ pico_frame_discard(c1); fail_if(*f->usage_count != 2); /* Discard 2 */ pico_frame_discard(c2); fail_if(*f->usage_count != 1); pico_frame_discard(f); }
END_TEST START_TEST(tc_pico_frame_grow) { struct pico_frame *f = pico_frame_alloc(3); fail_if(f->buffer_len != 3); /* Ensure that the usage_count starts at byte 4, for good alignment */ fail_if(((void*)f->usage_count - (void *)f->buffer) != 4); ((uint8_t *)f->buffer)[0] = 'a'; ((uint8_t *)f->buffer)[1] = 'b'; ((uint8_t *)f->buffer)[2] = 'c'; *f->usage_count = 12; /* First, the failing cases. */ fail_if(pico_frame_grow(NULL, 30) == 0); fail_if(pico_frame_grow(f, 2) == 0); f->flags = 0; pico_set_mm_failure(1); fail_if(pico_frame_grow(f, 21) == 0); /* Now, the good one. */ fail_if(pico_frame_grow(f, 21) != 0); fail_if(f->buffer_len != 21); fail_if(((void *)f->usage_count - (void *)f->buffer) != 24); fail_if(((uint8_t *)f->buffer)[0] != 'a'); fail_if(((uint8_t *)f->buffer)[1] != 'b'); fail_if(((uint8_t *)f->buffer)[2] != 'c'); fail_if(*f->usage_count != 12); *f->usage_count = 1; pico_frame_discard(f); f = pico_frame_alloc_skeleton(10, 1); fail_if(!f); fail_if(f->buffer); fail_if(!f->flags); f->buffer = PICO_ZALLOC(10); fail_if(pico_frame_grow(f, 22) != 0); fail_if (f->flags); pico_frame_discard(f); }
END_TEST START_TEST(tc_tftp_send_ack) { example_session.socket = &example_socket; #ifdef PICO_FAULTY /* send_ack must not segfault when out of memory */ pico_set_mm_failure(1); tftp_send_ack(&example_session); fail_if(called_sendto > 0); #endif expected_opcode = PICO_TFTP_ACK; tftp_send_ack(&example_session); fail_if(called_sendto < 1); }
END_TEST START_TEST(tc_pico_loop_create) { #ifdef PICO_FAULTY printf("Testing with faulty memory in pico_loop_create (1)\n"); pico_set_mm_failure(1); fail_if(pico_loop_create() != NULL); #endif fail = 1; fail_if(pico_loop_create() != NULL); fail = 0; fail_if(pico_loop_create() == NULL); }
END_TEST START_TEST(tc_pico_frame_deepcopy) { struct pico_frame *f = pico_frame_alloc(FRAME_SIZE); struct pico_frame *dc = pico_frame_deepcopy(f); fail_if(*f->usage_count != 1); fail_if(*dc->usage_count != 1); fail_if(dc->buffer == f->buffer); #ifdef PICO_FAULTY printf("Testing with faulty memory in frame_deepcopy (1)\n"); pico_set_mm_failure(1); dc = pico_frame_deepcopy(f); fail_if(dc); fail_if(!f); #endif }