summaryrefslogtreecommitdiffstats
path: root/plugins/python/regress/testhelpers.h
blob: b5abb9921e42e44b3c77300d47a7cb0c35eab1b7 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2020 Robert Manner <robert.manner@oneidentity.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef PYTHON_TESTHELPERS
#define PYTHON_TESTHELPERS

#include "iohelpers.h"

#include "../pyhelpers.h"

#include "sudo_conf.h"

// just for the IDE
#ifndef SRC_DIR
#define SRC_DIR ""
#endif
#define TESTDATA_DIR SRC_DIR "/regress/testdata/"

extern const char *sudo_conf_developer_mode;
extern const char *sudo_conf_normal_mode;

#define TEMP_PATH_TEMPLATE "/tmp/sudo_check_python_exampleXXXXXX"

extern struct TestData {
    char *tmp_dir;
    char *tmp_dir2;
    char stdout_str[MAX_OUTPUT];
    char stderr_str[MAX_OUTPUT];

    char conv_str[MAX_OUTPUT];
    const char *conv_replies[8];

    // some example test data used by multiple test cases:
    char ** settings;
    char ** user_info;
    char ** command_info;
    char ** plugin_argv;
    int plugin_argc;
    char ** user_env;
    char ** plugin_options;
} data;

const char * expected_path(const char *format, ...);

char ** create_str_array(size_t count, ...);

#define RUN_TEST(testcase) \
    do { \
        int success = 1; \
        printf("Running test " #testcase " ... \n"); \
        if (!init()) { \
            printf("FAILED: initialization of testcase %s at %s:%d\n", #testcase, __FILE__, __LINE__); \
            success = 0; \
        } else \
        if (!testcase) { \
            printf("FAILED: testcase %s at %s:%d\n", #testcase, __FILE__, __LINE__); \
            success = 0; \
        } \
        if (!cleanup(success)) { \
            printf("FAILED: deitialization of testcase %s at %s:%d\n", #testcase, __FILE__, __LINE__); \
            success = 0; \
        } \
        if (!success) { \
            errors++; \
        } \
    } while(false)

#define VERIFY_PRINT_MSG(fmt, actual_str, actual, expected_str, expected, expected_to_be_message) \
    printf("Expectation failed at %s:%d:\n  actual is <<" fmt ">>: %s\n  %s <<" fmt ">>: %s\n", \
           __FILE__, __LINE__, actual, actual_str, expected_to_be_message, expected, expected_str)

#define VERIFY_CUSTOM(fmt, type, actual, expected, invert) \
    do { \
        type actual_value = (type)(actual); \
        int failed = (actual_value != expected); \
        if (invert) \
            failed = !failed; \
        if (failed) { \
            VERIFY_PRINT_MSG(fmt, #actual, actual_value, #expected, expected, invert ? "not expected to be" : "expected to be"); \
            return false; \
        } \
    } while(false)

#define VERIFY_EQ(fmt, type, actual, expected) VERIFY_CUSTOM(fmt, type, actual, expected, false)
#define VERIFY_NE(fmt, type, actual, not_expected) VERIFY_CUSTOM(fmt, type, actual, not_expected, true)

#define VERIFY_INT(actual, expected) VERIFY_EQ("%d", int, actual, expected)

#define VERIFY_PTR(actual, expected) VERIFY_EQ("%p", const void *, (const void *)actual, (const void *)expected)
#define VERIFY_PTR_NE(actual, not_expected) VERIFY_NE("%p", const void *, (const void *)actual, (const void *)not_expected)

#define VERIFY_TRUE(actual) VERIFY_NE("%d", int, actual, 0)
#define VERIFY_FALSE(actual) VERIFY_INT(actual, false)

#define VERIFY_NOT_NULL(actual) VERIFY_NE("%p", const void *, actual, NULL)

#define VERIFY_STR(actual, expected) \
    do { \
        const char *actual_str = actual; \
        if (!actual_str || strcmp(actual_str, expected) != 0) { \
            VERIFY_PRINT_MSG("%s", #actual, actual_str ? actual_str : "(null)", #expected, expected, "expected to be"); \
            return false; \
        } \
    } while(false)

#define VERIFY_STR_CONTAINS(actual, expected) \
    do { \
        const char *actual_str = actual; \
        if (!actual_str || strstr(actual_str, expected) == NULL) { \
            VERIFY_PRINT_MSG("%s", #actual, actual_str ? actual_str : "(null)", #expected, expected, "expected to contain the string"); \
            return false; \
        } \
    } while(false)

int is_update(void);

int verify_content(char *actual_content, const char *reference_path);

#define VERIFY_CONTENT(actual_output, reference_path) \
    VERIFY_TRUE(verify_content(actual_output, reference_path))

#define VERIFY_STDOUT(reference_path) \
    VERIFY_CONTENT(data.stdout_str, reference_path)

#define VERIFY_STDERR(reference_path) \
    VERIFY_CONTENT(data.stderr_str, reference_path)

#define VERIFY_CONV(reference_name) \
    VERIFY_CONTENT(data.conv_str, reference_name)

int verify_file(const char *actual_dir, const char *actual_file_name, const char *reference_path);

#define VERIFY_FILE(actual_file_name, reference_path) \
    VERIFY_TRUE(verify_file(data.tmp_dir, actual_file_name, reference_path))

int fake_conversation(int num_msgs, const struct sudo_conv_message msgs[],
                      struct sudo_conv_reply replies[], struct sudo_conv_callback *callback);

int fake_conversation_with_suspend(int num_msgs, const struct sudo_conv_message msgs[],
                                   struct sudo_conv_reply replies[], struct sudo_conv_callback *callback);

int fake_printf(int msg_type, const char *fmt, ...);

int verify_log_lines(const char *reference_path);

int mock_python_datetime_now(const char *plugin_name, const char *date_str);

#define VERIFY_LOG_LINES(reference_path) \
    VERIFY_TRUE(verify_log_lines(reference_path))

int verify_str_set(char **actual_set, char **expected_set, const char *actual_variable_name);

#define VERIFY_STR_SET(actual_set, ...) \
    do { \
        char **expected_set = create_str_array(__VA_ARGS__); \
        VERIFY_TRUE(verify_str_set(actual_set, expected_set, #actual_set)); \
        str_array_free(&expected_set); \
    } while(false)

#endif // PYTHON_TESTHELPERS