Пример #1
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);
}
Пример #2
0
static bool
on_stdin(void *data, int fd, uint32_t flags)
{
    uint16_t i;
    struct sol_http_progressive_response *sse;
    struct sol_buffer value = SOL_BUFFER_INIT_EMPTY;

    if (flags & (SOL_FD_FLAGS_ERR | SOL_FD_FLAGS_HUP)) {
        fprintf(stderr, "ERROR: Something wrong happened with file descriptor: %d\n", fd);
        goto err;
    }

    if (flags & SOL_FD_FLAGS_IN) {
        int err;
        struct sol_blob *blob;

        /* this will loop trying to read as much data as possible to buffer. */
        err = sol_util_load_file_fd_buffer(fd, &value);
        if (err < 0) {
            fprintf(stderr, "ERROR: failed to read from stdin: %s\n",
                sol_util_strerrora(-err));
            goto err;
        }

        if (value.used == 0) {
            /* no data usually means ^D on the terminal, quit the application */
            printf("no data on stdin, quitting.\n");
            should_quit = true;
            SOL_PTR_VECTOR_FOREACH_IDX (&responses, sse, i)
                sol_http_progressive_response_del(sse, true);
            goto end;
        }

        blob = sol_buffer_to_blob(&value);
        if (!blob) {
            fprintf(stderr, "Could not alloc the blob data\n");
            goto err;
        }
        SOL_PTR_VECTOR_FOREACH_IDX (&responses, sse, i)
            sol_http_progressive_response_sse_feed(sse, blob);
        sol_blob_unref(blob);
    }

    sol_buffer_fini(&value);
    return true;

err:
    sol_quit_with_code(EXIT_FAILURE);
end:
    stdin_watch = NULL;
    sol_buffer_fini(&value);
    return false;
}
Пример #3
0
static void
fini_null_is_fine(void)
{
    struct sol_buffer buf;

    sol_buffer_init(&buf);
    sol_buffer_fini(&buf);

    /* Finish an already finished buffer. */
    sol_buffer_fini(&buf);

    sol_buffer_fini(NULL);
}
Пример #4
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);
}
Пример #5
0
static void
test_insert_slice(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    int err;

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

    slice = sol_str_slice_from_str("Hello");
    err = sol_buffer_insert_slice(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("HelloWorld"));
    ASSERT_STR_EQ(buf.data, "HelloWorld");

    slice = sol_str_slice_from_str(" -*- ");
    err = sol_buffer_insert_slice(&buf, strlen("Hello"), slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("Hello -*- World"));
    ASSERT_STR_EQ(buf.data, "Hello -*- World");

    sol_buffer_fini(&buf);
}
Пример #6
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
}
Пример #7
0
static void
test_copy_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_copy_slice(&buf, slice);
    ASSERT(err >= 0);

    ASSERT(buf.size >= strlen(backend) + 1);
    ASSERT(streq(buf.data, backend));

    backend[1] = 'a';
    ASSERT(!streq(buf.data, backend));
    ASSERT(streq(buf.data, str));

    sol_buffer_fini(&buf);

    free(backend);
}
Пример #8
0
static void
test_insert_as_base16(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    const char to_encode[] = "Test \x01\x09\x0a\x0f Hello";
    int err;

#define B16_ENCODED "546573742001090a0f2048656c6c6f"

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

    slice = sol_str_slice_from_str("Hello");
    err = sol_buffer_insert_slice(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("HelloWorld"));
    ASSERT_STR_EQ(buf.data, "HelloWorld");

    slice = sol_str_slice_from_str(to_encode);
    err = sol_buffer_insert_as_base16(&buf, strlen("Hello"), slice, false);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("Hello" B16_ENCODED "World"));
    ASSERT_STR_EQ(buf.data, "Hello" B16_ENCODED "World");

    sol_buffer_fini(&buf);

