s3-param Make lp_ncalrpc_dir() const
[Samba.git] / lib / socket_wrapper / testsuite.c
blob9274e7f4853043f99da141de34ef8cf468f06841
1 /*
2 Unix SMB/CIFS implementation.
4 local testing of the socket wrapper
6 Copyright (C) Jelmer Vernooij 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/network.h"
24 #include "../socket_wrapper/socket_wrapper.h"
25 #include "torture/torture.h"
27 static char *old_dir = NULL;
28 static char *old_iface = NULL;
30 static void backup_env(void)
32 old_dir = getenv("SOCKET_WRAPPER_DIR");
33 old_iface = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
36 static void restore_env(void)
38 if (old_dir == NULL)
39 unsetenv("SOCKET_WRAPPER_DIR");
40 else
41 setenv("SOCKET_WRAPPER_DIR", old_dir, 1);
42 if (old_iface == NULL)
43 unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
44 else
45 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", old_iface, 1);
48 static bool test_socket_wrapper_dir(struct torture_context *tctx)
50 backup_env();
52 setenv("SOCKET_WRAPPER_DIR", "foo", 1);
53 torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
54 setenv("SOCKET_WRAPPER_DIR", "./foo", 1);
55 torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
56 unsetenv("SOCKET_WRAPPER_DIR");
57 torture_assert_str_equal(tctx, socket_wrapper_dir(), NULL, "resetting failed");
59 restore_env();
61 return true;
64 static bool test_swrap_socket(struct torture_context *tctx)
66 backup_env();
67 setenv("SOCKET_WRAPPER_DIR", "foo", 1);
69 torture_assert_int_equal(tctx, swrap_socket(1337, 1337, 0), -1, "unknown address family fails");
70 torture_assert_int_equal(tctx, errno, EAFNOSUPPORT, "correct errno set");
71 torture_assert_int_equal(tctx, swrap_socket(AF_INET, 1337, 0), -1, "unknown type fails");
72 torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
73 torture_assert_int_equal(tctx, swrap_socket(AF_INET, SOCK_DGRAM, 10), -1, "unknown protocol fails");
74 torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
76 restore_env();
78 return true;
81 unsigned int socket_wrapper_default_iface(void);
82 static bool test_socket_wrapper_default_iface(struct torture_context *tctx)
84 backup_env();
85 unsetenv("SOCKET_WRAPPER_DEFAULT_IFACE");
86 torture_assert_int_equal(tctx, socket_wrapper_default_iface(), 1, "unset");
87 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "2", 1);
88 torture_assert_int_equal(tctx, socket_wrapper_default_iface(), 2, "unset");
89 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "bla", 1);
90 torture_assert_int_equal(tctx, socket_wrapper_default_iface(), 1, "unset");
91 restore_env();
92 return true;
95 struct torture_suite *torture_local_socket_wrapper(TALLOC_CTX *mem_ctx)
97 struct torture_suite *suite = torture_suite_create(mem_ctx,
98 "socket-wrapper");
100 torture_suite_add_simple_test(suite, "socket_wrapper_dir", test_socket_wrapper_dir);
101 torture_suite_add_simple_test(suite, "socket", test_swrap_socket);
102 torture_suite_add_simple_test(suite, "socket_wrapper_default_iface", test_socket_wrapper_default_iface);
104 return suite;