r21671: Add initial simple tests for socket wrapper
[Samba/bb.git] / source3 / lib / socket_wrapper / testsuite.c
bloba3e65804092fec6602a0cbf45ca597cdbb812554
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "system/network.h"
25 #include "lib/socket_wrapper/socket_wrapper.h"
26 #include "torture/torture.h"
28 static char *old_dir = NULL;
30 static void backup_env(void)
32 old_dir = getenv("SOCKET_WRAPPER_DIR");
35 static void restore_env(void)
37 setenv("SOCKET_WRAPPER_DIR", old_dir, 1);
40 static bool test_socket_wrapper_dir(struct torture_context *tctx)
42 backup_env();
44 setenv("SOCKET_WRAPPER_DIR", "foo", 1);
45 torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
46 setenv("SOCKET_WRAPPER_DIR", "./foo", 1);
47 torture_assert_str_equal(tctx, socket_wrapper_dir(), "foo", "setting failed");
48 unsetenv("SOCKET_WRAPPER_DIR");
49 torture_assert_str_equal(tctx, socket_wrapper_dir(), NULL, "resetting failed");
51 restore_env();
53 return true;
56 static bool test_swrap_socket(struct torture_context *tctx)
58 backup_env();
59 setenv("SOCKET_WRAPPER_DIR", "foo", 1);
61 torture_assert_int_equal(tctx, swrap_socket(1337, 1337, 0), -1, "unknown address family fails");
62 torture_assert_int_equal(tctx, errno, EAFNOSUPPORT, "correct errno set");
63 torture_assert_int_equal(tctx, swrap_socket(AF_INET, 1337, 0), -1, "unknown type fails");
64 torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
65 torture_assert_int_equal(tctx, swrap_socket(AF_INET, SOCK_DGRAM, 10), -1, "unknown protocol fails");
66 torture_assert_int_equal(tctx, errno, EPROTONOSUPPORT, "correct errno set");
68 restore_env();
70 return true;
73 struct torture_suite *torture_local_socket_wrapper(TALLOC_CTX *mem_ctx)
75 struct torture_suite *suite = torture_suite_create(mem_ctx,
76 "SOCKET-WRAPPER");
78 torture_suite_add_simple_test(suite, "socket_wrapper_dir", test_socket_wrapper_dir);
79 torture_suite_add_simple_test(suite, "socket", test_swrap_socket);
81 return suite;