#undef B16_ENCODED
}
Пример #9
0
static void
test_remove_data(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    int err;

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

    err = sol_buffer_remove_data(&buf, strlen("ABC"), 0);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("DEFGHI"));

    err = sol_buffer_remove_data(&buf, strlen("GHI"), 3);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("DEF"));

    err = sol_buffer_remove_data(&buf, strlen("DEF"), 0);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, 0);

    err = sol_buffer_remove_data(&buf, 100, 0);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, 0);

    err = sol_buffer_remove_data(&buf, 0, 100);
    ASSERT_INT_EQ(err, -EINVAL);

    sol_buffer_fini(&buf);
}
Пример #10
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);
}
Пример #11
0
static int
drange_to_string_convert(struct sol_flow_node *node,
    void *data,
    uint16_t port,
    uint16_t conn_id,
    const struct sol_flow_packet *packet)
{
    int r;
    struct sol_drange in_value;
    struct string_converter *mdata = data;
    struct sol_buffer out = SOL_BUFFER_INIT_EMPTY;

    mdata->node = node;

    r = sol_flow_packet_get_drange(packet, &in_value);
    SOL_INT_CHECK(r, < 0, r);

    r = do_float_markup(node, mdata->format, in_value, &out);
    SOL_INT_CHECK_GOTO(r, < 0, end);

    r = sol_flow_send_string_slice_packet(node,
        SOL_FLOW_NODE_TYPE_FORMAT_FLOAT_TO_STRING__OUT__OUT,
        sol_buffer_get_slice(&out));

end:
    sol_buffer_fini(&out);
    return r;
}
Пример #12
0
static void
test_insert_as_base64(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    const char to_encode[] = "This is a message that is multiple of 3 chars";
    int err;

#define B64_ENCODED "VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBtdWx0aXBsZSBvZiAzIGNoYXJz"

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

    slice = sol_str_slice_from_str("Hello");
    err = sol_buffer_insert_slice(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("HelloWorld"));
    ASSERT_STR_EQ(buf.data, "HelloWorld");

    slice = sol_str_slice_from_str(to_encode);
    err = sol_buffer_insert_as_base64(&buf, strlen("Hello"), slice, SOL_BASE64_MAP);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("Hello" B64_ENCODED "World"));
    ASSERT_STR_EQ(buf.data, "Hello" B64_ENCODED "World");

    sol_buffer_fini(&buf);

#undef B64_ENCODED
}
Пример #13
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
}
Пример #14
0
static int
child_read(struct sol_blob **p_blob, bool *eof, int fd)
{
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    struct timespec start = sol_util_timespec_get_current();
    size_t size;
    void *v;
    int ret = 0;

    *eof = false;
    do {
        struct timespec now = sol_util_timespec_get_current();
        struct timespec elapsed;
        ssize_t r;

        sol_util_timespec_sub(&now, &start, &elapsed);
        if (elapsed.tv_sec > 0 ||
            elapsed.tv_nsec > (time_t)CHUNK_MAX_TIME_NS)
            break;

        r = sol_util_fill_buffer(fd, &buf, CHUNK_READ_SIZE);
        if (r == 0) {
            *eof = true;
            break;
        } else if (r < 0) {
            /* Not a problem if failed because buffer could not be increased */
            if (r != -ENOMEM)
                ret = -errno;
            break;
        }
    } while (1);

    if (ret < 0 && ret != -EAGAIN) {
        sol_buffer_fini(&buf);
        return ret;
    }

    v = sol_buffer_steal(&buf, &size);
    *p_blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, v, size);
    SOL_NULL_CHECK_GOTO(*p_blob, blob_error);
    return 0;

