File: enterprise_extension_test.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (77 lines) | stat: -rw-r--r-- 2,259 bytes parent folder | download | duplicates (7)
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
#include <test.h>

#include <stdint.h>
#include <stdlib.h>

#include <enterprise_extension.h>


ENTERPRISE_FUNC_2ARG_DECLARE(int64_t, extension_function, int32_t, short_int, int64_t, long_int);
ENTERPRISE_FUNC_2ARG_DECLARE(int64_t, extension_function_broken, int32_t, short_int, int64_t, long_int);

ENTERPRISE_FUNC_2ARG_DEFINE_STUB(int64_t, extension_function, int32_t, short_int, int64_t, long_int)
{
    return short_int + long_int;
}

ENTERPRISE_FUNC_2ARG_DEFINE_STUB(int64_t, extension_function_broken, int32_t, short_int, int64_t, long_int)
{
    return short_int + long_int;
}

static void test_extension_function_stub(void)
{
    putenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR=nonexistingdir");

    assert_int_equal(extension_function(2, 3), 5);

    unsetenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR");
}

static void test_extension_function(void)
{
    putenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR=.libs");

    assert_int_equal(extension_function(2, 3), 6);

    unsetenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR");
}

static void test_extension_function_broken(void)
{
    putenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR=.libs");

    // This one should call the stub, even if the extension is available, because the
    // function signature is different.
    assert_int_equal(extension_function_broken(2, 3), 5);

    unsetenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR");
}

static void test_extension_function_version_mismatch()
{
    putenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR=.libs");
    putenv("CFENGINE_TEST_RETURN_VERSION=1.1.1");

    // This one should call the stub, even if the extension is available, because the
    // version is different.
    assert_int_equal(extension_function(2, 3), 5);

    unsetenv("CFENGINE_TEST_RETURN_VERSION");
    unsetenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR");
}

int main()
{
    putenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DO_CLOSE=1");
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_extension_function_stub),
        unit_test(test_extension_function),
        unit_test(test_extension_function_broken),
        unit_test(test_extension_function_version_mismatch),
    };

    return run_tests(tests);
}