Exemplo n.º 1
0
static void
sig_fuzz(struct sshkey *k, const char *sig_alg)
{
	struct fuzz *fuzz;
	u_char *sig, c[] = "some junk to be signed";
	size_t l;
	u_int fuzzers = FUZZ_1_BIT_FLIP | FUZZ_1_BYTE_FLIP | FUZZ_2_BYTE_FLIP |
	    FUZZ_TRUNCATE_START | FUZZ_TRUNCATE_END;

	if (test_is_fast())
		fuzzers &= ~FUZZ_2_BYTE_FLIP;
	if (test_is_slow())
		fuzzers |= FUZZ_2_BIT_FLIP;

	ASSERT_INT_EQ(sshkey_sign(k, &sig, &l, c, sizeof(c), sig_alg, 0), 0);
	ASSERT_SIZE_T_GT(l, 0);
	fuzz = fuzz_begin(fuzzers, sig, l);
	ASSERT_INT_EQ(sshkey_verify(k, sig, l, c, sizeof(c), NULL, 0), 0);
	free(sig);
	TEST_ONERROR(onerror, fuzz);
	for(; !fuzz_done(fuzz); fuzz_next(fuzz)) {
		/* Ensure 1-bit difference at least */
		if (fuzz_matches_original(fuzz))
			continue;
		ASSERT_INT_NE(sshkey_verify(k, fuzz_ptr(fuzz), fuzz_len(fuzz),
		    c, sizeof(c), NULL, 0), 0);
	}
	fuzz_cleanup(fuzz);
}
Exemplo n.º 2
0
static void
test_ensure(void)
{
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    const int size = 1024;
    char *buf_data;
    int i;

    sol_buffer_ensure(&buf, size);

    memset(buf.data, 22, size);

    sol_buffer_ensure(&buf, size * 2);
    buf_data = buf.data;
    for (i = 0; i < size; i++) {
        ASSERT_INT_EQ(buf_data[i], 22);
    }

    sol_buffer_ensure(&buf, size / 2);
    buf_data = buf.data;
    for (i = 0; i < size / 2; i++) {
        ASSERT_INT_EQ(buf_data[i], 22);
    }

    sol_buffer_fini(&buf);
}
Exemplo n.º 3
0
static void
munch(int fd, enum ev_type ev, void *arg)
{
	ssize_t nbytes;
	char buf[32] = { 0 };
	int err;

	if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
		FAIL_ERRNO("bad read");
	}
	VERBOSE(("read %ld bytes '%s'", nbytes, buf));

	err = mevent_disable(read_event);
	ASSERT_INT_EQ(("mevent_disable: ", strerror(err)), err, 0);

	pthread_mutex_lock(&mtx);

	ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_NONE);
	lastwake = CB_READ;

	pthread_cond_signal(&cv);
	VERBOSE(("wakeup"));

	pthread_mutex_unlock(&mtx);
}
Exemplo n.º 4
0
static void
test_append_from_base64(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    const char to_decode[] = "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYXJz";
    int err;

#define B64_DECODED "This is a message that is multiple of 3 chars"

    sol_buffer_init(&buf);
    slice = sol_str_slice_from_str("XYZ");
    err = sol_buffer_append_slice(&buf, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("XYZ"));
    ASSERT_STR_EQ(buf.data, "XYZ");

    slice = sol_str_slice_from_str(to_decode);
    err = sol_buffer_append_from_base64(&buf, slice, SOL_BASE64_MAP);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("XYZ" B64_DECODED));
    ASSERT_STR_EQ(buf.data, "XYZ" B64_DECODED);

    sol_buffer_fini(&buf);

