Debian Codegraph @

tests/test_basic.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include "amqp_time.h"
5 #include <rabbitmq-c/amqp.h>
6 #include <rabbitmq-c/tcp_socket.h>
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #ifdef _WIN32
13 #include <WinSock2.h>
14 #else
15 #include <sys/time.h>
16 #endif
17
18 #ifdef NDEBUG
19 #undef NDEBUG
20 #endif
21 #include <assert.h>
22
23 static const int fixed_channel_id = 1;
24 static const char test_queue_name[] = "test_queue";
25
26 amqp_connection_state_t setup_connection_and_channel(void) {
27 amqp_connection_state_t connection_state_ = amqp_new_connection();
28
29 amqp_socket_t *socket = amqp_tcp_socket_new(connection_state_);
30 assert(socket);
31
32 int rc = amqp_socket_open(socket, "localhost", AMQP_PROTOCOL_PORT);
33 assert(rc == AMQP_STATUS_OK);
34
35 amqp_rpc_reply_t rpc_reply = amqp_login(
36 connection_state_, "/", 1, AMQP_DEFAULT_FRAME_SIZE,
37 AMQP_DEFAULT_HEARTBEAT, AMQP_SASL_METHOD_PLAIN, "guest", "guest");
38 assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
39
40 amqp_channel_open_ok_t *res =
41 amqp_channel_open(connection_state_, fixed_channel_id);
42 assert(res != NULL);
43
44 return connection_state_;
45 }
46
47 void close_and_destroy_connection(amqp_connection_state_t connection_state_) {
48 amqp_rpc_reply_t rpc_reply =
49 amqp_connection_close(connection_state_, AMQP_REPLY_SUCCESS);
50 assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
51
52 int rc = amqp_destroy_connection(connection_state_);
53 assert(rc == AMQP_STATUS_OK);
54 }
55
56 void basic_publish(amqp_connection_state_t connectionState_,
57 const char *message_) {
58 amqp_bytes_t message_bytes = amqp_cstring_bytes(message_);
59
60 amqp_basic_properties_t properties;
61 properties._flags = 0;
62
63 properties._flags |= AMQP_BASIC_DELIVERY_MODE_FLAG;
64 properties.delivery_mode = AMQP_DELIVERY_NONPERSISTENT;
65
66 int retval = amqp_basic_publish(
67 connectionState_, fixed_channel_id, amqp_cstring_bytes(""),
68 amqp_cstring_bytes(test_queue_name),
69 /* mandatory=*/1,
70 /* immediate=*/0, /* RabbitMQ 3.x does not support the "immediate" flag
71 according to
72 https://www.rabbitmq.com/specification.html */
73 &properties, message_bytes);
74
75 assert(retval == 0);
76 }
77
78 void queue_declare(amqp_connection_state_t connection_state_,
79 const char *queue_name_) {
80 amqp_queue_declare_ok_t *res = amqp_queue_declare(
81 connection_state_, fixed_channel_id, amqp_cstring_bytes(queue_name_),
82 /*passive*/ 0,
83 /*durable*/ 0,
84 /*exclusive*/ 0,
85 /*auto_delete*/ 1, amqp_empty_table);
86 assert(res != NULL);
87 }
88
89 char *basic_get(amqp_connection_state_t connection_state_,
90 const char *queue_name_, uint64_t *out_body_size_) {
91 amqp_rpc_reply_t rpc_reply;
92 amqp_time_t deadline;
93 struct timeval timeout = {5, 0};
94 int time_rc = amqp_time_from_now(&deadline, &timeout);
95 assert(time_rc == AMQP_STATUS_OK);
96
97 do {
98 rpc_reply = amqp_basic_get(connection_state_, fixed_channel_id,
99 amqp_cstring_bytes(queue_name_), /*no_ack*/ 1);
100 } while (rpc_reply.reply_type == AMQP_RESPONSE_NORMAL &&
101 rpc_reply.reply.id == AMQP_BASIC_GET_EMPTY_METHOD &&
102 amqp_time_has_past(deadline) == AMQP_STATUS_OK);
103
104 assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
105 assert(rpc_reply.reply.id == AMQP_BASIC_GET_OK_METHOD);
106
107 amqp_message_t message;
108 rpc_reply =
109 amqp_read_message(connection_state_, fixed_channel_id, &message, 0);
110 assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
111
112 char *body = malloc(message.body.len);
113 if (body == NULL) {
114 return NULL;
115 }
116 memcpy(body, message.body.bytes, message.body.len);
117 *out_body_size_ = message.body.len;
118 amqp_destroy_message(&message);
119
120 return body;
121 }
122
123 void publish_and_basic_get_message(const char *msg_to_publish) {
124 amqp_connection_state_t connection_state = setup_connection_and_channel();
125
126 queue_declare(connection_state, test_queue_name);
127 basic_publish(connection_state, msg_to_publish);
128
129 uint64_t body_size;
130 char *msg = basic_get(connection_state, test_queue_name, &body_size);
131
132 assert(msg != NULL && "Test errored: memory allocation failed!");
133 assert(body_size == strlen(msg_to_publish));
134 assert(strncmp(msg_to_publish, msg, body_size) == 0);
135 free(msg);
136
137 close_and_destroy_connection(connection_state);
138 }
139
140 char *consume_message(amqp_connection_state_t connection_state_,
141 const char *queue_name_, uint64_t *out_body_size_) {
142 amqp_basic_consume_ok_t *result =
143 amqp_basic_consume(connection_state_, fixed_channel_id,
144 amqp_cstring_bytes(queue_name_), amqp_empty_bytes,
145 /*no_local*/ 0,
146 /*no_ack*/ 1,
147 /*exclusive*/ 0, amqp_empty_table);
148 assert(result != NULL);
149
150 amqp_envelope_t envelope;
151 struct timeval timeout = {5, 0};
152 amqp_rpc_reply_t rpc_reply =
153 amqp_consume_message(connection_state_, &envelope, &timeout, 0);
154 assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
155
156 *out_body_size_ = envelope.message.body.len;
157 char *body = malloc(*out_body_size_);
158 if (body == NULL) {
159 return NULL;
160 }
161 if (*out_body_size_) {
162 memcpy(body, envelope.message.body.bytes, *out_body_size_);
163 }
164
165 amqp_destroy_envelope(&envelope);
166 return body;
167 }
168
169 void publish_and_consume_message(const char *msg_to_publish) {
170 amqp_connection_state_t connection_state = setup_connection_and_channel();
171
172 queue_declare(connection_state, test_queue_name);
173 basic_publish(connection_state, msg_to_publish);
174
175 uint64_t body_size;
176 char *msg = consume_message(connection_state, test_queue_name, &body_size);
177
178 assert(msg != NULL && "Test errored: memory allocation failed!");
179 assert(body_size == strlen(msg_to_publish));
180 assert(strncmp(msg_to_publish, msg, body_size) == 0);
181 free(msg);
182
183 close_and_destroy_connection(connection_state);
184 }
185
186 int main(void) {
187 publish_and_basic_get_message("");
188 publish_and_basic_get_message("TEST");
189
190 publish_and_consume_message("");
191 publish_and_consume_message("TEST");
192
193 return 0;
194 }