blob_error:
    sol_buffer_fini(&buf);
    return -ENOMEM;
}
Пример #15
0
static void
clear_queue(struct sol_vector *vec)
{
    struct queue_item *item;
    uint16_t idx;

    SOL_VECTOR_FOREACH_IDX (vec, item, idx) {
        sol_util_secure_clear_memory(item->buffer.data, item->buffer.capacity);
        sol_buffer_fini(&item->buffer);
        sol_util_secure_clear_memory(item, sizeof(*item));
    }
Пример #16
0
static void
sol_mqtt_message_free(struct sol_mqtt_message *message)
{
    if (!message)
        return;

    sol_buffer_fini(message->payload);
    free(message->payload);
    free(message->topic);
    free(message);
}
Пример #17
0
static void
destroy_pending(struct sol_gatt_pending *op)
{
    if (op->buf)
        sol_buffer_fini(op->buf);

    if (op->type == PENDING_REMOTE_READ && op->read)
        op->read((void *)op->user_data, false, NULL, NULL);

    if (op->type == PENDING_REMOTE_WRITE && op->write)
        op->write((void *)op->user_data, false, NULL);

    op->slot = sd_bus_slot_unref(op->slot);

    sd_bus_message_unref(op->m);
    free(op);
}
Пример #18
0
static void
on_digest_ready_cb(void *data, struct sol_message_digest *md, struct sol_blob *output)
{
    struct update_get_hash_handle *handle = data;
    struct sol_buffer buffer = SOL_BUFFER_INIT_EMPTY;
    struct sol_str_slice slice = sol_str_slice_from_blob(output);
    int r = 0;

    r = sol_buffer_append_as_base16(&buffer, slice, false);
    SOL_INT_CHECK_GOTO(r, < 0, end);

end:
    handle->cb((void *)handle->user_data, r, (char *)buffer.data);
    sol_message_digest_del(md);
    sol_buffer_fini(&buffer);
    delete_handle(handle);
}
Пример #19
0
static void
test_insert_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("World");
    err = sol_buffer_insert_slice(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("World"));
    ASSERT_STR_EQ(buf.data, "World");

    slice = sol_str_slice_from_str("Hello");
    err = sol_buffer_insert_slice(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("HelloWorld"));
    ASSERT_STR_EQ(buf.data, "HelloWorld");

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

    slice = sol_str_slice_from_str("12x"); /* broken base16 */
    err = sol_buffer_insert_from_base16(&buf, strlen("Hello"), slice,
        SOL_DECODE_LOWERCASE);
    ASSERT_INT_NE(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("Hello" B16_DECODED "World"));
    ASSERT_STR_EQ(buf.data, "Hello" B16_DECODED "World");

    sol_buffer_fini(&buf);

#undef B16_DECODED
}
Пример #20
0
static void
test_append_printf(void)
{
    struct sol_buffer buf;
    int err;

    sol_buffer_init(&buf);
    err = sol_buffer_append_printf(&buf, "[%03d]", 1);
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "[001]");

    err = sol_buffer_append_printf(&buf, "'%s'", "This is a longer string, bla bla bla, bla bla bla");
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "[001]'This is a longer string, bla bla bla, bla bla bla'");

    err = sol_buffer_append_printf(&buf, ".");
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "[001]'This is a longer string, bla bla bla, bla bla bla'.");

    sol_buffer_fini(&buf);
}
Пример #21
0
static void
test_insert_printf(void)
{
    struct sol_buffer buf;
    int err;

    sol_buffer_init(&buf);
    err = sol_buffer_insert_printf(&buf, 0, "'%s'", "This is a longer string, bla bla bla, bla bla bla");
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "'This is a longer string, bla bla bla, bla bla bla'");

    err = sol_buffer_insert_printf(&buf, 0, "[%03d]", 1);
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "[001]'This is a longer string, bla bla bla, bla bla bla'");

    err = sol_buffer_insert_printf(&buf, strlen("[001]"), " ### ");
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "[001] ### 'This is a longer string, bla bla bla, bla bla bla'");

    sol_buffer_fini(&buf);
}
Пример #22
0
static void
test_append_slice(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    const char *str = "Hello";
    const char *expected_str = "HelloHello";
    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);

    err = sol_buffer_append_slice(&buf, slice);
    ASSERT(err >= 0);
    ASSERT_INT_EQ(buf.used, strlen(expected_str));

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

    slice = sol_buffer_get_slice(&buf);
    ASSERT_INT_EQ(slice.len, buf.used);
    ASSERT_STR_EQ(slice.data, buf.data);

    slice = sol_buffer_get_slice_at(&buf, 2);
    ASSERT_INT_EQ(slice.len, buf.used - 2);
    ASSERT_STR_EQ(slice.data, (char *)buf.data + 2);

    sol_buffer_fini(&buf);

    free(backend);
}
Пример #23
0
static int
uuid_gen(struct sol_uuid *ret)
{
    struct sol_buffer buf = SOL_BUFFER_INIT_FLAGS(ret, sizeof(*ret),
        SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED | SOL_BUFFER_FLAGS_NO_NUL_BYTE);
    struct sol_random *engine;
    ssize_t size;

    SOL_NULL_CHECK(ret, -EINVAL);
    engine = sol_random_new(SOL_RANDOM_DEFAULT, 0);
    SOL_NULL_CHECK(engine, -errno);

    size = sol_random_fill_buffer(engine, &buf, sizeof(*ret));
    sol_random_del(engine);
    sol_buffer_fini(&buf);

    if (size != (ssize_t)sizeof(*ret))
        return -EIO;

    *ret = assert_uuid_v4(*ret);
    return 0;
}
Пример #24
0
static void
test_memory_not_owned(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    char backend[10];
    int err;

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

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

    /* ensure considers null-byte, so we use sizeof - 1 */
    err = sol_buffer_ensure(&buf, sizeof(backend) - 1);
    ASSERT_INT_EQ(err, 0);

    err = sol_buffer_ensure(&buf, sizeof(backend) * 2);
    ASSERT_INT_EQ(err, -ENOMEM);

    err = sol_buffer_resize(&buf, 0);
    ASSERT_INT_EQ(err, -EPERM);

    slice = sol_str_slice_from_str("test");
    err = sol_buffer_append_slice(&buf, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "test");

    slice = sol_str_slice_from_str("other");
    err = sol_buffer_append_slice(&buf, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_STR_EQ(buf.data, "testother");

    slice = sol_str_slice_from_str("OVERFLOW");
    err = sol_buffer_append_slice(&buf, slice);
    ASSERT_INT_EQ(err, -ENOMEM);

    sol_buffer_fini(&buf);
}
Пример #25
0
static void
test_set_char_at(void)
{
    struct sol_buffer buf;
    int err;

    sol_buffer_init(&buf);
    err = sol_buffer_set_char_at(&buf, 0, 'a');
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, 1);
    ASSERT_STR_EQ(buf.data, "a");

    err = sol_buffer_set_char_at(&buf, 0, 'b');
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, 1);
    ASSERT_STR_EQ(buf.data, "b");

    err = sol_buffer_set_char_at(&buf, 1, 'c');
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("bc"));
    ASSERT_STR_EQ(buf.data, "bc");

    err = sol_buffer_set_char_at(&buf, 0, 'a');
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("ac"));
    ASSERT_STR_EQ(buf.data, "ac");

    //growing
    err = sol_buffer_set_char_at(&buf, 2, 'd');
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("acd"));
    ASSERT_STR_EQ(buf.data, "acd");

    err = sol_buffer_set_char_at(&buf, 222, 'e');
    ASSERT_INT_EQ(err, -EINVAL);

    sol_buffer_fini(&buf);
}
Пример #26
0
/*
 *----------------------- string --------------------------
 */
