File: test-path.c

package info (click to toggle)
ell 0.81-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,344 kB
  • sloc: ansic: 61,546; sh: 4,896; makefile: 574
file content (84 lines) | stat: -rw-r--r-- 1,687 bytes parent folder | download | duplicates (2)
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
/*
 * Embedded Linux library
 * Copyright (C) 2019  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>

#include <ell/ell.h>

static void test_path_next(const void *data)
{
	char *path;
	const char *path_str;

	path_str = l_path_next("Foo:bar", &path);
	assert(path_str);
	assert(!strcmp(path, "Foo"));
	l_free(path);

	path_str = l_path_next(path_str, &path);
	assert(path_str);
	assert(*path_str == '\0');
	assert(!strcmp(path, "bar"));
	l_free(path);

	path_str = l_path_next("\"foo\\:::bar\\", &path);
	assert(path_str);
	assert(!strcmp(path, "\"foo:"));
	l_free(path);

	path_str = l_path_next(path_str, &path);
	assert(path_str);
	assert(!strcmp(path, ""));
	l_free(path);

	path_str = l_path_next(path_str, &path);
	assert(path_str);
	assert(!strcmp(path, "bar"));
	l_free(path);
}

static void test_path_find(const void *data)
{
	static const char *cant_find = "/foo:/bar:/dir:fr";
	static const char *can_find = "/tmp";
	char *tmp_path = l_strdup("/tmp/foobarXXXXXX.tmp");
	char *base;
	char *path;
	int fd;

	fd = L_TFR(mkostemps(tmp_path, 4, O_CLOEXEC));
	assert(fd > 0);
	L_TFR(close(fd));
	base = basename(tmp_path);

	assert(l_path_find(base, cant_find, F_OK) == NULL);

	path = l_path_find(base, can_find, F_OK);
	assert(path);
	assert(!strcmp(path, tmp_path));
	l_free(path);
	l_free(tmp_path);
}

int main(int argc, char *argv[])
{
	l_test_init(&argc, &argv);

	l_test_add("path/next", test_path_next, NULL);
	l_test_add("path/find", test_path_find, NULL);

	return l_test_run();
}