Debian Codegraph @

crt/aws-c-common/tests/logging/log_channel_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/log_channel.h>
7
8 #include <aws/common/string.h>
9 #include <aws/common/thread.h>
10
11 #include <aws/common/log_writer.h>
12
13 #include <aws/testing/aws_test_harness.h>
14
15 #include <inttypes.h>
16
17 /*
18 * Mock writer to capture what gets passed through
19 */
20 struct mock_log_writer_impl {
21 struct aws_array_list log_lines;
22 };
23
24 static int s_mock_log_writer_write(struct aws_log_writer *writer, const struct aws_string *output) {
25 struct mock_log_writer_impl *impl = (struct mock_log_writer_impl *)writer->impl;
26
27 struct aws_string *output_copy = aws_string_new_from_string(writer->allocator, output);
28 if (output_copy == NULL) {
29 return AWS_OP_ERR;
30 }
31
32 aws_array_list_push_back(&impl->log_lines, &output_copy);
33
34 return AWS_OP_SUCCESS;
35 }
36
37 static void s_mock_log_writer_clean_up(struct aws_log_writer *writer) {
38 struct mock_log_writer_impl *impl = (struct mock_log_writer_impl *)writer->impl;
39
40 size_t line_count = aws_array_list_length(&impl->log_lines);
41 for (size_t i = 0; i < line_count; ++i) {
42 struct aws_string *line = NULL;
43 if (aws_array_list_get_at(&impl->log_lines, &line, i)) {
44 continue;
45 }
46
47 aws_string_destroy(line);
48 }
49
50 aws_array_list_clean_up(&impl->log_lines);
51
52 aws_mem_release(writer->allocator, impl);
53 }
54
55 static struct aws_log_writer_vtable s_mock_writer_vtable = {
56 .write = s_mock_log_writer_write,
57 .clean_up = s_mock_log_writer_clean_up,
58 };
59
60 static int s_aws_mock_log_writer_init(struct aws_log_writer *writer, struct aws_allocator *allocator) {
61 struct mock_log_writer_impl *impl =
62 (struct mock_log_writer_impl *)aws_mem_acquire(allocator, sizeof(struct mock_log_writer_impl));
63 if (impl == NULL) {
64 return AWS_OP_ERR;
65 }
66
67 if (aws_array_list_init_dynamic(&impl->log_lines, allocator, 10, sizeof(struct aws_string *))) {
68 aws_mem_release(allocator, impl);
69 return AWS_OP_ERR;
70 }
71
72 writer->vtable = &s_mock_writer_vtable;
73 writer->allocator = allocator;
74 writer->impl = impl;
75
76 return AWS_OP_SUCCESS;
77 }
78
79 /*
80 * Test utilities
81 */
82 static char s_test_error_message[4096];
83
84 static bool s_verify_mock_equal(
85 struct aws_log_writer *writer,
86 const struct aws_string ***test_lines,
87 size_t array_length) {
88 struct mock_log_writer_impl *impl = (struct mock_log_writer_impl *)writer->impl;
89
90 size_t line_count = aws_array_list_length(&impl->log_lines);
91 if (line_count != array_length) {
92 snprintf(
93 s_test_error_message,
94 sizeof(s_test_error_message),
95 "Expected %" PRIu64 " lines, but received %" PRIu64 "",
96 (uint64_t)array_length,
97 (uint64_t)line_count);
98 return false;
99 }
100
101 for (size_t i = 0; i < array_length; ++i) {
102 struct aws_string *captured = NULL;
103 if (aws_array_list_get_at(&impl->log_lines, &captured, i)) {
104 snprintf(s_test_error_message, sizeof(s_test_error_message), "Unable to fetch log line from array list");
105 return false;
106 }
107
108 const struct aws_string *original = *(test_lines[i]);
109 if (!aws_string_eq(original, captured)) {
110 snprintf(
111 s_test_error_message,
112 sizeof(s_test_error_message),
113 "Expected log line:\n%s\nbut received log line:\n%s",
114 (char *)original->bytes,
115 (char *)captured->bytes);
116 return false;
117 }
118 }
119
120 return true;
121 }
122
123 typedef int (
124 *init_channel_fn)(struct aws_log_channel *channel, struct aws_allocator *allocator, struct aws_log_writer *writer);
125
126 static int s_do_channel_test(
127 struct aws_allocator *allocator,
128 init_channel_fn init_fn,
129 const struct aws_string ***test_lines,
130 size_t test_lines_length,
131 int *sleep_times) {
132
133 struct aws_log_writer mock_writer;
134 if (s_aws_mock_log_writer_init(&mock_writer, allocator)) {
135 return AWS_OP_ERR;
136 }
137
138 struct aws_log_channel log_channel;
139 if (init_fn(&log_channel, allocator, &mock_writer)) {
140 return AWS_OP_ERR;
141 }
142
143 int result = AWS_OP_SUCCESS;
144
145 for (size_t i = 0; i < test_lines_length; ++i) {
146 struct aws_string *test_line_copy = aws_string_new_from_string(log_channel.allocator, *(test_lines[i]));
147
148 if ((log_channel.vtable->send)(&log_channel, test_line_copy)) {
149 snprintf(s_test_error_message, sizeof(s_test_error_message), "Failed call to log channel send");
150 result = AWS_OP_ERR;
151 }
152
153 /*
154 * For background case, optionally sleep to vary the push timing
155 */
156 if (sleep_times && sleep_times[i] > 0) {
157 aws_thread_current_sleep(sleep_times[i]);
158 }
159 }
160
161 aws_log_channel_clean_up(&log_channel);
162
163 if (!s_verify_mock_equal(log_channel.writer, test_lines, test_lines_length)) {
164 result = AWS_OP_ERR;
165 }
166
167 aws_log_writer_clean_up(&mock_writer);
168
169 return result;
170 }
171
172 /*
173 * Test body helper macros
174 */
175 #define DEFINE_FOREGROUND_LOG_CHANNEL_TEST(test_name, string_array_name) \
176 static int s_foreground_log_channel_##test_name(struct aws_allocator *allocator, void *ctx) { \
177 (void)ctx; \
178 return s_do_channel_test( \
179 allocator, \
180 aws_log_channel_init_foreground, \
181 string_array_name, \
182 sizeof(string_array_name) / sizeof(struct aws_string **), \
183 NULL); \
184 } \
185 AWS_TEST_CASE(test_foreground_log_channel_##test_name, s_foreground_log_channel_##test_name);
186
187 #define DEFINE_BACKGROUND_LOG_CHANNEL_TEST(test_name, string_array_name, sleep_times) \
188 static int s_background_log_channel_##test_name(struct aws_allocator *allocator, void *ctx) { \
189 (void)ctx; \
190 return s_do_channel_test( \
191 allocator, \
192 aws_log_channel_init_background, \
193 string_array_name, \
194 sizeof(string_array_name) / sizeof(struct aws_string **), \
195 sleep_times); \
196 } \
197 AWS_TEST_CASE(test_background_log_channel_##test_name, s_background_log_channel_##test_name);
198
199 /*
200 * Test data
201 */
202 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_1, "1");
203 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_2, "2");
204 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_3, "3");
205 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_4, "4");
206
207 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_simple, "A simple line.\n");
208 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_multiline, "There's\na lot\n\tof snow outside.\n");
209 AWS_STATIC_STRING_FROM_LITERAL(s_log_line_fake, "[DEBUG] [??] [1234567] - Time to crash\n");
210
211 const struct aws_string **s_channel_test_one_line[] = {&s_log_line_1};
212
213 const struct aws_string **s_channel_test_numbers[] = {&s_log_line_1, &s_log_line_2, &s_log_line_3, &s_log_line_4};
214
215 const struct aws_string **s_channel_test_words[] = {&s_log_line_simple, &s_log_line_multiline, &s_log_line_fake};
216
217 const struct aws_string **s_channel_test_all[] = {
218 &s_log_line_1,
219 &s_log_line_2,
220 &s_log_line_3,
221 &s_log_line_4,
222 &s_log_line_simple,
223 &s_log_line_multiline,
224 &s_log_line_fake,
225 };
226
227 static int s_background_sleep_times_ns[] = {0, 100000, 0, 0, 1000000, 0, 1000000};
228
229 /*
230 * Foreground channel tests
231 */
232 DEFINE_FOREGROUND_LOG_CHANNEL_TEST(single_line, s_channel_test_one_line)
233
234 DEFINE_FOREGROUND_LOG_CHANNEL_TEST(numbers, s_channel_test_numbers)
235
236 DEFINE_FOREGROUND_LOG_CHANNEL_TEST(words, s_channel_test_words)
237
238 DEFINE_FOREGROUND_LOG_CHANNEL_TEST(all, s_channel_test_all)
239
240 /*
241 * Background channel tests
242 */
243 DEFINE_BACKGROUND_LOG_CHANNEL_TEST(single_line, s_channel_test_one_line, NULL)
244
245 DEFINE_BACKGROUND_LOG_CHANNEL_TEST(numbers, s_channel_test_numbers, s_background_sleep_times_ns)
246
247 DEFINE_BACKGROUND_LOG_CHANNEL_TEST(words, s_channel_test_words, s_background_sleep_times_ns)
248
249 DEFINE_BACKGROUND_LOG_CHANNEL_TEST(all, s_channel_test_all, s_background_sleep_times_ns)