File: srunner.c

package info (click to toggle)
netcfg 1.198
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,636 kB
  • sloc: ansic: 5,652; sh: 209; makefile: 83
file content (91 lines) | stat: -rw-r--r-- 2,251 bytes parent folder | download | duplicates (4)
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
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#include "test/srunner.h"

static char *test_run_root;
static char *original_path;

int main(int argc, char *argv[])
{
	int number_failed;
	SRunner *sr;
	char *buf, *p;
	(void)argc;

	/* Put the absolute directory in which the binary resides into
	 * test_run_root, so that srunner_mock_path can find it again.
	 */
	if (argv[0][0] == '/') {
		buf = argv[0];
	} else {
		char *cwd = getcwd(NULL, 0);
		buf = malloc(strlen(cwd) + 1 + strlen(argv[0]) + 1);
		strcpy(buf, cwd);
		strcat(buf, "/");
		strcat(buf, argv[0]);
		free(cwd);
	}
	test_run_root = realpath(buf, NULL);
	p = strrchr(test_run_root, '/');
	*p = '\0';

	sr = srunner_create(test_inet_mton_suite());
	/* Test suite list starts here */
	srunner_add_suite(sr, test_inet_ptom_suite());
	srunner_add_suite(sr, test_netcfg_parse_cidr_address_suite());
	srunner_add_suite(sr, test_netcfg_network_address_suite());
	srunner_add_suite(sr, test_netcfg_gateway_reachable_suite());
	srunner_add_suite(sr, test_nc_v6_interface_configured_suite());
	
	srunner_run_all (sr, CK_NORMAL);
	number_failed = srunner_ntests_failed (sr);
	srunner_free (sr);
	return (number_failed == 0) ? 0 : 1;
}

void srunner_mock_path(const char *testcase)
{
	char *new_path;
	char *testcasedir;
	unsigned int new_path_len;
	unsigned int l;
	
	original_path = strdup(getenv("PATH"));
	testcasedir = strdup(testcase);
	
	if (testcasedir && (l=strlen(testcasedir)) >= 3 \
	    && testcasedir[l-3]=='_' \
	    && testcasedir[l-2]=='f' \
	    && testcasedir[l-1]=='n')
			testcasedir[l-3]='\0'; /* prune "_fn" suffix */
	else {
		if (testcasedir)
			free(testcasedir);
		testcasedir = NULL;
	}
	new_path_len = strlen(test_run_root)
	               + 12 /* /mock_paths/ */
	               + strlen(testcasedir?testcasedir:testcase) + 1 /* : */
	               + strlen(original_path) + 1 /* \0 */;
	
	new_path = malloc(new_path_len);
	
	snprintf(new_path, new_path_len, "%s/mock_paths/%s:%s",
		 test_run_root, testcasedir?testcasedir:testcase,
		 original_path);
	
	setenv("PATH", new_path, 1);
	
	free(new_path);
	if (testcasedir)
		free(testcasedir);
}

void srunner_reset_path()
{
	setenv("PATH", original_path, 1);
	free(original_path);
}