aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree/test/add_by_path.c
blob: d59467c985ee89931dd303d302bcc6094833e4f1 (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
138
139
140
141
142
143
144
145
146
147
148
/* 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"

static sqfs_dir_entry_t *mkentry(const char *name, sqfs_u16 mode, sqfs_u32 uid,
			    sqfs_u32 gid)
{
	sqfs_dir_entry_t *ent = sqfs_dir_entry_create(name, mode, 0);

	TEST_NOT_NULL(ent);
	ent->uid = uid;
	ent->gid = gid;
	return ent;
}

int main(int argc, char **argv)
{
	fstree_defaults_t fsd;
	tree_node_t *a, *b;
	sqfs_dir_entry_t *ent;
	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);
	TEST_EQUAL_UI(fs.root->link_count, 2);

	ent = mkentry("dir", S_IFDIR | 0750, 1000, 100);
	a = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(a);
	TEST_STR_EQUAL(a->name, "dir");
	TEST_EQUAL_UI(a->mode, (S_IFDIR | 0750));
	TEST_EQUAL_UI(a->uid, 1000);
	TEST_EQUAL_UI(a->gid, 100);
	TEST_ASSERT(a->parent == fs.root);
	TEST_EQUAL_UI(a->link_count, 2);
	TEST_NULL(a->next);
	TEST_ASSERT(fs.root->data.children == a);
	TEST_EQUAL_UI(fs.root->link_count, 3);
	TEST_ASSERT(!(a->flags & FLAG_DIR_CREATED_IMPLICITLY));

	ent = mkentry("blkdev", S_IFBLK | 0640, 0, 0);
	ent->rdev = 1234;
	b = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(b);
	TEST_ASSERT(b != a);
	TEST_STR_EQUAL(b->name, "blkdev");
	TEST_EQUAL_UI(b->mode, (S_IFBLK | 0640));
	TEST_EQUAL_UI(b->uid, 0);
	TEST_EQUAL_UI(b->gid, 0);
	TEST_ASSERT(b->parent == fs.root);
	TEST_EQUAL_UI(b->link_count, 1);
	TEST_EQUAL_UI(b->data.devno, 1234);
	TEST_ASSERT(b->next == a);
	TEST_EQUAL_UI(fs.root->link_count, 4);
	TEST_ASSERT(fs.root->data.children == b);

	ent = mkentry("blkdev/foo", S_IFBLK | 0640, 0, 0);
	TEST_NULL(fstree_add_generic(&fs, ent, NULL));
	TEST_EQUAL_UI(errno, ENOTDIR);
	free(ent);

	ent = mkentry("dir", S_IFBLK | 0640, 0, 0);
	TEST_NULL(fstree_add_generic(&fs, ent, NULL));
	TEST_EQUAL_UI(errno, EEXIST);
	free(ent);

	ent = mkentry("dir", S_IFDIR | 0755, 0, 0);
	TEST_NULL(fstree_add_generic(&fs, ent, NULL));
	TEST_EQUAL_UI(errno, EEXIST);
	free(ent);

	ent = mkentry("dir/chrdev", S_IFCHR | 0444, 0, 0);
	ent->rdev = 5678;
	b = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(b);
	TEST_EQUAL_UI(b->mode, (S_IFCHR | 0444));
	TEST_EQUAL_UI(b->uid, 0);
	TEST_EQUAL_UI(b->gid, 0);
	TEST_EQUAL_UI(b->link_count, 1);
	TEST_ASSERT(b->parent == a);
	TEST_EQUAL_UI(b->data.devno, 5678);
	TEST_NULL(b->next);
	TEST_ASSERT(a->data.children == b);

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

	ent = mkentry("dir/foo/chrdev", S_IFCHR | 0444, 0, 0);
	ent->rdev = 91011;
	b = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(b);
	TEST_NULL(b->next);
	TEST_EQUAL_UI(b->mode, (S_IFCHR | 0444));
	TEST_EQUAL_UI(b->uid, 0);
	TEST_EQUAL_UI(b->gid, 0);
	TEST_EQUAL_UI(b->link_count, 1);
	TEST_ASSERT(b->parent != a);
	TEST_ASSERT(b->parent->parent == a);
	TEST_EQUAL_UI(b->data.devno, 91011);
	TEST_NULL(b->next);

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

	b = b->parent;
	TEST_ASSERT((b->flags & FLAG_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);

	ent = mkentry("dir/foo", S_IFDIR | 0755, 1000, 100);
	a = fstree_add_generic(&fs, ent, NULL);
	TEST_NOT_NULL(a);
	TEST_ASSERT(a == b);
	TEST_ASSERT(!(a->flags & FLAG_DIR_CREATED_IMPLICITLY));
	TEST_EQUAL_UI(a->mode, (S_IFDIR | 0755));
	TEST_EQUAL_UI(a->uid, 1000);
	TEST_EQUAL_UI(a->gid, 100);
	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, ent, NULL));
	TEST_EQUAL_UI(errno, EEXIST);
	free(ent);

	fstree_cleanup(&fs);
	return EXIT_SUCCESS;
}