libamxp  1.4.0
Patterns C Implementation
test_dir.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 
57 #include <string.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <stdarg.h>
61 #include <stddef.h>
62 #include <setjmp.h>
63 #include <inttypes.h>
64 #include <limits.h>
65 #include <unistd.h>
66 #include <fcntl.h>
67 #include <cmocka.h>
68 
69 #include <amxc/amxc_macros.h>
70 #include <amxc/amxc.h>
71 #include <amxp/variant_siginfo.h>
72 #include <amxp/amxp_dir.h>
73 
74 #include "test_dir.h"
75 
76 static int test_add_to_list(const char* name, void* priv) {
77  amxc_string_t* f = NULL;
78  amxc_llist_t* l = (amxc_llist_t*) priv;
79 
80  amxc_string_new(&f, 0);
81  amxc_string_setf(f, "%s", name);
82  amxc_llist_append(l, &f->it);
83 
84  return 0;
85 }
86 
87 int test_dir_teardown(UNUSED void** state) {
88  rmdir("./subdir/nextlevel");
89  rmdir("./subdir/otherdir/next");
90  rmdir("./subdir/otherdir");
91  rmdir("./subdir/nextlevel");
92  rmdir("/tmp/subdir/nextlevel");
93  rmdir("./subdir");
94 
95  return 0;
96 }
97 
98 void test_dir_make(UNUSED void** state) {
99  assert_int_equal(amxp_dir_make("./subdir/nextlevel", 0777), 0);
100  assert_int_equal(amxp_dir_make("./subdir/otherdir", 0777), 0);
101  assert_int_equal(amxp_dir_make("./subdir/otherdir/next", 0777), 0);
102  assert_int_equal(amxp_dir_make("./subdir/nextlevel", 0777), 0);
103  assert_int_equal(amxp_dir_make("/tmp/subdir/nextlevel", 0777), 0);
104 
105  assert_int_not_equal(amxp_dir_make("", 0777), 0);
106  assert_int_not_equal(amxp_dir_make(NULL, 0777), 0);
107 }
108 
109 void test_dir_scan(UNUSED void** state) {
110  amxc_llist_t files;
111  amxc_llist_init(&files);
112 
113  assert_int_equal(amxp_dir_scan(".", "d_type == DT_REG", true, test_add_to_list, &files), 0);
114  assert_int_equal(amxc_llist_size(&files), 5);
115  amxc_llist_clean(&files, amxc_string_list_it_free);
116 
117  assert_int_equal(amxp_dir_scan(".", "d_type == DT_DIR", true, test_add_to_list, &files), 0);
118  assert_int_equal(amxc_llist_size(&files), 4);
119  amxc_llist_clean(&files, amxc_string_list_it_free);
120 
121  assert_int_equal(amxp_dir_scan(".", "d_type == DT_DIR", false, test_add_to_list, &files), 0);
122  assert_int_equal(amxc_llist_size(&files), 1);
123  amxc_llist_clean(&files, amxc_string_list_it_free);
124 
125  assert_int_equal(amxp_dir_scan(".", "d_type == DT_LNK", true, test_add_to_list, &files), 0);
126  assert_int_equal(amxc_llist_size(&files), 0);
127  amxc_llist_clean(&files, amxc_string_list_it_free);
128 
129  assert_int_equal(amxp_dir_scan(".", "d_type in [DT_BLK, DT_CHR, DT_FIFO, DT_SOCK, DT_UNKNOWN]", true, test_add_to_list, &files), 0);
130  assert_int_equal(amxc_llist_size(&files), 0);
131  amxc_llist_clean(&files, amxc_string_list_it_free);
132 
133  assert_int_equal(amxp_dir_scan(".", "d_type == DT_INVALID", true, test_add_to_list, &files), 0);
134  assert_int_equal(amxc_llist_size(&files), 0);
135  amxc_llist_clean(&files, amxc_string_list_it_free);
136 
137  assert_int_equal(amxp_dir_scan(".", "d_name matches '.*\\.c'", true, test_add_to_list, &files), 0);
138  assert_int_equal(amxc_llist_size(&files), 2);
139  amxc_llist_clean(&files, amxc_string_list_it_free);
140 
141  assert_int_not_equal(amxp_dir_scan("/notexisting", NULL, true, test_add_to_list, &files), 0);
142  assert_true(amxc_llist_is_empty(&files));
143 
144  assert_int_not_equal(amxp_dir_scan(NULL, NULL, true, test_add_to_list, &files), 0);
145  assert_true(amxc_llist_is_empty(&files));
146 
147  assert_int_not_equal(amxp_dir_scan("", NULL, true, test_add_to_list, &files), 0);
148  assert_true(amxc_llist_is_empty(&files));
149 
150  assert_int_not_equal(amxp_dir_scan("./test_dir.c", NULL, true, test_add_to_list, &files), 0);
151  assert_true(amxc_llist_is_empty(&files));
152 }
153 
154 void test_dir_is_empty(UNUSED void** state) {
155  assert_false(amxp_dir_is_empty("."));
156  assert_true(amxp_dir_is_empty(""));
157  assert_true(amxp_dir_is_empty(NULL));
158 
159  assert_false(amxp_dir_is_empty("./subdir"));
160  assert_true(amxp_dir_is_empty("./subdir/nextlevel"));
161 
162  assert_true(amxp_dir_is_empty("./test_dir.c"));
163 }
164 
165 void test_dir_is_a_directory(UNUSED void** state) {
166  assert_true(amxp_dir_is_directory("."));
167  assert_false(amxp_dir_is_directory(""));
168  assert_false(amxp_dir_is_directory(NULL));
169 
170  assert_true(amxp_dir_is_directory("./subdir"));
171  assert_true(amxp_dir_is_directory("./subdir/nextlevel"));
172 
173  assert_false(amxp_dir_is_directory("./test_dir.c"));
174 }
Ambiorix directory utilities.
#define UNUSED
Definition: main.c:68
int amxp_dir_make(const char *path, const mode_t mode)
Creates sub-directories.
Definition: amxp_dir.c:212
int amxp_dir_scan(const char *path, const char *filter, bool recursive, amxp_dir_match_fn_t fn, void *priv)
Scans a directory and calls a callback function for each matching entry found.
Definition: amxp_dir.c:216
bool amxp_dir_is_empty(const char *path)
Checks if a directory is empty.
Definition: amxp_dir.c:243
bool amxp_dir_is_directory(const char *path)
Checks if a path is a directory.
Definition: amxp_dir.c:257
static int test_add_to_list(const char *name, void *priv)
Definition: test_dir.c:76
void test_dir_is_a_directory(UNUSED void **state)
Definition: test_dir.c:165
int test_dir_teardown(UNUSED void **state)
Definition: test_dir.c:87
void test_dir_make(UNUSED void **state)
Definition: test_dir.c:98
void test_dir_is_empty(UNUSED void **state)
Definition: test_dir.c:154
void test_dir_scan(UNUSED void **state)
Definition: test_dir.c:109