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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* torture_string.c - torture tests for ssh_string functions
*
* This file is part of the SSH Library
*
* Copyright (c) 2025 Praneeth Sarode <praneethsarode@gmail.com>
*
* The SSH Library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* The SSH Library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the SSH Library; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#define LIBSSH_STATIC
#include "libssh/string.h"
#include "string.c"
#include "torture.h"
static void torture_ssh_string_new(void **state)
{
struct ssh_string_struct *str = NULL;
(void)state;
/* Test normal allocation */
str = ssh_string_new(100);
assert_non_null(str);
assert_int_equal(ssh_string_len(str), 100);
ssh_string_free(str);
/* Test zero size */
str = ssh_string_new(0);
assert_non_null(str);
assert_int_equal(ssh_string_len(str), 0);
ssh_string_free(str);
/* Test maximum size */
str = ssh_string_new(STRING_SIZE_MAX - 1);
assert_non_null(str);
assert_int_equal(ssh_string_len(str), STRING_SIZE_MAX - 1);
ssh_string_free(str);
/* Test size too large - should fail */
str = ssh_string_new(STRING_SIZE_MAX + 1);
assert_null(str);
assert_int_equal(errno, EINVAL);
}
static void torture_ssh_string_from_char(void **state)
{
struct ssh_string_struct *str = NULL;
const char *test_string = "Hello, World!";
const char *empty_string = "";
(void)state;
/* Test normal string */
str = ssh_string_from_char(test_string);
assert_non_null(str);
assert_int_equal(ssh_string_len(str), strlen(test_string));
assert_memory_equal(ssh_string_data(str), test_string, strlen(test_string));
ssh_string_free(str);
/* Test empty string */
str = ssh_string_from_char(empty_string);
assert_non_null(str);
assert_int_equal(ssh_string_len(str), 0);
ssh_string_free(str);
/* Test NULL input */
str = ssh_string_from_char(NULL);
assert_null(str);
assert_int_equal(errno, EINVAL);
}
static void torture_ssh_string_from_data(void **state)
{
ssh_string s;
const unsigned char raw[] = {0x00, 0x01, 0x00, 0x42, 0xFF};
(void)state;
/* Basic: copy arbitrary binary data (with embedded NUL) */
s = ssh_string_from_data(raw, sizeof(raw));
assert_non_null(s);
assert_int_equal(ssh_string_len(s), sizeof(raw));
assert_memory_equal(ssh_string_data(s), raw, sizeof(raw));
ssh_string_free(s);
/* Empty: len == 0 with NULL data returns empty string */
s = ssh_string_from_data(NULL, 0);
assert_non_null(s);
assert_int_equal(ssh_string_len(s), 0);
ssh_string_free(s);
/* Invalid: len > 0 with NULL data fails and sets errno */
errno = 0;
s = ssh_string_from_data(NULL, 42);
assert_null(s);
assert_int_equal(errno, EINVAL);
}
static void torture_ssh_string_fill(void **state)
{
struct ssh_string_struct *str = NULL;
const char *test_data = "Test data";
int rc;
(void)state;
/* Test normal fill */
str = ssh_string_new(20);
assert_non_null(str);
rc = ssh_string_fill(str, test_data, strlen(test_data));
assert_int_equal(rc, 0);
assert_memory_equal(ssh_string_data(str), test_data, strlen(test_data));
ssh_string_free(str);
/* Test fill with exact size */
str = ssh_string_new(strlen(test_data));
assert_non_null(str);
rc = ssh_string_fill(str, test_data, strlen(test_data));
assert_int_equal(rc, 0);
ssh_string_free(str);
/* Test NULL data */
str = ssh_string_new(10);
assert_non_null(str);
rc = ssh_string_fill(str, NULL, 5);
assert_int_equal(rc, -1);
ssh_string_free(str);
/* Test zero length */
str = ssh_string_new(10);
assert_non_null(str);
rc = ssh_string_fill(str, test_data, 0);
assert_int_equal(rc, -1);
ssh_string_free(str);
}
static void torture_ssh_string_to_char(void **state)
{
struct ssh_string_struct *str = NULL;
const char *test_string = "Convert to char";
char *result = NULL;
(void)state;
/* Test normal string */
str = ssh_string_from_char(test_string);
assert_non_null(str);
result = ssh_string_to_char(str);
assert_non_null(result);
assert_string_equal(result, test_string);
ssh_string_free_char(result);
ssh_string_free(str);
/* Test empty string */
str = ssh_string_from_char("");
assert_non_null(str);
result = ssh_string_to_char(str);
assert_non_null(result);
assert_string_equal(result, "");
ssh_string_free_char(result);
ssh_string_free(str);
/* Test NULL string */
result = ssh_string_to_char(NULL);
assert_null(result);
}
static void torture_ssh_string_copy(void **state)
{
struct ssh_string_struct *str = NULL, *copy = NULL;
const char *test_string = "Copy me!";
(void)state;
/* Test normal copy */
str = ssh_string_from_char(test_string);
assert_non_null(str);
copy = ssh_string_copy(str);
assert_non_null(copy);
assert_int_equal(ssh_string_len(copy), ssh_string_len(str));
assert_memory_equal(ssh_string_data(copy),
ssh_string_data(str),
ssh_string_len(str));
/* Ensure they are different objects */
assert_ptr_not_equal(str, copy);
assert_ptr_not_equal(ssh_string_data(str), ssh_string_data(copy));
ssh_string_free(str);
ssh_string_free(copy);
/* Test copy of empty string */
str = ssh_string_from_char("");
assert_non_null(str);
copy = ssh_string_copy(str);
assert_non_null(copy);
assert_int_equal(ssh_string_len(copy), 0);
ssh_string_free(str);
ssh_string_free(copy);
/* Test NULL string */
copy = ssh_string_copy(NULL);
assert_null(copy);
}
static void torture_ssh_string_burn(void **state)
{
struct ssh_string_struct *str = NULL;
const char *test_string = "Secret data";
void *data = NULL;
size_t len;
int i;
(void)state;
/* Test burning a string */
str = ssh_string_from_char(test_string);
assert_non_null(str);
data = ssh_string_data(str);
len = ssh_string_len(str);
/* Verify data is there initially */
assert_memory_equal(data, test_string, len);
/* Burn the string */
ssh_string_burn(str);
/* Verify data is zeroed out */
for (i = 0; i < (int)len; i++) {
assert_int_equal(((unsigned char *)data)[i], 0);
}
ssh_string_free(str);
/* Test burning NULL string (should not crash) */
ssh_string_burn(NULL);
/* Test burning zero-size string */
str = ssh_string_new(0);
assert_non_null(str);
ssh_string_burn(str);
ssh_string_free(str);
}
static void torture_ssh_string_cmp(void **state)
{
struct ssh_string_struct *str1 = NULL, *str2 = NULL;
const char *test_string1 = "Hello, World!";
const char *test_string2 = "Hello, libssh";
const char *test_string3 = "Hello";
const char *test_string4 = "Apple";
const char data1[] = "Hello\x00World!";
const char data2[] = "Hello\x00libssh";
const char data3[] = "Hello";
int rc;
(void)state;
/* Test comparing two NULL strings - should be equal */
assert_int_equal(ssh_string_cmp(NULL, NULL), 0);
/* Test comparing NULL with non-NULL string - NULL should be less */
str1 = ssh_string_from_char(test_string1);
assert_non_null(str1);
assert_true(ssh_string_cmp(NULL, str1) < 0);
assert_true(ssh_string_cmp(str1, NULL) > 0);
ssh_string_free(str1);
/* Test comparing empty strings */
str1 = ssh_string_from_char("");
str2 = ssh_string_from_char("");
assert_non_null(str1);
assert_non_null(str2);
/* Both empty strings should be equal */
assert_int_equal(ssh_string_cmp(str1, str2), 0);
ssh_string_free(str1);
ssh_string_free(str2);
/* Test comparing empty string with non-empty string */
str1 = ssh_string_from_char("");
str2 = ssh_string_from_char("test");
assert_non_null(str1);
assert_non_null(str2);
/* Empty string should be less than non-empty string */
assert_true(ssh_string_cmp(str1, str2) < 0);
assert_true(ssh_string_cmp(str2, str1) > 0);
ssh_string_free(str1);
ssh_string_free(str2);
/* Test comparing strings where one is a prefix of another */
str1 = ssh_string_from_char(test_string1); /* "Hello, World!" */
str2 = ssh_string_from_char(test_string3); /* "Hello" - prefix */
assert_non_null(str1);
assert_non_null(str2);
/* "Hello" is shorter and a prefix, so it should be < "Hello, World!" */
assert_true(ssh_string_cmp(str2, str1) < 0);
assert_true(ssh_string_cmp(str1, str2) > 0);
ssh_string_free(str1);
ssh_string_free(str2);
/* Test comparing different strings with same length */
str1 = ssh_string_from_char(test_string1); /* "Hello, World!" */
str2 = ssh_string_from_char(test_string2); /* "Hello, libssh" */
assert_non_null(str1);
assert_non_null(str2);
/* "Hello, World!" vs "Hello, libssh" - 'W' < 'l' */
assert_true(ssh_string_cmp(str1, str2) < 0);
assert_true(ssh_string_cmp(str2, str1) > 0);
ssh_string_free(str1);
ssh_string_free(str2);
/* Test comparing strings with different lengths and different characters */
str1 = ssh_string_from_char(test_string1); /* "Hello, World!" */
str2 = ssh_string_from_char(test_string4); /* "Apple" */
assert_non_null(str1);
assert_non_null(str2);
/* 'A' < 'H' so "Apple" < "Hello, World!" */
assert_true(ssh_string_cmp(str2, str1) < 0);
assert_true(ssh_string_cmp(str1, str2) > 0);
ssh_string_free(str1);
ssh_string_free(str2);
/* Test comparing identical strings - should be equal */
str1 = ssh_string_from_char(test_string1);
str2 = ssh_string_from_char(test_string1);
assert_non_null(str1);
assert_non_null(str2);
assert_int_equal(ssh_string_cmp(str1, str2), 0);
assert_int_equal(ssh_string_cmp(str2, str1), 0);
ssh_string_free(str1);
ssh_string_free(str2);
/* Test comparing strings with embedded null characters */
str1 = ssh_string_new(sizeof(data1)); /* "Hello\x00World!" */
str2 = ssh_string_new(sizeof(data3)); /* "Hello" */
assert_non_null(str1);
assert_non_null(str2);
rc = ssh_string_fill(str1, data1, sizeof(data1));
assert_int_equal(rc, 0);
rc = ssh_string_fill(str2, data3, sizeof(data3));
assert_int_equal(rc, 0);
/* "Hello\x00World!" > "Hello" because its length is greater */
assert_true(ssh_string_cmp(str1, str2) > 0); /* data1 > data3 */
assert_true(ssh_string_cmp(str2, str1) < 0); /* data3 < data1 */
ssh_string_free(str1);
ssh_string_free(str2);
/* Comparing binary strings with same length, but different characters */
str1 = ssh_string_new(sizeof(data1)); /* "Hello\x00World!" */
str2 = ssh_string_new(sizeof(data2)); /* "Hello\x00libssh" */
assert_non_null(str1);
assert_non_null(str2);
rc = ssh_string_fill(str1, data1, sizeof(data1));
assert_int_equal(rc, 0);
rc = ssh_string_fill(str2, data2, sizeof(data2));
assert_int_equal(rc, 0);
/* 'W' < 'l' so str1 < str2 */
assert_true(ssh_string_cmp(str1, str2) < 0); /* data1 < data2 */
assert_true(ssh_string_cmp(str2, str1) > 0); /* data2 > data1 */
ssh_string_free(str1);
ssh_string_free(str2);
}
int torture_run_tests(void)
{
int rc;
struct CMUnitTest tests[] = {
cmocka_unit_test(torture_ssh_string_new),
cmocka_unit_test(torture_ssh_string_from_char),
cmocka_unit_test(torture_ssh_string_from_data),
cmocka_unit_test(torture_ssh_string_fill),
cmocka_unit_test(torture_ssh_string_to_char),
cmocka_unit_test(torture_ssh_string_copy),
cmocka_unit_test(torture_ssh_string_burn),
cmocka_unit_test(torture_ssh_string_cmp),
};
ssh_init();
torture_filter_tests(tests);
rc = cmocka_run_group_tests(tests, NULL, NULL);
ssh_finalize();
return rc;
}
|