| 1 |
/* |
| 2 |
* torture_string.c - torture tests for ssh_string functions |
| 3 |
* |
| 4 |
* This file is part of the SSH Library |
| 5 |
* |
| 6 |
* Copyright (c) 2025 Praneeth Sarode <[email protected]> |
| 7 |
* |
| 8 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 11 |
* option) any later version. |
| 12 |
* |
| 13 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 |
* License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 20 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 21 |
* MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#define LIBSSH_STATIC |
| 30 |
|
| 31 |
#include "libssh/string.h" |
| 32 |
#include "string.c" |
| 33 |
#include "torture.h" |
| 34 |
|
| 35 |
static void torture_ssh_string_new(void **state) |
| 36 |
{ |
| 37 |
struct ssh_string_struct *str = NULL; |
| 38 |
|
| 39 |
(void)state; |
| 40 |
|
| 41 |
/* Test normal allocation */ |
| 42 |
str = ssh_string_new(100); |
| 43 |
assert_non_null(str); |
| 44 |
assert_int_equal(ssh_string_len(str), 100); |
| 45 |
ssh_string_free(str); |
| 46 |
|
| 47 |
/* Test zero size */ |
| 48 |
str = ssh_string_new(0); |
| 49 |
assert_non_null(str); |
| 50 |
assert_int_equal(ssh_string_len(str), 0); |
| 51 |
ssh_string_free(str); |
| 52 |
|
| 53 |
/* Test maximum size */ |
| 54 |
str = ssh_string_new(STRING_SIZE_MAX - 1); |
| 55 |
assert_non_null(str); |
| 56 |
assert_int_equal(ssh_string_len(str), STRING_SIZE_MAX - 1); |
| 57 |
ssh_string_free(str); |
| 58 |
|
| 59 |
/* Test size too large - should fail */ |
| 60 |
str = ssh_string_new(STRING_SIZE_MAX + 1); |
| 61 |
assert_null(str); |
| 62 |
assert_int_equal(errno, EINVAL); |
| 63 |
} |
| 64 |
|
| 65 |
static void torture_ssh_string_from_char(void **state) |
| 66 |
{ |
| 67 |
struct ssh_string_struct *str = NULL; |
| 68 |
const char *test_string = "Hello, World!"; |
| 69 |
const char *empty_string = ""; |
| 70 |
|
| 71 |
(void)state; |
| 72 |
|
| 73 |
/* Test normal string */ |
| 74 |
str = ssh_string_from_char(test_string); |
| 75 |
assert_non_null(str); |
| 76 |
assert_int_equal(ssh_string_len(str), strlen(test_string)); |
| 77 |
assert_memory_equal(ssh_string_data(str), test_string, strlen(test_string)); |
| 78 |
ssh_string_free(str); |
| 79 |
|
| 80 |
/* Test empty string */ |
| 81 |
str = ssh_string_from_char(empty_string); |
| 82 |
assert_non_null(str); |
| 83 |
assert_int_equal(ssh_string_len(str), 0); |
| 84 |
ssh_string_free(str); |
| 85 |
|
| 86 |
/* Test NULL input */ |
| 87 |
str = ssh_string_from_char(NULL); |
| 88 |
assert_null(str); |
| 89 |
assert_int_equal(errno, EINVAL); |
| 90 |
} |
| 91 |
|
| 92 |
static void torture_ssh_string_from_data(void **state) |
| 93 |
{ |
| 94 |
ssh_string s; |
| 95 |
const unsigned char raw[] = {0x00, 0x01, 0x00, 0x42, 0xFF}; |
| 96 |
|
| 97 |
(void)state; |
| 98 |
|
| 99 |
/* Basic: copy arbitrary binary data (with embedded NUL) */ |
| 100 |
s = ssh_string_from_data(raw, sizeof(raw)); |
| 101 |
assert_non_null(s); |
| 102 |
assert_int_equal(ssh_string_len(s), sizeof(raw)); |
| 103 |
assert_memory_equal(ssh_string_data(s), raw, sizeof(raw)); |
| 104 |
ssh_string_free(s); |
| 105 |
|
| 106 |
/* Empty: len == 0 with NULL data returns empty string */ |
| 107 |
s = ssh_string_from_data(NULL, 0); |
| 108 |
assert_non_null(s); |
| 109 |
assert_int_equal(ssh_string_len(s), 0); |
| 110 |
ssh_string_free(s); |
| 111 |
|
| 112 |
/* Invalid: len > 0 with NULL data fails and sets errno */ |
| 113 |
errno = 0; |
| 114 |
s = ssh_string_from_data(NULL, 42); |
| 115 |
assert_null(s); |
| 116 |
assert_int_equal(errno, EINVAL); |
| 117 |
} |
| 118 |
|
| 119 |
static void torture_ssh_string_fill(void **state) |
| 120 |
{ |
| 121 |
struct ssh_string_struct *str = NULL; |
| 122 |
const char *test_data = "Test data"; |
| 123 |
int rc; |
| 124 |
|
| 125 |
(void)state; |
| 126 |
|
| 127 |
/* Test normal fill */ |
| 128 |
str = ssh_string_new(20); |
| 129 |
assert_non_null(str); |
| 130 |
|
| 131 |
rc = ssh_string_fill(str, test_data, strlen(test_data)); |
| 132 |
assert_int_equal(rc, 0); |
| 133 |
assert_memory_equal(ssh_string_data(str), test_data, strlen(test_data)); |
| 134 |
ssh_string_free(str); |
| 135 |
|
| 136 |
/* Test fill with exact size */ |
| 137 |
str = ssh_string_new(strlen(test_data)); |
| 138 |
assert_non_null(str); |
| 139 |
|
| 140 |
rc = ssh_string_fill(str, test_data, strlen(test_data)); |
| 141 |
assert_int_equal(rc, 0); |
| 142 |
ssh_string_free(str); |
| 143 |
|
| 144 |
/* Test NULL data */ |
| 145 |
str = ssh_string_new(10); |
| 146 |
assert_non_null(str); |
| 147 |
|
| 148 |
rc = ssh_string_fill(str, NULL, 5); |
| 149 |
assert_int_equal(rc, -1); |
| 150 |
ssh_string_free(str); |
| 151 |
|
| 152 |
/* Test zero length */ |
| 153 |
str = ssh_string_new(10); |
| 154 |
assert_non_null(str); |
| 155 |
|
| 156 |
rc = ssh_string_fill(str, test_data, 0); |
| 157 |
assert_int_equal(rc, -1); |
| 158 |
ssh_string_free(str); |
| 159 |
} |
| 160 |
|
| 161 |
static void torture_ssh_string_to_char(void **state) |
| 162 |
{ |
| 163 |
struct ssh_string_struct *str = NULL; |
| 164 |
const char *test_string = "Convert to char"; |
| 165 |
char *result = NULL; |
| 166 |
|
| 167 |
(void)state; |
| 168 |
|
| 169 |
/* Test normal string */ |
| 170 |
str = ssh_string_from_char(test_string); |
| 171 |
assert_non_null(str); |
| 172 |
|
| 173 |
result = ssh_string_to_char(str); |
| 174 |
assert_non_null(result); |
| 175 |
assert_string_equal(result, test_string); |
| 176 |
|
| 177 |
ssh_string_free_char(result); |
| 178 |
ssh_string_free(str); |
| 179 |
|
| 180 |
/* Test empty string */ |
| 181 |
str = ssh_string_from_char(""); |
| 182 |
assert_non_null(str); |
| 183 |
|
| 184 |
result = ssh_string_to_char(str); |
| 185 |
assert_non_null(result); |
| 186 |
assert_string_equal(result, ""); |
| 187 |
|
| 188 |
ssh_string_free_char(result); |
| 189 |
ssh_string_free(str); |
| 190 |
|
| 191 |
/* Test NULL string */ |
| 192 |
result = ssh_string_to_char(NULL); |
| 193 |
assert_null(result); |
| 194 |
} |
| 195 |
|
| 196 |
static void torture_ssh_string_copy(void **state) |
| 197 |
{ |
| 198 |
struct ssh_string_struct *str = NULL, *copy = NULL; |
| 199 |
const char *test_string = "Copy me!"; |
| 200 |
|
| 201 |
(void)state; |
| 202 |
|
| 203 |
/* Test normal copy */ |
| 204 |
str = ssh_string_from_char(test_string); |
| 205 |
assert_non_null(str); |
| 206 |
|
| 207 |
copy = ssh_string_copy(str); |
| 208 |
assert_non_null(copy); |
| 209 |
assert_int_equal(ssh_string_len(copy), ssh_string_len(str)); |
| 210 |
assert_memory_equal(ssh_string_data(copy), |
| 211 |
ssh_string_data(str), |
| 212 |
ssh_string_len(str)); |
| 213 |
|
| 214 |
/* Ensure they are different objects */ |
| 215 |
assert_ptr_not_equal(str, copy); |
| 216 |
assert_ptr_not_equal(ssh_string_data(str), ssh_string_data(copy)); |
| 217 |
|
| 218 |
ssh_string_free(str); |
| 219 |
ssh_string_free(copy); |
| 220 |
|
| 221 |
/* Test copy of empty string */ |
| 222 |
str = ssh_string_from_char(""); |
| 223 |
assert_non_null(str); |
| 224 |
|
| 225 |
copy = ssh_string_copy(str); |
| 226 |
assert_non_null(copy); |
| 227 |
assert_int_equal(ssh_string_len(copy), 0); |
| 228 |
|
| 229 |
ssh_string_free(str); |
| 230 |
ssh_string_free(copy); |
| 231 |
|
| 232 |
/* Test NULL string */ |
| 233 |
copy = ssh_string_copy(NULL); |
| 234 |
assert_null(copy); |
| 235 |
} |
| 236 |
|
| 237 |
static void torture_ssh_string_burn(void **state) |
| 238 |
{ |
| 239 |
struct ssh_string_struct *str = NULL; |
| 240 |
const char *test_string = "Secret data"; |
| 241 |
void *data = NULL; |
| 242 |
size_t len; |
| 243 |
int i; |
| 244 |
|
| 245 |
(void)state; |
| 246 |
|
| 247 |
/* Test burning a string */ |
| 248 |
str = ssh_string_from_char(test_string); |
| 249 |
assert_non_null(str); |
| 250 |
|
| 251 |
data = ssh_string_data(str); |
| 252 |
len = ssh_string_len(str); |
| 253 |
|
| 254 |
/* Verify data is there initially */ |
| 255 |
assert_memory_equal(data, test_string, len); |
| 256 |
|
| 257 |
/* Burn the string */ |
| 258 |
ssh_string_burn(str); |
| 259 |
|
| 260 |
/* Verify data is zeroed out */ |
| 261 |
for (i = 0; i < (int)len; i++) { |
| 262 |
assert_int_equal(((unsigned char *)data)[i], 0); |
| 263 |
} |
| 264 |
|
| 265 |
ssh_string_free(str); |
| 266 |
|
| 267 |
/* Test burning NULL string (should not crash) */ |
| 268 |
ssh_string_burn(NULL); |
| 269 |
|
| 270 |
/* Test burning zero-size string */ |
| 271 |
str = ssh_string_new(0); |
| 272 |
assert_non_null(str); |
| 273 |
ssh_string_burn(str); |
| 274 |
ssh_string_free(str); |
| 275 |
} |
| 276 |
|
| 277 |
static void torture_ssh_string_cmp(void **state) |
| 278 |
{ |
| 279 |
struct ssh_string_struct *str1 = NULL, *str2 = NULL; |
| 280 |
const char *test_string1 = "Hello, World!"; |
| 281 |
const char *test_string2 = "Hello, libssh"; |
| 282 |
const char *test_string3 = "Hello"; |
| 283 |
const char *test_string4 = "Apple"; |
| 284 |
|
| 285 |
const char data1[] = "Hello\x00World!"; |
| 286 |
const char data2[] = "Hello\x00libssh"; |
| 287 |
const char data3[] = "Hello"; |
| 288 |
|
| 289 |
int rc; |
| 290 |
(void)state; |
| 291 |
|
| 292 |
/* Test comparing two NULL strings - should be equal */ |
| 293 |
assert_int_equal(ssh_string_cmp(NULL, NULL), 0); |
| 294 |
|
| 295 |
/* Test comparing NULL with non-NULL string - NULL should be less */ |
| 296 |
str1 = ssh_string_from_char(test_string1); |
| 297 |
assert_non_null(str1); |
| 298 |
assert_true(ssh_string_cmp(NULL, str1) < 0); |
| 299 |
assert_true(ssh_string_cmp(str1, NULL) > 0); |
| 300 |
ssh_string_free(str1); |
| 301 |
|
| 302 |
/* Test comparing empty strings */ |
| 303 |
str1 = ssh_string_from_char(""); |
| 304 |
str2 = ssh_string_from_char(""); |
| 305 |
assert_non_null(str1); |
| 306 |
assert_non_null(str2); |
| 307 |
|
| 308 |
/* Both empty strings should be equal */ |
| 309 |
assert_int_equal(ssh_string_cmp(str1, str2), 0); |
| 310 |
ssh_string_free(str1); |
| 311 |
ssh_string_free(str2); |
| 312 |
|
| 313 |
/* Test comparing empty string with non-empty string */ |
| 314 |
str1 = ssh_string_from_char(""); |
| 315 |
str2 = ssh_string_from_char("test"); |
| 316 |
assert_non_null(str1); |
| 317 |
assert_non_null(str2); |
| 318 |
|
| 319 |
/* Empty string should be less than non-empty string */ |
| 320 |
assert_true(ssh_string_cmp(str1, str2) < 0); |
| 321 |
assert_true(ssh_string_cmp(str2, str1) > 0); |
| 322 |
ssh_string_free(str1); |
| 323 |
ssh_string_free(str2); |
| 324 |
|
| 325 |
/* Test comparing strings where one is a prefix of another */ |
| 326 |
str1 = ssh_string_from_char(test_string1); /* "Hello, World!" */ |
| 327 |
str2 = ssh_string_from_char(test_string3); /* "Hello" - prefix */ |
| 328 |
assert_non_null(str1); |
| 329 |
assert_non_null(str2); |
| 330 |
|
| 331 |
/* "Hello" is shorter and a prefix, so it should be < "Hello, World!" */ |
| 332 |
assert_true(ssh_string_cmp(str2, str1) < 0); |
| 333 |
assert_true(ssh_string_cmp(str1, str2) > 0); |
| 334 |
ssh_string_free(str1); |
| 335 |
ssh_string_free(str2); |
| 336 |
|
| 337 |
/* Test comparing different strings with same length */ |
| 338 |
str1 = ssh_string_from_char(test_string1); /* "Hello, World!" */ |
| 339 |
str2 = ssh_string_from_char(test_string2); /* "Hello, libssh" */ |
| 340 |
assert_non_null(str1); |
| 341 |
assert_non_null(str2); |
| 342 |
|
| 343 |
/* "Hello, World!" vs "Hello, libssh" - 'W' < 'l' */ |
| 344 |
assert_true(ssh_string_cmp(str1, str2) < 0); |
| 345 |
assert_true(ssh_string_cmp(str2, str1) > 0); |
| 346 |
ssh_string_free(str1); |
| 347 |
ssh_string_free(str2); |
| 348 |
|
| 349 |
/* Test comparing strings with different lengths and different characters */ |
| 350 |
str1 = ssh_string_from_char(test_string1); /* "Hello, World!" */ |
| 351 |
str2 = ssh_string_from_char(test_string4); /* "Apple" */ |
| 352 |
assert_non_null(str1); |
| 353 |
assert_non_null(str2); |
| 354 |
|
| 355 |
/* 'A' < 'H' so "Apple" < "Hello, World!" */ |
| 356 |
assert_true(ssh_string_cmp(str2, str1) < 0); |
| 357 |
assert_true(ssh_string_cmp(str1, str2) > 0); |
| 358 |
ssh_string_free(str1); |
| 359 |
ssh_string_free(str2); |
| 360 |
|
| 361 |
/* Test comparing identical strings - should be equal */ |
| 362 |
str1 = ssh_string_from_char(test_string1); |
| 363 |
str2 = ssh_string_from_char(test_string1); |
| 364 |
assert_non_null(str1); |
| 365 |
assert_non_null(str2); |
| 366 |
assert_int_equal(ssh_string_cmp(str1, str2), 0); |
| 367 |
assert_int_equal(ssh_string_cmp(str2, str1), 0); |
| 368 |
ssh_string_free(str1); |
| 369 |
ssh_string_free(str2); |
| 370 |
|
| 371 |
/* Test comparing strings with embedded null characters */ |
| 372 |
str1 = ssh_string_new(sizeof(data1)); /* "Hello\x00World!" */ |
| 373 |
str2 = ssh_string_new(sizeof(data3)); /* "Hello" */ |
| 374 |
assert_non_null(str1); |
| 375 |
assert_non_null(str2); |
| 376 |
rc = ssh_string_fill(str1, data1, sizeof(data1)); |
| 377 |
assert_int_equal(rc, 0); |
| 378 |
rc = ssh_string_fill(str2, data3, sizeof(data3)); |
| 379 |
assert_int_equal(rc, 0); |
| 380 |
|
| 381 |
/* "Hello\x00World!" > "Hello" because its length is greater */ |
| 382 |
assert_true(ssh_string_cmp(str1, str2) > 0); /* data1 > data3 */ |
| 383 |
assert_true(ssh_string_cmp(str2, str1) < 0); /* data3 < data1 */ |
| 384 |
ssh_string_free(str1); |
| 385 |
ssh_string_free(str2); |
| 386 |
|
| 387 |
/* Comparing binary strings with same length, but different characters */ |
| 388 |
str1 = ssh_string_new(sizeof(data1)); /* "Hello\x00World!" */ |
| 389 |
str2 = ssh_string_new(sizeof(data2)); /* "Hello\x00libssh" */ |
| 390 |
assert_non_null(str1); |
| 391 |
assert_non_null(str2); |
| 392 |
rc = ssh_string_fill(str1, data1, sizeof(data1)); |
| 393 |
assert_int_equal(rc, 0); |
| 394 |
rc = ssh_string_fill(str2, data2, sizeof(data2)); |
| 395 |
assert_int_equal(rc, 0); |
| 396 |
|
| 397 |
/* 'W' < 'l' so str1 < str2 */ |
| 398 |
assert_true(ssh_string_cmp(str1, str2) < 0); /* data1 < data2 */ |
| 399 |
assert_true(ssh_string_cmp(str2, str1) > 0); /* data2 > data1 */ |
| 400 |
ssh_string_free(str1); |
| 401 |
ssh_string_free(str2); |
| 402 |
} |
| 403 |
|
| 404 |
int torture_run_tests(void) |
| 405 |
{ |
| 406 |
int rc; |
| 407 |
struct CMUnitTest tests[] = { |
| 408 |
cmocka_unit_test(torture_ssh_string_new), |
| 409 |
cmocka_unit_test(torture_ssh_string_from_char), |
| 410 |
cmocka_unit_test(torture_ssh_string_from_data), |
| 411 |
cmocka_unit_test(torture_ssh_string_fill), |
| 412 |
cmocka_unit_test(torture_ssh_string_to_char), |
| 413 |
cmocka_unit_test(torture_ssh_string_copy), |
| 414 |
cmocka_unit_test(torture_ssh_string_burn), |
| 415 |
cmocka_unit_test(torture_ssh_string_cmp), |
| 416 |
}; |
| 417 |
|
| 418 |
ssh_init(); |
| 419 |
torture_filter_tests(tests); |
| 420 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 421 |
ssh_finalize(); |
| 422 |
|
| 423 |
return rc; |
| 424 |
} |