Debian Codegraph @

tests/client/torture_request_env.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2013 by Andreas Schneider <[email protected]>
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #include <libssh/libssh.h>
28
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <errno.h>
32
33 static int sshd_setup(void **state)
34 {
35 torture_setup_sshd_server(state, false);
36
37 return 0;
38 }
39
40 static int sshd_teardown(void **state) {
41 torture_teardown_sshd_server(state);
42
43 return 0;
44 }
45
46 static int session_setup(void **state)
47 {
48 struct torture_state *s = *state;
49 struct passwd *pwd;
50 int rc;
51
52 pwd = getpwnam("bob");
53 assert_non_null(pwd);
54
55 rc = setuid(pwd->pw_uid);
56 assert_return_code(rc, errno);
57
58 s->ssh.session = torture_ssh_session(s,
59 TORTURE_SSH_SERVER,
60 NULL,
61 TORTURE_SSH_USER_ALICE,
62 NULL);
63 assert_non_null(s->ssh.session);
64
65 return 0;
66 }
67
68 static int session_teardown(void **state)
69 {
70 struct torture_state *s = *state;
71
72 ssh_disconnect(s->ssh.session);
73 ssh_free(s->ssh.session);
74
75 return 0;
76 }
77
78 static void torture_request_env(void **state)
79 {
80 struct torture_state *s = *state;
81 ssh_session session = s->ssh.session;
82 ssh_channel c;
83 char buffer[4096] = {0};
84 int nbytes;
85 int rc;
86 int lang_found = 0;
87
88 c = ssh_channel_new(session);
89 assert_non_null(c);
90
91 rc = ssh_channel_open_session(c);
92 assert_ssh_return_code(session, rc);
93
94 rc = ssh_channel_request_env(c, "LC_LIBSSH", "LIBSSH_EXPORTED_VARIABLE");
95 assert_ssh_return_code(session, rc);
96
97 rc = ssh_channel_request_exec(c, "echo $LC_LIBSSH");
98 assert_ssh_return_code(session, rc);
99
100 nbytes = ssh_channel_read(c, buffer, sizeof(buffer) - 1, 0);
101 printf("nbytes=%d\n", nbytes);
102 while (nbytes > 0) {
103 #if 1
104 rc = fwrite(buffer, 1, nbytes, stdout);
105 assert_int_equal(rc, nbytes);
106 #endif
107 buffer[nbytes]='\0';
108 if (strstr(buffer, "LIBSSH_EXPORTED_VARIABLE")) {
109 lang_found = 1;
110 break;
111 }
112
113 nbytes = ssh_channel_read(c, buffer, sizeof(buffer), 0);
114 }
115 assert_int_equal(lang_found, 1);
116
117 ssh_channel_close(c);
118 }
119
120 int torture_run_tests(void) {
121 int rc;
122
123 struct CMUnitTest tests[] = {
124 cmocka_unit_test_setup_teardown(torture_request_env,
125 session_setup,
126 session_teardown),
127 };
128
129 ssh_init();
130
131 torture_filter_tests(tests);
132 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
133
134 ssh_finalize();
135 return rc;
136 }
137