aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree/test/mknode_dir.c
blob: f85270605abd90cb8d48db46665e6db3efcc1bb5 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * mknode_dir.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

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

static sqfs_dir_entry_t *mkentry(const char *name)
{
	sqfs_dir_entry_t *ent = dir_entry_create(name);
	TEST_NOT_NULL(ent);
	ent->mode = S_IFDIR | 0654;
	ent->uid = 123;
	ent->gid = 456;
	ent->rdev = 789;
	ent->size = 4096;
	return ent;
}

int main(int argc, char **argv)
{
	fstree_defaults_t defaults;
	tree_node_t *root, *a, *b;
	sqfs_dir_entry_t *ent;
	fstree_t fs;
	int ret;
	(void)argc; (void)argv;

	memset(&defaults, 0, sizeof(defaults));
	ret = fstree_init(&fs, &defaults);
	TEST_EQUAL_I(ret, 0);

	ent = mkentry("rootdir");
	root = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(root);
	TEST_ASSERT(root->parent == fs.root);
	TEST_EQUAL_UI(root->uid, 123);
	TEST_EQUAL_UI(root->gid, 456);
	TEST_EQUAL_UI(root->mode, (S_IFDIR | 0654));
	TEST_EQUAL_UI(root->link_count, 2);
	TEST_ASSERT(root->name >= (char *)root->payload);
	TEST_STR_EQUAL(root->name, "rootdir");
	TEST_NULL(root->data.children);
	TEST_NULL(root->next);

	ent = mkentry("rootdir/adir");
	a = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(a);
	TEST_ASSERT(a->parent == root);
	TEST_NULL(a->next);
	TEST_EQUAL_UI(a->link_count, 2);
	TEST_EQUAL_UI(root->link_count, 3);
	TEST_ASSERT(root->data.children == a);
	TEST_ASSERT(root->parent == fs.root);
	TEST_NULL(root->next);

	ent = mkentry("rootdir/bdir");
	b = fstree_add_generic(&fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(b);
	TEST_ASSERT(a->parent == root);
	TEST_ASSERT(b->parent == root);
	TEST_EQUAL_UI(b->link_count, 2);
	TEST_ASSERT(root->data.children == a);
	TEST_ASSERT(a->next == b);
	TEST_EQUAL_UI(root->link_count, 4);
	TEST_NULL(b->next);
	TEST_ASSERT(root->parent == fs.root);
	TEST_NULL(root->next);

	fstree_cleanup(&fs);
	return EXIT_SUCCESS;
}