File: log_writer_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (163 lines) | stat: -rw-r--r-- 4,811 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include "logging_test_utilities.h"

#include <aws/common/log_writer.h>

#include <aws/common/file.h>
#include <aws/common/string.h>
#include <aws/testing/aws_test_harness.h>

#include <errno.h>
#include <stdio.h>

#ifndef _WIN32
#    include <sys/file.h>
#endif /* _WIN32 */

#define TEST_WRITER_MAX_BUFFER_SIZE 4096

int do_default_log_writer_test(
    struct aws_log_writer *writer,
    const char *test_file_name,
    const char *expected_file_content,
    const struct aws_string *output,
    FILE *close_fp) {

    int result = writer->vtable->write(writer, output);

    aws_log_writer_clean_up(writer);

    /*
     * When we redirect stdout/stderr to a file, we need to close the file manually since the writer implementations do
     * not do so.
     */
    if (close_fp != NULL) {
        fclose(close_fp);
    }

    char buffer[TEST_WRITER_MAX_BUFFER_SIZE];
    FILE *file = aws_fopen(test_file_name, "r");
    int open_error = errno;
    size_t bytes_read = 0;

    if (file != NULL) {
        bytes_read = fread(buffer, 1, TEST_WRITER_MAX_BUFFER_SIZE - 1, file);
        fclose(file);
    }
    remove(test_file_name);

    /*
     * Check that the write call was successful
     */
    ASSERT_TRUE(result == AWS_OP_SUCCESS, "Writing operation failed");

    /*
     * Check the file was read successfully
     */
    ASSERT_TRUE(
        file != NULL, "Unable to open output file \"%s\" to verify contents. Error: %d", test_file_name, open_error);
    ASSERT_TRUE(bytes_read >= 0, "Failed to read test output file \"%s\"", test_file_name);

    /*
     * add end of string marker
     */
    buffer[bytes_read] = 0;

    /*
     * Check file contents
     */
    ASSERT_TRUE(
        strcmp(buffer, expected_file_content) == 0,
        "Expected log file to contain:\n\n%s\n\nbut instead it contained:\n\n%s\n",
        expected_file_content,
        buffer);

    return AWS_OP_SUCCESS;
}

/*
 * Test cases
 */

#define EXISTING_TEXT "Some existing text\n"
#define SIMPLE_FILE_CONTENT "A few\nlog lines.\n"
static const char *s_combined_text = EXISTING_TEXT SIMPLE_FILE_CONTENT;

AWS_STATIC_STRING_FROM_LITERAL(s_simple_file_content, SIMPLE_FILE_CONTENT);

/*
 * Simple file test
 */
static int s_log_writer_simple_file_test(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    struct aws_string *test_file_str = aws_string_new_log_writer_test_filename(allocator);
    const char *test_file_cstr = aws_string_c_str(test_file_str);
    remove(test_file_cstr);

    struct aws_log_writer_file_options options = {.filename = test_file_cstr};

    struct aws_log_writer writer;
    ASSERT_SUCCESS(aws_log_writer_init_file(&writer, allocator, &options));

    ASSERT_SUCCESS(
        do_default_log_writer_test(&writer, test_file_cstr, SIMPLE_FILE_CONTENT, s_simple_file_content, NULL));

    aws_string_destroy(test_file_str);
    return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_log_writer_simple_file_test, s_log_writer_simple_file_test);

/*
 * Existing file test (verifies append is being used)
 */
static int s_log_writer_existing_file_test(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    struct aws_string *test_file_str = aws_string_new_log_writer_test_filename(allocator);
    const char *test_file_cstr = aws_string_c_str(test_file_str);
    remove(test_file_cstr);

    FILE *fp = aws_fopen(test_file_cstr, "w+");
    fprintf(fp, EXISTING_TEXT);
    fclose(fp);

    struct aws_log_writer_file_options options = {.filename = test_file_cstr};

    struct aws_log_writer writer;
    ASSERT_SUCCESS(aws_log_writer_init_file(&writer, allocator, &options));

    ASSERT_SUCCESS(do_default_log_writer_test(&writer, test_file_cstr, s_combined_text, s_simple_file_content, NULL));

    aws_string_destroy(test_file_str);
    return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_log_writer_existing_file_test, s_log_writer_existing_file_test);

/*
 * (Error case) Bad filename test
 */
static int s_log_writer_bad_file_test(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    struct aws_log_writer_file_options options = {.filename = "."};

    struct aws_log_writer writer;
    int result = aws_log_writer_init_file(&writer, allocator, &options);
    int aws_error = aws_last_error();

    ASSERT_TRUE(result == AWS_OP_ERR, "Log file open succeeded despite an invalid file name");

#ifdef _WIN32
    ASSERT_TRUE(aws_error == AWS_ERROR_NO_PERMISSION, "File open error was not no permission as expected");
#else
    ASSERT_TRUE(aws_error == AWS_ERROR_FILE_INVALID_PATH, "File open error was not invalid path as expected");
#endif /* _WIN32 */

    return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_log_writer_bad_file_test, s_log_writer_bad_file_test);