/** Create a new socket. * * @param[in,out] local_sockets Local sockets to be updated. * @param[in] sess Application session. * @param[in] specific_data Socket specific data. * @param[in,out] socket_id New socket identifier. A new identifier * is chosen if set to zero or negative. * A negative identifier is chosen if set * to negative. * * @return EOK on success. * @return EINVAL if the socket_id parameter is NULL. * @return ENOMEM if there is not enough memory left. * */ int socket_create(socket_cores_t *local_sockets, async_sess_t* sess, void *specific_data, int *socket_id) { socket_core_t *socket; int positive; int rc; if (!socket_id) return EINVAL; /* Store the socket */ if (*socket_id <= 0) { positive = (*socket_id == 0); *socket_id = socket_generate_new_id(local_sockets, positive); if (*socket_id <= 0) return *socket_id; if (!positive) *socket_id *= -1; } else if(socket_cores_find(local_sockets, *socket_id)) { return EEXIST; } socket = (socket_core_t *) malloc(sizeof(*socket)); if (!socket) return ENOMEM; /* Initialize */ socket->sess = sess; socket->port = -1; socket->key = NULL; socket->key_length = 0; socket->specific_data = specific_data; rc = dyn_fifo_initialize(&socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE); if (rc != EOK) { free(socket); return rc; } socket->socket_id = *socket_id; rc = socket_cores_add(local_sockets, socket->socket_id, socket); if (rc < 0) { dyn_fifo_destroy(&socket->accepted); free(socket); return rc; } return EOK; }
/** Self-test start function. * * Run all self-tests. * * @returns EOK on success. * @returns The first error occurred. * */ int self_test(void) { printf("Running networking self-tests\n"); printf("\nChar map test"); char_map_t cm; TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL); TEST(char_map_initialize(&cm), EOK); TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_add(&cm, "bla", 0, 1), EOK); TEST(char_map_find(&cm, "bla", 0), 1); TEST(char_map_add(&cm, "bla", 0, 10), EEXISTS); TEST(char_map_update(&cm, "bla", 0, 2), EOK); TEST(char_map_find(&cm, "bla", 0), 2); TEST(char_map_update(&cm, "ucho", 0, 2), EOK); TEST(char_map_exclude(&cm, "bla", 0), 2); TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_find(&cm, "ucho", 0), 2); TEST(char_map_update(&cm, "ucho", 0, 3), EOK); TEST(char_map_find(&cm, "ucho", 0), 3); TEST(char_map_add(&cm, "blabla", 0, 5), EOK); TEST(char_map_find(&cm, "blabla", 0), 5); TEST(char_map_add(&cm, "bla", 0, 6), EOK); TEST(char_map_find(&cm, "bla", 0), 6); TEST(char_map_exclude(&cm, "bla", 0), 6); TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_find(&cm, "blabla", 0), 5); TEST(char_map_add(&cm, "auto", 0, 7), EOK); TEST(char_map_find(&cm, "auto", 0), 7); TEST(char_map_add(&cm, "kara", 0, 8), EOK); TEST(char_map_find(&cm, "kara", 0), 8); TEST(char_map_add(&cm, "nic", 0, 9), EOK); TEST(char_map_find(&cm, "nic", 0), 9); TEST(char_map_find(&cm, "blabla", 0), 5); TEST(char_map_add(&cm, "micnicnic", 5, 9), EOK); TEST(char_map_find(&cm, "micni", 0), 9); TEST(char_map_find(&cm, "micnicn", 5), 9); TEST(char_map_add(&cm, "\x10\x0\x2\x2", 4, 15), EOK); TEST(char_map_find(&cm, "\x10\x0\x2\x2", 4), 15); TEST((char_map_destroy(&cm), EOK), EOK); TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL); printf("\nCRC computation test"); uint32_t value; TEST(value = ~compute_crc32(~0, "123456789", 8 * 9), 0xcbf43926); TEST(value = ~compute_crc32(~0, "1", 8), 0x83dcefb7); TEST(value = ~compute_crc32(~0, "12", 8 * 2), 0x4f5344cd); TEST(value = ~compute_crc32(~0, "123", 8 * 3), 0x884863d2); TEST(value = ~compute_crc32(~0, "1234", 8 * 4), 0x9be3e0a3); TEST(value = ~compute_crc32(~0, "12345678", 8 * 8), 0x9ae0daaf); TEST(value = ~compute_crc32(~0, "ahoj pane", 8 * 9), 0x5fc3d706); printf("\nDynamic fifo test"); dyn_fifo_t fifo; TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL); TEST(dyn_fifo_initialize(&fifo, 1), EOK); TEST(dyn_fifo_push(&fifo, 1, 0), EOK); TEST(dyn_fifo_pop(&fifo), 1); TEST(dyn_fifo_pop(&fifo), ENOENT); TEST(dyn_fifo_push(&fifo, 2, 1), EOK); TEST(dyn_fifo_push(&fifo, 3, 1), ENOMEM); TEST(dyn_fifo_push(&fifo, 3, 0), EOK); TEST(dyn_fifo_pop(&fifo), 2); TEST(dyn_fifo_pop(&fifo), 3); TEST(dyn_fifo_push(&fifo, 4, 2), EOK); TEST(dyn_fifo_push(&fifo, 5, 2), EOK); TEST(dyn_fifo_push(&fifo, 6, 2), ENOMEM); TEST(dyn_fifo_push(&fifo, 6, 5), EOK); TEST(dyn_fifo_push(&fifo, 7, 5), EOK); TEST(dyn_fifo_pop(&fifo), 4); TEST(dyn_fifo_pop(&fifo), 5); TEST(dyn_fifo_push(&fifo, 8, 5), EOK); TEST(dyn_fifo_push(&fifo, 9, 5), EOK); TEST(dyn_fifo_push(&fifo, 10, 6), EOK); TEST(dyn_fifo_push(&fifo, 11, 6), EOK); TEST(dyn_fifo_pop(&fifo), 6); TEST(dyn_fifo_pop(&fifo), 7); TEST(dyn_fifo_push(&fifo, 12, 6), EOK); TEST(dyn_fifo_push(&fifo, 13, 6), EOK); TEST(dyn_fifo_push(&fifo, 14, 6), ENOMEM); TEST(dyn_fifo_push(&fifo, 14, 8), EOK); TEST(dyn_fifo_pop(&fifo), 8); TEST(dyn_fifo_pop(&fifo), 9); TEST(dyn_fifo_pop(&fifo), 10); TEST(dyn_fifo_pop(&fifo), 11); TEST(dyn_fifo_pop(&fifo), 12); TEST(dyn_fifo_pop(&fifo), 13); TEST(dyn_fifo_pop(&fifo), 14); TEST(dyn_fifo_destroy(&fifo), EOK); TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL); printf("\nGeneric char map test"); int *x; int *y; int *z; int *u; int *v; int *w; XMALLOC(x, int); XMALLOC(y, int); XMALLOC(z, int); XMALLOC(u, int); XMALLOC(v, int); XMALLOC(w, int); int_char_map_t icm; icm.magic = 0; TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL); TEST(int_char_map_initialize(&icm), EOK); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST(int_char_map_find(&icm, "bla", 0), NULL); TEST(int_char_map_add(&icm, "bla", 0, x), EOK); TEST(int_char_map_find(&icm, "bla", 0), x); TEST(int_char_map_add(&icm, "bla", 0, y), EEXISTS); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST(int_char_map_add(&icm, "blabla", 0, v), EOK); TEST(int_char_map_find(&icm, "blabla", 0), v); TEST(int_char_map_add(&icm, "bla", 0, w), EOK); TEST(int_char_map_find(&icm, "bla", 0), w); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST(int_char_map_find(&icm, "bla", 0), NULL); TEST(int_char_map_find(&icm, "blabla", 0), v); TEST(int_char_map_add(&icm, "auto", 0, u), EOK); TEST(int_char_map_find(&icm, "auto", 0), u); TEST((int_char_map_destroy(&icm), EOK), EOK); TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL); printf("\nGeneric field test"); XMALLOC(x, int); XMALLOC(y, int); XMALLOC(z, int); XMALLOC(u, int); XMALLOC(v, int); XMALLOC(w, int); int_field_t gf; gf.magic = 0; TEST(int_field_add(&gf, x), EINVAL); TEST(int_field_count(&gf), -1); TEST(int_field_initialize(&gf), EOK); TEST(int_field_count(&gf), 0); TEST(int_field_get_index(&gf, 1), NULL); TEST(int_field_add(&gf, x), 0); TEST(int_field_get_index(&gf, 0), x); TEST((int_field_exclude_index(&gf, 0), EOK), EOK); TEST(int_field_get_index(&gf, 0), NULL); TEST(int_field_add(&gf, y), 1); TEST(int_field_get_index(&gf, 1), y); TEST(int_field_add(&gf, z), 2); TEST(int_field_get_index(&gf, 2), z); TEST(int_field_get_index(&gf, 1), y); TEST(int_field_count(&gf), 3); TEST(int_field_add(&gf, u), 3); TEST(int_field_get_index(&gf, 3), u); TEST(int_field_add(&gf, v), 4); TEST(int_field_get_index(&gf, 4), v); TEST(int_field_add(&gf, w), 5); TEST(int_field_get_index(&gf, 5), w); TEST(int_field_count(&gf), 6); TEST((int_field_exclude_index(&gf, 1), EOK), EOK); TEST(int_field_get_index(&gf, 1), NULL); TEST(int_field_get_index(&gf, 3), u); TEST((int_field_exclude_index(&gf, 7), EOK), EOK); TEST(int_field_get_index(&gf, 3), u); TEST(int_field_get_index(&gf, 5), w); TEST((int_field_exclude_index(&gf, 4), EOK), EOK); TEST(int_field_get_index(&gf, 4), NULL); TEST((int_field_destroy(&gf), EOK), EOK); TEST(int_field_count(&gf), -1); printf("\nInt map test"); XMALLOC(x, int); XMALLOC(y, int); XMALLOC(z, int); XMALLOC(u, int); XMALLOC(v, int); XMALLOC(w, int); int_map_t im; im.magic = 0; TEST(int_map_add(&im, 1, x), EINVAL); TEST(int_map_count(&im), -1); TEST(int_map_initialize(&im), EOK); TEST(int_map_count(&im), 0); TEST(int_map_find(&im, 1), NULL); TEST(int_map_add(&im, 1, x), 0); TEST(int_map_find(&im, 1), x); TEST((int_map_exclude(&im, 1), EOK), EOK); TEST(int_map_find(&im, 1), NULL); TEST(int_map_add(&im, 1, y), 1); TEST(int_map_find(&im, 1), y); TEST(int_map_add(&im, 4, z), 2); TEST(int_map_get_index(&im, 2), z); TEST(int_map_find(&im, 4), z); TEST(int_map_find(&im, 1), y); TEST(int_map_count(&im), 3); TEST(int_map_add(&im, 2, u), 3); TEST(int_map_find(&im, 2), u); TEST(int_map_add(&im, 3, v), 4); TEST(int_map_find(&im, 3), v); TEST(int_map_get_index(&im, 4), v); TEST(int_map_add(&im, 6, w), 5); TEST(int_map_find(&im, 6), w); TEST(int_map_count(&im), 6); TEST((int_map_exclude(&im, 1), EOK), EOK); TEST(int_map_find(&im, 1), NULL); TEST(int_map_find(&im, 2), u); TEST((int_map_exclude(&im, 7), EOK), EOK); TEST(int_map_find(&im, 2), u); TEST(int_map_find(&im, 6), w); TEST((int_map_exclude_index(&im, 4), EOK), EOK); TEST(int_map_get_index(&im, 4), NULL); TEST(int_map_find(&im, 3), NULL); TEST((int_map_destroy(&im), EOK), EOK); TEST(int_map_count(&im), -1); printf("\nMeasured strings test"); measured_string_ref string = measured_string_create_bulk("I am a measured string!", 0); printf("\n%x, %s at %x of %d\n", string, string->value, string->value, string->length); return EOK; }