File: verify_databases_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 (73 lines) | stat: -rw-r--r-- 2,272 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
#include <test.h>

#include <verify_databases.c> // Include .c file to test static functions

void test_ValidateSQLTableName(void)
{
    char db[CF_MAXVARSIZE];
    char table[CF_MAXVARSIZE];

    // Valid database table paths:
    assert_true(ValidateSQLTableName("cfsettings.users", db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "cfsettings");
    assert_string_equal(table, "users");

    assert_true(ValidateSQLTableName("a.b", db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "a");
    assert_string_equal(table, "b");

    assert_true(ValidateSQLTableName(".b", db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "");
    assert_string_equal(table, "b");

    assert_true(ValidateSQLTableName("a.", db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "a");
    assert_string_equal(table, "");


    // Invalid paths:

    char path[CF_MAXVARSIZE]; // ValidateSQLTableName will write to it :O

    strcpy(path, "nosep");
    db[0] = table[0] = '\0';
    assert_false(ValidateSQLTableName(path, db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "");
    assert_string_equal(table, "");

    strcpy(path, "cfsettings\\users/users");
    db[0] = table[0] = '\0';
    assert_false(ValidateSQLTableName(path, db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "");
    assert_string_equal(table, "");

    strcpy(path, "a.b/c");
    db[0] = table[0] = '\0';
    assert_false(ValidateSQLTableName(path, db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "");
    assert_string_equal(table, "");

    // Seems like they should work, but they are invalid because of bugs:
    db[0] = table[0] = '\0';
    strcpy(path, "a/b");
    assert_false(ValidateSQLTableName(path, db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "");
    assert_string_equal(table, "");

    db[0] = table[0] = '\0';
    strcpy(path, "a\\b");
    assert_false(ValidateSQLTableName(path, db, sizeof(db), table, sizeof(table)));
    assert_string_equal(db, "");
    assert_string_equal(table, "");
}

int main()
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_ValidateSQLTableName),
    };

    return run_tests(tests);
}