File: test_config-file.c

package info (click to toggle)
torsocks 2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: ansic: 7,437; sh: 1,038; makefile: 105
file content (159 lines) | stat: -rw-r--r-- 4,849 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2013 - David Goulet <dgoulet@ev0ke.net>
 *                      Luke Gallagher <luke@hypergeometric.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2 only, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <limits.h>

#include <common/utils.h>
#include <common/defaults.h>
#include <common/config-file.h>

#include <tap/tap.h>
#include <fixtures.h>

#define NUM_TESTS 11

static void test_config_file_read_none(void)
{
	int ret = 0;
	struct configuration config;
	char buf[DEFAULT_DOMAIN_NAME_SIZE];

	diag("Config file read none");

	ret = config_file_read(NULL, &config);
	inet_ntop(AF_INET, &config.conf_file.onion_base, buf, sizeof(buf));
	ok(ret == 0 &&
		config.conf_file.tor_port == DEFAULT_TOR_PORT &&
		strcmp(config.conf_file.tor_address, DEFAULT_TOR_ADDRESS) == 0 &&
		strcmp(buf, DEFAULT_ONION_ADDR_RANGE) == 0 &&
		config.conf_file.onion_mask == strtoul(DEFAULT_ONION_ADDR_MASK, NULL, 0),
		"Use default when no config file");
}

static void test_config_file_read_valid(void)
{
	int ret = 0;
	struct configuration config;
	char buf[DEFAULT_DOMAIN_NAME_SIZE];

	diag("Config file read valid");

	ret = config_file_read(fixture("config0"), &config);
	inet_ntop(AF_INET, &config.conf_file.onion_base, buf, sizeof(buf));
	ok(ret == 0 &&
		config.conf_file.tor_port == DEFAULT_TOR_PORT &&
		strcmp(config.conf_file.tor_address, DEFAULT_TOR_ADDRESS) == 0 &&
		strcmp(buf, DEFAULT_ONION_ADDR_RANGE) == 0 &&
		config.conf_file.onion_mask == strtoul(DEFAULT_ONION_ADDR_MASK, NULL, 0),
		"Read valid config file");
}

static void test_config_file_read_empty(void)
{
	int ret = 0;
	struct configuration config;
	char buf[DEFAULT_DOMAIN_NAME_SIZE];

	diag("Config file read empty");

	ret = config_file_read(fixture("config1"), &config);
	inet_ntop(AF_INET, &config.conf_file.onion_base, buf, sizeof(buf));
	ok(ret == 0 &&
		config.conf_file.tor_port == 0 &&
		config.conf_file.tor_address == NULL &&
		strcmp(buf, "0.0.0.0") == 0 &&
		config.conf_file.onion_mask == 0,
		"Read empty config file");
}

static void test_config_file_read_invalid_values(void)
{
	int ret = 0;
	struct configuration config;

	diag("Config file read invalid values");

	ret = config_file_read(fixture("config2"), &config);
	ok(ret == -EINVAL &&
		config.conf_file.tor_port == 0,
		"TorPort 65536 returns -EINVAL");

	memset(&config, 0x0, sizeof(config));
	ret = config_file_read(fixture("config3"), &config);
	ok(ret == -EINVAL &&
		config.conf_file.tor_port == 0,
		"TorPort 0 returns -EINVAL");

	memset(&config, 0x0, sizeof(config));
	ret = config_file_read(fixture("config4"), &config);
	ok(ret == -EAFNOSUPPORT &&
		config.conf_file.tor_address == NULL,
		"TorAddress invalid IPv4 returns -EAFNOSUPPORT");

	memset(&config, 0x0, sizeof(config));
	ret = config_file_read(fixture("config5"), &config);
	ok(ret == -EAFNOSUPPORT &&
		config.conf_file.tor_address == NULL,
		"TorAddress invalid IPv6 returns -EAFNOSUPPORT");

	memset(&config, 0x0, sizeof(config));
	ret = config_file_read(fixture("config6"), &config);
	ok(ret == -EINVAL &&
		config.conf_file.onion_mask == 0,
		"OnionAdrRange invalid range returns -EINVAL");

	memset(&config, 0x0, sizeof(config));
	ret = config_file_read(fixture("config7"), &config);
	ok(ret == -EINVAL &&
		config.conf_file.onion_base == 0,
		"OnionAdrRange invalid IPv4 address returns -EINVAL");

	memset(&config, 0x0, sizeof(config));
	ret = config_file_read(fixture("config8"), &config);
	ok(ret == -EINVAL &&
		config.conf_file.onion_base == 0,
		"OnionAdrRange invalid IPv6 address returns -EINVAL");

	memset(&config, 0x0, sizeof(config));
#if (defined(__LP64__))
	ret = config_file_read(fixture("config9_64"), &config);
#else
	ret = config_file_read(fixture("config9_32"), &config);
#endif
	ok(ret == -EINVAL &&
		config.conf_file.onion_base == 0,
		"OnionAdrRange invalid mask returns -EINVAL");
}

int main(int argc, char **argv)
{
	/* Libtap call for the number of tests planned. */
	plan_tests(NUM_TESTS);

	test_config_file_read_none();
	skip_start(0 == TORSOCKS_FIXTURE_PATH, 10, "TORSOCKS_FIXTURE_PATH not defined");
	test_config_file_read_valid();
	test_config_file_read_empty();
	test_config_file_read_invalid_values();
	skip_end();

	return exit_status();
}