static void brigade_flatten_leftover(void **state) { char buf[80]; char bufcmp[80]; char buf2[2]; char bufcmp2[2]; size_t len = sizeof(buf); size_t len2 = sizeof(buf2); sln_brigade_t *bb; sln_bucket_t *e1; sln_bucket_t *e2; SLN_ERR(sln_brigade_create(&bb)); SLN_ERR(sln_bucket_create_empty(&e1, 40)); memset(e1->data, 'A', e1->size); SLN_BRIGADE_INSERT_TAIL(bb, e1); SLN_ERR(sln_bucket_create_empty(&e2, 42)); memset(e2->data, 'B', e2->size); SLN_BRIGADE_INSERT_TAIL(bb, e2); SLN_ERR(sln_brigade_flatten(bb, &buf[0], &len)); assert_int_equal(len, 80); assert_int_equal(sln_brigade_size(bb), 2); SLN_ERR(sln_brigade_flatten(bb, &buf2[0], &len2)); assert_int_equal(len2, 2); sln_brigade_destroy(bb); memset(&bufcmp[0], 'A', 40); memset(&bufcmp[0]+40, 'B', 40); assert_memory_equal(buf, bufcmp, 80); memset(&bufcmp2[0], 'B', 2); assert_memory_equal(buf2, bufcmp2, 2); }
selene_error_t *sln_handshake_serialize_finished(selene_t *s, sln_msg_finished_t *fin, sln_bucket_t **p_b) { sln_bucket_t *b = NULL; size_t len = 0; size_t off = 0; /* message type size */ len += 1; /* length size */ len += 3; /* verify data size */ len += SLN_MSG_FINISHED_VERIFY_LENGTH; sln_bucket_create_empty(s->alloc, &b, len); b->data[off] = SLN_HS_MT_FINISHED; off += 1; b->data[off] = SLN_MSG_FINISHED_VERIFY_LENGTH >> 16; b->data[off + 1] = SLN_MSG_FINISHED_VERIFY_LENGTH >> 8; b->data[off + 2] = SLN_MSG_FINISHED_VERIFY_LENGTH; off += 3; memcpy(b->data + off, fin->vdata, SLN_MSG_FINISHED_VERIFY_LENGTH); off += SLN_MSG_FINISHED_VERIFY_LENGTH; SLN_ASSERT(off == len); *p_b = b; return SELENE_SUCCESS; }
static void brigade_pread_simple(void **state) { sln_brigade_t *bb; sln_bucket_t *e; char buf[5]; size_t len = 0; SLN_ERR(sln_brigade_create(&bb)); SLN_ERR(sln_bucket_create_empty(&e, 20)); SLN_BRIGADE_INSERT_TAIL(bb, e); memset(e->data, 'B', e->size); memset(e->data, 'A', 1); SLN_ERR(sln_brigade_pread_bytes(bb, 0, 1, &buf[0], &len)); assert_int_equal(len, 1); assert_memory_equal(buf, "A", 1); SLN_ERR(sln_brigade_pread_bytes(bb, 1, 1, &buf[0], &len)); assert_int_equal(len, 1); assert_memory_equal(buf, "B", 1); SLN_ERR(sln_brigade_pread_bytes(bb, 0, 2, &buf[0], &len)); assert_int_equal(len, 2); assert_memory_equal(buf, "AB", 2); SLN_ERR(sln_brigade_pread_bytes(bb, 2, 2, &buf[0], &len)); assert_int_equal(len, 2); assert_memory_equal(buf, "BB", 2); sln_brigade_destroy(bb); }
static void brigade_copy_into(void **state) { sln_brigade_t *source; sln_brigade_t *dest; sln_bucket_t *e1; char buf[20]; size_t len = 0; SLN_ERR(sln_brigade_create(sln_test_alloc, &source)); SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10)); SLN_BRIGADE_INSERT_TAIL(source, e1); memset(e1->data, 'A', e1->size); SLN_ERR(sln_brigade_create(sln_test_alloc, &dest)); SLN_ERR(sln_brigade_copy_into(source, 0, 10, dest)); assert_int_equal(sln_brigade_size(dest), 10); SLN_ERR(sln_brigade_pread_bytes(dest, 0, 10, &buf[0], &len)); assert_memory_equal(buf, "AAAAAAAAAA", 2); sln_brigade_clear(dest); sln_brigade_destroy(dest); SLN_ERR(sln_brigade_create(sln_test_alloc, &dest)); SLN_ERR(sln_brigade_copy_into(source, 2, 4, dest)); assert_int_equal(sln_brigade_size(dest), 4); SLN_ERR(sln_brigade_pread_bytes(dest, 0, 4, &buf[0], &len)); assert_memory_equal(buf, "AAAA", 4); sln_brigade_clear(dest); sln_brigade_destroy(dest); sln_brigade_destroy(source); }
selene_error_t *sln_tls_serialize_header(selene_t *s, sln_msg_tls_t *tls, sln_bucket_t **p_b) { sln_bucket_t *b = NULL; size_t len = 5; sln_bucket_create_empty(s->alloc, &b, len); switch (tls->content_type) { case SLN_CONTENT_TYPE_CHANGE_CIPHER_SPEC: b->data[0] = 0x14; break; case SLN_CONTENT_TYPE_ALERT: b->data[0] = 0x15; break; case SLN_CONTENT_TYPE_HANDSHAKE: b->data[0] = 0x16; break; case SLN_CONTENT_TYPE_APPLICATION: b->data[0] = 0x17; break; default: return selene_error_createf(SELENE_EINVAL, "Unknown content type: %d", tls->content_type); } b->data[1] = tls->version_major; b->data[2] = tls->version_minor; b->data[3] = tls->length >> 8; b->data[4] = tls->length; *p_b = b; return SELENE_SUCCESS; }
static void brigade_chomp(void **state) { sln_brigade_t *bb; sln_bucket_t *e1; char buf[20]; size_t len = 0; SLN_ERR(sln_brigade_create(sln_test_alloc, &bb)); SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10)); SLN_BRIGADE_INSERT_TAIL(bb, e1); memset(e1->data, 'A', e1->size); assert_int_equal(sln_brigade_size(bb), 10); SLN_ERR(sln_brigade_chomp(bb, 1)); assert_int_equal(sln_brigade_size(bb), 9); SLN_ERR(sln_brigade_chomp(bb, 8)); assert_int_equal(sln_brigade_size(bb), 1); sln_brigade_destroy(bb); SLN_ERR(sln_brigade_create(sln_test_alloc, &bb)); SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10)); SLN_BRIGADE_INSERT_TAIL(bb, e1); memset(e1->data, 'A', e1->size); SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10)); SLN_BRIGADE_INSERT_TAIL(bb, e1); memset(e1->data, 'B', e1->size); assert_int_equal(sln_brigade_size(bb), 20); SLN_ERR(sln_brigade_chomp(bb, 8)); assert_int_equal(sln_brigade_size(bb), 12); SLN_ERR(sln_brigade_pread_bytes(bb, 0, 4, &buf[0], &len)); assert_memory_equal(buf, "AABB", 4); SLN_ERR(sln_brigade_chomp(bb, 2)); assert_int_equal(sln_brigade_size(bb), 10); SLN_ERR(sln_brigade_pread_bytes(bb, 0, 4, &buf[0], &len)); assert_memory_equal(buf, "BBBB", 4); SLN_ERR(sln_brigade_chomp(bb, 2)); SLN_ERR(sln_brigade_pread_bytes(bb, 0, 8, &buf[0], &len)); assert_memory_equal(buf, "BBBBBBBB", 8); assert_int_equal(sln_brigade_size(bb), 8); sln_brigade_destroy(bb); }
static void brigade_operations(void **state) { sln_brigade_t *bb; sln_bucket_t *e; SLN_ERR(sln_brigade_create(&bb)); assert_int_equal(sln_brigade_size(bb), 0); SLN_ERR(sln_bucket_create_empty(&e, 40)); SLN_BRIGADE_INSERT_TAIL(bb, e); assert_int_equal(sln_brigade_size(bb), 40); SLN_BUCKET_REMOVE(e); assert_int_equal(sln_brigade_size(bb), 0); sln_brigade_destroy(bb); }
static void brigade_pread_more_buckets(void **state) { sln_brigade_t *bb; sln_bucket_t *e1; sln_bucket_t *e2; char buf[20]; size_t len = 0; SLN_ERR(sln_brigade_create(&bb)); SLN_ERR(sln_bucket_create_empty(&e1, 10)); SLN_BRIGADE_INSERT_TAIL(bb, e1); memset(e1->data, 'A', e1->size); SLN_ERR(sln_bucket_create_empty(&e2, 10)); SLN_BRIGADE_INSERT_TAIL(bb, e2); memset(e2->data, 'B', e2->size); SLN_ERR(sln_brigade_pread_bytes(bb, 9, 1, &buf[0], &len)); assert_int_equal(len, 1); assert_memory_equal(buf, "A", 1); SLN_ERR(sln_brigade_pread_bytes(bb, 11, 1, &buf[0], &len)); assert_int_equal(len, 1); assert_memory_equal(buf, "B", 1); SLN_ERR(sln_brigade_pread_bytes(bb, 9, 2, &buf[0], &len)); assert_int_equal(len, 2); assert_memory_equal(buf, "AB", 2); SLN_ERR(sln_brigade_pread_bytes(bb, 11, 2, &buf[0], &len)); assert_int_equal(len, 2); assert_memory_equal(buf, "BB", 2); SLN_ERR(sln_brigade_pread_bytes(bb, 0, 20, &buf[0], &len)); assert_int_equal(len, 20); assert_memory_equal(buf, "AAAAAAAAAABBBBBBBBBB", 20); sln_brigade_destroy(bb); }
static void tok_bytes(void **state) { sln_brigade_t *bb; sln_bucket_t *e1; baton_t baton; baton.count = 0; SLN_ERR(sln_brigade_create(sln_test_alloc, &bb)); SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10)); SLN_BRIGADE_INSERT_TAIL(bb, e1); memset(e1->data, 'A', e1->size); SLN_ERR(sln_tok_parser(bb, tok_bytes_cb, &baton)); assert_int_equal(2, baton.count); sln_brigade_destroy(bb); }