init
This commit is contained in:
43
tests/CMakeLists.txt
Normal file
43
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
# Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
# SPDX-License-Identifier: mit
|
||||
|
||||
include_directories(
|
||||
${LIBRABBITMQ_INCLUDE_DIRS}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/../librabbitmq/
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../librabbitmq/)
|
||||
|
||||
add_definitions(-DHAVE_CONFIG_H)
|
||||
add_definitions(-DAMQP_STATIC)
|
||||
|
||||
add_executable(test_parse_url test_parse_url.c)
|
||||
target_link_libraries(test_parse_url rabbitmq-static)
|
||||
add_test(parse_url test_parse_url)
|
||||
|
||||
add_executable(test_tables test_tables.c)
|
||||
target_link_libraries(test_tables rabbitmq-static)
|
||||
add_test(tables test_tables)
|
||||
configure_file(test_tables.expected ${CMAKE_CURRENT_BINARY_DIR}/tests/test_tables.expected COPYONLY)
|
||||
|
||||
add_executable(test_status_enum
|
||||
test_status_enum.c)
|
||||
target_link_libraries(test_status_enum rabbitmq-static)
|
||||
add_test(status_enum test_status_enum)
|
||||
|
||||
add_executable(test_basic
|
||||
test_basic.c)
|
||||
target_link_libraries(test_basic rabbitmq-static)
|
||||
|
||||
if (RUN_SYSTEM_TESTS)
|
||||
if (NOT APPLE)
|
||||
add_test(basic test_basic)
|
||||
endif()
|
||||
endif(RUN_SYSTEM_TESTS)
|
||||
|
||||
add_executable(test_sasl_mechanism test_sasl_mechanism.c)
|
||||
target_link_libraries(test_sasl_mechanism rabbitmq-static)
|
||||
add_test(sasl_mechanism test_sasl_mechanism)
|
||||
|
||||
add_executable(test_merge_capabilities test_merge_capabilities.c)
|
||||
target_link_libraries(test_merge_capabilities rabbitmq-static)
|
||||
add_test(merge_capabilities test_merge_capabilities)
|
||||
|
||||
194
tests/test_basic.c
Normal file
194
tests/test_basic.c
Normal file
@@ -0,0 +1,194 @@
|
||||
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
// SPDX-License-Identifier: mit
|
||||
|
||||
#include "amqp_time.h"
|
||||
#include <rabbitmq-c/amqp.h>
|
||||
#include <rabbitmq-c/tcp_socket.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <WinSock2.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
|
||||
static const int fixed_channel_id = 1;
|
||||
static const char test_queue_name[] = "test_queue";
|
||||
|
||||
amqp_connection_state_t setup_connection_and_channel(void) {
|
||||
amqp_connection_state_t connection_state_ = amqp_new_connection();
|
||||
|
||||
amqp_socket_t *socket = amqp_tcp_socket_new(connection_state_);
|
||||
assert(socket);
|
||||
|
||||
int rc = amqp_socket_open(socket, "localhost", AMQP_PROTOCOL_PORT);
|
||||
assert(rc == AMQP_STATUS_OK);
|
||||
|
||||
amqp_rpc_reply_t rpc_reply = amqp_login(
|
||||
connection_state_, "/", 1, AMQP_DEFAULT_FRAME_SIZE,
|
||||
AMQP_DEFAULT_HEARTBEAT, AMQP_SASL_METHOD_PLAIN, "guest", "guest");
|
||||
assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
|
||||
|
||||
amqp_channel_open_ok_t *res =
|
||||
amqp_channel_open(connection_state_, fixed_channel_id);
|
||||
assert(res != NULL);
|
||||
|
||||
return connection_state_;
|
||||
}
|
||||
|
||||
void close_and_destroy_connection(amqp_connection_state_t connection_state_) {
|
||||
amqp_rpc_reply_t rpc_reply =
|
||||
amqp_connection_close(connection_state_, AMQP_REPLY_SUCCESS);
|
||||
assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
|
||||
|
||||
int rc = amqp_destroy_connection(connection_state_);
|
||||
assert(rc == AMQP_STATUS_OK);
|
||||
}
|
||||
|
||||
void basic_publish(amqp_connection_state_t connectionState_,
|
||||
const char *message_) {
|
||||
amqp_bytes_t message_bytes = amqp_cstring_bytes(message_);
|
||||
|
||||
amqp_basic_properties_t properties;
|
||||
properties._flags = 0;
|
||||
|
||||
properties._flags |= AMQP_BASIC_DELIVERY_MODE_FLAG;
|
||||
properties.delivery_mode = AMQP_DELIVERY_NONPERSISTENT;
|
||||
|
||||
int retval = amqp_basic_publish(
|
||||
connectionState_, fixed_channel_id, amqp_cstring_bytes(""),
|
||||
amqp_cstring_bytes(test_queue_name),
|
||||
/* mandatory=*/1,
|
||||
/* immediate=*/0, /* RabbitMQ 3.x does not support the "immediate" flag
|
||||
according to
|
||||
https://www.rabbitmq.com/specification.html */
|
||||
&properties, message_bytes);
|
||||
|
||||
assert(retval == 0);
|
||||
}
|
||||
|
||||
void queue_declare(amqp_connection_state_t connection_state_,
|
||||
const char *queue_name_) {
|
||||
amqp_queue_declare_ok_t *res = amqp_queue_declare(
|
||||
connection_state_, fixed_channel_id, amqp_cstring_bytes(queue_name_),
|
||||
/*passive*/ 0,
|
||||
/*durable*/ 0,
|
||||
/*exclusive*/ 0,
|
||||
/*auto_delete*/ 1, amqp_empty_table);
|
||||
assert(res != NULL);
|
||||
}
|
||||
|
||||
char *basic_get(amqp_connection_state_t connection_state_,
|
||||
const char *queue_name_, uint64_t *out_body_size_) {
|
||||
amqp_rpc_reply_t rpc_reply;
|
||||
amqp_time_t deadline;
|
||||
struct timeval timeout = {5, 0};
|
||||
int time_rc = amqp_time_from_now(&deadline, &timeout);
|
||||
assert(time_rc == AMQP_STATUS_OK);
|
||||
|
||||
do {
|
||||
rpc_reply = amqp_basic_get(connection_state_, fixed_channel_id,
|
||||
amqp_cstring_bytes(queue_name_), /*no_ack*/ 1);
|
||||
} while (rpc_reply.reply_type == AMQP_RESPONSE_NORMAL &&
|
||||
rpc_reply.reply.id == AMQP_BASIC_GET_EMPTY_METHOD &&
|
||||
amqp_time_has_past(deadline) == AMQP_STATUS_OK);
|
||||
|
||||
assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
|
||||
assert(rpc_reply.reply.id == AMQP_BASIC_GET_OK_METHOD);
|
||||
|
||||
amqp_message_t message;
|
||||
rpc_reply =
|
||||
amqp_read_message(connection_state_, fixed_channel_id, &message, 0);
|
||||
assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
|
||||
|
||||
char *body = malloc(message.body.len);
|
||||
if (body == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memcpy(body, message.body.bytes, message.body.len);
|
||||
*out_body_size_ = message.body.len;
|
||||
amqp_destroy_message(&message);
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
void publish_and_basic_get_message(const char *msg_to_publish) {
|
||||
amqp_connection_state_t connection_state = setup_connection_and_channel();
|
||||
|
||||
queue_declare(connection_state, test_queue_name);
|
||||
basic_publish(connection_state, msg_to_publish);
|
||||
|
||||
uint64_t body_size;
|
||||
char *msg = basic_get(connection_state, test_queue_name, &body_size);
|
||||
|
||||
assert(msg != NULL && "Test errored: memory allocation failed!");
|
||||
assert(body_size == strlen(msg_to_publish));
|
||||
assert(strncmp(msg_to_publish, msg, body_size) == 0);
|
||||
free(msg);
|
||||
|
||||
close_and_destroy_connection(connection_state);
|
||||
}
|
||||
|
||||
char *consume_message(amqp_connection_state_t connection_state_,
|
||||
const char *queue_name_, uint64_t *out_body_size_) {
|
||||
amqp_basic_consume_ok_t *result =
|
||||
amqp_basic_consume(connection_state_, fixed_channel_id,
|
||||
amqp_cstring_bytes(queue_name_), amqp_empty_bytes,
|
||||
/*no_local*/ 0,
|
||||
/*no_ack*/ 1,
|
||||
/*exclusive*/ 0, amqp_empty_table);
|
||||
assert(result != NULL);
|
||||
|
||||
amqp_envelope_t envelope;
|
||||
struct timeval timeout = {5, 0};
|
||||
amqp_rpc_reply_t rpc_reply =
|
||||
amqp_consume_message(connection_state_, &envelope, &timeout, 0);
|
||||
assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
|
||||
|
||||
*out_body_size_ = envelope.message.body.len;
|
||||
char *body = malloc(*out_body_size_);
|
||||
if (body == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (*out_body_size_) {
|
||||
memcpy(body, envelope.message.body.bytes, *out_body_size_);
|
||||
}
|
||||
|
||||
amqp_destroy_envelope(&envelope);
|
||||
return body;
|
||||
}
|
||||
|
||||
void publish_and_consume_message(const char *msg_to_publish) {
|
||||
amqp_connection_state_t connection_state = setup_connection_and_channel();
|
||||
|
||||
queue_declare(connection_state, test_queue_name);
|
||||
basic_publish(connection_state, msg_to_publish);
|
||||
|
||||
uint64_t body_size;
|
||||
char *msg = consume_message(connection_state, test_queue_name, &body_size);
|
||||
|
||||
assert(msg != NULL && "Test errored: memory allocation failed!");
|
||||
assert(body_size == strlen(msg_to_publish));
|
||||
assert(strncmp(msg_to_publish, msg, body_size) == 0);
|
||||
free(msg);
|
||||
|
||||
close_and_destroy_connection(connection_state);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
publish_and_basic_get_message("");
|
||||
publish_and_basic_get_message("TEST");
|
||||
|
||||
publish_and_consume_message("");
|
||||
publish_and_consume_message("TEST");
|
||||
|
||||
return 0;
|
||||
}
|
||||
182
tests/test_merge_capabilities.c
Normal file
182
tests/test_merge_capabilities.c
Normal file
@@ -0,0 +1,182 @@
|
||||
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
// SPDX-License-Identifier: mit
|
||||
|
||||
#include "amqp_socket.h"
|
||||
#include "amqp_table.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int compare_bytes(amqp_bytes_t l, amqp_bytes_t r);
|
||||
static int compare_amqp_table_entry(amqp_table_entry_t result,
|
||||
amqp_table_entry_t expect);
|
||||
static int compare_field_value(amqp_field_value_t result,
|
||||
amqp_field_value_t expect);
|
||||
static int compare_amqp_table(amqp_table_t* result, amqp_table_t* expect);
|
||||
|
||||
static int compare_bytes(amqp_bytes_t l, amqp_bytes_t r) {
|
||||
if (l.len == r.len &&
|
||||
(l.bytes == r.bytes || 0 == memcmp(l.bytes, r.bytes, l.len))) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compare_amqp_table_entry(amqp_table_entry_t result,
|
||||
amqp_table_entry_t expect) {
|
||||
if (!compare_bytes(result.key, expect.key)) {
|
||||
return 0;
|
||||
}
|
||||
return compare_field_value(result.value, expect.value);
|
||||
}
|
||||
|
||||
static int compare_field_value(amqp_field_value_t result,
|
||||
amqp_field_value_t expect) {
|
||||
if (result.kind != expect.kind) {
|
||||
return 0;
|
||||
}
|
||||
switch (result.kind) {
|
||||
case AMQP_FIELD_KIND_BOOLEAN:
|
||||
return result.value.boolean == expect.value.boolean;
|
||||
case AMQP_FIELD_KIND_I8:
|
||||
return result.value.i8 == expect.value.i8;
|
||||
case AMQP_FIELD_KIND_U8:
|
||||
return result.value.u8 == expect.value.u8;
|
||||
case AMQP_FIELD_KIND_I16:
|
||||
return result.value.i16 == expect.value.i16;
|
||||
case AMQP_FIELD_KIND_U16:
|
||||
return result.value.u16 == expect.value.u16;
|
||||
case AMQP_FIELD_KIND_I32:
|
||||
return result.value.i32 == expect.value.i32;
|
||||
case AMQP_FIELD_KIND_U32:
|
||||
return result.value.u32 == expect.value.u32;
|
||||
case AMQP_FIELD_KIND_I64:
|
||||
return result.value.i64 == expect.value.i64;
|
||||
case AMQP_FIELD_KIND_U64:
|
||||
case AMQP_FIELD_KIND_TIMESTAMP:
|
||||
return result.value.u64 == expect.value.u64;
|
||||
case AMQP_FIELD_KIND_F32:
|
||||
return result.value.f32 == expect.value.f32;
|
||||
case AMQP_FIELD_KIND_F64:
|
||||
return result.value.f64 == expect.value.f64;
|
||||
case AMQP_FIELD_KIND_DECIMAL:
|
||||
return !memcmp(&result.value.decimal, &expect.value.decimal,
|
||||
sizeof(expect.value.decimal));
|
||||
case AMQP_FIELD_KIND_UTF8:
|
||||
case AMQP_FIELD_KIND_BYTES:
|
||||
return compare_bytes(result.value.bytes, expect.value.bytes);
|
||||
case AMQP_FIELD_KIND_ARRAY: {
|
||||
int i;
|
||||
if (result.value.array.num_entries != expect.value.array.num_entries) {
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < result.value.array.num_entries; ++i) {
|
||||
if (!compare_field_value(result.value.array.entries[i],
|
||||
expect.value.array.entries[i])) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
case AMQP_FIELD_KIND_TABLE:
|
||||
return compare_amqp_table(&result.value.table, &expect.value.table);
|
||||
case AMQP_FIELD_KIND_VOID:
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int compare_amqp_table(amqp_table_t* result, amqp_table_t* expect) {
|
||||
int i;
|
||||
|
||||
if (result->num_entries != expect->num_entries) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < expect->num_entries; ++i) {
|
||||
if (!compare_amqp_table_entry(expect->entries[i], result->entries[i])) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void test_merge_capabilities(amqp_table_t* base, amqp_table_t* add,
|
||||
amqp_table_t* expect) {
|
||||
amqp_pool_t pool;
|
||||
amqp_table_t result;
|
||||
int res;
|
||||
init_amqp_pool(&pool, 4096);
|
||||
|
||||
res = amqp_merge_capabilities(base, add, &result, &pool);
|
||||
if (AMQP_STATUS_OK != res) {
|
||||
fprintf(stderr, "amqp_merge_capabilities returned !ok: %d\n", res);
|
||||
abort();
|
||||
}
|
||||
|
||||
if (!compare_amqp_table(&result, expect)) {
|
||||
fprintf(stderr, "amqp_merge_capabilities incorrect result.\n");
|
||||
abort();
|
||||
}
|
||||
empty_amqp_pool(&pool);
|
||||
return;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
{
|
||||
amqp_table_t sub_base;
|
||||
amqp_table_t sub_add;
|
||||
amqp_table_t sub_expect;
|
||||
amqp_table_t base;
|
||||
amqp_table_t add;
|
||||
amqp_table_t expect;
|
||||
|
||||
amqp_table_entry_t sub_base_entries[1];
|
||||
amqp_table_entry_t sub_add_entries[2];
|
||||
amqp_table_entry_t sub_expect_entries[2];
|
||||
|
||||
amqp_table_entry_t base_entries[3];
|
||||
amqp_table_entry_t add_entries[3];
|
||||
amqp_table_entry_t expect_entries[4];
|
||||
|
||||
sub_base_entries[0] = amqp_table_construct_utf8_entry("foo", "bar");
|
||||
sub_base.num_entries =
|
||||
sizeof(sub_base_entries) / sizeof(amqp_table_entry_t);
|
||||
sub_base.entries = sub_base_entries;
|
||||
|
||||
sub_add_entries[0] = amqp_table_construct_utf8_entry("something", "else");
|
||||
sub_add_entries[1] = amqp_table_construct_utf8_entry("foo", "baz");
|
||||
sub_add.num_entries = sizeof(sub_add_entries) / sizeof(amqp_table_entry_t);
|
||||
sub_add.entries = sub_add_entries;
|
||||
|
||||
sub_expect_entries[0] = amqp_table_construct_utf8_entry("foo", "baz");
|
||||
sub_expect_entries[1] =
|
||||
amqp_table_construct_utf8_entry("something", "else");
|
||||
sub_expect.num_entries =
|
||||
sizeof(sub_expect_entries) / sizeof(amqp_table_entry_t);
|
||||
sub_expect.entries = sub_expect_entries;
|
||||
|
||||
base_entries[0] = amqp_table_construct_utf8_entry("product", "1.0");
|
||||
base_entries[1] = amqp_table_construct_utf8_entry("nooverride", "yeah");
|
||||
base_entries[2] = amqp_table_construct_table_entry("props", &sub_base);
|
||||
base.num_entries = sizeof(base_entries) / sizeof(amqp_table_entry_t);
|
||||
base.entries = base_entries;
|
||||
|
||||
add_entries[0] = amqp_table_construct_bool_entry("bool_entry", 1);
|
||||
add_entries[1] = amqp_table_construct_utf8_entry("product", "2.0");
|
||||
add_entries[2] = amqp_table_construct_table_entry("props", &sub_add);
|
||||
add.num_entries = sizeof(add_entries) / sizeof(amqp_table_entry_t);
|
||||
add.entries = add_entries;
|
||||
|
||||
expect_entries[0] = amqp_table_construct_utf8_entry("product", "2.0"),
|
||||
expect_entries[1] = amqp_table_construct_utf8_entry("nooverride", "yeah"),
|
||||
expect_entries[2] = amqp_table_construct_table_entry("props", &sub_expect);
|
||||
expect_entries[3] = amqp_table_construct_bool_entry("bool_entry", 1);
|
||||
expect.num_entries = sizeof(expect_entries) / sizeof(amqp_table_entry_t);
|
||||
expect.entries = expect_entries;
|
||||
|
||||
test_merge_capabilities(&base, &add, &expect);
|
||||
}
|
||||
fprintf(stderr, "ok\n");
|
||||
return 0;
|
||||
}
|
||||
188
tests/test_parse_url.c
Normal file
188
tests/test_parse_url.c
Normal file
@@ -0,0 +1,188 @@
|
||||
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
// SPDX-License-Identifier: mit
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/* MSVC complains about strdup being deprecated in favor of _strdup */
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <rabbitmq-c/amqp.h>
|
||||
|
||||
static void match_string(const char *what, const char *expect,
|
||||
const char *got) {
|
||||
if (strcmp(got, expect)) {
|
||||
fprintf(stderr, "Expected %s '%s', got '%s'\n", what, expect, got);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
static void match_int(const char *what, int expect, int got) {
|
||||
if (got != expect) {
|
||||
fprintf(stderr, "Expected %s '%d', got '%d'\n", what, expect, got);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_success(const char *url, const char *user,
|
||||
const char *password, const char *host, int port,
|
||||
const char *vhost) {
|
||||
char *s = strdup(url);
|
||||
struct amqp_connection_info ci;
|
||||
int res;
|
||||
|
||||
res = amqp_parse_url(s, &ci);
|
||||
if (res) {
|
||||
fprintf(stderr, "Expected to successfully parse URL, but didn't: %s (%s)\n",
|
||||
url, amqp_error_string2(res));
|
||||
abort();
|
||||
}
|
||||
|
||||
match_string("user", user, ci.user);
|
||||
match_string("password", password, ci.password);
|
||||
match_string("host", host, ci.host);
|
||||
match_int("port", port, ci.port);
|
||||
match_string("vhost", vhost, ci.vhost);
|
||||
|
||||
free(s);
|
||||
}
|
||||
|
||||
static void parse_fail(const char *url) {
|
||||
char *s = strdup(url);
|
||||
struct amqp_connection_info ci;
|
||||
|
||||
amqp_default_connection_info(&ci);
|
||||
if (amqp_parse_url(s, &ci) >= 0) {
|
||||
fprintf(stderr, "Expected to fail parsing URL, but didn't: %s\n", url);
|
||||
abort();
|
||||
}
|
||||
|
||||
free(s);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
/* From the spec */
|
||||
parse_success("amqp://user:pass@host:10000/vhost", "user", "pass", "host",
|
||||
10000, "vhost");
|
||||
parse_success("amqps://user:pass@host:10000/vhost", "user", "pass", "host",
|
||||
10000, "vhost");
|
||||
|
||||
parse_success("amqp://user%61:%61pass@ho%61st:10000/v%2fhost", "usera",
|
||||
"apass", "hoast", 10000, "v/host");
|
||||
parse_success("amqps://user%61:%61pass@ho%61st:10000/v%2fhost", "usera",
|
||||
"apass", "hoast", 10000, "v/host");
|
||||
|
||||
parse_success("amqp://", "guest", "guest", "localhost", 5672, "/");
|
||||
parse_success("amqps://", "guest", "guest", "localhost", 5671, "/");
|
||||
|
||||
parse_success("amqp://:@/", "", "", "localhost", 5672, "");
|
||||
parse_success("amqps://:@/", "", "", "localhost", 5671, "");
|
||||
|
||||
parse_success("amqp://user@", "user", "guest", "localhost", 5672, "/");
|
||||
parse_success("amqps://user@", "user", "guest", "localhost", 5671, "/");
|
||||
|
||||
parse_success("amqp://user:pass@", "user", "pass", "localhost", 5672, "/");
|
||||
parse_success("amqps://user:pass@", "user", "pass", "localhost", 5671, "/");
|
||||
|
||||
parse_success("amqp://host", "guest", "guest", "host", 5672, "/");
|
||||
parse_success("amqps://host", "guest", "guest", "host", 5671, "/");
|
||||
|
||||
parse_success("amqp://:10000", "guest", "guest", "localhost", 10000, "/");
|
||||
parse_success("amqps://:10000", "guest", "guest", "localhost", 10000, "/");
|
||||
|
||||
parse_success("amqp:///vhost", "guest", "guest", "localhost", 5672, "vhost");
|
||||
parse_success("amqps:///vhost", "guest", "guest", "localhost", 5671, "vhost");
|
||||
|
||||
parse_success("amqp://host/", "guest", "guest", "host", 5672, "");
|
||||
parse_success("amqps://host/", "guest", "guest", "host", 5671, "");
|
||||
|
||||
parse_success("amqp://host/%2f", "guest", "guest", "host", 5672, "/");
|
||||
parse_success("amqps://host/%2f", "guest", "guest", "host", 5671, "/");
|
||||
|
||||
parse_success("amqp://[::1]", "guest", "guest", "::1", 5672, "/");
|
||||
parse_success("amqps://[::1]", "guest", "guest", "::1", 5671, "/");
|
||||
|
||||
/* Various other success cases */
|
||||
parse_success("amqp://host:100", "guest", "guest", "host", 100, "/");
|
||||
parse_success("amqps://host:100", "guest", "guest", "host", 100, "/");
|
||||
|
||||
parse_success("amqp://[::1]:100", "guest", "guest", "::1", 100, "/");
|
||||
parse_success("amqps://[::1]:100", "guest", "guest", "::1", 100, "/");
|
||||
|
||||
parse_success("amqp://host/blah", "guest", "guest", "host", 5672, "blah");
|
||||
parse_success("amqps://host/blah", "guest", "guest", "host", 5671, "blah");
|
||||
|
||||
parse_success("amqp://host:100/blah", "guest", "guest", "host", 100, "blah");
|
||||
parse_success("amqps://host:100/blah", "guest", "guest", "host", 100, "blah");
|
||||
|
||||
parse_success("amqp://:100/blah", "guest", "guest", "localhost", 100, "blah");
|
||||
parse_success("amqps://:100/blah", "guest", "guest", "localhost", 100,
|
||||
"blah");
|
||||
|
||||
parse_success("amqp://[::1]/blah", "guest", "guest", "::1", 5672, "blah");
|
||||
parse_success("amqps://[::1]/blah", "guest", "guest", "::1", 5671, "blah");
|
||||
|
||||
parse_success("amqp://[::1]:100/blah", "guest", "guest", "::1", 100, "blah");
|
||||
parse_success("amqps://[::1]:100/blah", "guest", "guest", "::1", 100, "blah");
|
||||
|
||||
parse_success("amqp://user:pass@host", "user", "pass", "host", 5672, "/");
|
||||
parse_success("amqps://user:pass@host", "user", "pass", "host", 5671, "/");
|
||||
|
||||
parse_success("amqp://user:pass@host:100", "user", "pass", "host", 100, "/");
|
||||
parse_success("amqps://user:pass@host:100", "user", "pass", "host", 100, "/");
|
||||
|
||||
parse_success("amqp://user:pass@:100", "user", "pass", "localhost", 100, "/");
|
||||
parse_success("amqps://user:pass@:100", "user", "pass", "localhost", 100,
|
||||
"/");
|
||||
|
||||
parse_success("amqp://user:pass@[::1]", "user", "pass", "::1", 5672, "/");
|
||||
parse_success("amqps://user:pass@[::1]", "user", "pass", "::1", 5671, "/");
|
||||
|
||||
parse_success("amqp://user:pass@[::1]:100", "user", "pass", "::1", 100, "/");
|
||||
parse_success("amqps://user:pass@[::1]:100", "user", "pass", "::1", 100, "/");
|
||||
|
||||
/* Various failure cases */
|
||||
parse_fail("http://www.rabbitmq.com");
|
||||
|
||||
parse_fail("amqp://foo:bar:baz");
|
||||
parse_fail("amqps://foo:bar:baz");
|
||||
|
||||
parse_fail("amqp://foo[::1]");
|
||||
parse_fail("amqps://foo[::1]");
|
||||
|
||||
parse_fail("amqp://foo[::1]");
|
||||
parse_fail("amqps://foo[::1]");
|
||||
|
||||
parse_fail("amqp://foo:[::1]");
|
||||
parse_fail("amqps://foo:[::1]");
|
||||
|
||||
parse_fail("amqp://[::1]foo");
|
||||
parse_fail("amqps://[::1]foo");
|
||||
|
||||
parse_fail("amqp://foo:1000xyz");
|
||||
parse_fail("amqps://foo:1000xyz");
|
||||
|
||||
parse_fail("amqp://foo:1000000");
|
||||
parse_fail("amqps://foo:1000000");
|
||||
|
||||
parse_fail("amqp://foo/bar/baz");
|
||||
parse_fail("amqps://foo/bar/baz");
|
||||
|
||||
parse_fail("amqp://foo%1");
|
||||
parse_fail("amqps://foo%1");
|
||||
|
||||
parse_fail("amqp://foo%1x");
|
||||
parse_fail("amqps://foo%1x");
|
||||
|
||||
parse_fail("amqp://foo%xy");
|
||||
parse_fail("amqps://foo%xy");
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
tests/test_sasl_mechanism.c
Normal file
38
tests/test_sasl_mechanism.c
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
// SPDX-License-Identifier: mit
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <amqp_socket.h>
|
||||
|
||||
static void parse_success(amqp_bytes_t mechanisms,
|
||||
amqp_sasl_method_enum method) {
|
||||
if (!sasl_mechanism_in_list(mechanisms, method)) {
|
||||
fprintf(stderr, "Expected to find mechanism in list, but didn't: %s\n",
|
||||
(char *)mechanisms.bytes);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_fail(amqp_bytes_t mechanisms, amqp_sasl_method_enum method) {
|
||||
if (sasl_mechanism_in_list(mechanisms, method)) {
|
||||
fprintf(stderr,
|
||||
"Expected the mechanism not on the list, but it was present: %s\n",
|
||||
(char *)mechanisms.bytes);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
parse_success(amqp_literal_bytes("DIGEST-MD5 CRAM-MD5 LOGIN PLAIN"),
|
||||
AMQP_SASL_METHOD_PLAIN);
|
||||
parse_fail(amqp_literal_bytes("DIGEST-MD5 CRAM-MD5 LOGIN PLAIN"),
|
||||
AMQP_SASL_METHOD_EXTERNAL);
|
||||
parse_success(amqp_literal_bytes("DIGEST-MD5 CRAM-MD5 EXTERNAL"),
|
||||
AMQP_SASL_METHOD_EXTERNAL);
|
||||
parse_fail(amqp_literal_bytes("DIGEST-MD5 CRAM-MD5 EXTERNAL"),
|
||||
AMQP_SASL_METHOD_PLAIN);
|
||||
return 0;
|
||||
}
|
||||
31
tests/test_status_enum.c
Normal file
31
tests/test_status_enum.c
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
// SPDX-License-Identifier: mit
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <rabbitmq-c/amqp.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void check_errorstrings(amqp_status_enum start, amqp_status_enum end) {
|
||||
int i;
|
||||
for (i = start; i > end; --i) {
|
||||
const char* err = amqp_error_string2(i);
|
||||
if (0 == strcmp(err, "(unknown error)")) {
|
||||
printf("amqp_status_enum value %s%X", i < 0 ? "-" : "", (unsigned)i);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
check_errorstrings(AMQP_STATUS_OK, _AMQP_STATUS_NEXT_VALUE);
|
||||
check_errorstrings(AMQP_STATUS_TCP_ERROR, _AMQP_STATUS_TCP_NEXT_VALUE);
|
||||
check_errorstrings(AMQP_STATUS_SSL_ERROR, _AMQP_STATUS_SSL_NEXT_VALUE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
434
tests/test_tables.c
Normal file
434
tests/test_tables.c
Normal file
@@ -0,0 +1,434 @@
|
||||
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
|
||||
// SPDX-License-Identifier: mit
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define _USE_MATH_DEFINES
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <rabbitmq-c/amqp.h>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
void die(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fprintf(stderr, "\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
static void dump_indent(int indent, FILE *out) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < indent; i++) {
|
||||
fputc(' ', out);
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_value(int indent, amqp_field_value_t v, FILE *out) {
|
||||
int i;
|
||||
|
||||
dump_indent(indent, out);
|
||||
fputc(v.kind, out);
|
||||
|
||||
switch (v.kind) {
|
||||
case AMQP_FIELD_KIND_BOOLEAN:
|
||||
fputs(v.value.boolean ? " true\n" : " false\n", out);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_I8:
|
||||
fprintf(out, " %" PRId8 "\n", v.value.i8);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_U8:
|
||||
fprintf(out, " %" PRIu8 "\n", v.value.u8);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_I16:
|
||||
fprintf(out, " %" PRId16 "\n", v.value.i16);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_U16:
|
||||
fprintf(out, " %" PRIu16 "\n", v.value.u16);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_I32:
|
||||
fprintf(out, " %" PRId32 "\n", v.value.i32);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_U32:
|
||||
fprintf(out, " %" PRIu32 "\n", v.value.u32);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_I64:
|
||||
fprintf(out, " %" PRId64 "\n", v.value.i64);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_F32:
|
||||
fprintf(out, " %g\n", (double)v.value.f32);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_F64:
|
||||
fprintf(out, " %g\n", v.value.f64);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_DECIMAL:
|
||||
fprintf(out, " %u:::%u\n", v.value.decimal.decimals,
|
||||
v.value.decimal.value);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_UTF8:
|
||||
fprintf(out, " %.*s\n", (int)v.value.bytes.len,
|
||||
(char *)v.value.bytes.bytes);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_BYTES:
|
||||
fputc(' ', out);
|
||||
for (i = 0; i < (int)v.value.bytes.len; i++) {
|
||||
fprintf(out, "%02x", ((char *)v.value.bytes.bytes)[i]);
|
||||
}
|
||||
|
||||
fputc('\n', out);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_ARRAY:
|
||||
fputc('\n', out);
|
||||
for (i = 0; i < v.value.array.num_entries; i++) {
|
||||
dump_value(indent + 2, v.value.array.entries[i], out);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_TIMESTAMP:
|
||||
fprintf(out, " %" PRIu64 "\n", v.value.u64);
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_TABLE:
|
||||
fputc('\n', out);
|
||||
for (i = 0; i < v.value.table.num_entries; i++) {
|
||||
dump_indent(indent + 2, out);
|
||||
fprintf(out, "%.*s ->\n", (int)v.value.table.entries[i].key.len,
|
||||
(char *)v.value.table.entries[i].key.bytes);
|
||||
dump_value(indent + 4, v.value.table.entries[i].value, out);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case AMQP_FIELD_KIND_VOID:
|
||||
fputc('\n', out);
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(out, "???\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_dump_value(FILE *out) {
|
||||
amqp_table_entry_t entries[8];
|
||||
amqp_table_t table;
|
||||
amqp_field_value_t val;
|
||||
|
||||
entries[0].key = amqp_literal_bytes("zebra");
|
||||
entries[0].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
entries[0].value.value.bytes = amqp_literal_bytes("last");
|
||||
|
||||
entries[1].key = amqp_literal_bytes("aardvark");
|
||||
entries[1].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
entries[1].value.value.bytes = amqp_literal_bytes("first");
|
||||
|
||||
entries[2].key = amqp_literal_bytes("middle");
|
||||
entries[2].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
entries[2].value.value.bytes = amqp_literal_bytes("third");
|
||||
|
||||
entries[3].key = amqp_literal_bytes("number");
|
||||
entries[3].value.kind = AMQP_FIELD_KIND_I32;
|
||||
entries[3].value.value.i32 = 1234;
|
||||
|
||||
entries[4].key = amqp_literal_bytes("decimal");
|
||||
entries[4].value.kind = AMQP_FIELD_KIND_DECIMAL;
|
||||
entries[4].value.value.decimal.decimals = 2;
|
||||
entries[4].value.value.decimal.value = 1234;
|
||||
|
||||
entries[5].key = amqp_literal_bytes("time");
|
||||
entries[5].value.kind = AMQP_FIELD_KIND_TIMESTAMP;
|
||||
entries[5].value.value.u64 = 1234123412341234;
|
||||
|
||||
entries[6].key = amqp_literal_bytes("beta");
|
||||
entries[6].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
entries[6].value.value.bytes = amqp_literal_bytes("second");
|
||||
|
||||
entries[7].key = amqp_literal_bytes("wombat");
|
||||
entries[7].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
entries[7].value.value.bytes = amqp_literal_bytes("fourth");
|
||||
|
||||
table.num_entries = 8;
|
||||
table.entries = entries;
|
||||
|
||||
qsort(table.entries, table.num_entries, sizeof(amqp_table_entry_t),
|
||||
&amqp_table_entry_cmp);
|
||||
|
||||
val.kind = AMQP_FIELD_KIND_TABLE;
|
||||
val.value.table = table;
|
||||
|
||||
dump_value(0, val, out);
|
||||
}
|
||||
|
||||
static uint8_t pre_encoded_table[] = {
|
||||
0x00, 0x00, 0x00, 0xff, 0x07, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72,
|
||||
0x53, 0x00, 0x00, 0x00, 0x15, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73,
|
||||
0x20, 0x61, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x74, 0x72, 0x69,
|
||||
0x6e, 0x67, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x69, 0x6e, 0x74,
|
||||
0x49, 0x00, 0x00, 0x30, 0x39, 0x07, 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61,
|
||||
0x6c, 0x44, 0x03, 0x00, 0x01, 0xe2, 0x40, 0x09, 0x74, 0x69, 0x6d, 0x65,
|
||||
0x73, 0x74, 0x61, 0x6d, 0x70, 0x54, 0x00, 0x00, 0x63, 0xee, 0xa0, 0x53,
|
||||
0xc1, 0x94, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x00, 0x00, 0x00,
|
||||
0x1f, 0x03, 0x6f, 0x6e, 0x65, 0x49, 0x00, 0x00, 0xd4, 0x31, 0x03, 0x74,
|
||||
0x77, 0x6f, 0x53, 0x00, 0x00, 0x00, 0x0d, 0x41, 0x20, 0x6c, 0x6f, 0x6e,
|
||||
0x67, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x04, 0x62, 0x79, 0x74,
|
||||
0x65, 0x62, 0xff, 0x04, 0x6c, 0x6f, 0x6e, 0x67, 0x6c, 0x00, 0x00, 0x00,
|
||||
0x00, 0x49, 0x96, 0x02, 0xd2, 0x05, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x73,
|
||||
0x02, 0x8f, 0x04, 0x62, 0x6f, 0x6f, 0x6c, 0x74, 0x01, 0x06, 0x62, 0x69,
|
||||
0x6e, 0x61, 0x72, 0x79, 0x78, 0x00, 0x00, 0x00, 0x0f, 0x61, 0x20, 0x62,
|
||||
0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
|
||||
0x04, 0x76, 0x6f, 0x69, 0x64, 0x56, 0x05, 0x61, 0x72, 0x72, 0x61, 0x79,
|
||||
0x41, 0x00, 0x00, 0x00, 0x17, 0x49, 0x00, 0x00, 0xd4, 0x31, 0x53, 0x00,
|
||||
0x00, 0x00, 0x0d, 0x41, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x74,
|
||||
0x72, 0x69, 0x6e, 0x67, 0x05, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x66, 0x40,
|
||||
0x49, 0x0f, 0xdb, 0x06, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x64, 0x40,
|
||||
0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18};
|
||||
|
||||
static void test_table_codec(FILE *out) {
|
||||
amqp_pool_t pool;
|
||||
int result;
|
||||
|
||||
amqp_table_entry_t inner_entries[2];
|
||||
amqp_table_t inner_table;
|
||||
|
||||
amqp_field_value_t inner_values[2];
|
||||
amqp_array_t inner_array;
|
||||
|
||||
amqp_table_entry_t entries[14];
|
||||
amqp_table_t table;
|
||||
|
||||
inner_entries[0].key = amqp_literal_bytes("one");
|
||||
inner_entries[0].value.kind = AMQP_FIELD_KIND_I32;
|
||||
inner_entries[0].value.value.i32 = 54321;
|
||||
|
||||
inner_entries[1].key = amqp_literal_bytes("two");
|
||||
inner_entries[1].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
inner_entries[1].value.value.bytes = amqp_literal_bytes("A long string");
|
||||
|
||||
inner_table.num_entries = 2;
|
||||
inner_table.entries = inner_entries;
|
||||
|
||||
inner_values[0].kind = AMQP_FIELD_KIND_I32;
|
||||
inner_values[0].value.i32 = 54321;
|
||||
|
||||
inner_values[1].kind = AMQP_FIELD_KIND_UTF8;
|
||||
inner_values[1].value.bytes = amqp_literal_bytes("A long string");
|
||||
|
||||
inner_array.num_entries = 2;
|
||||
inner_array.entries = inner_values;
|
||||
|
||||
entries[0].key = amqp_literal_bytes("longstr");
|
||||
entries[0].value.kind = AMQP_FIELD_KIND_UTF8;
|
||||
entries[0].value.value.bytes = amqp_literal_bytes("Here is a long string");
|
||||
|
||||
entries[1].key = amqp_literal_bytes("signedint");
|
||||
entries[1].value.kind = AMQP_FIELD_KIND_I32;
|
||||
entries[1].value.value.i32 = 12345;
|
||||
|
||||
entries[2].key = amqp_literal_bytes("decimal");
|
||||
entries[2].value.kind = AMQP_FIELD_KIND_DECIMAL;
|
||||
entries[2].value.value.decimal.decimals = 3;
|
||||
entries[2].value.value.decimal.value = 123456;
|
||||
|
||||
entries[3].key = amqp_literal_bytes("timestamp");
|
||||
entries[3].value.kind = AMQP_FIELD_KIND_TIMESTAMP;
|
||||
entries[3].value.value.u64 = 109876543209876;
|
||||
|
||||
entries[4].key = amqp_literal_bytes("table");
|
||||
entries[4].value.kind = AMQP_FIELD_KIND_TABLE;
|
||||
entries[4].value.value.table = inner_table;
|
||||
|
||||
entries[5].key = amqp_literal_bytes("byte");
|
||||
entries[5].value.kind = AMQP_FIELD_KIND_I8;
|
||||
entries[5].value.value.i8 = (int8_t)-1;
|
||||
|
||||
entries[6].key = amqp_literal_bytes("long");
|
||||
entries[6].value.kind = AMQP_FIELD_KIND_I64;
|
||||
entries[6].value.value.i64 = 1234567890;
|
||||
|
||||
entries[7].key = amqp_literal_bytes("short");
|
||||
entries[7].value.kind = AMQP_FIELD_KIND_I16;
|
||||
entries[7].value.value.i16 = 655;
|
||||
|
||||
entries[8].key = amqp_literal_bytes("bool");
|
||||
entries[8].value.kind = AMQP_FIELD_KIND_BOOLEAN;
|
||||
entries[8].value.value.boolean = 1;
|
||||
|
||||
entries[9].key = amqp_literal_bytes("binary");
|
||||
entries[9].value.kind = AMQP_FIELD_KIND_BYTES;
|
||||
entries[9].value.value.bytes = amqp_literal_bytes("a binary string");
|
||||
|
||||
entries[10].key = amqp_literal_bytes("void");
|
||||
entries[10].value.kind = AMQP_FIELD_KIND_VOID;
|
||||
|
||||
entries[11].key = amqp_literal_bytes("array");
|
||||
entries[11].value.kind = AMQP_FIELD_KIND_ARRAY;
|
||||
entries[11].value.value.array = inner_array;
|
||||
|
||||
entries[12].key = amqp_literal_bytes("float");
|
||||
entries[12].value.kind = AMQP_FIELD_KIND_F32;
|
||||
entries[12].value.value.f32 = (float)M_PI;
|
||||
|
||||
entries[13].key = amqp_literal_bytes("double");
|
||||
entries[13].value.kind = AMQP_FIELD_KIND_F64;
|
||||
entries[13].value.value.f64 = M_PI;
|
||||
|
||||
table.num_entries = 14;
|
||||
table.entries = entries;
|
||||
|
||||
fprintf(out, "AAAAAAAAAA\n");
|
||||
|
||||
{
|
||||
amqp_field_value_t val;
|
||||
val.kind = AMQP_FIELD_KIND_TABLE;
|
||||
val.value.table = table;
|
||||
dump_value(0, val, out);
|
||||
}
|
||||
|
||||
init_amqp_pool(&pool, 4096);
|
||||
|
||||
{
|
||||
amqp_table_t decoded;
|
||||
size_t decoding_offset = 0;
|
||||
amqp_bytes_t decoding_bytes;
|
||||
decoding_bytes.len = sizeof(pre_encoded_table);
|
||||
decoding_bytes.bytes = pre_encoded_table;
|
||||
|
||||
result =
|
||||
amqp_decode_table(decoding_bytes, &pool, &decoded, &decoding_offset);
|
||||
if (result < 0) {
|
||||
die("Table decoding failed: %s", amqp_error_string2(result));
|
||||
}
|
||||
|
||||
fprintf(out, "BBBBBBBBBB\n");
|
||||
|
||||
{
|
||||
amqp_field_value_t val;
|
||||
val.kind = AMQP_FIELD_KIND_TABLE;
|
||||
val.value.table = decoded;
|
||||
|
||||
dump_value(0, val, out);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t encoding_buffer[4096];
|
||||
amqp_bytes_t encoding_result;
|
||||
size_t offset = 0;
|
||||
|
||||
memset(&encoding_buffer[0], 0, sizeof(encoding_buffer));
|
||||
encoding_result.len = sizeof(encoding_buffer);
|
||||
encoding_result.bytes = &encoding_buffer[0];
|
||||
|
||||
result = amqp_encode_table(encoding_result, &table, &offset);
|
||||
if (result < 0) {
|
||||
die("Table encoding failed: %s", amqp_error_string2(result));
|
||||
}
|
||||
|
||||
if (offset != sizeof(pre_encoded_table))
|
||||
die("Offset should be %ld, was %ld", (long)sizeof(pre_encoded_table),
|
||||
(long)offset);
|
||||
|
||||
result = memcmp(pre_encoded_table, encoding_buffer, offset);
|
||||
if (result != 0) {
|
||||
die("Table encoding differed", result);
|
||||
}
|
||||
}
|
||||
|
||||
empty_amqp_pool(&pool);
|
||||
}
|
||||
|
||||
#define CHUNK_SIZE 4096
|
||||
|
||||
static int compare_files(FILE *f1_in, FILE *f2_in) {
|
||||
char f1_buf[CHUNK_SIZE];
|
||||
char f2_buf[CHUNK_SIZE];
|
||||
int res;
|
||||
|
||||
rewind(f1_in);
|
||||
rewind(f2_in);
|
||||
|
||||
for (;;) {
|
||||
size_t f1_got = fread(f1_buf, 1, CHUNK_SIZE, f1_in);
|
||||
size_t f2_got = fread(f2_buf, 1, CHUNK_SIZE, f2_in);
|
||||
res = memcmp(f1_buf, f2_buf, f1_got < f2_got ? f1_got : f2_got);
|
||||
|
||||
if (res) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (f1_got < CHUNK_SIZE || f2_got < CHUNK_SIZE) {
|
||||
if (f1_got != f2_got) {
|
||||
res = (f1_got < f2_got ? -1 : 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
const char *expected_file_name = "tests/test_tables.expected";
|
||||
|
||||
int main(void) {
|
||||
char *srcdir = getenv("srcdir");
|
||||
FILE *out, *expected = NULL;
|
||||
char *expected_path;
|
||||
|
||||
out = tmpfile();
|
||||
if (out == NULL) {
|
||||
die("failed to create temporary file: %s", strerror(errno));
|
||||
}
|
||||
|
||||
test_table_codec(out);
|
||||
fprintf(out, "----------\n");
|
||||
test_dump_value(out);
|
||||
|
||||
if (srcdir == NULL) {
|
||||
srcdir = ".";
|
||||
}
|
||||
|
||||
expected_path = malloc(strlen(srcdir) + strlen(expected_file_name) + 2);
|
||||
if (!expected_path) {
|
||||
die("out of memory");
|
||||
}
|
||||
sprintf(expected_path, "%s/%s", srcdir, expected_file_name);
|
||||
expected = fopen(expected_path, "r");
|
||||
if (!expected) {
|
||||
die("failed to open %s: %s", expected_path, strerror(errno));
|
||||
}
|
||||
|
||||
if (compare_files(expected, out)) {
|
||||
die("output file did not have expected contents");
|
||||
}
|
||||
|
||||
fclose(expected);
|
||||
free(expected_path);
|
||||
fclose(out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
90
tests/test_tables.expected
Normal file
90
tests/test_tables.expected
Normal file
@@ -0,0 +1,90 @@
|
||||
AAAAAAAAAA
|
||||
F
|
||||
longstr ->
|
||||
S Here is a long string
|
||||
signedint ->
|
||||
I 12345
|
||||
decimal ->
|
||||
D 3:::123456
|
||||
timestamp ->
|
||||
T 109876543209876
|
||||
table ->
|
||||
F
|
||||
one ->
|
||||
I 54321
|
||||
two ->
|
||||
S A long string
|
||||
byte ->
|
||||
b -1
|
||||
long ->
|
||||
l 1234567890
|
||||
short ->
|
||||
s 655
|
||||
bool ->
|
||||
t true
|
||||
binary ->
|
||||
x 612062696e61727920737472696e67
|
||||
void ->
|
||||
V
|
||||
array ->
|
||||
A
|
||||
I 54321
|
||||
S A long string
|
||||
float ->
|
||||
f 3.14159
|
||||
double ->
|
||||
d 3.14159
|
||||
BBBBBBBBBB
|
||||
F
|
||||
longstr ->
|
||||
S Here is a long string
|
||||
signedint ->
|
||||
I 12345
|
||||
decimal ->
|
||||
D 3:::123456
|
||||
timestamp ->
|
||||
T 109876543209876
|
||||
table ->
|
||||
F
|
||||
one ->
|
||||
I 54321
|
||||
two ->
|
||||
S A long string
|
||||
byte ->
|
||||
b -1
|
||||
long ->
|
||||
l 1234567890
|
||||
short ->
|
||||
s 655
|
||||
bool ->
|
||||
t true
|
||||
binary ->
|
||||
x 612062696e61727920737472696e67
|
||||
void ->
|
||||
V
|
||||
array ->
|
||||
A
|
||||
I 54321
|
||||
S A long string
|
||||
float ->
|
||||
f 3.14159
|
||||
double ->
|
||||
d 3.14159
|
||||
----------
|
||||
F
|
||||
aardvark ->
|
||||
S first
|
||||
beta ->
|
||||
S second
|
||||
decimal ->
|
||||
D 2:::1234
|
||||
middle ->
|
||||
S third
|
||||
number ->
|
||||
I 1234
|
||||
time ->
|
||||
T 1234123412341234
|
||||
wombat ->
|
||||
S fourth
|
||||
zebra ->
|
||||
S last
|
||||
Reference in New Issue
Block a user