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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
20140423
Jan Mojzis
Public domain.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include "crypto_uint32.h"
#include "fail.h"
#include "run.h"
#include "byte.h"
#include "channel.h"
const char *user = "user";
const char *termname = "xterm";
crypto_uint32 id = 0;
crypto_uint32 remotewindow = 100;
crypto_uint32 maxpacket = 100;
crypto_uint32 localwindow;
unsigned char ch = 'x';
static char *name(void) {
struct passwd *pw;
pw = getpwuid(geteuid());
return pw->pw_name;
}
/* channel can't be opened 2x */
static void testopen1(void) {
int r;
r = channel_open(user, id, remotewindow, maxpacket, &localwindow);
if (r != 1) fail("first channel_open not returns 1");
r = channel_open(user, id, remotewindow, maxpacket, &localwindow);
if (r != 0) fail("first channel_open not returns 0");
_exit(0);
}
/* *localwindow can't be 0 */
static void testopen2(void) {
channel_open(user, id, remotewindow, maxpacket, 0);
}
/* maxpacket can't be 0 */
static void testopen3(void) {
channel_open(user, id, remotewindow, 0, &localwindow);
}
/* remotewindow can't be 0 */
static void testopen4(void) {
channel_open(user, id, 0, maxpacket, &localwindow);
}
/* channel_openterminal() can't be called before channel_open() */
static void testtermopen1(void) {
channel_openterminal(termname, 0, 0, 0, 0);
}
/* channel_env() can't be called before channel_open() */
static void testenv1(void) {
channel_env("a", "b");
}
/* channel_env() can't be called after channel_exec() */
static void testenv2(void) {
channel_open(name(), id, remotewindow, maxpacket, &localwindow);
channel_exec("exit 0");
channel_env("a", "b");
}
/* channel_exec() can't be called before channel_open() */
static void testexec1(void) {
channel_exec("true");
}
/* channel_put() can't be called before channel_exec() */
static void testput1(void) {
channel_open(user, id, remotewindow, maxpacket, &localwindow);
channel_put(&ch, 1);
}
/* channel_puteof() can't be called before channel_exec() */
static void testputeof1(void) {
channel_open(user, id, remotewindow, maxpacket, &localwindow);
channel_puteof();
}
/* channel_read() can't be called before channel_exec() */
static void testread1(void) {
channel_open(user, id, remotewindow, maxpacket, &localwindow);
channel_read(&ch, 1);
}
/* channel_extendedread() can't be called before channel_exec() */
static void testextendedread1(void) {
channel_open(user, id, remotewindow, maxpacket, &localwindow);
channel_extendedread(&ch, 1);
}
/* channel_write() can't be called before channel_exec() */
static void testwrite1(void) {
channel_open(user, id, remotewindow, maxpacket, &localwindow);
channel_write();
}
/* OK - tests */
static void testok1(void) {
int r, s, e;
channel_open(name(), id, remotewindow, maxpacket, &localwindow);
channel_exec("exit 0");
do {
r = channel_waitnohang(&s, &e);
} while (r == 0);
if (s != 0) fail("process killed");
if (e != 0) fail("bad status");
_exit(0);
}
static void testok2(void) {
int r, s, e;
channel_open(name(), id, remotewindow, maxpacket, &localwindow);
channel_exec("exit 1");
do {
r = channel_waitnohang(&s, &e);
} while (r == 0);
if (s != 0) fail("process killed");
if (e != 1) fail("bad status");
_exit(0);
}
static void testok3(void) {
int r, s, e;
channel_open(name(), id, remotewindow, maxpacket, &localwindow);
channel_exec("kill -9 $$");
do {
r = channel_waitnohang(&s, &e);
} while (r == 0);
if (s != 9) fail("bad exit signal");
_exit(0);
}
static void testok4(void) {
int r, s, e;
unsigned char buf[10];
channel_open(name(), id, remotewindow, maxpacket, &localwindow);
channel_exec("cat");
ch = 'a';
channel_put(&ch, 1);
do {
r = channel_write();
} while (r == 0);
do {
r = channel_read(buf, sizeof buf);
} while (r == 0);
if (r != 1) fail("channel_write()/channel_read() failure");
if (buf[0] != 'a') fail("channel_write()/channel_read() failure");
channel_puteof();
do {
r = channel_waitnohang(&s, &e);
} while (r == 0);
if (s != 0) fail("process killed");
if (e != 0) fail("bad status");
_exit(0);
}
#define warn_(a, b, x) do { fprintf(stderr, "%s:%llu: warning: %s\n", (a), (unsigned long long)(b), (x)); fflush(stderr); _exit(0); } while (0);
#define warn(x) warn_(__FILE__, __LINE__, (x))
static int homedir(void) {
struct passwd *pw;
struct stat st;
pw = getpwuid(geteuid());
if (!pw) return 0;
return 1 + stat(pw->pw_dir, &st);
}
static int shell(void) {
pid_t pid;
int status;
pid = fork();
if (pid == -1) return 0;
if (pid == 0) {
struct passwd *pw;
char *run[4];
pw = getpwuid(geteuid());
if (!pw) _exit(1);
run[0] = pw->pw_shell;
run[1] = (char *)"-c";
run[2] = (char *)"exit 0";
run[3] = (char *)0;
execvp(run[0], run);
_exit(1);
}
while (waitpid(pid, &status, 0) != pid) {};
if (!WIFEXITED(status)) return 0;
if (WEXITSTATUS(status)) return 0;
return 1;
}
int main(void) {
/* don't run check when homedir not exist */
if (!homedir()) {
warn("homedir not exist - skipping tests")
_exit(0);
}
/* don't run check when shell returns non-zero */
if (!shell()) {
warn("shell returns non-zero - skipping tests")
_exit(0);
}
run_mustpass(testopen1);
run_mustfail(testopen2);
run_mustfail(testopen3);
run_mustfail(testopen4);
run_mustfail(testtermopen1);
run_mustfail(testenv1);
run_mustfail(testenv2);
run_mustfail(testexec1);
run_mustfail(testput1);
run_mustfail(testputeof1);
run_mustfail(testread1);
run_mustfail(testextendedread1);
run_mustfail(testwrite1);
/* temp. disable the rest */
_exit(0);
run_mustpass(testok1);
run_mustpass(testok2);
run_mustpass(testok3);
run_mustpass(testok4);
_exit(0);
}
|