summaryrefslogtreecommitdiffstats
path: root/lib/socket_wrapper/testsuite.c
blob: e6e08e3be685327f7b4c4fee83d4ecffa635372a (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
/* 
   Unix SMB/CIFS implementation.

   local testing of the socket wrapper

   Copyright (C) Jelmer Vernooij 2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/network.h"
#include "../socket_wrapper/socket_wrapper.h"
#include "torture/torture.h"

static char *old_dir = NULL;
static char *old_iface = NULL;

static void backup_env(void)
{
	old_dir = getenv("SOCKET_WRAPPER_DIR");
	old_iface = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
}

static void restore_env(void)
{
	if (old_dir == NULL)
		unsetenv("SOCKET_WRAPPER_DIR");
	else
		setenv("SOCKET_WRAPPER_DIR", old_dir, 1);
	if (old_iface == NULL)
		unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
	else
		setenv("SOCKET_WRAPPER_DEFAULT_IFACE", old_iface, 1);
}

static bool test_socket_wrapper_dir(struct torture_context *tctx)
{
	backup_env();

	setenv("SOCKET_WRAPPER_DIR", "foo", 1);
	torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
	setenv("SOCKET_WRAPPER_DIR", "./foo", 1);
	torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
	unsetenv("SOCKET_WRAPPER_DIR");
	torture_assert_str_equal(tctx, socket_wrapper_dir(), NULL, "resetting failed");

	restore_env();

	return true;
}

static bool test_swrap_socket(struct torture_context *tctx)
{
	backup_env();
	setenv("SOCKET_WRAPPER_DIR", "foo", 1);

	torture_assert_int_equal(tctx, swrap_socket(1337, 1337, 0), -1, "unknown address family fails");
	torture_assert_int_equal(tctx, errno, EAFNOSUPPORT, "correct errno set");
	torture_assert_int_equal(tctx, swrap_socket(AF_INET, 1337, 0), -1, "unknown type fails");
	torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
	torture_assert_int_equal(tctx, swrap_socket(AF_INET, SOCK_DGRAM, 10), -1, "unknown protocol fails");
	torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");

	restore_env();

	return true;
}

unsigned int socket_wrapper_default_iface(void);
static bool test_socket_wrapper_default_iface(struct torture_context *tctx)
{
	backup_env();
	unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
	torture_assert_int_equal(tctx, socket_wrapper_default_iface(), 1, "unset");
	setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "2", 1);
	torture_assert_int_equal(tctx, socket_wrapper_default_iface(), 2, "unset");
	setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "bla", 1);
	torture_assert_int_equal(tctx, socket_wrapper_default_iface(), 1, "unset");
	restore_env();
	return true;
}

struct torture_suite *torture_local_socket_wrapper(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, 
													   "SOCKET-WRAPPER");

	torture_suite_add_simple_test(suite, "socket_wrapper_dir", test_socket_wrapper_dir);
	torture_suite_add_simple_test(suite, "socket", test_swrap_socket);
	torture_suite_add_simple_test(suite, "socket_wrapper_default_iface", test_socket_wrapper_default_iface);

	return suite;
}