#undef B64_DECODED
}
Exemplo n.º 5
0
static void
test_append_from_base16(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    const char to_decode[] = "546573742001090a0f2048656c6c6f";
    int err;

#define B16_DECODED "Test \x01\x09\x0a\x0f Hello"

    sol_buffer_init(&buf);
    slice = sol_str_slice_from_str("XYZ");
    err = sol_buffer_append_slice(&buf, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("XYZ"));
    ASSERT_STR_EQ(buf.data, "XYZ");

    slice = sol_str_slice_from_str(to_decode);
    err = sol_buffer_append_from_base16(&buf, slice, SOL_DECODE_LOWERCASE);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("XYZ" B16_DECODED));
    ASSERT_STR_EQ(buf.data, "XYZ" B16_DECODED);

    sol_buffer_fini(&buf);

#undef B16_DECODED
}
Exemplo n.º 6
0
static void
public_fuzz(struct sshkey *k)
{
	struct sshkey *k1;
	struct sshbuf *buf;
	struct fuzz *fuzz;

	ASSERT_PTR_NE(buf = sshbuf_new(), NULL);
	ASSERT_INT_EQ(sshkey_to_blob_buf(k, buf), 0);
	/* XXX need a way to run the tests in "slow, but complete" mode */
	fuzz = fuzz_begin(FUZZ_1_BIT_FLIP | /* XXX too slow FUZZ_2_BIT_FLIP | */
	    FUZZ_1_BYTE_FLIP | /* XXX too slow FUZZ_2_BYTE_FLIP | */
	    FUZZ_TRUNCATE_START | FUZZ_TRUNCATE_END,
	    sshbuf_mutable_ptr(buf), sshbuf_len(buf));
	ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(buf), sshbuf_len(buf),
	    &k1), 0);
	sshkey_free(k1);
	sshbuf_free(buf);
	TEST_ONERROR(onerror, fuzz);
	for(; !fuzz_done(fuzz); fuzz_next(fuzz)) {
		if (sshkey_from_blob(fuzz_ptr(fuzz), fuzz_len(fuzz), &k1) == 0)
			sshkey_free(k1);
	}
	fuzz_cleanup(fuzz);
}
Exemplo n.º 7
0
static void
test_no_nul_byte(void)
{
    struct sol_buffer buf;
    int32_t backend;
    int32_t value = 0xdeadbeef;
    int err;

    sol_buffer_init_flags(&buf, &backend, sizeof(backend),
        SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);

    err = sol_buffer_ensure(&buf, sizeof(int32_t));
    ASSERT_INT_EQ(err, 0);

    err = sol_buffer_append_slice(&buf, SOL_STR_SLICE_STR((const char *)&value, sizeof(value)));
    ASSERT_INT_EQ(err, 0);

    err = sol_buffer_append_slice(&buf, SOL_STR_SLICE_STR((const char *)&value, sizeof(value)));
    ASSERT_INT_NE(err, 0);

    sol_buffer_fini(&buf);

    sol_buffer_init_flags(&buf, NULL, 0, SOL_BUFFER_FLAGS_NO_NUL_BYTE);
    err = sol_buffer_append_printf(&buf, "123");
    ASSERT_INT_EQ(err, 0);
    err = sol_buffer_append_printf(&buf, "4");
    ASSERT_INT_EQ(err, 0);

    ASSERT(sol_str_slice_eq(sol_buffer_get_slice(&buf),
        SOL_STR_SLICE_STR("1234", 4)));
    sol_buffer_fini(&buf);
}
Exemplo n.º 8
0
void
test_s2k(void)
{
    TEST_START("s2k");

    static unsigned char salt[S2K_SALT_BYTES] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
    const char * pphrase = "ImGumbyAndYouAreNot";

    static unsigned char expected_key1[AES128_KEY_BYTES] = {
        0x0e, 0xa5, 0x00, 0x1c, 0xce, 0xad, 0x7e, 0xa8, 0xa0, 0x81, 0x38, 0xae, 0xaf, 0x4e, 0x28, 0xd5
    };
    unsigned char s2k_key1[AES128_KEY_BYTES];

    compute_gpg_s2k_key(pphrase, sizeof(s2k_key1), salt, S2K_ITER_BYTE_COUNT, s2k_key1);
    ASSERT_INT_EQ(memcmp(s2k_key1, expected_key1, sizeof(s2k_key1)), 0);

    //  Second test to handle the case where we need to run multiple hashes to generate enough bits
    //  Note that the first 16 bytes are the same as the previous test - this is to be expected, since the
    //  salt and passphrase are the same, so the first hash is executed identically.
    static unsigned char expected_key2[48] = {
        0x0e, 0xa5, 0x00, 0x1c, 0xce, 0xad, 0x7e, 0xa8, 0xa0, 0x81, 0x38, 0xae, 0xaf, 0x4e, 0x28, 0xd5,
        0x21, 0xf1, 0xee, 0x4b, 0x02, 0xc0, 0x0f, 0x63, 0x6a, 0x17, 0xbf, 0x62, 0x34, 0x10, 0x26, 0x48,
        0x7b, 0xc6, 0x3f, 0x08, 0x9d, 0xb5, 0x6b, 0x34, 0x70, 0x3b, 0x71, 0xdb, 0x67, 0x92, 0x6f, 0x5f
    };
    unsigned char s2k_key2[48];

    compute_gpg_s2k_key(pphrase, sizeof(s2k_key2), salt, S2K_ITER_BYTE_COUNT, s2k_key2);
    ASSERT_INT_EQ(memcmp(s2k_key2, expected_key2, sizeof(s2k_key2)), 0);

    TEST_DONE();
}
Exemplo n.º 9
0
static void
test_coap_parse_empty_pdu(void)
{
    uint8_t pdu[] = { 0x40, 0x01, 0, 0 };
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    size_t offset;
    uint8_t ver, type, code;
    uint16_t id;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    sol_coap_header_get_version(pkt, &ver);
    sol_coap_header_get_type(pkt, &type);
    sol_coap_header_get_code(pkt, &code);
    sol_coap_header_get_id(pkt, &id);

    ASSERT_INT_EQ(ver, 1);
    ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_CON);
    ASSERT_INT_EQ(code, SOL_COAP_METHOD_GET);
    ASSERT_INT_EQ(id, 0);

    sol_coap_packet_unref(pkt);
}
Exemplo n.º 10
0
static void
test_coap_find_options(void)
{
    uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n',
                      0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 };
    struct sol_str_slice options[16];
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    int count = 16;
    size_t offset;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count);
    ASSERT_INT_EQ(count, 1);
    ASSERT_INT_EQ(options[0].len, 1);
    ASSERT_INT_EQ((uint8_t)options[0].data[0], 0);

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count);
    ASSERT_INT_EQ(count, 0);

    sol_coap_packet_unref(pkt);
}
Exemplo n.º 11
0
int
TEST_MAINLOOP_MAIN_FN(int argc, char *argv[])
{
    int err, i;
    struct sol_timeout *timeout_to_del;
    struct sol_idle *idler_to_del;

    err = sol_init();
    ASSERT(!err);

    timeout_to_del = sol_timeout_add(100, timeout_never_called, NULL);
    sol_timeout_add(20, on_timeout_del_and_new, timeout_to_del);

    sol_timeout_add(1, on_timeout_renew_twice, NULL);
    sol_idle_add(on_idler_renew_twice, NULL);

    for (i = 0; i < 5; i++)
        sol_idle_add(on_idler, (void *)(intptr_t)i);

    sol_idle_add(on_idler_del_another, &idler_to_del);
    idler_to_del = sol_idle_add(on_idler_never_called, NULL);

    sol_run();
    ASSERT_INT_EQ(timeout_called, 3);
    ASSERT_INT_EQ(timeout_renewed, 2);
    ASSERT_INT_EQ(idler_renewed, 2);

    for (i = 0; i < 10; i++)
        ASSERT_INT_EQ(idler_sequence[i], i);

    sol_shutdown();

    return 0;
}
Exemplo n.º 12
0
static void
test_base64_encode(void)
{
    const char base64_map[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    const char instr[] = "This is a message that is multiple of 3 chars";
    const char *expectstrs[] = {
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYXJz",
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYXI=",
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYQ==",
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNo"
    };
    struct sol_str_slice slice;
    char outstr[(sizeof(instr) / 3 + 1) * 4 + 1];
    size_t r, i;

    slice = sol_str_slice_from_str(instr);

    for (i = 0; i < SOL_UTIL_ARRAY_SIZE(expectstrs); i++) {
        struct sol_str_slice exp = sol_str_slice_from_str(expectstrs[i]);

        memset(outstr, 0xff, sizeof(outstr));
        r = sol_util_base64_encode(outstr, sizeof(outstr), slice, base64_map);
        ASSERT_INT_EQ(r, exp.len);
        ASSERT_INT_EQ(outstr[r], (char)0xff);
        outstr[r] = '\0';
        ASSERT_STR_EQ(outstr, exp.data);

        slice.len--;
    }
}
Exemplo n.º 13
0
static void
public_fuzz(struct sshkey *k)
{
	struct sshkey *k1;
	struct sshbuf *buf;
	struct fuzz *fuzz;
	u_int fuzzers = FUZZ_1_BIT_FLIP | FUZZ_1_BYTE_FLIP |
	    FUZZ_TRUNCATE_START | FUZZ_TRUNCATE_END;

	if (test_is_fast())
		fuzzers &= ~FUZZ_1_BIT_FLIP;
	if (test_is_slow())
		fuzzers |= FUZZ_2_BIT_FLIP | FUZZ_2_BYTE_FLIP;
	ASSERT_PTR_NE(buf = sshbuf_new(), NULL);
	ASSERT_INT_EQ(sshkey_putb(k, buf), 0);
	fuzz = fuzz_begin(fuzzers, sshbuf_mutable_ptr(buf), sshbuf_len(buf));
	ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(buf), sshbuf_len(buf),
	    &k1), 0);
	sshkey_free(k1);
	sshbuf_free(buf);
	TEST_ONERROR(onerror, fuzz);
	for(; !fuzz_done(fuzz); fuzz_next(fuzz)) {
		if (sshkey_from_blob(fuzz_ptr(fuzz), fuzz_len(fuzz), &k1) == 0)
			sshkey_free(k1);
	}
	fuzz_cleanup(fuzz);
}
Exemplo n.º 14
0
static void
test_coap_find_options(void)
{
    uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n',
                      0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 };
    struct sol_coap_packet *pkt;
    struct sol_str_slice options[16];
    int count = 16;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    memcpy(pkt->buf, pdu, sizeof(pdu));
    pkt->payload.size = sizeof(pdu);

    ASSERT(!coap_packet_parse(pkt));

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count);
    ASSERT_INT_EQ(count, 1);
    ASSERT_INT_EQ(options[0].len, 1);
    ASSERT_INT_EQ((uint8_t)options[0].data[0], 0);

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count);
    ASSERT_INT_EQ(count, 0);

    sol_coap_packet_unref(pkt);
}
Exemplo n.º 15
0
static void
test_coap_parse_simple_pdu(void)
{
    uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e',
                      'n',  0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y',
                      'l', 'o', 'a', 'd', 0x00 };
    struct sol_str_slice options[16];
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    uint8_t *token;
    int count = 16;
    size_t offset;
    uint8_t tkl, code, ver, type;
    uint16_t id;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    sol_coap_header_get_version(pkt, &ver);
    sol_coap_header_get_type(pkt, &type);

    ASSERT_INT_EQ(ver, 1);
    ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_NON_CON);

    token = sol_coap_header_get_token(pkt, &tkl);
    ASSERT(token);
    ASSERT_INT_EQ(tkl, 5);
    ASSERT(!strcmp((char *)token, "token"));

    sol_coap_header_get_code(pkt, &code);
    sol_coap_header_get_id(pkt, &id);
    ASSERT_INT_EQ(code, SOL_COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
    ASSERT_INT_EQ(id, 0x1234);

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count);
    ASSERT_INT_EQ(count, 1);
    ASSERT_INT_EQ(options[0].len, 1);
    ASSERT_INT_EQ((uint8_t)options[0].data[0], 0);

    /* Not existent. */
    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count);
    ASSERT_INT_EQ(count, 0);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT_INT_EQ(offset + sizeof("payload"), buf->used);
    ASSERT(!strcmp((char *)buf->data + offset, "payload"));

    sol_coap_packet_unref(pkt);
}
void
TestQPSyntax_run_tests(Folder *index)
{
    u32_t i;
    TestBatch   *batch = Test_new_batch("TestQueryParserSyntax", 48, NULL);
    Searcher    *searcher     = Searcher_new((Obj*)index);
    QueryParser *qparser      = QParser_new(Searcher_Get_Schema(searcher), 
        NULL, NULL, NULL);
    QParser_Set_Heed_Colons(qparser, true);

    PLAN(batch);

    for (i = 0; leaf_test_funcs[i] != NULL; i++) {
        kino_TestQPSyntax_test_t test_func = leaf_test_funcs[i];
        TestQueryParser *test_case = test_func(i);
        Query *tree     = QParser_Tree(qparser, test_case->query_string);
        Query *expanded = QParser_Expand_Leaf(qparser, test_case->tree);
        Query *parsed   = QParser_Parse(qparser, test_case->query_string);
        Hits  *hits     = Searcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL);

        ASSERT_TRUE(batch, Query_Equals(tree, (Obj*)test_case->tree),
            "tree()    %s", test_case->query_string->ptr);
        ASSERT_TRUE(batch, Query_Equals(expanded, (Obj*)test_case->expanded),
            "expand_leaf()    %s", test_case->query_string->ptr);
        ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits,
            "hits:    %s", test_case->query_string->ptr);
        DECREF(hits);
        DECREF(parsed);
        DECREF(expanded);
        DECREF(tree);
        DECREF(test_case);
    }

    for (i = 0; syntax_test_funcs[i] != NULL; i++) {
        kino_TestQPSyntax_test_t test_func = syntax_test_funcs[i];
        TestQueryParser *test_case = test_func(i);
        Query *tree   = QParser_Tree(qparser, test_case->query_string);
        Query *parsed = QParser_Parse(qparser, test_case->query_string);
        Hits  *hits   = Searcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL);

        ASSERT_TRUE(batch, Query_Equals(tree, (Obj*)test_case->tree),
            "tree()    %s", test_case->query_string->ptr);
        ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits,
            "hits:    %s", test_case->query_string->ptr);
        DECREF(hits);
        DECREF(parsed);
        DECREF(tree);
        DECREF(test_case);
    }

    batch->destroy(batch);
    DECREF(searcher);
    DECREF(qparser);
}
Exemplo n.º 17
0
static void
test_size_mul(void)
{
    const size_t half_size = SIZE_MAX / 2;
    const size_t half_double_size = (SIZE_MAX % 2) ? (SIZE_MAX - 1) : SIZE_MAX;
    size_t out;

    ASSERT(sol_util_size_mul(half_size, 2, &out) == 0);
    ASSERT_INT_EQ(out, half_double_size);

    ASSERT_INT_EQ(sol_util_size_mul(half_size, 4, &out), -EOVERFLOW);
}
Exemplo n.º 18
0
static void
put_opt(struct sshbuf *b, const char *name, const char *value)
{
	struct sshbuf *sect;

	sect = sshbuf_new();
	ASSERT_PTR_NE(sect, NULL);
	ASSERT_INT_EQ(sshbuf_put_cstring(b, name), 0);
	if (value != NULL)
		ASSERT_INT_EQ(sshbuf_put_cstring(sect, value), 0);
	ASSERT_INT_EQ(sshbuf_put_stringb(b, sect), 0);
	sshbuf_free(sect);
}
Exemplo n.º 19
0
void
badarg(void)
{
	char	 buf[16];
	int	 len, width;

	width = 1;
	TEST_START("utf8_badarg");
	len = snmprintf(buf, sizeof(buf), &width, "\377");
	ASSERT_INT_EQ(len, -1);
	ASSERT_STRING_EQ(buf, "");
	ASSERT_INT_EQ(width, 0);
	TEST_DONE();
}
Exemplo n.º 20
0
static void
test_check_slices_after_adding_all(void)
{
    static const char *gladiators[] = { "Spartacus", "C r i x u s", "Priscus and Verus", "Tetraites",
                                        "Spiculus", "Marcus Attilius", "Carpophorus", "Flamma", "Commodus", "Mevia", "Hoplomachus",
                                        "Laquearius", "Lorarius", "Paegniarius", "Sagittarius", "Pegasasu no Seiya", "Thraex", "Gladiatrix",
                                        "Crupellarii", "Cestus", "Arbelas", "Retiarius", "Samnite", "Venator", "Dimachaerus", "Bustuarius",
                                        "This is a loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong name for a gladiator"
                                      };

    struct sol_arena *arena;
    struct sol_str_slice results[sol_util_array_size(gladiators)];
    unsigned int i;

    arena = sol_arena_new();
    ASSERT(arena);

    for (i = 0; i < sol_util_array_size(gladiators); i++)
        ASSERT_INT_EQ(sol_arena_slice_dup_str(arena, &results[i], gladiators[i]), 0);

    for (i = 0; i < sol_util_array_size(gladiators); i++)
        ASSERT(sol_str_slice_eq(results[i], sol_str_slice_from_str(gladiators[i])));

    sol_arena_del(arena);
}
Exemplo n.º 21
0
static void
test_set_slice(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    const char *str = "Hello";
    char *backend;
    int err;

    backend = strdup(str);
    slice = sol_str_slice_from_str(backend);

    sol_buffer_init(&buf);
    err = sol_buffer_set_slice(&buf, slice);
    ASSERT(err >= 0);

    ASSERT_INT_EQ(buf.used, strlen(backend));
    ASSERT_STR_EQ(buf.data, backend);

    backend[1] = 'a';
    ASSERT_STR_NE(buf.data, backend);
    ASSERT_STR_EQ(buf.data, str);

    sol_buffer_fini(&buf);

    free(backend);
}
Exemplo n.º 22
0
static void
signature_test(struct sshkey *k, struct sshkey *bad, const u_char *d, size_t l)
{
	size_t len;
	u_char *sig;

	ASSERT_INT_EQ(sshkey_sign(k, &sig, &len, d, l, 0), 0);
	ASSERT_SIZE_T_GT(len, 8);
	ASSERT_PTR_NE(sig, NULL);
	ASSERT_INT_EQ(sshkey_verify(k, sig, len, d, l, 0), 0);
	ASSERT_INT_NE(sshkey_verify(bad, sig, len, d, l, 0), 0);
	/* Fuzz test is more comprehensive, this is just a smoke test */
	sig[len - 5] ^= 0x10;
	ASSERT_INT_NE(sshkey_verify(k, sig, len, d, l, 0), 0);
	free(sig);
}
Exemplo n.º 23
0
static void
test_base16_decode(void)
{
    const char expstr[] = "Test \x01\x09\x0a\x0f Hello";
    const char *instrs[] = {
        "546573742001090a0f2048656c6c6f",
        "546573742001090A0F2048656C6C6F"
    };
    char outstr[sizeof(expstr)];
    struct sol_str_slice slice;
    ssize_t r, i;
    enum sol_decode_case decode_case;

    for (i = 0; i < 4; i++) {
        slice = sol_str_slice_from_str(instrs[i % 2]);

        if (i == 0)
            decode_case = SOL_DECODE_LOWERCASE;
        else if (i == 1)
            decode_case = SOL_DECODE_UPERCASE;
        else
            decode_case = SOL_DECODE_BOTH;

        memset(outstr, 0xff, sizeof(outstr));
        r = sol_util_base16_decode(outstr, sizeof(outstr), slice, decode_case);
        ASSERT_INT_EQ(r, strlen(expstr));
        ASSERT_INT_EQ(outstr[r], (char)0xff);
        outstr[r] = '\0';
        ASSERT_STR_EQ(outstr, expstr);
    }

    /* negative test (case swap) */
    for (i = 0; i < 2; i++) {
        slice = sol_str_slice_from_str(instrs[i]);

        memset(outstr, 0xff, sizeof(outstr));
        r = sol_util_base16_decode(outstr, sizeof(outstr), slice, !i ?
            SOL_DECODE_UPERCASE : SOL_DECODE_LOWERCASE);
    }

    /* short sequence (not multiple of 2) */
    slice = sol_str_slice_from_str("1");
    memset(outstr, 0xff, sizeof(outstr));
    r = sol_util_base16_decode(outstr, sizeof(outstr), slice,
        SOL_DECODE_UPERCASE);
    ASSERT_INT_EQ(r, -EINVAL);
}
Exemplo n.º 24
0
static void
test_coap_parse_simple_pdu(void)
{
    uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e',
                      'n',  0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y',
                      'l', 'o', 'a', 'd', 0x00 };
    struct sol_coap_packet *pkt;
    struct sol_str_slice options[16];
    uint8_t *payload, *token;
    uint16_t len;
    int count = 16;
    uint8_t tkl;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    memcpy(pkt->buf, pdu, sizeof(pdu));
    pkt->payload.size = sizeof(pdu);

    ASSERT(!coap_packet_parse(pkt));

    ASSERT_INT_EQ(sol_coap_header_get_ver(pkt), 1);
    ASSERT_INT_EQ(sol_coap_header_get_type(pkt), SOL_COAP_TYPE_NONCON);

    token = sol_coap_header_get_token(pkt, &tkl);
    ASSERT(token);
    ASSERT_INT_EQ(tkl, 5);
    ASSERT(!strcmp((char *)token, "token"));

    ASSERT_INT_EQ(sol_coap_header_get_code(pkt), SOL_COAP_RSPCODE_PROXYING_NOT_SUPPORTED);
    ASSERT_INT_EQ(sol_coap_header_get_id(pkt), 0x1234);

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count);
    ASSERT_INT_EQ(count, 1);
    ASSERT_INT_EQ(options[0].len, 1);
    ASSERT_INT_EQ((uint8_t)options[0].data[0], 0);

    /* Not existent. */
    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count);
    ASSERT_INT_EQ(count, 0);

    ASSERT(!sol_coap_packet_get_payload(pkt, &payload, &len));
    ASSERT_INT_EQ(len, sizeof("payload"));
    ASSERT(!strcmp((char *)payload, "payload"));

    sol_coap_packet_unref(pkt);
}
Exemplo n.º 25
0
static void
test_base64_decode(void)
{
    const char base64_map[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    char expstr[] = "This is a message that is multiple of 3 chars";
    const char *instrs[] = {
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYXJz",
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYXI=",
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYQ==",
        "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNo"
    };
    struct sol_str_slice exp, slice;
    char outstr[sizeof(expstr)];
    size_t r, i;

    exp = sol_str_slice_from_str(expstr);

    for (i = 0; i < SOL_UTIL_ARRAY_SIZE(instrs); i++) {
        slice = sol_str_slice_from_str(instrs[i]);

        memset(outstr, 0xff, sizeof(outstr));
        r = sol_util_base64_decode(outstr, sizeof(outstr), slice, base64_map);
        ASSERT_INT_EQ(r, exp.len);
        ASSERT_INT_EQ(outstr[r], (char)0xff);
        outstr[r] = '\0';
        ASSERT_STR_EQ(outstr, exp.data);

        exp.len--;
        expstr[exp.len] = '\0';
    }

    /* negative test (invalid char) */
    slice = sol_str_slice_from_str("****");
    memset(outstr, 0xff, sizeof(outstr));
    r = sol_util_base64_decode(outstr, sizeof(outstr), slice, base64_map);
    ASSERT_INT_EQ(r, -EINVAL);

    /* short sequence (not multiple of 4) */
    slice = sol_str_slice_from_str("123");
    memset(outstr, 0xff, sizeof(outstr));
    r = sol_util_base64_decode(outstr, sizeof(outstr), slice, base64_map);
    ASSERT_INT_EQ(r, -EINVAL);
}
Exemplo n.º 26
0
struct sshbuf *
load_text_file(const char *name)
{
	struct sshbuf *ret = load_file(name);
	const u_char *p;

	/* Trim whitespace at EOL */
	for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) {
		if (p[sshbuf_len(ret) - 1] == '\r' ||
		    p[sshbuf_len(ret) - 1] == '\t' ||
		    p[sshbuf_len(ret) - 1] == ' ' ||
		    p[sshbuf_len(ret) - 1] == '\n')
			ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0);
		else
			break;
	}
	/* \0 terminate */
	ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
	return ret;
}
Exemplo n.º 27
0
static void
test_ssize_mul(void)
{
    ssize_t half_ssize = SSIZE_MAX / 2;
    ssize_t half_double_ssize = (SSIZE_MAX % 2) ?
        (SSIZE_MAX - 1) : SSIZE_MAX;
    ssize_t out;

    ASSERT(sol_util_ssize_mul(half_ssize, 2, &out) == 0);
    ASSERT(out == half_double_ssize);

    ASSERT_INT_EQ(sol_util_ssize_mul(half_ssize, 4, &out), -EOVERFLOW);

    half_ssize *= -1;
    half_double_ssize *= -1;
    ASSERT(sol_util_ssize_mul(half_ssize, 2, &out) == 0);
    ASSERT(out == half_double_ssize);

    ASSERT_INT_EQ(sol_util_ssize_mul(half_ssize, 4, &out), -EOVERFLOW);
}
Exemplo n.º 28
0
static bool
on_timeout_del_and_new(void *data)
{
    struct sol_timeout *timeout_to_del = data;

    timeout_called++;
    ASSERT_INT_EQ(sol_timeout_del(timeout_to_del), 1);
    sol_timeout_add(250, on_timeout_quit, NULL);
    sol_timeout_add(200, on_timeout_chained, NULL);
    return false;
}
Exemplo n.º 29
0
void
one(const char *name, const char *mbs, int width,
    int wantwidth, int wantlen, const char *wants)
{
	char	 buf[16];
	int	*wp;
	int	 len;

	if (wantlen == -2)
		wantlen = strlen(wants);
	(void)strlcpy(buf, "utf8_", sizeof(buf));
	(void)strlcat(buf, name, sizeof(buf));
	TEST_START(buf);
	wp = wantwidth == -2 ? NULL : &width;
	len = snmprintf(buf, sizeof(buf), wp, "%s", mbs);
	ASSERT_INT_EQ(len, wantlen);
	ASSERT_STRING_EQ(buf, wants);
	ASSERT_INT_EQ(width, wantwidth);
	TEST_DONE();
}
Exemplo n.º 30
0
static struct sshkey *
get_private(const char *n)
{
	struct sshbuf *b;
	struct sshkey *ret;

	b = load_file(n);
	ASSERT_INT_EQ(sshkey_parse_private_fileblob(b, "", n, &ret, NULL), 0);
	sshbuf_free(b);
	return ret;
}