File: files_interfaces_test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (112 lines) | stat: -rw-r--r-- 2,466 bytes parent folder | download | duplicates (2)
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
#include <test.h>

#include <files_interfaces.h>
#include <misc_lib.h>                                          /* xsnprintf */


#define FILE_SIZE (sizeof(FILE_CONTENTS) - 1)
#define FILE_LINE "some garbage!"
#define FILE_CORRUPTED_LINE "some \0 , gar\0bage!"
#define LINE_SIZE (sizeof(FILE_LINE) - 1)

char CFWORKDIR[CF_BUFSIZE];
char FILE_NAME[CF_BUFSIZE];
char FILE_NAME_CORRUPT[CF_BUFSIZE];
char FILE_NAME_EMPTY[CF_BUFSIZE];

static void tests_setup(void)
{
    xsnprintf(CFWORKDIR, CF_BUFSIZE, "/tmp/files_interfaces_test.XXXXXX");
    mkdtemp(CFWORKDIR);
    xsnprintf(FILE_NAME, CF_BUFSIZE, "%s/cf_files_interfaces_test", CFWORKDIR);
    xsnprintf(FILE_NAME_CORRUPT, CF_BUFSIZE, "%s/cf_files_interfaces_test_corrupt", CFWORKDIR);
    xsnprintf(FILE_NAME_EMPTY, CF_BUFSIZE, "%s/cf_files_interfaces_test_empty", CFWORKDIR);
}

static void tests_teardown(void)
{
    char cmd[CF_BUFSIZE];
    xsnprintf(cmd, CF_BUFSIZE, "rm -rf '%s'", CFWORKDIR);
    system(cmd);
}

static void CreateGarbage(const char *filename)
{
    FILE *fh = fopen(filename, "w");
    for(int i = 0; i < 10; ++i)
    {
        fwrite(FILE_LINE, 14, 1, fh);
    }
    fclose(fh);
}

static void CreateCorruptedGarbage(const char *filename)
{
    FILE *fh = fopen(filename, "w");
    for(int i = 0; i < 10; ++i)
    {
        fwrite(FILE_CORRUPTED_LINE, 18, 1, fh);
    }
    fclose(fh);
}

static void test_cfreadline_valid(void)
{
    CreateGarbage(FILE_NAME);
    FILE *fin = fopen(FILE_NAME, "r");

    //test with non-empty file and valid file pointer
    size_t bs = CF_BUFSIZE;
    char *b = xmalloc(bs);

    ssize_t read = CfReadLine(&b, &bs, fin);
    assert_true(read > 0);
    assert_string_equal(b, FILE_LINE);

    if (fin)
    {
        fclose(fin);
    }

    free(b);
}

static void test_cfreadline_corrupted(void)
{
    CreateCorruptedGarbage(FILE_NAME);
    FILE *fin = fopen(FILE_NAME, "r");

    size_t bs = CF_BUFSIZE;
    char *b = xmalloc(bs);

    //test with non-empty file and valid file pointer
    ssize_t read = CfReadLine(&b, &bs, fin);
    assert_true(read > 0);
    assert_string_not_equal(b, FILE_LINE);

    if (fin)
    {
        fclose(fin);
    }

    free(b);
}

int main()
{
    PRINT_TEST_BANNER();
    tests_setup();

    const UnitTest tests[] =
    {
        unit_test(test_cfreadline_valid),
        unit_test(test_cfreadline_corrupted),
    };

    PRINT_TEST_BANNER();
    int ret = run_tests(tests);

    tests_teardown();
    return ret;
}