Debian Codegraph @

crt/aws-c-common/tests/logging/log_writer_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 "logging_test_utilities.h"
7
8 #include <aws/common/log_writer.h>
9
10 #include <aws/common/file.h>
11 #include <aws/common/string.h>
12 #include <aws/testing/aws_test_harness.h>
13
14 #include <errno.h>
15 #include <stdio.h>
16
17 #ifndef _WIN32
18 # include <sys/file.h>
19 #endif /* _WIN32 */
20
21 #define TEST_WRITER_MAX_BUFFER_SIZE 4096
22
23 int do_default_log_writer_test(
24 struct aws_log_writer *writer,
25 const char *test_file_name,
26 const char *expected_file_content,
27 const struct aws_string *output,
28 FILE *close_fp) {
29
30 int result = writer->vtable->write(writer, output);
31
32 aws_log_writer_clean_up(writer);
33
34 /*
35 * When we redirect stdout/stderr to a file, we need to close the file manually since the writer implementations do
36 * not do so.
37 */
38 if (close_fp != NULL) {
39 fclose(close_fp);
40 }
41
42 char buffer[TEST_WRITER_MAX_BUFFER_SIZE];
43 FILE *file = aws_fopen(test_file_name, "r");
44 int open_error = errno;
45 size_t bytes_read = 0;
46
47 if (file != NULL) {
48 bytes_read = fread(buffer, 1, TEST_WRITER_MAX_BUFFER_SIZE - 1, file);
49 fclose(file);
50 }
51 remove(test_file_name);
52
53 /*
54 * Check that the write call was successful
55 */
56 ASSERT_TRUE(result == AWS_OP_SUCCESS, "Writing operation failed");
57
58 /*
59 * Check the file was read successfully
60 */
61 ASSERT_TRUE(
62 file != NULL, "Unable to open output file \"%s\" to verify contents. Error: %d", test_file_name, open_error);
63 ASSERT_TRUE(bytes_read >= 0, "Failed to read test output file \"%s\"", test_file_name);
64
65 /*
66 * add end of string marker
67 */
68 buffer[bytes_read] = 0;
69
70 /*
71 * Check file contents
72 */
73 ASSERT_TRUE(
74 strcmp(buffer, expected_file_content) == 0,
75 "Expected log file to contain:\n\n%s\n\nbut instead it contained:\n\n%s\n",
76 expected_file_content,
77 buffer);
78
79 return AWS_OP_SUCCESS;
80 }
81
82 /*
83 * Test cases
84 */
85
86 #define EXISTING_TEXT "Some existing text\n"
87 #define SIMPLE_FILE_CONTENT "A few\nlog lines.\n"
88 static const char *s_combined_text = EXISTING_TEXT SIMPLE_FILE_CONTENT;
89
90 AWS_STATIC_STRING_FROM_LITERAL(s_simple_file_content, SIMPLE_FILE_CONTENT);
91
92 /*
93 * Simple file test
94 */
95 static int s_log_writer_simple_file_test(struct aws_allocator *allocator, void *ctx) {
96 (void)ctx;
97
98 struct aws_string *test_file_str = aws_string_new_log_writer_test_filename(allocator);
99 const char *test_file_cstr = aws_string_c_str(test_file_str);
100 remove(test_file_cstr);
101
102 struct aws_log_writer_file_options options = {.filename = test_file_cstr};
103
104 struct aws_log_writer writer;
105 ASSERT_SUCCESS(aws_log_writer_init_file(&writer, allocator, &options));
106
107 ASSERT_SUCCESS(
108 do_default_log_writer_test(&writer, test_file_cstr, SIMPLE_FILE_CONTENT, s_simple_file_content, NULL));
109
110 aws_string_destroy(test_file_str);
111 return AWS_OP_SUCCESS;
112 }
113 AWS_TEST_CASE(test_log_writer_simple_file_test, s_log_writer_simple_file_test);
114
115 /*
116 * Existing file test (verifies append is being used)
117 */
118 static int s_log_writer_existing_file_test(struct aws_allocator *allocator, void *ctx) {
119 (void)ctx;
120
121 struct aws_string *test_file_str = aws_string_new_log_writer_test_filename(allocator);
122 const char *test_file_cstr = aws_string_c_str(test_file_str);
123 remove(test_file_cstr);
124
125 FILE *fp = aws_fopen(test_file_cstr, "w+");
126 fprintf(fp, EXISTING_TEXT);
127 fclose(fp);
128
129 struct aws_log_writer_file_options options = {.filename = test_file_cstr};
130
131 struct aws_log_writer writer;
132 ASSERT_SUCCESS(aws_log_writer_init_file(&writer, allocator, &options));
133
134 ASSERT_SUCCESS(do_default_log_writer_test(&writer, test_file_cstr, s_combined_text, s_simple_file_content, NULL));
135
136 aws_string_destroy(test_file_str);
137 return AWS_OP_SUCCESS;
138 }
139 AWS_TEST_CASE(test_log_writer_existing_file_test, s_log_writer_existing_file_test);
140
141 /*
142 * (Error case) Bad filename test
143 */
144 static int s_log_writer_bad_file_test(struct aws_allocator *allocator, void *ctx) {
145 (void)ctx;
146
147 struct aws_log_writer_file_options options = {.filename = "."};
148
149 struct aws_log_writer writer;
150 int result = aws_log_writer_init_file(&writer, allocator, &options);
151 int aws_error = aws_last_error();
152
153 ASSERT_TRUE(result == AWS_OP_ERR, "Log file open succeeded despite an invalid file name");
154
155 #ifdef _WIN32
156 ASSERT_TRUE(aws_error == AWS_ERROR_NO_PERMISSION, "File open error was not no permission as expected");
157 #else
158 ASSERT_TRUE(aws_error == AWS_ERROR_FILE_INVALID_PATH, "File open error was not invalid path as expected");
159 #endif /* _WIN32 */
160
161 return AWS_OP_SUCCESS;
162 }
163 AWS_TEST_CASE(test_log_writer_bad_file_test, s_log_writer_bad_file_test);