| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#ifdef HAVE_UNISTD_H |
| 4 |
#include <unistd.h> |
| 5 |
#endif |
| 6 |
#include <sys/types.h> |
| 7 |
#include <fcntl.h> |
| 8 |
|
| 9 |
#ifndef _WIN32 |
| 10 |
#define _POSIX_PTHREAD_SEMANTICS |
| 11 |
#include <pwd.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#define LIBSSH_STATIC |
| 15 |
#include <libssh/priv.h> |
| 16 |
|
| 17 |
#include "misc.c" |
| 18 |
#include "torture.h" |
| 19 |
#include "error.c" |
| 20 |
|
| 21 |
#ifdef _WIN32 |
| 22 |
#include <netioapi.h> |
| 23 |
#else |
| 24 |
#include <net/if.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#define TORTURE_TEST_DIR "/usr/local/bin/truc/much/.." |
| 28 |
#define TORTURE_IPV6_LOCAL_LINK "fe80::98e1:82ff:fe8d:28b3%%%s" |
| 29 |
|
| 30 |
const char template[] = "temp_dir_XXXXXX"; |
| 31 |
|
| 32 |
static int setup(void **state) |
| 33 |
{ |
| 34 |
ssh_session session = ssh_new(); |
| 35 |
*state = session; |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static int teardown(void **state) |
| 41 |
{ |
| 42 |
ssh_free(*state); |
| 43 |
|
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
static void torture_get_user_home_dir(void **state) { |
| 48 |
#ifndef _WIN32 |
| 49 |
struct passwd *pwd = getpwuid(getuid()); |
| 50 |
#endif /* _WIN32 */ |
| 51 |
char *user; |
| 52 |
|
| 53 |
(void) state; |
| 54 |
|
| 55 |
user = ssh_get_user_home_dir(NULL); |
| 56 |
assert_non_null(user); |
| 57 |
#ifndef _WIN32 |
| 58 |
assert_string_equal(user, pwd->pw_dir); |
| 59 |
#endif /* _WIN32 */ |
| 60 |
|
| 61 |
SAFE_FREE(user); |
| 62 |
} |
| 63 |
|
| 64 |
static void torture_basename(void **state) { |
| 65 |
char *path; |
| 66 |
|
| 67 |
(void) state; |
| 68 |
|
| 69 |
path=ssh_basename(TORTURE_TEST_DIR "/test"); |
| 70 |
assert_non_null(path); |
| 71 |
assert_string_equal(path, "test"); |
| 72 |
SAFE_FREE(path); |
| 73 |
path=ssh_basename(TORTURE_TEST_DIR "/test/"); |
| 74 |
assert_non_null(path); |
| 75 |
assert_string_equal(path, "test"); |
| 76 |
SAFE_FREE(path); |
| 77 |
} |
| 78 |
|
| 79 |
static void torture_dirname(void **state) { |
| 80 |
char *path; |
| 81 |
|
| 82 |
(void) state; |
| 83 |
|
| 84 |
path=ssh_dirname(TORTURE_TEST_DIR "/test"); |
| 85 |
assert_non_null(path); |
| 86 |
assert_string_equal(path, TORTURE_TEST_DIR ); |
| 87 |
SAFE_FREE(path); |
| 88 |
path=ssh_dirname(TORTURE_TEST_DIR "/test/"); |
| 89 |
assert_non_null(path); |
| 90 |
assert_string_equal(path, TORTURE_TEST_DIR); |
| 91 |
SAFE_FREE(path); |
| 92 |
} |
| 93 |
|
| 94 |
static void torture_ntohll(void **state) { |
| 95 |
uint64_t value = 0x0123456789abcdef; |
| 96 |
uint32_t sample = 1; |
| 97 |
unsigned char *ptr = (unsigned char *) &sample; |
| 98 |
uint64_t check; |
| 99 |
|
| 100 |
(void) state; |
| 101 |
|
| 102 |
if (ptr[0] == 1){ |
| 103 |
/* we're in little endian */ |
| 104 |
check = 0xefcdab8967452301; |
| 105 |
} else { |
| 106 |
/* big endian */ |
| 107 |
check = value; |
| 108 |
} |
| 109 |
value = ntohll(value); |
| 110 |
assert_true(value == check); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @brief Compare fields of two (struct tm) type structures. |
| 115 |
* |
| 116 |
* @param[in] a Pointer to the first structure to compare |
| 117 |
* |
| 118 |
* @param[in] b Pointer to the second structure to compare |
| 119 |
* |
| 120 |
* @returns -1 on error |
| 121 |
* @returns 0 if the fields of the structures are the same |
| 122 |
* @returns 1 if the fields of the structures are not the same |
| 123 |
*/ |
| 124 |
static int tm_cmp(const struct tm *a, const struct tm *b) |
| 125 |
{ |
| 126 |
if (a == NULL || b == NULL) { |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
return !(a->tm_sec == b->tm_sec && |
| 131 |
a->tm_min == b->tm_min && |
| 132 |
a->tm_hour == b->tm_hour && |
| 133 |
a->tm_mday == b->tm_mday && |
| 134 |
a->tm_mon == b->tm_mon && |
| 135 |
a->tm_year == b->tm_year && |
| 136 |
a->tm_wday == b->tm_wday && |
| 137 |
a->tm_yday == b->tm_yday && |
| 138 |
a->tm_isdst == b->tm_isdst); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @brief Validate that localtime_r() works properly. |
| 143 |
* |
| 144 |
* This test is mainly to check that the libssh implementation of |
| 145 |
* localtime_r() on Windows works properly (Windows does not provide |
| 146 |
* localtime_r()) |
| 147 |
*/ |
| 148 |
static void torture_localtime_r(UNUSED_PARAM(void **state)) |
| 149 |
{ |
| 150 |
/* |
| 151 |
* The tm_wday and tm_yday fields of tm1 and tm2 would be filled |
| 152 |
* appropriately due to the mktime() call further in the test. |
| 153 |
*/ |
| 154 |
|
| 155 |
/* Linux release date: 17/09/1991 (random time: 02:01:00) */ |
| 156 |
struct tm tm1 = {.tm_sec = 0, |
| 157 |
.tm_min = 1, |
| 158 |
.tm_hour = 2, |
| 159 |
.tm_mday = 17, |
| 160 |
.tm_mon = 9 - 1, |
| 161 |
.tm_year = 1991 - 1900, |
| 162 |
.tm_isdst = 0}; |
| 163 |
|
| 164 |
/* Windows release date: 20/11/1985 (random time 05:04:03) */ |
| 165 |
struct tm tm2 = {.tm_sec = 3, |
| 166 |
.tm_min = 4, |
| 167 |
.tm_hour = 5, |
| 168 |
.tm_mday = 20, |
| 169 |
.tm_mon = 11 - 1, |
| 170 |
.tm_year = 1985 - 1900, |
| 171 |
.tm_isdst = 0}; |
| 172 |
|
| 173 |
time_t t1, t2; |
| 174 |
struct tm *static_tm_ptr = NULL, *tm_ptr = NULL; |
| 175 |
struct tm our_tm = {0}; |
| 176 |
int cmp; |
| 177 |
|
| 178 |
/* |
| 179 |
* Convert time represented as (struct tm) to time represented as |
| 180 |
* a (time_t) |
| 181 |
*/ |
| 182 |
t1 = mktime(&tm1); |
| 183 |
assert_int_not_equal(t1, (time_t)-1); |
| 184 |
|
| 185 |
t2 = mktime(&tm2); |
| 186 |
assert_int_not_equal(t2, (time_t)-1); |
| 187 |
|
| 188 |
/* Test that localtime_r() gives the correct broken down time */ |
| 189 |
tm_ptr = localtime_r(&t1, &our_tm); |
| 190 |
assert_ptr_equal(tm_ptr, &our_tm); |
| 191 |
|
| 192 |
cmp = tm_cmp(&our_tm, &tm1); |
| 193 |
assert_int_equal(cmp, 0); |
| 194 |
|
| 195 |
/* |
| 196 |
* Test that localtime_r() does not modify the static structure used by |
| 197 |
* localtime(). (This is an attempt to test that the localtime_r() |
| 198 |
* implementation does not use localtime() internally) |
| 199 |
* |
| 200 |
* To test this, we first use localtime() on some time, then use |
| 201 |
* localtime_r() on another time and then validate that the time |
| 202 |
* corresponding to the pointer (to the static structure) returned by |
| 203 |
* the first localtime() call does not change. |
| 204 |
*/ |
| 205 |
static_tm_ptr = localtime(&t1); |
| 206 |
assert_non_null(static_tm_ptr); |
| 207 |
|
| 208 |
cmp = tm_cmp(static_tm_ptr, &tm1); |
| 209 |
assert_int_equal(cmp, 0); |
| 210 |
|
| 211 |
tm_ptr = localtime_r(&t2, &our_tm); |
| 212 |
assert_ptr_equal(tm_ptr, &our_tm); |
| 213 |
|
| 214 |
cmp = tm_cmp(static_tm_ptr, &tm1); |
| 215 |
assert_int_equal(cmp, 0); |
| 216 |
|
| 217 |
/* |
| 218 |
* Ideally, it should be checked that the localtime_r() implementation |
| 219 |
* is thread safe by testing it under multiple threads, but we are not |
| 220 |
* checking that as of now. This is because we trust localtime_r() provided |
| 221 |
* by POSIX systems to be thread safe and the libssh implementation of |
| 222 |
* localtime_r() on Windows should be a simple wrapper around Windows's |
| 223 |
* localtime_s() which should also be thread safe. |
| 224 |
*/ |
| 225 |
} |
| 226 |
|
| 227 |
#ifdef _WIN32 |
| 228 |
|
| 229 |
static void torture_path_expand_tilde_win(void **state) { |
| 230 |
char *d; |
| 231 |
|
| 232 |
(void) state; |
| 233 |
|
| 234 |
d = ssh_path_expand_tilde("~\\.ssh"); |
| 235 |
assert_non_null(d); |
| 236 |
print_message("Expanded path: %s\n", d); |
| 237 |
free(d); |
| 238 |
|
| 239 |
d = ssh_path_expand_tilde("/guru/meditation"); |
| 240 |
assert_string_equal(d, "/guru/meditation"); |
| 241 |
free(d); |
| 242 |
} |
| 243 |
|
| 244 |
#else /* _WIN32 */ |
| 245 |
|
| 246 |
static void torture_path_expand_tilde_unix(void **state) { |
| 247 |
char h[256] = {0}; |
| 248 |
char *d = NULL; |
| 249 |
char *user = NULL; |
| 250 |
char *home = NULL; |
| 251 |
struct passwd *pw = NULL; |
| 252 |
|
| 253 |
(void) state; |
| 254 |
|
| 255 |
pw = getpwuid(getuid()); |
| 256 |
assert_non_null(pw); |
| 257 |
|
| 258 |
user = pw->pw_name; |
| 259 |
assert_non_null(user); |
| 260 |
home = pw->pw_dir; |
| 261 |
assert_non_null(home); |
| 262 |
|
| 263 |
snprintf(h, 256 - 1, "%s/.ssh", home); |
| 264 |
|
| 265 |
d = ssh_path_expand_tilde("~/.ssh"); |
| 266 |
assert_non_null(d); |
| 267 |
assert_string_equal(d, h); |
| 268 |
free(d); |
| 269 |
|
| 270 |
d = ssh_path_expand_tilde("/guru/meditation"); |
| 271 |
assert_non_null(d); |
| 272 |
assert_string_equal(d, "/guru/meditation"); |
| 273 |
free(d); |
| 274 |
|
| 275 |
snprintf(h, 256 - 1, "~%s/.ssh", user); |
| 276 |
d = ssh_path_expand_tilde(h); |
| 277 |
assert_non_null(d); |
| 278 |
|
| 279 |
snprintf(h, 256 - 1, "%s/.ssh", home); |
| 280 |
assert_string_equal(d, h); |
| 281 |
free(d); |
| 282 |
} |
| 283 |
|
| 284 |
#endif /* _WIN32 */ |
| 285 |
|
| 286 |
static void torture_path_expand_escape(void **state) { |
| 287 |
ssh_session session = *state; |
| 288 |
const char *s = "%d/%h/%p/by/%r"; |
| 289 |
char *e; |
| 290 |
|
| 291 |
/* Set the homedir here to prevent querying the NSS DB */ |
| 292 |
session->opts.homedir = strdup("guru"); |
| 293 |
session->opts.host = strdup("meditation"); |
| 294 |
session->opts.port = 0; |
| 295 |
session->opts.username = strdup("root"); |
| 296 |
|
| 297 |
e = ssh_path_expand_escape(session, s); |
| 298 |
assert_non_null(e); |
| 299 |
assert_string_equal(e, "guru/meditation/22/by/root"); |
| 300 |
ssh_string_free_char(e); |
| 301 |
|
| 302 |
session->opts.port = 222; |
| 303 |
|
| 304 |
e = ssh_path_expand_escape(session, s); |
| 305 |
assert_non_null(e); |
| 306 |
assert_string_equal(e, "guru/meditation/222/by/root"); |
| 307 |
ssh_string_free_char(e); |
| 308 |
} |
| 309 |
|
| 310 |
static void torture_path_expand_known_hosts(void **state) { |
| 311 |
ssh_session session = *state; |
| 312 |
char *tmp; |
| 313 |
|
| 314 |
/* Set the homedir here to prevent querying the NSS DB */ |
| 315 |
session->opts.homedir = strdup("/home/guru"); |
| 316 |
|
| 317 |
tmp = ssh_path_expand_escape(session, "%d/.ssh/known_hosts"); |
| 318 |
assert_non_null(tmp); |
| 319 |
assert_string_equal(tmp, "/home/guru/.ssh/known_hosts"); |
| 320 |
free(tmp); |
| 321 |
} |
| 322 |
|
| 323 |
static void torture_path_expand_percent(void **state) { |
| 324 |
ssh_session session = *state; |
| 325 |
char *tmp; |
| 326 |
|
| 327 |
/* Set the homedir here to prevent querying the NSS DB */ |
| 328 |
session->opts.homedir = strdup("/home/guru"); |
| 329 |
|
| 330 |
tmp = ssh_path_expand_escape(session, "%d/.ssh/config%%1"); |
| 331 |
assert_non_null(tmp); |
| 332 |
assert_string_equal(tmp, "/home/guru/.ssh/config%1"); |
| 333 |
free(tmp); |
| 334 |
} |
| 335 |
|
| 336 |
static void torture_timeout_elapsed(void **state){ |
| 337 |
struct ssh_timestamp ts; |
| 338 |
(void) state; |
| 339 |
ssh_timestamp_init(&ts); |
| 340 |
usleep(30000); |
| 341 |
|
| 342 |
assert_true(ssh_timeout_elapsed(&ts,25)); |
| 343 |
assert_false(ssh_timeout_elapsed(&ts,30000)); |
| 344 |
assert_false(ssh_timeout_elapsed(&ts,300)); |
| 345 |
assert_true(ssh_timeout_elapsed(&ts,0)); |
| 346 |
assert_false(ssh_timeout_elapsed(&ts,-1)); |
| 347 |
} |
| 348 |
|
| 349 |
static void torture_timeout_update(void **state){ |
| 350 |
struct ssh_timestamp ts; |
| 351 |
(void) state; |
| 352 |
ssh_timestamp_init(&ts); |
| 353 |
usleep(50000); |
| 354 |
assert_int_equal(ssh_timeout_update(&ts,25), 0); |
| 355 |
assert_in_range(ssh_timeout_update(&ts,30000),29000,29960); |
| 356 |
assert_in_range(ssh_timeout_update(&ts,500),1,460); |
| 357 |
assert_int_equal(ssh_timeout_update(&ts,0),0); |
| 358 |
assert_int_equal(ssh_timeout_update(&ts,-1),-1); |
| 359 |
} |
| 360 |
|
| 361 |
static void torture_ssh_analyze_banner(void **state) { |
| 362 |
int rc = 0; |
| 363 |
ssh_session session = NULL; |
| 364 |
(void) state; |
| 365 |
|
| 366 |
#define reset_banner_test() \ |
| 367 |
do { \ |
| 368 |
rc = 0; \ |
| 369 |
ssh_free(session); \ |
| 370 |
session = ssh_new(); \ |
| 371 |
assert_non_null(session); \ |
| 372 |
} while (0) |
| 373 |
|
| 374 |
#define assert_banner_rejected(is_server) \ |
| 375 |
do { \ |
| 376 |
rc = ssh_analyze_banner(session, is_server); \ |
| 377 |
assert_int_not_equal(0, rc); \ |
| 378 |
} while (0); |
| 379 |
|
| 380 |
#define assert_client_banner_rejected(banner) \ |
| 381 |
do { \ |
| 382 |
reset_banner_test(); \ |
| 383 |
session->clientbanner = strdup(banner); \ |
| 384 |
assert_non_null(session->clientbanner); \ |
| 385 |
assert_banner_rejected(1 /*server*/); \ |
| 386 |
SAFE_FREE(session->clientbanner); \ |
| 387 |
} while (0) |
| 388 |
|
| 389 |
#define assert_server_banner_rejected(banner) \ |
| 390 |
do { \ |
| 391 |
reset_banner_test(); \ |
| 392 |
session->serverbanner = strdup(banner); \ |
| 393 |
assert_non_null(session->serverbanner); \ |
| 394 |
assert_banner_rejected(0 /*client*/); \ |
| 395 |
SAFE_FREE(session->serverbanner); \ |
| 396 |
} while (0) |
| 397 |
|
| 398 |
#define assert_banner_accepted(is_server) \ |
| 399 |
do { \ |
| 400 |
rc = ssh_analyze_banner(session, is_server); \ |
| 401 |
assert_int_equal(0, rc); \ |
| 402 |
} while (0) |
| 403 |
|
| 404 |
#define assert_client_banner_accepted(banner) \ |
| 405 |
do { \ |
| 406 |
reset_banner_test(); \ |
| 407 |
session->clientbanner = strdup(banner); \ |
| 408 |
assert_non_null(session->clientbanner); \ |
| 409 |
assert_banner_accepted(1 /*server*/); \ |
| 410 |
SAFE_FREE(session->clientbanner); \ |
| 411 |
} while (0) |
| 412 |
|
| 413 |
#define assert_server_banner_accepted(banner) \ |
| 414 |
do { \ |
| 415 |
reset_banner_test(); \ |
| 416 |
session->serverbanner = strdup(banner); \ |
| 417 |
assert_non_null(session->serverbanner); \ |
| 418 |
assert_banner_accepted(0 /*client*/); \ |
| 419 |
SAFE_FREE(session->serverbanner); \ |
| 420 |
} while (0) |
| 421 |
|
| 422 |
/* no banner is set */ |
| 423 |
reset_banner_test(); |
| 424 |
assert_banner_rejected(0 /*client*/); |
| 425 |
reset_banner_test(); |
| 426 |
assert_banner_rejected(1 /*server*/); |
| 427 |
|
| 428 |
/* banner is too short */ |
| 429 |
assert_client_banner_rejected("abc"); |
| 430 |
assert_server_banner_rejected("abc"); |
| 431 |
|
| 432 |
/* banner doesn't start "SSH-" */ |
| 433 |
assert_client_banner_rejected("abc-2.0"); |
| 434 |
assert_server_banner_rejected("abc-2.0"); |
| 435 |
|
| 436 |
/* SSH v1 */ |
| 437 |
assert_client_banner_rejected("SSH-1.0"); |
| 438 |
assert_server_banner_rejected("SSH-1.0"); |
| 439 |
|
| 440 |
/* SSH v1.9 gets counted as both v1 and v2 */ |
| 441 |
assert_client_banner_accepted("SSH-1.9"); |
| 442 |
assert_server_banner_accepted("SSH-1.9"); |
| 443 |
|
| 444 |
/* SSH v2 */ |
| 445 |
assert_client_banner_accepted("SSH-2.0"); |
| 446 |
assert_server_banner_accepted("SSH-2.0"); |
| 447 |
|
| 448 |
/* OpenSSH banners: too short to extract major and minor versions */ |
| 449 |
assert_client_banner_accepted("SSH-2.0-OpenSSH"); |
| 450 |
assert_int_equal(0, session->openssh); |
| 451 |
assert_server_banner_accepted("SSH-2.0-OpenSSH"); |
| 452 |
assert_int_equal(0, session->openssh); |
| 453 |
|
| 454 |
|
| 455 |
/* OpenSSH banners: big enough to extract major and minor versions */ |
| 456 |
assert_client_banner_accepted("SSH-2.0-OpenSSH_5.9p1"); |
| 457 |
assert_int_equal(SSH_VERSION_INT(5, 9, 0), session->openssh); |
| 458 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_5.9p1"); |
| 459 |
assert_int_equal(SSH_VERSION_INT(5, 9, 0), session->openssh); |
| 460 |
|
| 461 |
assert_client_banner_accepted("SSH-2.0-OpenSSH_1.99"); |
| 462 |
assert_int_equal(SSH_VERSION_INT(1, 99, 0), session->openssh); |
| 463 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_1.99"); |
| 464 |
assert_int_equal(SSH_VERSION_INT(1, 99, 0), session->openssh); |
| 465 |
|
| 466 |
/* OpenSSH banners: major, minor version limits result in zero */ |
| 467 |
assert_client_banner_accepted("SSH-2.0-OpenSSH_0.99p1"); |
| 468 |
assert_int_equal(0, session->openssh); |
| 469 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_0.99p1"); |
| 470 |
assert_int_equal(0, session->openssh); |
| 471 |
assert_client_banner_accepted("SSH-2.0-OpenSSH_1.101p1"); |
| 472 |
assert_int_equal(0, session->openssh); |
| 473 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_1.101p1"); |
| 474 |
assert_int_equal(0, session->openssh); |
| 475 |
|
| 476 |
/* OpenSSH banners: bogus major results in zero */ |
| 477 |
assert_client_banner_accepted("SSH-2.0-OpenSSH_X.9p1"); |
| 478 |
assert_int_equal(0, session->openssh); |
| 479 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_X.9p1"); |
| 480 |
assert_int_equal(0, session->openssh); |
| 481 |
|
| 482 |
/* OpenSSH banners: bogus minor results in zero */ |
| 483 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_5.Yp1"); |
| 484 |
assert_int_equal(0, session->openssh); |
| 485 |
assert_client_banner_accepted("SSH-2.0-OpenSSH_5.Yp1"); |
| 486 |
assert_int_equal(0, session->openssh); |
| 487 |
|
| 488 |
/* OpenSSH banners: ssh-keyscan(1) */ |
| 489 |
assert_client_banner_accepted("SSH-2.0-OpenSSH-keyscan"); |
| 490 |
assert_int_equal(0, session->openssh); |
| 491 |
assert_server_banner_accepted("SSH-2.0-OpenSSH-keyscan"); |
| 492 |
assert_int_equal(0, session->openssh); |
| 493 |
|
| 494 |
/* OpenSSH banners: Double digit in major version */ |
| 495 |
assert_server_banner_accepted("SSH-2.0-OpenSSH_10.0p1"); |
| 496 |
assert_int_equal(SSH_VERSION_INT(10, 0, 0), session->openssh); |
| 497 |
|
| 498 |
ssh_free(session); |
| 499 |
} |
| 500 |
|
| 501 |
static void torture_ssh_dir_writeable(UNUSED_PARAM(void **state)) |
| 502 |
{ |
| 503 |
char *tmp_dir = NULL; |
| 504 |
int rc = 0; |
| 505 |
FILE *file = NULL; |
| 506 |
char buffer[256]; |
| 507 |
|
| 508 |
tmp_dir = torture_make_temp_dir(template); |
| 509 |
assert_non_null(tmp_dir); |
| 510 |
|
| 511 |
rc = ssh_dir_writeable(tmp_dir); |
| 512 |
assert_int_equal(rc, 1); |
| 513 |
|
| 514 |
/* Create a file */ |
| 515 |
snprintf(buffer, sizeof(buffer), "%s/a", tmp_dir); |
| 516 |
|
| 517 |
file = fopen(buffer, "w"); |
| 518 |
assert_non_null(file); |
| 519 |
|
| 520 |
fprintf(file, "Hello world!\n"); |
| 521 |
fclose(file); |
| 522 |
|
| 523 |
/* Negative test for checking a normal file */ |
| 524 |
rc = ssh_dir_writeable(buffer); |
| 525 |
assert_int_equal(rc, 0); |
| 526 |
|
| 527 |
/* Negative test for non existent file */ |
| 528 |
snprintf(buffer, sizeof(buffer), "%s/b", tmp_dir); |
| 529 |
rc = ssh_dir_writeable(buffer); |
| 530 |
assert_int_equal(rc, 0); |
| 531 |
|
| 532 |
#ifndef _WIN32 |
| 533 |
/* Negative test for directory without write permission */ |
| 534 |
rc = ssh_mkdir(buffer, 0400); |
| 535 |
assert_return_code(rc, errno); |
| 536 |
|
| 537 |
rc = ssh_dir_writeable(buffer); |
| 538 |
assert_int_equal(rc, 0); |
| 539 |
#endif |
| 540 |
|
| 541 |
torture_rmdirs(tmp_dir); |
| 542 |
|
| 543 |
SAFE_FREE(tmp_dir); |
| 544 |
} |
| 545 |
|
| 546 |
static void torture_ssh_mkdirs(UNUSED_PARAM(void **state)) |
| 547 |
{ |
| 548 |
char *tmp_dir = NULL; |
| 549 |
char *cwd = NULL; |
| 550 |
char buffer[256]; |
| 551 |
|
| 552 |
ssize_t count = 0; |
| 553 |
|
| 554 |
int rc; |
| 555 |
|
| 556 |
/* Get current working directory */ |
| 557 |
cwd = torture_get_current_working_dir(); |
| 558 |
assert_non_null(cwd); |
| 559 |
|
| 560 |
/* Create a base disposable directory */ |
| 561 |
tmp_dir = torture_make_temp_dir(template); |
| 562 |
assert_non_null(tmp_dir); |
| 563 |
|
| 564 |
/* Create a single directory */ |
| 565 |
count = snprintf(buffer, sizeof(buffer), "%s/a", tmp_dir); |
| 566 |
assert_return_code(count, errno); |
| 567 |
|
| 568 |
rc = ssh_mkdirs(buffer, 0700); |
| 569 |
assert_return_code(rc, errno); |
| 570 |
|
| 571 |
rc = ssh_dir_writeable(buffer); |
| 572 |
assert_int_equal(rc, 1); |
| 573 |
|
| 574 |
/* Create directories recursively */ |
| 575 |
count = snprintf(buffer, sizeof(buffer), "%s/b/c/d", tmp_dir); |
| 576 |
assert_return_code(count, errno); |
| 577 |
|
| 578 |
rc = ssh_mkdirs(buffer, 0700); |
| 579 |
assert_return_code(rc, errno); |
| 580 |
|
| 581 |
rc = ssh_dir_writeable(buffer); |
| 582 |
assert_int_equal(rc, 1); |
| 583 |
|
| 584 |
/* Change directory */ |
| 585 |
rc = torture_change_dir(tmp_dir); |
| 586 |
assert_return_code(rc, errno); |
| 587 |
|
| 588 |
/* Create single local directory */ |
| 589 |
rc = ssh_mkdirs("e", 0700); |
| 590 |
assert_return_code(rc, errno); |
| 591 |
|
| 592 |
rc = ssh_dir_writeable("e"); |
| 593 |
assert_int_equal(rc, 1); |
| 594 |
|
| 595 |
/* Create local directories recursively */ |
| 596 |
rc = ssh_mkdirs("f/g/h", 0700); |
| 597 |
assert_return_code(rc, errno); |
| 598 |
|
| 599 |
rc = ssh_dir_writeable("f/g/h"); |
| 600 |
assert_int_equal(rc, 1); |
| 601 |
|
| 602 |
/* Negative test for creating "." directory */ |
| 603 |
rc = ssh_mkdirs(".", 0700); |
| 604 |
assert_int_equal(rc, -1); |
| 605 |
assert_int_equal(errno, EINVAL); |
| 606 |
|
| 607 |
/* Negative test for creating "/" directory */ |
| 608 |
rc = ssh_mkdirs("/", 0700); |
| 609 |
assert_int_equal(rc, -1); |
| 610 |
assert_int_equal(errno, EINVAL); |
| 611 |
|
| 612 |
/* Negative test for creating "" directory */ |
| 613 |
rc = ssh_mkdirs("", 0700); |
| 614 |
assert_int_equal(rc, -1); |
| 615 |
assert_int_equal(errno, EINVAL); |
| 616 |
|
| 617 |
/* Negative test for creating NULL directory */ |
| 618 |
rc = ssh_mkdirs(NULL, 0700); |
| 619 |
assert_int_equal(rc, -1); |
| 620 |
assert_int_equal(errno, EINVAL); |
| 621 |
|
| 622 |
/* Negative test for creating existing directory */ |
| 623 |
rc = ssh_mkdirs("a", 0700); |
| 624 |
assert_int_equal(rc, -1); |
| 625 |
assert_int_equal(errno, EEXIST); |
| 626 |
|
| 627 |
/* Return to original directory */ |
| 628 |
rc = torture_change_dir(cwd); |
| 629 |
assert_return_code(rc, errno); |
| 630 |
|
| 631 |
/* Cleanup */ |
| 632 |
torture_rmdirs(tmp_dir); |
| 633 |
|
| 634 |
SAFE_FREE(tmp_dir); |
| 635 |
SAFE_FREE(cwd); |
| 636 |
} |
| 637 |
|
| 638 |
static void torture_ssh_quote_file_name(UNUSED_PARAM(void **state)) |
| 639 |
{ |
| 640 |
char buffer[2048]; |
| 641 |
int rc; |
| 642 |
|
| 643 |
/* Only ordinary chars */ |
| 644 |
rc = ssh_quote_file_name("a b", buffer, 2048); |
| 645 |
assert_int_equal(rc, 5); |
| 646 |
assert_string_equal(buffer, "'a b'"); |
| 647 |
|
| 648 |
/* Single quote in file name */ |
| 649 |
rc = ssh_quote_file_name("a'b", buffer, 2048); |
| 650 |
assert_int_equal(rc, 9); |
| 651 |
assert_string_equal(buffer, "'a'\"'\"'b'"); |
| 652 |
|
| 653 |
/* Exclamation in file name */ |
| 654 |
rc = ssh_quote_file_name("a!b", buffer, 2048); |
| 655 |
assert_int_equal(rc, 8); |
| 656 |
assert_string_equal(buffer, "'a'\\!'b'"); |
| 657 |
|
| 658 |
/* All together */ |
| 659 |
rc = ssh_quote_file_name("'a!b'", buffer, 2048); |
| 660 |
assert_int_equal(rc, 14); |
| 661 |
assert_string_equal(buffer, "\"'\"'a'\\!'b'\"'\""); |
| 662 |
|
| 663 |
rc = ssh_quote_file_name("a'!b", buffer, 2048); |
| 664 |
assert_int_equal(rc, 11); |
| 665 |
assert_string_equal(buffer, "'a'\"'\"\\!'b'"); |
| 666 |
|
| 667 |
rc = ssh_quote_file_name("a'$b", buffer, 2048); |
| 668 |
assert_int_equal(rc, 10); |
| 669 |
assert_string_equal(buffer, "'a'\"'\"'$b'"); |
| 670 |
|
| 671 |
rc = ssh_quote_file_name("a'`b", buffer, 2048); |
| 672 |
assert_int_equal(rc, 10); |
| 673 |
assert_string_equal(buffer, "'a'\"'\"'`b'"); |
| 674 |
|
| 675 |
|
| 676 |
rc = ssh_quote_file_name(" ", buffer, 2048); |
| 677 |
assert_int_equal(rc, 3); |
| 678 |
assert_string_equal(buffer, "' '"); |
| 679 |
|
| 680 |
rc = ssh_quote_file_name(" ", buffer, 2048); |
| 681 |
assert_int_equal(rc, 4); |
| 682 |
assert_string_equal(buffer, "' '"); |
| 683 |
|
| 684 |
|
| 685 |
rc = ssh_quote_file_name("\r", buffer, 2048); |
| 686 |
assert_int_equal(rc, 3); |
| 687 |
assert_string_equal(buffer, "'\r'"); |
| 688 |
|
| 689 |
rc = ssh_quote_file_name("\n", buffer, 2048); |
| 690 |
assert_int_equal(rc, 3); |
| 691 |
assert_string_equal(buffer, "'\n'"); |
| 692 |
|
| 693 |
rc = ssh_quote_file_name("\r\n", buffer, 2048); |
| 694 |
assert_int_equal(rc, 4); |
| 695 |
assert_string_equal(buffer, "'\r\n'"); |
| 696 |
|
| 697 |
|
| 698 |
rc = ssh_quote_file_name("\\r", buffer, 2048); |
| 699 |
assert_int_equal(rc, 4); |
| 700 |
assert_string_equal(buffer, "'\\r'"); |
| 701 |
|
| 702 |
rc = ssh_quote_file_name("\\n", buffer, 2048); |
| 703 |
assert_int_equal(rc, 4); |
| 704 |
assert_string_equal(buffer, "'\\n'"); |
| 705 |
|
| 706 |
rc = ssh_quote_file_name("\\r\\n", buffer, 2048); |
| 707 |
assert_int_equal(rc, 6); |
| 708 |
assert_string_equal(buffer, "'\\r\\n'"); |
| 709 |
|
| 710 |
|
| 711 |
rc = ssh_quote_file_name("\t", buffer, 2048); |
| 712 |
assert_int_equal(rc, 3); |
| 713 |
assert_string_equal(buffer, "'\t'"); |
| 714 |
|
| 715 |
rc = ssh_quote_file_name("\v", buffer, 2048); |
| 716 |
assert_int_equal(rc, 3); |
| 717 |
assert_string_equal(buffer, "'\v'"); |
| 718 |
|
| 719 |
rc = ssh_quote_file_name("\t\v", buffer, 2048); |
| 720 |
assert_int_equal(rc, 4); |
| 721 |
assert_string_equal(buffer, "'\t\v'"); |
| 722 |
|
| 723 |
|
| 724 |
rc = ssh_quote_file_name("'", buffer, 2048); |
| 725 |
assert_int_equal(rc, 3); |
| 726 |
assert_string_equal(buffer, "\"'\""); |
| 727 |
|
| 728 |
rc = ssh_quote_file_name("''", buffer, 2048); |
| 729 |
assert_int_equal(rc, 4); |
| 730 |
assert_string_equal(buffer, "\"''\""); |
| 731 |
|
| 732 |
|
| 733 |
rc = ssh_quote_file_name("\"", buffer, 2048); |
| 734 |
assert_int_equal(rc, 3); |
| 735 |
assert_string_equal(buffer, "'\"'"); |
| 736 |
|
| 737 |
rc = ssh_quote_file_name("\"\"", buffer, 2048); |
| 738 |
assert_int_equal(rc, 4); |
| 739 |
assert_string_equal(buffer, "'\"\"'"); |
| 740 |
|
| 741 |
rc = ssh_quote_file_name("'\"", buffer, 2048); |
| 742 |
assert_int_equal(rc, 6); |
| 743 |
assert_string_equal(buffer, "\"'\"'\"'"); |
| 744 |
|
| 745 |
rc = ssh_quote_file_name("\"'", buffer, 2048); |
| 746 |
assert_int_equal(rc, 6); |
| 747 |
assert_string_equal(buffer, "'\"'\"'\""); |
| 748 |
|
| 749 |
|
| 750 |
/* Worst case */ |
| 751 |
rc = ssh_quote_file_name("a'b'", buffer, 3 * 4 + 1); |
| 752 |
assert_int_equal(rc, 12); |
| 753 |
assert_string_equal(buffer, "'a'\"'\"'b'\"'\""); |
| 754 |
|
| 755 |
/* Negative tests */ |
| 756 |
|
| 757 |
/* NULL params */ |
| 758 |
rc = ssh_quote_file_name(NULL, buffer, 3 * 4 + 1); |
| 759 |
assert_int_equal(rc, SSH_ERROR); |
| 760 |
|
| 761 |
/* NULL params */ |
| 762 |
rc = ssh_quote_file_name("a b", NULL, 3 * 4 + 1); |
| 763 |
assert_int_equal(rc, SSH_ERROR); |
| 764 |
|
| 765 |
/* Small buffer size */ |
| 766 |
rc = ssh_quote_file_name("a b", buffer, 0); |
| 767 |
assert_int_equal(rc, SSH_ERROR); |
| 768 |
|
| 769 |
/* Worst case and small buffer size */ |
| 770 |
rc = ssh_quote_file_name("a'b'", buffer, 3 * 4); |
| 771 |
assert_int_equal(rc, SSH_ERROR); |
| 772 |
} |
| 773 |
|
| 774 |
static void torture_ssh_newline_vis(UNUSED_PARAM(void **state)) |
| 775 |
{ |
| 776 |
int rc; |
| 777 |
char buffer[1024]; |
| 778 |
|
| 779 |
rc = ssh_newline_vis("\n", buffer, 1024); |
| 780 |
assert_int_equal(rc, 2); |
| 781 |
assert_string_equal(buffer, "\\n"); |
| 782 |
|
| 783 |
rc = ssh_newline_vis("\n\n\n\n", buffer, 1024); |
| 784 |
assert_int_equal(rc, 8); |
| 785 |
assert_string_equal(buffer, "\\n\\n\\n\\n"); |
| 786 |
|
| 787 |
rc = ssh_newline_vis("a\nb\n", buffer, 1024); |
| 788 |
assert_int_equal(rc, 6); |
| 789 |
assert_string_equal(buffer, "a\\nb\\n"); |
| 790 |
} |
| 791 |
|
| 792 |
static void torture_ssh_strreplace(void **state) |
| 793 |
{ |
| 794 |
char test_string1[] = "this;is;a;test"; |
| 795 |
char test_string2[] = "test;is;a;this"; |
| 796 |
char test_string3[] = "this;test;is;a"; |
| 797 |
char *replaced_string = NULL; |
| 798 |
|
| 799 |
(void) state; |
| 800 |
|
| 801 |
/* pattern and replacement are of the same size */ |
| 802 |
replaced_string = ssh_strreplace(test_string1, "test", "kiwi"); |
| 803 |
assert_string_equal(replaced_string, "this;is;a;kiwi"); |
| 804 |
free(replaced_string); |
| 805 |
|
| 806 |
replaced_string = ssh_strreplace(test_string2, "test", "kiwi"); |
| 807 |
assert_string_equal(replaced_string, "kiwi;is;a;this"); |
| 808 |
free(replaced_string); |
| 809 |
|
| 810 |
replaced_string = ssh_strreplace(test_string3, "test", "kiwi"); |
| 811 |
assert_string_equal(replaced_string, "this;kiwi;is;a"); |
| 812 |
free(replaced_string); |
| 813 |
|
| 814 |
/* replacement is greater than pattern */ |
| 815 |
replaced_string = ssh_strreplace(test_string1, "test", "an;apple"); |
| 816 |
assert_string_equal(replaced_string, "this;is;a;an;apple"); |
| 817 |
free(replaced_string); |
| 818 |
|
| 819 |
replaced_string = ssh_strreplace(test_string2, "test", "an;apple"); |
| 820 |
assert_string_equal(replaced_string, "an;apple;is;a;this"); |
| 821 |
free(replaced_string); |
| 822 |
|
| 823 |
replaced_string = ssh_strreplace(test_string3, "test", "an;apple"); |
| 824 |
assert_string_equal(replaced_string, "this;an;apple;is;a"); |
| 825 |
free(replaced_string); |
| 826 |
|
| 827 |
/* replacement is less than pattern */ |
| 828 |
replaced_string = ssh_strreplace(test_string1, "test", "an"); |
| 829 |
assert_string_equal(replaced_string, "this;is;a;an"); |
| 830 |
free(replaced_string); |
| 831 |
|
| 832 |
replaced_string = ssh_strreplace(test_string2, "test", "an"); |
| 833 |
assert_string_equal(replaced_string, "an;is;a;this"); |
| 834 |
free(replaced_string); |
| 835 |
|
| 836 |
replaced_string = ssh_strreplace(test_string3, "test", "an"); |
| 837 |
assert_string_equal(replaced_string, "this;an;is;a"); |
| 838 |
free(replaced_string); |
| 839 |
|
| 840 |
/* pattern not found in teststring */ |
| 841 |
replaced_string = ssh_strreplace(test_string1, "banana", "an"); |
| 842 |
assert_string_equal(replaced_string, test_string1); |
| 843 |
free(replaced_string); |
| 844 |
|
| 845 |
/* pattern is NULL */ |
| 846 |
replaced_string = ssh_strreplace(test_string1, NULL , "an"); |
| 847 |
assert_string_equal(replaced_string, test_string1); |
| 848 |
free(replaced_string); |
| 849 |
|
| 850 |
/* replacement is NULL */ |
| 851 |
replaced_string = ssh_strreplace(test_string1, "test", NULL); |
| 852 |
assert_string_equal(replaced_string, test_string1); |
| 853 |
free(replaced_string); |
| 854 |
|
| 855 |
/* src is NULL */ |
| 856 |
replaced_string = ssh_strreplace(NULL, "test", "kiwi"); |
| 857 |
assert_null(replaced_string); |
| 858 |
} |
| 859 |
|
| 860 |
static void torture_ssh_strerror(void **state) |
| 861 |
{ |
| 862 |
char buf[1024]; |
| 863 |
size_t bufflen = sizeof(buf); |
| 864 |
char *out = NULL; |
| 865 |
|
| 866 |
(void) state; |
| 867 |
|
| 868 |
out = ssh_strerror(ENOENT, buf, 1); /* too short */ |
| 869 |
assert_string_equal(out, "\0"); |
| 870 |
|
| 871 |
out = ssh_strerror(256, buf, bufflen); /* unknown error code */ |
| 872 |
/* This error is always different: |
| 873 |
* Freebd: "Unknown error: 256" |
| 874 |
* MinGW/Win: "Unknown error" |
| 875 |
* Linux/glibc: "Unknown error 256" |
| 876 |
* Alpine/musl: "No error information" |
| 877 |
*/ |
| 878 |
assert_non_null(out); |
| 879 |
|
| 880 |
out = ssh_strerror(ENOMEM, buf, bufflen); |
| 881 |
/* This actually differs too for glibc/musl: |
| 882 |
* musl: "Out of memory" |
| 883 |
* everything else: "Cannot allocate memory" |
| 884 |
*/ |
| 885 |
assert_non_null(out); |
| 886 |
} |
| 887 |
|
| 888 |
static void torture_ssh_readn(void **state) |
| 889 |
{ |
| 890 |
char *write_buf = NULL, *read_buf = NULL, *file_path = NULL; |
| 891 |
size_t data_len = 10 * 1024 * 1024; |
| 892 |
size_t read_buf_size = data_len + 1024; |
| 893 |
size_t i, total_bytes_written = 0; |
| 894 |
|
| 895 |
const char *file_template = "libssh_torture_ssh_readn_test_XXXXXX"; |
| 896 |
|
| 897 |
off_t off; |
| 898 |
ssize_t bytes_read, bytes_written; |
| 899 |
int fd, rc, flags; |
| 900 |
|
| 901 |
(void)state; |
| 902 |
|
| 903 |
write_buf = malloc(data_len); |
| 904 |
assert_non_null(write_buf); |
| 905 |
|
| 906 |
/* Fill the write buffer with random data */ |
| 907 |
for (i = 0; i < data_len; ++i) { |
| 908 |
rc = rand(); |
| 909 |
write_buf[i] = (char)(rc & 0xff); |
| 910 |
} |
| 911 |
|
| 912 |
/* |
| 913 |
* The read buffer's size is intentionally kept larger than data_len. |
| 914 |
* |
| 915 |
* This is done so that we are able to test the scenario when the user |
| 916 |
* requests ssh_readn() to read more bytes than the number of bytes present |
| 917 |
* in the file. |
| 918 |
* |
| 919 |
* If the read buffer's size is kept same as data_len and if the test |
| 920 |
* requests ssh_readn() to read more than data_len bytes starting from file |
| 921 |
* offset 0, it will lead to a valgrind failure as in this case, ssh_readn() |
| 922 |
* will pass an unallocated memory address in the last call it makes to |
| 923 |
* read(). |
| 924 |
*/ |
| 925 |
read_buf = malloc(read_buf_size); |
| 926 |
assert_non_null(read_buf); |
| 927 |
|
| 928 |
file_path = torture_create_temp_file(file_template); |
| 929 |
assert_non_null(file_path); |
| 930 |
|
| 931 |
/* Open a file for reading and writing */ |
| 932 |
flags = O_RDWR; |
| 933 |
#ifdef _WIN32 |
| 934 |
flags |= O_BINARY; |
| 935 |
#endif |
| 936 |
|
| 937 |
fd = open(file_path, flags, 0); |
| 938 |
assert_int_not_equal(fd, -1); |
| 939 |
|
| 940 |
/* Write the data present in the write buffer to the file */ |
| 941 |
do { |
| 942 |
bytes_written = write(fd, |
| 943 |
write_buf + total_bytes_written, |
| 944 |
data_len - total_bytes_written); |
| 945 |
|
| 946 |
if (bytes_written == -1 && errno == EINTR) { |
| 947 |
continue; |
| 948 |
} |
| 949 |
|
| 950 |
assert_int_not_equal(bytes_written, -1); |
| 951 |
total_bytes_written += bytes_written; |
| 952 |
} while (total_bytes_written < data_len); |
| 953 |
|
| 954 |
/* Seek to the start of the file */ |
| 955 |
off = lseek(fd, 0, SEEK_SET); |
| 956 |
assert_int_not_equal(off, -1); |
| 957 |
|
| 958 |
bytes_read = ssh_readn(fd, read_buf, data_len); |
| 959 |
assert_int_equal(bytes_read, data_len); |
| 960 |
|
| 961 |
/* |
| 962 |
* Ensure that the data stored in the read buffer is same as the data |
| 963 |
* present in the file and not some garbage. |
| 964 |
*/ |
| 965 |
assert_memory_equal(read_buf, write_buf, data_len); |
| 966 |
|
| 967 |
/* |
| 968 |
* Ensure that the file offset is on EOF and requesting to read more leads |
| 969 |
* to 0 bytes getting read. |
| 970 |
*/ |
| 971 |
off = lseek(fd, 0, SEEK_CUR); |
| 972 |
assert_int_equal(off, data_len); |
| 973 |
|
| 974 |
bytes_read = ssh_readn(fd, read_buf, data_len); |
| 975 |
assert_int_equal(bytes_read, 0); |
| 976 |
|
| 977 |
/* Try to read more bytes than what are present in the file */ |
| 978 |
off = lseek(fd, 0, SEEK_SET); |
| 979 |
assert_int_not_equal(off, -1); |
| 980 |
|
| 981 |
bytes_read = ssh_readn(fd, read_buf, read_buf_size); |
| 982 |
assert_int_equal(bytes_read, data_len); |
| 983 |
|
| 984 |
/* |
| 985 |
* Ensure that the data stored in the read buffer is same as the data |
| 986 |
* present in the file and not some garbage. |
| 987 |
*/ |
| 988 |
assert_memory_equal(read_buf, write_buf, data_len); |
| 989 |
|
| 990 |
/* Negative tests start */ |
| 991 |
bytes_read = ssh_readn(-2, read_buf, data_len); |
| 992 |
assert_int_equal(bytes_read, -1); |
| 993 |
|
| 994 |
bytes_read = ssh_readn(fd, NULL, data_len); |
| 995 |
assert_int_equal(bytes_read, -1); |
| 996 |
|
| 997 |
bytes_read = ssh_readn(fd, read_buf, 0); |
| 998 |
assert_int_equal(bytes_read, -1); |
| 999 |
|
| 1000 |
/* Clean up */ |
| 1001 |
rc = close(fd); |
| 1002 |
assert_int_equal(rc, 0); |
| 1003 |
|
| 1004 |
rc = unlink(file_path); |
| 1005 |
assert_int_equal(rc, 0); |
| 1006 |
|
| 1007 |
free(file_path); |
| 1008 |
free(read_buf); |
| 1009 |
free(write_buf); |
| 1010 |
} |
| 1011 |
|
| 1012 |
static void torture_ssh_writen(void **state) |
| 1013 |
{ |
| 1014 |
char *write_buf = NULL, *read_buf = NULL, *file_path = NULL; |
| 1015 |
const char *file_template = "libssh_torture_ssh_writen_test_XXXXXX"; |
| 1016 |
|
| 1017 |
size_t data_len = 10 * 1024 * 1024; |
| 1018 |
size_t i, total_bytes_read = 0; |
| 1019 |
ssize_t bytes_written, bytes_read; |
| 1020 |
off_t off; |
| 1021 |
int rc, fd, flags; |
| 1022 |
|
| 1023 |
(void)state; |
| 1024 |
|
| 1025 |
write_buf = malloc(data_len); |
| 1026 |
assert_non_null(write_buf); |
| 1027 |
|
| 1028 |
/* Fill the write buffer with random data */ |
| 1029 |
for (i = 0; i < data_len; ++i) { |
| 1030 |
rc = rand(); |
| 1031 |
write_buf[i] = (char)(rc & 0xff); |
| 1032 |
} |
| 1033 |
|
| 1034 |
read_buf = malloc(data_len); |
| 1035 |
assert_non_null(read_buf); |
| 1036 |
|
| 1037 |
file_path = torture_create_temp_file(file_template); |
| 1038 |
assert_non_null(file_path); |
| 1039 |
|
| 1040 |
/* Open a file for reading and writing */ |
| 1041 |
flags = O_RDWR; |
| 1042 |
#ifdef _WIN32 |
| 1043 |
flags |= O_BINARY; |
| 1044 |
#endif |
| 1045 |
|
| 1046 |
fd = open(file_path, flags, 0); |
| 1047 |
assert_int_not_equal(fd, -1); |
| 1048 |
|
| 1049 |
/* Write the data present in the write buffer to the file */ |
| 1050 |
bytes_written = ssh_writen(fd, write_buf, data_len); |
| 1051 |
assert_int_equal(bytes_written, data_len); |
| 1052 |
|
| 1053 |
/* |
| 1054 |
* Ensure that the file offset is incremented by the number of bytes |
| 1055 |
* written. |
| 1056 |
*/ |
| 1057 |
off = lseek(fd, 0, SEEK_CUR); |
| 1058 |
assert_int_equal(off, data_len); |
| 1059 |
|
| 1060 |
/* |
| 1061 |
* Ensure that the data present in the write buffer has been written to the |
| 1062 |
* file and not some garbage. |
| 1063 |
*/ |
| 1064 |
off = lseek(fd, 0, SEEK_SET); |
| 1065 |
assert_int_not_equal(off, -1); |
| 1066 |
|
| 1067 |
do { |
| 1068 |
bytes_read = read(fd, |
| 1069 |
read_buf + total_bytes_read, |
| 1070 |
data_len - total_bytes_read); |
| 1071 |
|
| 1072 |
if (bytes_read == -1 && errno == EINTR) { |
| 1073 |
continue; |
| 1074 |
} |
| 1075 |
|
| 1076 |
assert_int_not_equal(bytes_read, -1); |
| 1077 |
assert_int_not_equal(bytes_read, 0); |
| 1078 |
|
| 1079 |
total_bytes_read += bytes_read; |
| 1080 |
} while (total_bytes_read < data_len); |
| 1081 |
|
| 1082 |
assert_memory_equal(write_buf, read_buf, data_len); |
| 1083 |
|
| 1084 |
/* Negative tests start */ |
| 1085 |
bytes_written = ssh_writen(-3, write_buf, data_len); |
| 1086 |
assert_int_equal(bytes_written, -1); |
| 1087 |
|
| 1088 |
bytes_written = ssh_writen(fd, NULL, data_len); |
| 1089 |
assert_int_equal(bytes_written, -1); |
| 1090 |
|
| 1091 |
bytes_written = ssh_writen(fd, write_buf, 0); |
| 1092 |
assert_int_equal(bytes_written, -1); |
| 1093 |
|
| 1094 |
/* Clean up */ |
| 1095 |
rc = close(fd); |
| 1096 |
assert_int_equal(rc, 0); |
| 1097 |
|
| 1098 |
rc = unlink(file_path); |
| 1099 |
assert_int_equal(rc, 0); |
| 1100 |
|
| 1101 |
free(file_path); |
| 1102 |
free(read_buf); |
| 1103 |
free(write_buf); |
| 1104 |
} |
| 1105 |
|
| 1106 |
static void torture_ssh_check_hostname_syntax(void **state) |
| 1107 |
{ |
| 1108 |
int rc; |
| 1109 |
(void)state; |
| 1110 |
|
| 1111 |
rc = ssh_check_hostname_syntax("duckduckgo.com"); |
| 1112 |
assert_int_equal(rc, SSH_OK); |
| 1113 |
rc = ssh_check_hostname_syntax("www.libssh.org"); |
| 1114 |
assert_int_equal(rc, SSH_OK); |
| 1115 |
rc = ssh_check_hostname_syntax("Some-Thing.com"); |
| 1116 |
assert_int_equal(rc, SSH_OK); |
| 1117 |
rc = ssh_check_hostname_syntax("amazon.a23456789012345678901234567890123456789012345678901234567890123"); |
| 1118 |
assert_int_equal(rc, SSH_OK); |
| 1119 |
rc = ssh_check_hostname_syntax("amazon.a23456789012345678901234567890123456789012345678901234567890123.a23456789012345678901234567890123456789012345678901234567890123.ok"); |
| 1120 |
assert_int_equal(rc, SSH_OK); |
| 1121 |
rc = ssh_check_hostname_syntax("amazon.a23456789012345678901234567890123456789012345678901234567890123.a23456789012345678901234567890123456789012345678901234567890123.a23456789012345678901234567890123456789012345678901234567890123"); |
| 1122 |
assert_int_equal(rc, SSH_OK); |
| 1123 |
rc = ssh_check_hostname_syntax("lavabo-inter.innocentes-manus-meas"); |
| 1124 |
assert_int_equal(rc, SSH_OK); |
| 1125 |
rc = ssh_check_hostname_syntax("localhost"); |
| 1126 |
assert_int_equal(rc, SSH_OK); |
| 1127 |
rc = ssh_check_hostname_syntax("a"); |
| 1128 |
assert_int_equal(rc, SSH_OK); |
| 1129 |
rc = ssh_check_hostname_syntax("a-0.b-b"); |
| 1130 |
assert_int_equal(rc, SSH_OK); |
| 1131 |
rc = ssh_check_hostname_syntax("libssh."); |
| 1132 |
assert_int_equal(rc, SSH_OK); |
| 1133 |
// IDN |
| 1134 |
rc = ssh_check_hostname_syntax("xn--bcher-kva.tld"); |
| 1135 |
assert_int_equal(rc, SSH_OK); |
| 1136 |
|
| 1137 |
rc = ssh_check_hostname_syntax(NULL); |
| 1138 |
assert_int_equal(rc, SSH_ERROR); |
| 1139 |
rc = ssh_check_hostname_syntax(""); |
| 1140 |
assert_int_equal(rc, SSH_ERROR); |
| 1141 |
rc = ssh_check_hostname_syntax("/"); |
| 1142 |
assert_int_equal(rc, SSH_ERROR); |
| 1143 |
rc = ssh_check_hostname_syntax("@"); |
| 1144 |
assert_int_equal(rc, SSH_ERROR); |
| 1145 |
rc = ssh_check_hostname_syntax("["); |
| 1146 |
assert_int_equal(rc, SSH_ERROR); |
| 1147 |
rc = ssh_check_hostname_syntax("`"); |
| 1148 |
assert_int_equal(rc, SSH_ERROR); |
| 1149 |
rc = ssh_check_hostname_syntax("{"); |
| 1150 |
assert_int_equal(rc, SSH_ERROR); |
| 1151 |
rc = ssh_check_hostname_syntax("&"); |
| 1152 |
assert_int_equal(rc, SSH_ERROR); |
| 1153 |
rc = ssh_check_hostname_syntax("|"); |
| 1154 |
assert_int_equal(rc, SSH_ERROR); |
| 1155 |
rc = ssh_check_hostname_syntax("\""); |
| 1156 |
assert_int_equal(rc, SSH_ERROR); |
| 1157 |
rc = ssh_check_hostname_syntax("`"); |
| 1158 |
assert_int_equal(rc, SSH_ERROR); |
| 1159 |
rc = ssh_check_hostname_syntax(" "); |
| 1160 |
assert_int_equal(rc, SSH_ERROR); |
| 1161 |
rc = ssh_check_hostname_syntax("*the+giant&\"rooks\".c0m"); |
| 1162 |
assert_int_equal(rc, SSH_ERROR); |
| 1163 |
rc = ssh_check_hostname_syntax("!www.libssh.org"); |
| 1164 |
assert_int_equal(rc, SSH_ERROR); |
| 1165 |
rc = ssh_check_hostname_syntax("--.--"); |
| 1166 |
assert_int_equal(rc, SSH_ERROR); |
| 1167 |
rc = ssh_check_hostname_syntax("libssh.a234567890123456789012345678901234567890123456789012345678901234"); |
| 1168 |
assert_int_equal(rc, SSH_ERROR); |
| 1169 |
rc = ssh_check_hostname_syntax("libssh.a234567890123456789012345678901234567890123456789012345678901234.a234567890123456789012345678901234567890123456789012345678901234"); |
| 1170 |
assert_int_equal(rc, SSH_ERROR); |
| 1171 |
rc = ssh_check_hostname_syntax("libssh-"); |
| 1172 |
assert_int_equal(rc, SSH_ERROR); |
| 1173 |
rc = ssh_check_hostname_syntax("fe80::9656:d028:8652:66b6"); |
| 1174 |
assert_int_equal(rc, SSH_ERROR); |
| 1175 |
rc = ssh_check_hostname_syntax("."); |
| 1176 |
assert_int_equal(rc, SSH_ERROR); |
| 1177 |
rc = ssh_check_hostname_syntax(".."); |
| 1178 |
assert_int_equal(rc, SSH_ERROR); |
| 1179 |
// IDN non-encoded |
| 1180 |
rc = ssh_check_hostname_syntax("bücher.tld"); |
| 1181 |
assert_int_equal(rc, SSH_ERROR); |
| 1182 |
} |
| 1183 |
|
| 1184 |
static void torture_ssh_check_username_syntax(void **state) { |
| 1185 |
int rc; |
| 1186 |
(void)state; |
| 1187 |
|
| 1188 |
rc = ssh_check_username_syntax("username"); |
| 1189 |
assert_int_equal(rc, SSH_OK); |
| 1190 |
rc = ssh_check_username_syntax("Alice"); |
| 1191 |
assert_int_equal(rc, SSH_OK); |
| 1192 |
rc = ssh_check_username_syntax("Alice and Bob"); |
| 1193 |
assert_int_equal(rc, SSH_OK); |
| 1194 |
rc = ssh_check_username_syntax("n4me?"); |
| 1195 |
assert_int_equal(rc, SSH_OK); |
| 1196 |
|
| 1197 |
rc = ssh_check_username_syntax("alice&bob"); |
| 1198 |
assert_int_equal(rc, SSH_ERROR); |
| 1199 |
rc = ssh_check_username_syntax("backslash\\"); |
| 1200 |
assert_int_equal(rc, SSH_ERROR); |
| 1201 |
rc = ssh_check_username_syntax("&var|()us\"<ha`r{}'"); |
| 1202 |
assert_int_equal(rc, SSH_ERROR); |
| 1203 |
rc = ssh_check_username_syntax(" -"); |
| 1204 |
assert_int_equal(rc, SSH_ERROR); |
| 1205 |
rc = ssh_check_username_syntax("me and -"); |
| 1206 |
assert_int_equal(rc, SSH_ERROR); |
| 1207 |
rc = ssh_check_username_syntax("los -santos"); |
| 1208 |
assert_int_equal(rc, SSH_ERROR); |
| 1209 |
rc = ssh_check_username_syntax("- who?"); |
| 1210 |
assert_int_equal(rc, SSH_ERROR); |
| 1211 |
rc = ssh_check_username_syntax(NULL); |
| 1212 |
assert_int_equal(rc, SSH_ERROR); |
| 1213 |
rc = ssh_check_username_syntax(""); |
| 1214 |
assert_int_equal(rc, SSH_ERROR); |
| 1215 |
} |
| 1216 |
|
| 1217 |
static void torture_ssh_is_ipaddr(void **state) { |
| 1218 |
int rc; |
| 1219 |
char *interf = malloc(64); |
| 1220 |
char *test_interf = malloc(128); |
| 1221 |
(void)state; |
| 1222 |
|
| 1223 |
assert_non_null(interf); |
| 1224 |
assert_non_null(test_interf); |
| 1225 |
rc = ssh_is_ipaddr("201.255.3.69"); |
| 1226 |
assert_int_equal(rc, 1); |
| 1227 |
rc = ssh_is_ipaddr("::1"); |
| 1228 |
assert_int_equal(rc, 1); |
| 1229 |
rc = ssh_is_ipaddr("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); |
| 1230 |
assert_int_equal(rc, 1); |
| 1231 |
if_indextoname(1, interf); |
| 1232 |
assert_non_null(interf); |
| 1233 |
rc = sprintf(test_interf, TORTURE_IPV6_LOCAL_LINK, interf); |
| 1234 |
/* the "%%s" is not written */ |
| 1235 |
assert_int_equal(rc, strlen(interf) + strlen(TORTURE_IPV6_LOCAL_LINK) - 3); |
| 1236 |
rc = ssh_is_ipaddr(test_interf); |
| 1237 |
assert_int_equal(rc, 1); |
| 1238 |
free(interf); |
| 1239 |
free(test_interf); |
| 1240 |
|
| 1241 |
rc = ssh_is_ipaddr(".."); |
| 1242 |
assert_int_equal(rc, 0); |
| 1243 |
rc = ssh_is_ipaddr(":::"); |
| 1244 |
assert_int_equal(rc, 0); |
| 1245 |
rc = ssh_is_ipaddr("1.1.1.1.1"); |
| 1246 |
assert_int_equal(rc, 0); |
| 1247 |
rc = ssh_is_ipaddr("1.1"); |
| 1248 |
assert_int_equal(rc, 0); |
| 1249 |
rc = ssh_is_ipaddr("caesar"); |
| 1250 |
assert_int_equal(rc, 0); |
| 1251 |
rc = ssh_is_ipaddr("::xa:1"); |
| 1252 |
assert_int_equal(rc, 0); |
| 1253 |
} |
| 1254 |
|
| 1255 |
static void torture_ssh_get_hexa(void **state) |
| 1256 |
{ |
| 1257 |
const unsigned char *bin = NULL; |
| 1258 |
char *hex = NULL; |
| 1259 |
|
| 1260 |
(void)state; |
| 1261 |
|
| 1262 |
/* Null pointer should not crash */ |
| 1263 |
bin = NULL; |
| 1264 |
hex = ssh_get_hexa(bin, 0); |
| 1265 |
assert_null(hex); |
| 1266 |
|
| 1267 |
/* Null pointer should not crash regardless the length */ |
| 1268 |
bin = NULL; |
| 1269 |
hex = ssh_get_hexa(bin, 99); |
| 1270 |
assert_null(hex); |
| 1271 |
|
| 1272 |
/* Zero length input is not much useful. Just expect NULL too */ |
| 1273 |
bin = (const unsigned char *)""; |
| 1274 |
hex = ssh_get_hexa(bin, 0); |
| 1275 |
assert_null(hex); |
| 1276 |
|
| 1277 |
/* Valid inputs */ |
| 1278 |
bin = (const unsigned char *)"\x00\xFF"; |
| 1279 |
hex = ssh_get_hexa(bin, 2); |
| 1280 |
assert_non_null(hex); |
| 1281 |
assert_string_equal(hex, "00:ff"); |
| 1282 |
ssh_string_free_char(hex); |
| 1283 |
} |
| 1284 |
|
| 1285 |
int torture_run_tests(void) { |
| 1286 |
int rc; |
| 1287 |
struct CMUnitTest tests[] = { |
| 1288 |
cmocka_unit_test(torture_get_user_home_dir), |
| 1289 |
cmocka_unit_test(torture_basename), |
| 1290 |
cmocka_unit_test(torture_dirname), |
| 1291 |
cmocka_unit_test(torture_ntohll), |
| 1292 |
cmocka_unit_test(torture_localtime_r), |
| 1293 |
#ifdef _WIN32 |
| 1294 |
cmocka_unit_test(torture_path_expand_tilde_win), |
| 1295 |
#else |
| 1296 |
cmocka_unit_test(torture_path_expand_tilde_unix), |
| 1297 |
#endif |
| 1298 |
cmocka_unit_test_setup_teardown(torture_path_expand_escape, |
| 1299 |
setup, |
| 1300 |
teardown), |
| 1301 |
cmocka_unit_test_setup_teardown(torture_path_expand_known_hosts, |
| 1302 |
setup, |
| 1303 |
teardown), |
| 1304 |
cmocka_unit_test_setup_teardown(torture_path_expand_percent, |
| 1305 |
setup, |
| 1306 |
teardown), |
| 1307 |
cmocka_unit_test(torture_timeout_elapsed), |
| 1308 |
cmocka_unit_test(torture_timeout_update), |
| 1309 |
cmocka_unit_test(torture_ssh_analyze_banner), |
| 1310 |
cmocka_unit_test(torture_ssh_dir_writeable), |
| 1311 |
cmocka_unit_test(torture_ssh_newline_vis), |
| 1312 |
cmocka_unit_test(torture_ssh_mkdirs), |
| 1313 |
cmocka_unit_test(torture_ssh_quote_file_name), |
| 1314 |
cmocka_unit_test(torture_ssh_strreplace), |
| 1315 |
cmocka_unit_test(torture_ssh_strerror), |
| 1316 |
cmocka_unit_test(torture_ssh_readn), |
| 1317 |
cmocka_unit_test(torture_ssh_writen), |
| 1318 |
cmocka_unit_test(torture_ssh_check_hostname_syntax), |
| 1319 |
cmocka_unit_test(torture_ssh_check_username_syntax), |
| 1320 |
cmocka_unit_test(torture_ssh_is_ipaddr), |
| 1321 |
cmocka_unit_test(torture_ssh_get_hexa), |
| 1322 |
}; |
| 1323 |
|
| 1324 |
ssh_init(); |
| 1325 |
torture_filter_tests(tests); |
| 1326 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 1327 |
ssh_finalize(); |
| 1328 |
return rc; |
| 1329 |
} |