aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree/test/add_by_path.c
blob: 76bae6d6bea57b96b975f26ad076b0af1040bcea (plain)
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
131
132
133
134
135
136
137
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * add_by_path.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "fstree.h"
#include "common.h"
#include "util/test.h"

int main(int argc, char **argv)
{
	fstree_defaults_t fsd;
	tree_node_t *a, *b;
	struct stat sb;
	fstree_t fs;
	char *opts;
	(void)argc; (void)argv;

	opts = strdup("mode=0755,uid=21,gid=42");
	TEST_ASSERT(parse_fstree_defaults(&fsd, opts) == 0);
	free(opts);
	TEST_ASSERT(fstree_init(&fs, &fsd) == 0);

	memset(&sb, 0, sizeof(sb));
	sb.st_mode = S_IFDIR | 0750;
	sb.st_uid = 1000;
	sb.st_gid = 100;

	TEST_EQUAL_UI(fs.root->link_count, 2);

	a = fstree_add_generic(&fs, "dir", &sb, NULL);
	TEST_NOT_NULL(a);
	TEST_STR_EQUAL(a->name, "dir");
	TEST_EQUAL_UI(a->mode, sb.st_mode);
	TEST_EQUAL_UI(a->uid, sb.st_uid);
	TEST_EQUAL_UI(a->gid, sb.st_gid);
	TEST_ASSERT(a->parent == fs.root);
	TEST_EQUAL_UI(a->link_count, 2);
	TEST_NULL(a->next);
	TEST_ASSERT(fs.root->data.dir.children == a);
	TEST_EQUAL_UI(fs.root->link_count, 3);
	TEST_ASSERT(!a->data.dir.created_implicitly);

	memset(&sb, 0, sizeof(sb));
	sb.st_mode = S_IFBLK | 0640;
	sb.st_rdev = 1234;

	b = fstree_add_generic(&fs, "blkdev", &sb, NULL);
	TEST_NOT_NULL(b);
	TEST_ASSERT(b != a);
	TEST_STR_EQUAL(b->name, "blkdev");
	TEST_EQUAL_UI(b->mode, sb.st_mode);
	TEST_EQUAL_UI(b->uid, sb.st_uid);
	TEST_EQUAL_UI(b->gid, sb.st_gid);
	TEST_ASSERT(b->parent == fs.root);
	TEST_EQUAL_UI(b->link_count, 1);
	TEST_EQUAL_UI(b->data.devno, sb.st_rdev);
	TEST_ASSERT(b->next == a);
	TEST_EQUAL_UI(fs.root->link_count, 4);
	TEST_ASSERT(fs.root->data.dir.children == b);

	TEST_NULL(fstree_add_generic(&fs, "blkdev/foo", &sb, NULL));
	TEST_EQUAL_UI(errno, ENOTDIR);

	TEST_NULL(fstree_add_generic(&fs, "dir", &sb, NULL));
	TEST_EQUAL_UI(errno, EEXIST);

	memset(&sb, 0, sizeof(sb));
	sb.st_mode = S_IFDIR | 0755;
	TEST_NULL(fstree_add_generic(&fs, "dir", &sb, NULL));
	TEST_EQUAL_UI(errno, EEXIST);

	memset(&sb, 0, sizeof(sb));
	sb.st_mode = S_IFCHR | 0444;
	b = fstree_add_generic(&fs, "dir/chrdev", &sb, NULL);
	TEST_NOT_NULL(b);
	TEST_EQUAL_UI(b->mode, sb.st_mode);
	TEST_EQUAL_UI(b->uid, sb.st_uid);
	TEST_EQUAL_UI(b->gid, sb.st_gid);
	TEST_EQUAL_UI(b->link_count, 1);
	TEST_ASSERT(b->parent == a);
	TEST_EQUAL_UI(b->data.devno, sb.st_rdev);
	TEST_NULL(b->next);
	TEST_ASSERT(a->data.dir.children == b);

	TEST_EQUAL_UI(a->link_count, 3);
	TEST_EQUAL_UI(fs.root->link_count, 4);

	b = fstree_add_generic(&fs, "dir/foo/chrdev", &sb, NULL);
	TEST_NOT_NULL(b);
	TEST_NULL(b->next);
	TEST_EQUAL_UI(b->mode, sb.st_mode);
	TEST_EQUAL_UI(b->uid, sb.st_uid);
	TEST_EQUAL_UI(b->gid, sb.st_gid);
	TEST_EQUAL_UI(b->link_count, 1);
	TEST_ASSERT(b->parent != a);
	TEST_ASSERT(b->parent->parent == a);
	TEST_EQUAL_UI(b->data.devno, sb.st_rdev);
	TEST_NULL(b->next);

	TEST_EQUAL_UI(a->link_count, 4);
	TEST_EQUAL_UI(fs.root->link_count, 4);
	TEST_ASSERT(a->data.dir.children != b);

	b = b->parent;
	TEST_ASSERT(b->data.dir.created_implicitly);
	TEST_EQUAL_UI(b->mode, S_IFDIR | 0755);
	TEST_EQUAL_UI(b->uid, 21);
	TEST_EQUAL_UI(b->gid, 42);
	TEST_EQUAL_UI(b->link_count, 3);

	memset(&sb, 0, sizeof(sb));
	sb.st_mode = S_IFDIR | 0750;
	sb.st_uid = 1000;
	sb.st_gid = 100;

	a = fstree_add_generic(&fs, "dir/foo", &sb, NULL);
	TEST_NOT_NULL(a);
	TEST_ASSERT(a == b);
	TEST_ASSERT(!a->data.dir.created_implicitly);
	TEST_EQUAL_UI(a->mode, sb.st_mode);
	TEST_EQUAL_UI(a->uid, sb.st_uid);
	TEST_EQUAL_UI(a->gid, sb.st_gid);
	TEST_EQUAL_UI(a->link_count, 3);

	TEST_EQUAL_UI(a->parent->link_count, 4);
	TEST_EQUAL_UI(fs.root->link_count, 4);

	TEST_NULL(fstree_add_generic(&fs, "dir/foo", &sb, NULL));
	TEST_EQUAL_UI(errno, EEXIST);

	fstree_cleanup(&fs);
	return EXIT_SUCCESS;
}