]> git.ipfire.org Git - people/ms/pakfire.git/blob - tests/libpakfire/path.c
path: Make pakfire_dirname into the path library
[people/ms/pakfire.git] / tests / libpakfire / path.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2019 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <limits.h>
22
23 #include <pakfire/path.h>
24 #include <pakfire/string.h>
25
26 #include "../testsuite.h"
27
28 static int test_path_normalize(const struct test* t) {
29 char path[PATH_MAX];
30
31 ASSERT_SUCCESS(pakfire_string_set(path, "/usr/bin/bash"));
32 ASSERT_SUCCESS(pakfire_path_normalize(path));
33 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
34
35 ASSERT_SUCCESS(pakfire_string_set(path, "/"));
36 ASSERT_SUCCESS(pakfire_path_normalize(path));
37 ASSERT_STRING_EQUALS(path, "/");
38
39 ASSERT_SUCCESS(pakfire_string_set(path, "/.."));
40 ASSERT_SUCCESS(pakfire_path_normalize(path));
41 ASSERT_STRING_EQUALS(path, "/");
42
43 ASSERT_SUCCESS(pakfire_string_set(path, "/../.."));
44 ASSERT_SUCCESS(pakfire_path_normalize(path));
45 ASSERT_STRING_EQUALS(path, "/");
46
47 ASSERT_SUCCESS(pakfire_string_set(path, "/usr/bin/bash"));
48 ASSERT_SUCCESS(pakfire_path_normalize(path));
49 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
50
51 ASSERT_SUCCESS(pakfire_string_set(path, "/usr/bin/sh/../bash"));
52 ASSERT_SUCCESS(pakfire_path_normalize(path));
53 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
54
55 return EXIT_SUCCESS;
56
57 FAIL:
58 return EXIT_FAILURE;
59 }
60
61 static int test_path_append(const struct test* t) {
62 char path[PATH_MAX];
63
64 ASSERT_SUCCESS(pakfire_path_append(path, "/usr/bin", "bash"));
65 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
66
67 ASSERT_SUCCESS(pakfire_path_append(path, "/usr/bin", "/bash"));
68 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
69
70 ASSERT_SUCCESS(pakfire_path_append(path, "/usr/bin/sh", "../bash"));
71 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
72
73 return EXIT_SUCCESS;
74
75 FAIL:
76 return EXIT_FAILURE;
77 }
78
79 static int test_path_merge(const struct test* t) {
80 char path[PATH_MAX];
81
82 ASSERT_SUCCESS(pakfire_path_merge(path, "/usr/bin/sh", "bash"));
83 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
84
85 ASSERT_SUCCESS(pakfire_path_merge(path, "/usr/bin", "/bash"));
86 ASSERT_STRING_EQUALS(path, "/bash");
87
88 ASSERT_SUCCESS(pakfire_path_merge(path, "/usr/bin/sh", "../bin/bash"));
89 ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
90
91 return EXIT_SUCCESS;
92
93 FAIL:
94 return EXIT_FAILURE;
95 }
96
97 static int test_path_dirname(const struct test* t) {
98 char path[PATH_MAX];
99
100 ASSERT_SUCCESS(pakfire_path_dirname(path, "/usr/bin/bash"));
101 ASSERT_STRING_EQUALS(path, "/usr/bin");
102
103 return EXIT_SUCCESS;
104
105 FAIL:
106 return EXIT_FAILURE;
107 }
108
109 int main(int argc, const char* argv[]) {
110 testsuite_add_test(test_path_normalize);
111 testsuite_add_test(test_path_append);
112 testsuite_add_test(test_path_merge);
113 testsuite_add_test(test_path_dirname);
114
115 return testsuite_run(argc, argv);
116 }