Debian Codegraph @

tests/test_basic.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.cxx . . $ `<file>/tests/test_basic.c`/
2 // SPDX-License-Identifier: mit
3
4 #include "amqp_time.h"cxx . . $ `<file>/librabbitmq/amqp_time.h`/
5 #include <rabbitmq-c/amqp.h>cxx . . $ `<file>/include/rabbitmq-c/amqp.h`/
6 #include <rabbitmq-c/tcp_socket.h>cxx . . $ `<file>/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 NDEBUGcxx . . $ `<built-in>:414:9`!
19 #undef NDEBUGcxx . . $ `<built-in>:414:9`!
20 #endif
21 #include <assert.h>
22
23 static const int fixed_channel_idcxx . . $ fixed_channel_id. = 1;
24 static const char test_queue_namecxx . . $ test_queue_name.[] = "test_queue";
25
26 amqp_connection_state_tcxx . . $ amqp_connection_state_t# setup_connection_and_channelcxx . . $ setup_connection_and_channel(79af3635f28998e8).(void) {
27 amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state_ = amqp_new_connectioncxx . . $ amqp_new_connection(79af3635f28998e8).();
28
29 amqp_socket_tcxx . . $ amqp_socket_t# *socket = amqp_tcp_socket_newcxx . . $ amqp_tcp_socket_new(e91028c0d79ca0c8).(connection_state_);
30 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(socket);
31
32 int rc = amqp_socket_opencxx . . $ amqp_socket_open(82fa06a0634953de).(socket, "localhost", AMQP_PROTOCOL_PORTcxx . . $ `include/rabbitmq-c/framing.h:19:9`!);
33 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rc == AMQP_STATUS_OK);
34
35 amqp_rpc_reply_tcxx . . $ amqp_rpc_reply_t# rpc_reply = amqp_logincxx . . $ amqp_login(922feb39dd67fa43).(
36 connection_state_, "/", 1, AMQP_DEFAULT_FRAME_SIZEcxx . . $ `include/rabbitmq-c/amqp.h:225:9`!,
37 AMQP_DEFAULT_HEARTBEATcxx . . $ `include/rabbitmq-c/amqp.h:250:9`!, AMQP_SASL_METHOD_PLAINcxx . . $ AMQP_SASL_METHOD_PLAIN., "guest", "guest");
38 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
39
40 amqp_channel_open_ok_tcxx . . $ amqp_channel_open_ok_t# *res =
41 amqp_channel_opencxx . . $ amqp_channel_open(528520d9d871ef29).(connection_state_, fixed_channel_idcxx . . $ fixed_channel_id.);
42 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(res != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
43
44 return connection_state_;
45 }
46
47 void close_and_destroy_connectioncxx . . $ close_and_destroy_connection(40203a97fc6341ef).(amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state_) {
48 amqp_rpc_reply_tcxx . . $ amqp_rpc_reply_t# rpc_reply =
49 amqp_connection_closecxx . . $ amqp_connection_close(69ab15b27171de0d).(connection_state_, AMQP_REPLY_SUCCESScxx . . $ `include/rabbitmq-c/framing.h:26:9`!);
50 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
51
52 int rc = amqp_destroy_connectioncxx . . $ amqp_destroy_connection(6448aa3e62633771).(connection_state_);
53 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rc == AMQP_STATUS_OK);
54 }
55
56 void basic_publishcxx . . $ basic_publish(e6c1e831a42f4ae7).(amqp_connection_state_tcxx . . $ amqp_connection_state_t# connectionState_,
57 const char *message_) {
58 amqp_bytes_tcxx . . $ amqp_bytes_t# message_bytes = amqp_cstring_bytescxx . . $ amqp_cstring_bytes(985756a1be2124b1).(message_);
59
60 amqp_basic_properties_tcxx . . $ amqp_basic_properties_t# properties;
61 properties._flagscxx . . $ amqp_basic_properties_t_#_flags. = 0;
62
63 properties._flagscxx . . $ amqp_basic_properties_t_#_flags. |= AMQP_BASIC_DELIVERY_MODE_FLAGcxx . . $ `include/rabbitmq-c/framing.h:822:9`!;
64 properties.delivery_modecxx . . $ amqp_basic_properties_t_#delivery_mode. = AMQP_DELIVERY_NONPERSISTENTcxx . . $ AMQP_DELIVERY_NONPERSISTENT.;
65
66 int retval = amqp_basic_publishcxx . . $ amqp_basic_publish(758b43e0008e1285).(
67 connectionState_, fixed_channel_idcxx . . $ fixed_channel_id., amqp_cstring_bytescxx . . $ amqp_cstring_bytes(985756a1be2124b1).(""),
68 amqp_cstring_bytescxx . . $ amqp_cstring_bytes(985756a1be2124b1).(test_queue_namecxx . . $ 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 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(retval == 0);
76 }
77
78 void queue_declarecxx . . $ queue_declare(e6c1e831a42f4ae7).(amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state_,
79 const char *queue_name_) {
80 amqp_queue_declare_ok_tcxx . . $ amqp_queue_declare_ok_t# *res = amqp_queue_declarecxx . . $ amqp_queue_declare(eb91ea1173bea64c).(
81 connection_state_, fixed_channel_idcxx . . $ fixed_channel_id., amqp_cstring_bytescxx . . $ amqp_cstring_bytes(985756a1be2124b1).(queue_name_),
82 /*passive*/ 0,
83 /*durable*/ 0,
84 /*exclusive*/ 0,
85 /*auto_delete*/ 1, amqp_empty_tablecxx . . $ amqp_empty_table.);
86 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(res != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
87 }
88
89 char *basic_getcxx . . $ basic_get(c7505ba16795bfeb).(amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state_,
90 const char *queue_name_, uint64_tcxx . . $ uint64_t# *out_body_size_) {
91 amqp_rpc_reply_tcxx . . $ amqp_rpc_reply_t# rpc_reply;
92 amqp_time_tcxx . . $ amqp_time_t# deadline;
93 struct timevalcxx . . $ timeval# timeout = {5, 0};
94 int time_rc = amqp_time_from_nowcxx . . $ amqp_time_from_now(64a1f94b3ba1ec68).(&deadline, &timeout);
95 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(time_rc == AMQP_STATUS_OK);
96
97 do {
98 rpc_reply = amqp_basic_getcxx . . $ amqp_basic_get(937581a40e68f038).(connection_state_, fixed_channel_idcxx . . $ fixed_channel_id.,
99 amqp_cstring_bytescxx . . $ amqp_cstring_bytes(985756a1be2124b1).(queue_name_), /*no_ack*/ 1);
100 } while (rpc_reply.reply_typecxx . . $ amqp_rpc_reply_t_#reply_type. == AMQP_RESPONSE_NORMALcxx . . $ AMQP_RESPONSE_NORMAL. &&
101 rpc_reply.replycxx . . $ amqp_rpc_reply_t_#reply..idcxx . . $ amqp_method_t_#id. == AMQP_BASIC_GET_EMPTY_METHODcxx . . $ `include/rabbitmq-c/framing.h:655:9`! &&
102 amqp_time_has_pastcxx . . $ amqp_time_has_past(14b75da8f64c9fb6).(deadline) == AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.);
103
104 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
105 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rpc_reply.reply.id == AMQP_BASIC_GET_OK_METHODcxx . . $ `include/rabbitmq-c/framing.h:643:9`!);
106
107 amqp_message_tcxx . . $ amqp_message_t# message;
108 rpc_reply =
109 amqp_read_messagecxx . . $ amqp_read_message(5d1787fcf772053e).(connection_state_, fixed_channel_idcxx . . $ fixed_channel_id., &message, 0);
110 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
111
112 char *body = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(message.bodycxx . . $ amqp_message_t_#body..lencxx . . $ amqp_bytes_t_#len.);
113 if (body == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
114 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
115 }
116 memcpycxx . . $ memcpy(cc817cb175f282d).(body, message.bodycxx . . $ amqp_message_t_#body..bytescxx . . $ amqp_bytes_t_#bytes., message.bodycxx . . $ amqp_message_t_#body..lencxx . . $ amqp_bytes_t_#len.);
117 *out_body_size_ = message.bodycxx . . $ amqp_message_t_#body..lencxx . . $ amqp_bytes_t_#len.;
118 amqp_destroy_messagecxx . . $ amqp_destroy_message(e9fcd52d5d74763b).(&message);
119
120 return body;
121 }
122
123 void publish_and_basic_get_messagecxx . . $ publish_and_basic_get_message(85c52e162fed56f9).(const char *msg_to_publish) {
124 amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state = setup_connection_and_channelcxx . . $ setup_connection_and_channel(79af3635f28998e8).();
125
126 queue_declarecxx . . $ queue_declare(e6c1e831a42f4ae7).(connection_state, test_queue_namecxx . . $ test_queue_name.);
127 basic_publishcxx . . $ basic_publish(e6c1e831a42f4ae7).(connection_state, msg_to_publish);
128
129 uint64_tcxx . . $ uint64_t# body_size;
130 char *msg = basic_getcxx . . $ basic_get(c7505ba16795bfeb).(connection_state, test_queue_namecxx . . $ test_queue_name., &body_size);
131
132 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(msg != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && "Test errored: memory allocation failed!");
133 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(body_size == strlen(msg_to_publish));
134 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(strncmp(msg_to_publish, msg, body_size) == 0);
135 freecxx . . $ free(5d22bdacc48458e8).(msg);
136
137 close_and_destroy_connectioncxx . . $ close_and_destroy_connection(40203a97fc6341ef).(connection_state);
138 }
139
140 char *consume_messagecxx . . $ consume_message(c7505ba16795bfeb).(amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state_,
141 const char *queue_name_, uint64_tcxx . . $ uint64_t# *out_body_size_) {
142 amqp_basic_consume_ok_tcxx . . $ amqp_basic_consume_ok_t# *result =
143 amqp_basic_consumecxx . . $ amqp_basic_consume(404f68cdaaa643c3).(connection_state_, fixed_channel_idcxx . . $ fixed_channel_id.,
144 amqp_cstring_bytescxx . . $ amqp_cstring_bytes(985756a1be2124b1).(queue_name_), amqp_empty_bytescxx . . $ amqp_empty_bytes.,
145 /*no_local*/ 0,
146 /*no_ack*/ 1,
147 /*exclusive*/ 0, amqp_empty_tablecxx . . $ amqp_empty_table.);
148 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(result != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
149
150 amqp_envelope_tcxx . . $ amqp_envelope_t# envelope;
151 struct timevalcxx . . $ timeval# timeout = {5, 0};
152 amqp_rpc_reply_tcxx . . $ amqp_rpc_reply_t# rpc_reply =
153 amqp_consume_messagecxx . . $ amqp_consume_message(808493c90d553fc8).(connection_state_, &envelope, &timeout, 0);
154 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);
155
156 *out_body_size_ = envelope.messagecxx . . $ amqp_envelope_t_#message..bodycxx . . $ amqp_message_t_#body..lencxx . . $ amqp_bytes_t_#len.;
157 char *body = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(*out_body_size_);
158 if (body == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
159 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
160 }
161 if (*out_body_size_) {
162 memcpycxx . . $ memcpy(cc817cb175f282d).(body, envelope.messagecxx . . $ amqp_envelope_t_#message..bodycxx . . $ amqp_message_t_#body..bytescxx . . $ amqp_bytes_t_#bytes., *out_body_size_);
163 }
164
165 amqp_destroy_envelopecxx . . $ amqp_destroy_envelope(d33fdf5f709a4a28).(&envelope);
166 return body;
167 }
168
169 void publish_and_consume_messagecxx . . $ publish_and_consume_message(85c52e162fed56f9).(const char *msg_to_publish) {
170 amqp_connection_state_tcxx . . $ amqp_connection_state_t# connection_state = setup_connection_and_channelcxx . . $ setup_connection_and_channel(79af3635f28998e8).();
171
172 queue_declarecxx . . $ queue_declare(e6c1e831a42f4ae7).(connection_state, test_queue_namecxx . . $ test_queue_name.);
173 basic_publishcxx . . $ basic_publish(e6c1e831a42f4ae7).(connection_state, msg_to_publish);
174
175 uint64_tcxx . . $ uint64_t# body_size;
176 char *msg = consume_messagecxx . . $ consume_message(c7505ba16795bfeb).(connection_state, test_queue_namecxx . . $ test_queue_name., &body_size);
177
178 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(msg != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && "Test errored: memory allocation failed!");
179 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(body_size == strlen(msg_to_publish));
180 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(strncmp(msg_to_publish, msg, body_size) == 0);
181 freecxx . . $ free(5d22bdacc48458e8).(msg);
182
183 close_and_destroy_connectioncxx . . $ close_and_destroy_connection(40203a97fc6341ef).(connection_state);
184 }
185
186 int maincxx . . $ main(b126dc7c1de90089).(void) {
187 publish_and_basic_get_messagecxx . . $ publish_and_basic_get_message(85c52e162fed56f9).("");
188 publish_and_basic_get_messagecxx . . $ publish_and_basic_get_message(85c52e162fed56f9).("TEST");
189
190 publish_and_consume_messagecxx . . $ publish_and_consume_message(85c52e162fed56f9).("");
191 publish_and_consume_messagecxx . . $ publish_and_consume_message(85c52e162fed56f9).("TEST");
192
193 return 0;
194 }