1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | local testing of the socket wrapper
|
---|
5 |
|
---|
6 | Copyright (C) Jelmer Vernooij 2007
|
---|
7 |
|
---|
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.
|
---|
12 |
|
---|
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.
|
---|
17 |
|
---|
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/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "system/network.h"
|
---|
24 | #include "../socket_wrapper/socket_wrapper.h"
|
---|
25 | #include "torture/torture.h"
|
---|
26 |
|
---|
27 | static char *old_dir = NULL;
|
---|
28 | static char *old_iface = NULL;
|
---|
29 |
|
---|
30 | static void backup_env(void)
|
---|
31 | {
|
---|
32 | old_dir = getenv("SOCKET_WRAPPER_DIR");
|
---|
33 | old_iface = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
|
---|
34 | }
|
---|
35 |
|
---|
36 | static void restore_env(void)
|
---|
37 | {
|
---|
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);
|
---|
46 | }
|
---|
47 |
|
---|
48 | static bool test_socket_wrapper_dir(struct torture_context *tctx)
|
---|
49 | {
|
---|
50 | backup_env();
|
---|
51 |
|
---|
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");
|
---|
58 |
|
---|
59 | restore_env();
|
---|
60 |
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 | static bool test_swrap_socket(struct torture_context *tctx)
|
---|
65 | {
|
---|
66 | backup_env();
|
---|
67 | setenv("SOCKET_WRAPPER_DIR", "foo", 1);
|
---|
68 |
|
---|
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");
|
---|
75 |
|
---|
76 | restore_env();
|
---|
77 |
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 |
|
---|
81 | unsigned int socket_wrapper_default_iface(void);
|
---|
82 | static bool test_socket_wrapper_default_iface(struct torture_context *tctx)
|
---|
83 | {
|
---|
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;
|
---|
93 | }
|
---|
94 |
|
---|
95 | struct torture_suite *torture_local_socket_wrapper(TALLOC_CTX *mem_ctx)
|
---|
96 | {
|
---|
97 | struct torture_suite *suite = torture_suite_create(mem_ctx,
|
---|
98 | "SOCKET-WRAPPER");
|
---|
99 |
|
---|
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);
|
---|
103 |
|
---|
104 | return suite;
|
---|
105 | }
|
---|