File: test-cautil.c

package info (click to toggle)
casync 2%2B20201210-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,952 kB
  • sloc: ansic: 31,284; sh: 423; python: 69; makefile: 6
file content (130 lines) | stat: -rw-r--r-- 6,496 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
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "cautil.h"
#include "util.h"

static void test_locator_has_suffix(void) {

        assert_se(ca_locator_has_suffix(NULL, NULL));
        assert_se(ca_locator_has_suffix("", ""));
        assert_se(ca_locator_has_suffix(NULL, ""));
        assert_se(ca_locator_has_suffix("", NULL));

        assert_se(ca_locator_has_suffix("foo", NULL));
        assert_se(ca_locator_has_suffix("foo", ""));
        assert_se(!ca_locator_has_suffix("", "foo"));
        assert_se(!ca_locator_has_suffix(NULL, "foo"));

        assert_se(ca_locator_has_suffix("foo.bar", ".bar"));
        assert_se(ca_locator_has_suffix("foo.bar", "bar"));
        assert_se(ca_locator_has_suffix("foo.bar", "ar"));
        assert_se(ca_locator_has_suffix("foo.bar", "r"));
        assert_se(ca_locator_has_suffix("foo.bar", ""));
        assert_se(!ca_locator_has_suffix("foo.bar", "foo"));

        assert_se(ca_locator_has_suffix("http://foobar.com/foo.bar", ".bar"));
        assert_se(!ca_locator_has_suffix("http://foobar.com/foo.bar", ".qux"));

        assert_se(ca_locator_has_suffix("http://foobar.com/foo.bar?miep=mup", ".bar"));
        assert_se(ca_locator_has_suffix("http://foobar.com/foo.bar?miep=.qux", ".bar"));
        assert_se(!ca_locator_has_suffix("http://foobar.com/foo.bar?miep=.qux", ".qux"));

        assert_se(ca_locator_has_suffix("http://foobar.com/foo.bar;miep=mup", ".bar"));
        assert_se(ca_locator_has_suffix("http://foobar.com/foo.bar;miep=.qux", ".bar"));
        assert_se(!ca_locator_has_suffix("http://foobar.com/foo.bar;miep=.qux", ".qux"));
}

static void test_strip_file_url_one(const char *a, const char *b) {
        char *s;

        s = ca_strip_file_url(a);
        assert_se(s);

        assert_se(streq(s, b));

        free(s);
}

static void test_strip_file_url(void) {

        test_strip_file_url_one("/foo/bar", "/foo/bar");
        test_strip_file_url_one("", "");
        test_strip_file_url_one("file:///foobar.txt", "/foobar.txt");
        test_strip_file_url_one("file:///foo%20bar.txt", "/foo bar.txt");
        test_strip_file_url_one("file:///foo bar.txt", "/foo bar.txt");
        test_strip_file_url_one("file:///foo%%xyz.txt", "/foo%%xyz.txt");

        test_strip_file_url_one("file://localhost/piff.txt", "/piff.txt");

        test_strip_file_url_one("file://elsewhere/piff.txt", "file://elsewhere/piff.txt");
        test_strip_file_url_one("http://online.com/piff.txt", "http://online.com/piff.txt");
}

static void test_classify_locator(void) {
        assert_se(ca_classify_locator(NULL) == _CA_LOCATOR_CLASS_INVALID);
        assert_se(ca_classify_locator("") == _CA_LOCATOR_CLASS_INVALID);
        assert_se(ca_classify_locator("x") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator(".") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator("..") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator("./") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator("/") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator("/foo") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator("./foo") == CA_LOCATOR_PATH);
        assert_se(ca_classify_locator("http://foobar.com/xyz.txt") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("http://foobar.com/xyz.txt?piff=paff") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("http://user@foobar.com/xyz.txt?piff=paff") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("http://user@localhost:1234/xyz.txt?piff=paff") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("http://127.0.0.1:1234/xyz.txt?piff=paff") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("http://[::]:1234/xyz.txt?piff=paff") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("http://user@foobar.com") == CA_LOCATOR_URL);
        assert_se(ca_classify_locator("foobar:quux.txt") == CA_LOCATOR_SSH);
        assert_se(ca_classify_locator("lennart@foobar:quux.txt") == CA_LOCATOR_SSH);
}

static void test_locator_patch_last_component_one(const char *old, const char *last_component, int code, const char *result) {
        char *p;

        assert_se(ca_locator_patch_last_component(old, last_component, &p) == code);

        if (code >= 0) {
                assert_se(streq(p, result));
                free(p);
        }
}

static void test_locator_patch_last_component(void) {

        test_locator_patch_last_component_one("", "quux", -EINVAL, NULL);

        test_locator_patch_last_component_one("foo", "quux", 0, "quux");
        test_locator_patch_last_component_one("./miepf", "quux", 0, "./quux");
        test_locator_patch_last_component_one("../miepf", "quux", 0, "../quux");
        test_locator_patch_last_component_one("some/thing/miepf", "quux", 0, "some/thing/quux");
        test_locator_patch_last_component_one("/some/thing/miepf", "quux", 0, "/some/thing/quux");
        test_locator_patch_last_component_one("localhost:", "quux", 0, "quux");

        test_locator_patch_last_component_one("localhost:miepf", "quux", 0, "localhost:quux");
        test_locator_patch_last_component_one("localhost:miepf/waldo", "quux", 0, "localhost:miepf/quux");
        test_locator_patch_last_component_one("localhost:/miepf", "quux", 0, "localhost:/quux");
        test_locator_patch_last_component_one("localhost:/miepf/waldo", "quux", 0, "localhost:/miepf/quux");

        test_locator_patch_last_component_one("http://example.com", "quux", 0, "http://example.com/quux");
        test_locator_patch_last_component_one("http://example.com?foo", "quux", 0, "http://example.com/quux");
        test_locator_patch_last_component_one("http://example.com/", "quux", 0, "http://example.com/quux");
        test_locator_patch_last_component_one("http://example.com/?foo", "quux", 0, "http://example.com/quux");
        test_locator_patch_last_component_one("http://example.com/miepf", "quux", 0, "http://example.com/quux");
        test_locator_patch_last_component_one("http://example.com/miepf?foo", "quux", 0, "http://example.com/quux");
        test_locator_patch_last_component_one("http://example.com/miepf/more", "quux", 0, "http://example.com/miepf/quux");
        test_locator_patch_last_component_one("http://example.com/miepf/more?foo", "quux", 0, "http://example.com/miepf/quux");

}

int main(int argc, char *argv[]) {

        test_locator_has_suffix();
        test_strip_file_url();
        test_classify_locator();
        test_locator_patch_last_component();

        return 0;
}