static void
string_read_data(void *data, int fd)
{
    struct unix_socket_data *mdata = data;
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    char *str;
    size_t len;
    int r;

    if (FILL_BUFFER(fd, len) < 0)
        return;

    r = sol_util_fill_buffer_exactly(fd, &buf, len);
    if (r < 0)
        goto end;

    str = sol_buffer_steal(&buf, NULL);
    sol_flow_send_string_take_packet(mdata->node,
        SOL_FLOW_NODE_TYPE_UNIX_SOCKET_STRING_READER__OUT__OUT, str);

end:
    sol_buffer_fini(&buf);
}
Пример #27
0
static void
test_ensure(void)
{
    struct sol_buffer buf = SOL_BUFFER_EMPTY;
    const int size = 1024;
    int i;

    sol_buffer_ensure(&buf, size);

    memset(buf.data, 22, size);

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

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

    sol_buffer_fini(&buf);
}
Пример #28
0
// 37 = 2 * 16 (chars) + 4 (hyphens) + 1 (\0)
SOL_API int
sol_util_uuid_gen(bool upcase,
    bool with_hyphens,
    char id[SOL_STATIC_ARRAY_SIZE(37)])
{
    static struct sol_str_slice hyphen = SOL_STR_SLICE_LITERAL("-");
    /* hyphens on positions 8, 13, 18, 23 (from 0) */
    static const int hyphens_pos[] = { 8, 13, 18, 23 };
    struct sol_uuid uuid = { { 0 } };
    unsigned i;
    int r;

    struct sol_buffer buf = { 0 };

    sol_buffer_init_flags(&buf, id, 37,
        SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED);

    r = uuid_gen(&uuid);
    SOL_INT_CHECK(r, < 0, r);

    for (i = 0; i < SOL_UTIL_ARRAY_SIZE(uuid.bytes); i++) {
        r = sol_buffer_append_printf(&buf, upcase ? "%02hhX" : "%02hhx",
            uuid.bytes[i]);
        SOL_INT_CHECK_GOTO(r, < 0, err);
    }

    if (with_hyphens) {
        for (i = 0; i < SOL_UTIL_ARRAY_SIZE(hyphens_pos); i++) {
            r = sol_buffer_insert_slice(&buf, hyphens_pos[i], hyphen);
            SOL_INT_CHECK_GOTO(r, < 0, err);
        }
    }

err:
    sol_buffer_fini(&buf);
    return r;
}
Пример #29
0
static void
on_digest_ready(void *data, struct sol_message_digest *handle, struct sol_blob *digest)
{
    struct feed_ctx *ctx = data;
    struct sol_buffer buf;
    int r;

    sol_buffer_init(&buf);
    r = sol_buffer_append_as_base16(&buf, sol_str_slice_from_blob(digest), false);
    if (r == 0) {
        printf("%s\t%s\n", (char *)buf.data, ctx->file);
    }

    sol_buffer_fini(&buf);

    print_time(ctx, ctx->done, "final");

    sol_message_digest_del(handle);
    free(ctx);

    pending--;
    if (pending == 0)
        sol_quit();
}
Пример #30
0
static void
test_set_slice_at(void)
{
    struct sol_buffer buf;
    struct sol_str_slice slice;
    int err;

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

    slice = sol_str_slice_from_str("Hello");
    err = sol_buffer_set_slice_at(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("Hello"));
    ASSERT_STR_EQ(buf.data, "Hello");

    slice = sol_str_slice_from_str("World");
    err = sol_buffer_set_slice_at(&buf, strlen("Hello"), slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("HelloWorld"));
    ASSERT_STR_EQ(buf.data, "HelloWorld");

    slice = sol_str_slice_from_str(" -*- ");
    err = sol_buffer_set_slice_at(&buf, 2, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("He -*- rld"));
    ASSERT_STR_EQ(buf.data, "He -*- rld");

    //overlapping
    slice = SOL_STR_SLICE_STR((char *)buf.data + 3, 3);
    err = sol_buffer_set_slice_at(&buf, 7, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("He -*- -*-"));
    ASSERT_STR_EQ(buf.data, "He -*- -*-");

    slice = sol_str_slice_from_str("whatever");
    err = sol_buffer_set_slice_at(&buf, 222, slice);
    ASSERT_INT_EQ(err, -EINVAL);

    sol_buffer_fini(&buf);

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

    slice = sol_str_slice_from_str("XY");
    err = sol_buffer_set_slice_at(&buf, 4, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("abcdXY"));
    ASSERT_STR_EQ(buf.data, "abcdXY");
    sol_buffer_fini(&buf);

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

    slice = sol_str_slice_from_str("XY");
    err = sol_buffer_set_slice_at(&buf, 3, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("abcXY"));
    ASSERT_STR_EQ(buf.data, "abcXY");
    sol_buffer_fini(&buf);

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

    slice = sol_str_slice_from_str("XY");
    err = sol_buffer_set_slice_at(&buf, 2, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("abXY"));
    ASSERT_STR_EQ(buf.data, "abXY");
    sol_buffer_fini(&buf);

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

    slice = sol_str_slice_from_str("XY");
    err = sol_buffer_set_slice_at(&buf, 1, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("aXYd"));
    ASSERT_STR_EQ(buf.data, "aXYd");
    sol_buffer_fini(&buf);

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

    slice = sol_str_slice_from_str("XY");
    err = sol_buffer_set_slice_at(&buf, 0, slice);
    ASSERT_INT_EQ(err, 0);
    ASSERT_INT_EQ(buf.used, strlen("XYcd"));
    ASSERT_STR_EQ(buf.data, "XYcd");
    sol_buffer_fini(&buf);
}