| 1 |
/* Copyright 2002-2015 Paul Hsieh |
| 2 |
* This file is part of Bstrlib. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* 3. Neither the name of bstrlib nor the names of its contributors may be |
| 15 |
* used to endorse or promote products derived from this software |
| 16 |
* without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
* POSSIBILITY OF SUCH DAMAGE. |
| 29 |
* |
| 30 |
* Alternatively, the contents of this file may be used under the terms of |
| 31 |
* GNU General Public License Version 2 (the "GPL"). |
| 32 |
*/ |
| 33 |
|
| 34 |
/* |
| 35 |
* This file is the C unit test for the bstraux module of Bstrlib. |
| 36 |
*/ |
| 37 |
|
| 38 |
#ifdef HAVE_CONFIG_H |
| 39 |
#include <config.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "bstraux.h" |
| 43 |
#include "bstrlib.h" |
| 44 |
#include <check.h> |
| 45 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
| 47 |
|
| 48 |
static int |
| 49 |
tWrite(const void *buf, size_t elsize, size_t nelem, void *parm) |
| 50 |
{ |
| 51 |
bstring b = (bstring) parm; |
| 52 |
size_t i; |
| 53 |
if (NULL == b || NULL == buf || 0 == elsize || 0 == nelem) { |
| 54 |
return -__LINE__; |
| 55 |
} |
| 56 |
for (i = 0; i < nelem; ++i) { |
| 57 |
if (0 > bcatblk(b, buf, elsize)) { |
| 58 |
break; |
| 59 |
} |
| 60 |
buf = (const void *)(elsize + (const char *)buf); |
| 61 |
} |
| 62 |
return (int)i; |
| 63 |
} |
| 64 |
|
| 65 |
START_TEST(core_000) |
| 66 |
{ |
| 67 |
int ret = 0; |
| 68 |
bstring s, t; |
| 69 |
struct bwriteStream *ws; |
| 70 |
s = bfromcstr(""); |
| 71 |
ck_assert(s != NULL); |
| 72 |
ws = bwsOpen((bNwrite)tWrite, s); |
| 73 |
ck_assert(ws != NULL); |
| 74 |
(void)bwsBuffLength(ws, 8); |
| 75 |
ret = bwsBuffLength(ws, 0); |
| 76 |
ck_assert_int_eq(ret, 8); |
| 77 |
ret = bwsWriteBlk(ws, bsStaticBlkParms("Hello ")); |
| 78 |
ck_assert_int_eq(ret, BSTR_OK); |
| 79 |
ret = biseqcstr(s, ""); |
| 80 |
ck_assert_int_eq(ret, 1); |
| 81 |
ret = bwsWriteBlk(ws, bsStaticBlkParms("World\n")); |
| 82 |
ck_assert_int_eq(ret, BSTR_OK); |
| 83 |
ret = biseqcstr(s, "Hello Wo"); |
| 84 |
ck_assert_int_eq(ret, 1); |
| 85 |
t = bwsClose(ws); |
| 86 |
ck_assert(t == s); |
| 87 |
ret = biseqcstr(s, "Hello World\n"); |
| 88 |
ck_assert_int_eq(ret, 1); |
| 89 |
ret = bdestroy(s); |
| 90 |
ck_assert_int_eq(ret, BSTR_OK); |
| 91 |
} |
| 92 |
END_TEST |
| 93 |
|
| 94 |
START_TEST(core_001) |
| 95 |
{ |
| 96 |
struct tagbstring t = bsStatic("Hello world"); |
| 97 |
bstring b, c, d; |
| 98 |
int ret = 0; |
| 99 |
b = bTail(&t, 5); |
| 100 |
ck_assert(b != NULL); |
| 101 |
c = bHead(&t, 5); |
| 102 |
ck_assert(c != NULL); |
| 103 |
ret = biseqcstr(b, "world"); |
| 104 |
ck_assert_int_eq(ret, 1); |
| 105 |
ret = biseqcstr(c, "Hello"); |
| 106 |
ck_assert_int_eq(ret, 1); |
| 107 |
ret = bdestroy(b); |
| 108 |
ck_assert_int_eq(ret, BSTR_OK); |
| 109 |
ret = bdestroy(c); |
| 110 |
ck_assert_int_eq(ret, BSTR_OK); |
| 111 |
b = bTail(&t, 0); |
| 112 |
ck_assert(b != NULL); |
| 113 |
c = bHead(&t, 0); |
| 114 |
ck_assert(c != NULL); |
| 115 |
ret = biseqcstr(b, ""); |
| 116 |
ck_assert_int_eq(ret, 1); |
| 117 |
ret = biseqcstr(c, ""); |
| 118 |
ck_assert_int_eq(ret, 1); |
| 119 |
ret = bdestroy(b); |
| 120 |
ck_assert_int_eq(ret, BSTR_OK); |
| 121 |
ret = bdestroy(c); |
| 122 |
ck_assert_int_eq(ret, BSTR_OK); |
| 123 |
d = bstrcpy(&t); |
| 124 |
ck_assert(d != NULL); |
| 125 |
b = bTail(d, 5); |
| 126 |
ck_assert(b != NULL); |
| 127 |
c = bHead(d, 5); |
| 128 |
ck_assert(c != NULL); |
| 129 |
ret = biseqcstr(b, "world"); |
| 130 |
ck_assert_int_eq(ret, 1); |
| 131 |
ret = biseqcstr(c, "Hello"); |
| 132 |
ck_assert_int_eq(ret, 1); |
| 133 |
ret = bdestroy(b); |
| 134 |
ck_assert_int_eq(ret, BSTR_OK); |
| 135 |
ret = bdestroy(c); |
| 136 |
ck_assert_int_eq(ret, BSTR_OK); |
| 137 |
ret = bdestroy(d); |
| 138 |
ck_assert_int_eq(ret, BSTR_OK); |
| 139 |
} |
| 140 |
END_TEST |
| 141 |
|
| 142 |
START_TEST(core_002) |
| 143 |
{ |
| 144 |
struct tagbstring t = bsStatic("Hello world"); |
| 145 |
int ret = 0; |
| 146 |
bstring b; |
| 147 |
ret = bSetChar(&t, 4, ','); |
| 148 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 149 |
ret = bSetChar(b = bstrcpy(&t), 4, ','); |
| 150 |
ck_assert_int_eq(ret, 0); |
| 151 |
ret = biseqcstr(b, "Hell, world"); |
| 152 |
ck_assert_int_eq(ret, 1); |
| 153 |
ret = bSetChar(b, -1, 'x'); |
| 154 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 155 |
b->slen = 2; |
| 156 |
ret = bSetChar(b, 1, 'i'); |
| 157 |
ck_assert_int_eq(ret, 0); |
| 158 |
ret = biseqcstr(b, "Hi"); |
| 159 |
ck_assert_int_eq(ret, 1); |
| 160 |
ret = bSetChar(b, 2, 's'); |
| 161 |
ck_assert_int_eq(ret, 0); |
| 162 |
ret = biseqcstr(b, "His"); |
| 163 |
ck_assert_int_eq(ret, 1); |
| 164 |
ret = bSetChar(b, 1, '\0'); |
| 165 |
ck_assert_int_eq(ret, 0); |
| 166 |
ret = blength(b); |
| 167 |
ck_assert_int_eq(ret, 3); |
| 168 |
ret = bchare(b, 0, '?'); |
| 169 |
ck_assert_int_eq(ret, 'H'); |
| 170 |
ret = bchare(b, 1, '?'); |
| 171 |
ck_assert_int_eq(ret, '\0'); |
| 172 |
ret = bchare(b, 2, '?'); |
| 173 |
ck_assert_int_eq(ret, 's'); |
| 174 |
ret = bdestroy(b); |
| 175 |
ck_assert_int_eq(ret, BSTR_OK); |
| 176 |
ret = 0; |
| 177 |
ret = bSetCstrChar(&t, 4, ','); |
| 178 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 179 |
b = bstrcpy(&t); |
| 180 |
ck_assert(b != NULL); |
| 181 |
ret = bSetCstrChar(b, 4, ','); |
| 182 |
ck_assert_int_eq(ret, 0); |
| 183 |
ret = biseqcstr(b, "Hell, world"); |
| 184 |
ck_assert_int_eq(ret, 1); |
| 185 |
ret = bSetCstrChar(b, -1, 'x'); |
| 186 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 187 |
b->slen = 2; |
| 188 |
ret = bSetCstrChar(b, 1, 'i'); |
| 189 |
ck_assert_int_eq(ret, 0); |
| 190 |
ret = biseqcstr(b, "Hi"); |
| 191 |
ck_assert_int_eq(ret, 1); |
| 192 |
ret = bSetCstrChar(b, 2, 's'); |
| 193 |
ck_assert_int_eq(ret, 0); |
| 194 |
ret = biseqcstr(b, "His"); |
| 195 |
ck_assert_int_eq(ret, 1); |
| 196 |
ret = bSetCstrChar(b, 1, '\0'); |
| 197 |
ck_assert_int_eq(ret, 0); |
| 198 |
ret = blength(b); |
| 199 |
ck_assert_int_eq(ret, 1); |
| 200 |
ret = bchare(b, 0, '?'); |
| 201 |
ck_assert_int_eq(ret, 'H'); |
| 202 |
ret = bdestroy(b); |
| 203 |
ck_assert_int_eq(ret, BSTR_OK); |
| 204 |
} |
| 205 |
END_TEST |
| 206 |
|
| 207 |
START_TEST(core_003) |
| 208 |
{ |
| 209 |
struct tagbstring t = bsStatic("Hello world"); |
| 210 |
bstring b; |
| 211 |
int ret = 0; |
| 212 |
ret = bFill(&t, 'x', 7); |
| 213 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 214 |
b = bstrcpy(&t); |
| 215 |
ck_assert(b != NULL); |
| 216 |
ret = bFill(b, 'x', 7); |
| 217 |
ck_assert_int_eq(ret, 0); |
| 218 |
ret = biseqcstr(b, "xxxxxxx"); |
| 219 |
ck_assert_int_eq(ret, 1); |
| 220 |
ret = bFill(b, 'x', -1); |
| 221 |
ck_assert(ret < 0); |
| 222 |
ret = bFill(b, 'x', 0); |
| 223 |
ck_assert_int_eq(ret, 0); |
| 224 |
ret = biseqcstr(b, ""); |
| 225 |
ck_assert_int_eq(ret, 1); |
| 226 |
ret = bdestroy(b); |
| 227 |
ck_assert_int_eq(ret, BSTR_OK); |
| 228 |
} |
| 229 |
END_TEST |
| 230 |
|
| 231 |
START_TEST(core_004) |
| 232 |
{ |
| 233 |
struct tagbstring t = bsStatic("foo"); |
| 234 |
int ret = 0; |
| 235 |
bstring b; |
| 236 |
ret = bReplicate(&t, 4); |
| 237 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 238 |
b = bstrcpy(&t); |
| 239 |
ck_assert(b != NULL); |
| 240 |
ret = bReplicate(b, -1); |
| 241 |
ck_assert_int_eq(ret, BSTR_ERR); |
| 242 |
ret = bReplicate(b, 4); |
| 243 |
ck_assert_int_eq(ret, 0); |
| 244 |
ret = biseqcstr(b, "foofoofoofoo"); |
| 245 |
ck_assert_int_eq(ret, 1); |
| 246 |
ret = bReplicate(b, 0); |
| 247 |
ck_assert_int_eq(ret, 0); |
| 248 |
ret = biseqcstr(b, ""); |
| 249 |
ck_assert_int_eq(ret, 1); |
| 250 |
ret = bdestroy(b); |
| 251 |
ck_assert_int_eq(ret, BSTR_OK); |
| 252 |
} |
| 253 |
END_TEST |
| 254 |
|
| 255 |
START_TEST(core_005) |
| 256 |
{ |
| 257 |
struct tagbstring t = bsStatic("Hello world"); |
| 258 |
int ret = 0; |
| 259 |
bstring b; |
| 260 |
ret = bReverse(&t); |
| 261 |
ck_assert(ret < 0); |
| 262 |
b = bstrcpy(&t); |
| 263 |
ck_assert(b != NULL); |
| 264 |
ret = bReverse(b); |
| 265 |
ck_assert_int_eq(ret, 0); |
| 266 |
ret = biseqcstr(b, "dlrow olleH"); |
| 267 |
ck_assert_int_eq(ret, 1); |
| 268 |
b->slen = 0; |
| 269 |
ret = bReverse(b); |
| 270 |
ck_assert_int_eq(ret, 0); |
| 271 |
ret = biseqcstr(b, ""); |
| 272 |
ck_assert_int_eq(ret, 1); |
| 273 |
ret = bdestroy(b); |
| 274 |
ck_assert_int_eq(ret, BSTR_OK); |
| 275 |
} |
| 276 |
END_TEST |
| 277 |
|
| 278 |
START_TEST(core_006) |
| 279 |
{ |
| 280 |
struct tagbstring t = bsStatic("Hello world"); |
| 281 |
int ret = 0; |
| 282 |
bstring b; |
| 283 |
ret = bInsertChrs(&t, 6, 4, 'x', '?'); |
| 284 |
ck_assert(ret < 0); |
| 285 |
b = bstrcpy(&t); |
| 286 |
ck_assert(b != NULL); |
| 287 |
ret = bInsertChrs(b, 6, 4, 'x', '?'); |
| 288 |
ck_assert_int_eq(ret, 0); |
| 289 |
ret = biseqcstr(b, "Hello xxxxworld"); |
| 290 |
ck_assert_int_eq(ret, 1); |
| 291 |
ret = bdestroy(b); |
| 292 |
ck_assert_int_eq(ret, BSTR_OK); |
| 293 |
} |
| 294 |
END_TEST |
| 295 |
|
| 296 |
START_TEST(core_007) |
| 297 |
{ |
| 298 |
struct tagbstring t = bsStatic(" i am "); |
| 299 |
int ret = 0; |
| 300 |
bstring b; |
| 301 |
ret = bJustifyLeft(&t, ' '); |
| 302 |
ck_assert(ret < 0); |
| 303 |
ret = bJustifyRight(&t, 8, ' '); |
| 304 |
ck_assert(ret < 0); |
| 305 |
ret = bJustifyMargin(&t, 8, ' '); |
| 306 |
ck_assert(ret < 0); |
| 307 |
ret = bJustifyCenter(&t, 8, ' '); |
| 308 |
ck_assert(ret < 0); |
| 309 |
b = bstrcpy(&t); |
| 310 |
ck_assert(b != NULL); |
| 311 |
ret = bJustifyLeft(b, ' '); |
| 312 |
ck_assert_int_eq(ret, BSTR_OK); |
| 313 |
ret = biseqcstr(b, "i am"); |
| 314 |
ck_assert_int_eq(ret, 1); |
| 315 |
ret = bJustifyRight(b, 8, ' '); |
| 316 |
ck_assert_int_eq(ret, BSTR_OK); |
| 317 |
ret = biseqcstr(b, " i am"); |
| 318 |
ck_assert_int_eq(ret, 1); |
| 319 |
ret = bJustifyMargin(b, 8, ' '); |
| 320 |
ck_assert_int_eq(ret, BSTR_OK); |
| 321 |
ret = biseqcstr(b, "i am"); |
| 322 |
ck_assert_int_eq(ret, 1); |
| 323 |
ret = bJustifyCenter(b, 8, ' '); |
| 324 |
ck_assert_int_eq(ret, BSTR_OK); |
| 325 |
ret = biseqcstr(b, " i am"); |
| 326 |
ck_assert_int_eq(ret, 1); |
| 327 |
ret = bdestroy(b); |
| 328 |
ck_assert_int_eq(ret, BSTR_OK); |
| 329 |
} |
| 330 |
END_TEST |
| 331 |
|
| 332 |
START_TEST(core_008) |
| 333 |
{ |
| 334 |
struct tagbstring t = bsStatic("Hello world"); |
| 335 |
int ret = 0; |
| 336 |
bstring b; |
| 337 |
char *c; |
| 338 |
c = bStr2NetStr(&t); |
| 339 |
ck_assert(c != NULL); |
| 340 |
ret = strcmp(c, "11:Hello world,"); |
| 341 |
ck_assert_int_eq(ret, 0); |
| 342 |
b = bNetStr2Bstr(c); |
| 343 |
ck_assert(b != NULL); |
| 344 |
ret = biseq(b, &t); |
| 345 |
ck_assert_int_eq(ret, 1); |
| 346 |
ret = bdestroy(b); |
| 347 |
ck_assert_int_eq(ret, BSTR_OK); |
| 348 |
ret = bcstrfree(c); |
| 349 |
ck_assert_int_eq(ret, BSTR_OK); |
| 350 |
} |
| 351 |
END_TEST |
| 352 |
|
| 353 |
START_TEST(core_009) |
| 354 |
{ |
| 355 |
struct tagbstring t = bsStatic("Hello world"); |
| 356 |
int err, ret = 0; |
| 357 |
bstring b, c; |
| 358 |
b = bBase64Encode(&t); |
| 359 |
ck_assert(b != NULL); |
| 360 |
ret += 0 >= biseqcstr(b, "SGVsbG8gd29ybGQ="); |
| 361 |
c = bBase64DecodeEx(b, &err); |
| 362 |
ck_assert(b != NULL); |
| 363 |
ck_assert_int_eq(err, 0); |
| 364 |
ret += 0 >= biseq(c, &t); |
| 365 |
ck_assert_int_eq(ret, 0); |
| 366 |
ret = bdestroy(b); |
| 367 |
ck_assert_int_eq(ret, BSTR_OK); |
| 368 |
ret = bdestroy(c); |
| 369 |
ck_assert_int_eq(ret, BSTR_OK); |
| 370 |
} |
| 371 |
END_TEST |
| 372 |
|
| 373 |
START_TEST(core_010) |
| 374 |
{ |
| 375 |
struct tagbstring t = bsStatic("Hello world"); |
| 376 |
int err, ret = 0; |
| 377 |
bstring b, c; |
| 378 |
b = bUuEncode(&t); |
| 379 |
ck_assert(b != NULL); |
| 380 |
ret = biseqcstr(b, "+2&5L;&\\@=V]R;&0`\r\n"); |
| 381 |
ck_assert_int_eq(ret, 1); |
| 382 |
c = bUuDecodeEx(b, &err); |
| 383 |
ck_assert(c != NULL); |
| 384 |
ck_assert_int_eq(err, 0); |
| 385 |
ret = biseq(c, &t); |
| 386 |
ck_assert_int_eq(ret, 1); |
| 387 |
ret = bdestroy(b); |
| 388 |
ck_assert_int_eq(ret, BSTR_OK); |
| 389 |
ret = bdestroy(c); |
| 390 |
ck_assert_int_eq(ret, BSTR_OK); |
| 391 |
} |
| 392 |
END_TEST |
| 393 |
|
| 394 |
START_TEST(core_011) |
| 395 |
{ |
| 396 |
struct tagbstring t = bsStatic("Hello world"); |
| 397 |
unsigned char Ytstr[] = { |
| 398 |
0x72, 0x8f, 0x96, 0x96, 0x99, 0x4a, |
| 399 |
0xa1, 0x99, 0x9c, 0x96, 0x8e |
| 400 |
}; |
| 401 |
bstring b, c; |
| 402 |
int ret = 0; |
| 403 |
b = bYEncode(&t); |
| 404 |
ck_assert(b != NULL); |
| 405 |
ck_assert_int_eq(ret, 0); |
| 406 |
ret = bisstemeqblk(b, Ytstr, 11); |
| 407 |
ck_assert_int_eq(ret, 1); |
| 408 |
c = bYDecode(b); |
| 409 |
ck_assert(c != NULL); |
| 410 |
ret = biseq(c, &t); |
| 411 |
ck_assert_int_eq(ret, 1); |
| 412 |
ret = bdestroy(b); |
| 413 |
ck_assert_int_eq(ret, BSTR_OK); |
| 414 |
ret = bdestroy(c); |
| 415 |
ck_assert_int_eq(ret, BSTR_OK); |
| 416 |
} |
| 417 |
END_TEST |
| 418 |
|
| 419 |
START_TEST(core_012) |
| 420 |
{ |
| 421 |
struct tagbstring t = bsStatic("Hello world"); |
| 422 |
struct bStream * s; |
| 423 |
int ret = 0; |
| 424 |
bstring b, c; |
| 425 |
s = bsFromBstr(&t); |
| 426 |
ck_assert(s != NULL); |
| 427 |
b = bfromcstr(""); |
| 428 |
ck_assert(b != NULL); |
| 429 |
ret = bsread(b, s, 6); |
| 430 |
ck_assert_int_eq(ret, 0); |
| 431 |
ret = biseqcstr(b, "Hello "); |
| 432 |
ck_assert_int_eq(ret, 1); |
| 433 |
if (b) { |
| 434 |
b->slen = 0; |
| 435 |
} |
| 436 |
ret = bsread(b, s, 6); |
| 437 |
ck_assert_int_eq(ret, 0); |
| 438 |
ret = biseqcstr(b, "world"); |
| 439 |
ck_assert_int_eq(ret, 1); |
| 440 |
c = bsclose(s); |
| 441 |
ck_assert(c == NULL); |
| 442 |
ret = bdestroy(b); |
| 443 |
ck_assert_int_eq(ret, BSTR_OK); |
| 444 |
} |
| 445 |
END_TEST |
| 446 |
|
| 447 |
struct vfgetc { |
| 448 |
int ofs; |
| 449 |
bstring base; |
| 450 |
}; |
| 451 |
|
| 452 |
static int |
| 453 |
core13_fgetc(void *ctx) |
| 454 |
{ |
| 455 |
struct vfgetc * vctx = (struct vfgetc *) ctx; |
| 456 |
int c; |
| 457 |
if (NULL == vctx || NULL == vctx->base) { |
| 458 |
return EOF; |
| 459 |
} |
| 460 |
if (vctx->ofs >= blength (vctx->base)) { |
| 461 |
return EOF; |
| 462 |
} |
| 463 |
c = bchare(vctx->base, vctx->ofs, EOF); |
| 464 |
vctx->ofs++; |
| 465 |
return c; |
| 466 |
} |
| 467 |
|
| 468 |
START_TEST(core_013) |
| 469 |
{ |
| 470 |
struct tagbstring t0 = bsStatic("Random String, long enough to cause to reallocing"); |
| 471 |
struct vfgetc vctx; |
| 472 |
bstring b; |
| 473 |
int ret = 0; |
| 474 |
int i; |
| 475 |
for (i = 0; i < 1000; i++) { |
| 476 |
vctx.ofs = 0; |
| 477 |
vctx.base = &t0; |
| 478 |
b = bSecureInput(INT_MAX, '\n', (bNgetc)core13_fgetc, &vctx); |
| 479 |
ret = biseq(b, &t0); |
| 480 |
ck_assert_int_eq(ret, 1); |
| 481 |
bSecureDestroy(b); |
| 482 |
} |
| 483 |
} |
| 484 |
END_TEST |
| 485 |
|
| 486 |
START_TEST(core_014) |
| 487 |
{ |
| 488 |
bstring b; |
| 489 |
|
| 490 |
/* All four entities: &, ", <, > */ |
| 491 |
b = bfromcstr("<\"Hello, you, me, & world\">"); |
| 492 |
ck_assert_int_eq(bSGMLEncode(b), BSTR_OK); |
| 493 |
ck_assert_int_eq(biseqcstr(b, "<"Hello, you, me, & world">"), 1); |
| 494 |
bdestroy(b); |
| 495 |
|
| 496 |
/* No special characters — string should be unchanged */ |
| 497 |
b = bfromcstr("Hello, world"); |
| 498 |
ck_assert_int_eq(bSGMLEncode(b), BSTR_OK); |
| 499 |
ck_assert_int_eq(biseqcstr(b, "Hello, world"), 1); |
| 500 |
bdestroy(b); |
| 501 |
|
| 502 |
/* Empty string */ |
| 503 |
b = bfromcstr(""); |
| 504 |
ck_assert_int_eq(bSGMLEncode(b), BSTR_OK); |
| 505 |
ck_assert_int_eq(biseqcstr(b, ""), 1); |
| 506 |
bdestroy(b); |
| 507 |
|
| 508 |
/* NULL input */ |
| 509 |
ck_assert_int_eq(bSGMLEncode(NULL), BSTR_ERR); |
| 510 |
} |
| 511 |
END_TEST |
| 512 |
|
| 513 |
START_TEST(core_015) |
| 514 |
{ |
| 515 |
bstring b; |
| 516 |
|
| 517 |
/* |
| 518 |
* Craft a buffer where: |
| 519 |
* - The netstring claims length 5 ("5:abc"), but the actual content |
| 520 |
* after ':' is only "abc" (3 bytes, null-terminated at index 5). |
| 521 |
* - A ',' is planted at buff[7], which is exactly buff[i+1+x] |
| 522 |
* (i=1, x=5) — the position the un-fixed code reads without bounds |
| 523 |
* checking. |
| 524 |
* |
| 525 |
* Without the fix: buff[7]=',' passes the terminator check and the |
| 526 |
* function returns a non-NULL bstring containing out-of-bounds data. |
| 527 |
* With the fix: the bounds check catches i+1+x (7) >= blen (5) first |
| 528 |
* and returns NULL. |
| 529 |
*/ |
| 530 |
char crafted[16] = "5:abc"; |
| 531 |
crafted[7] = ','; |
| 532 |
b = bNetStr2Bstr(crafted); |
| 533 |
ck_assert(b == NULL); |
| 534 |
} |
| 535 |
END_TEST |
| 536 |
|
| 537 |
START_TEST(core_016) |
| 538 |
{ |
| 539 |
/* Empty input: bUuDecodeEx must return an empty bstring (not NULL) |
| 540 |
* and must not leak the internal decode context. Before the fix, |
| 541 |
* bsread was called with INT_MAX on empty input, immediately returned |
| 542 |
* BSTR_ERR, and the function took the error path — returning NULL and |
| 543 |
* leaking ctx, ctx->io.dst and ctx->io.src. */ |
| 544 |
struct tagbstring empty = bsStatic(""); |
| 545 |
int err = 0; |
| 546 |
bstring c = bUuDecodeEx(&empty, &err); |
| 547 |
ck_assert_ptr_nonnull(c); |
| 548 |
ck_assert_int_eq(c->slen, 0); |
| 549 |
bdestroy(c); |
| 550 |
} |
| 551 |
END_TEST |
| 552 |
|
| 553 |
int |
| 554 |
main(void) |
| 555 |
{ |
| 556 |
/* Build test suite */ |
| 557 |
Suite *suite = suite_create("bstr-aux"); |
| 558 |
/* Core tests */ |
| 559 |
TCase *core = tcase_create("Core"); |
| 560 |
tcase_add_test(core, core_000); |
| 561 |
tcase_add_test(core, core_001); |
| 562 |
tcase_add_test(core, core_002); |
| 563 |
tcase_add_test(core, core_003); |
| 564 |
tcase_add_test(core, core_004); |
| 565 |
tcase_add_test(core, core_005); |
| 566 |
tcase_add_test(core, core_006); |
| 567 |
tcase_add_test(core, core_007); |
| 568 |
tcase_add_test(core, core_008); |
| 569 |
tcase_add_test(core, core_009); |
| 570 |
tcase_add_test(core, core_010); |
| 571 |
tcase_add_test(core, core_011); |
| 572 |
tcase_add_test(core, core_012); |
| 573 |
tcase_add_test(core, core_013); |
| 574 |
tcase_add_test(core, core_014); |
| 575 |
tcase_add_test(core, core_015); |
| 576 |
tcase_add_test(core, core_016); |
| 577 |
suite_add_tcase(suite, core); |
| 578 |
/* Run tests */ |
| 579 |
SRunner *runner = srunner_create(suite); |
| 580 |
srunner_run_all(runner, CK_ENV); |
| 581 |
int number_failed = srunner_ntests_failed(runner); |
| 582 |
srunner_free(runner); |
| 583 |
return (0 == number_failed) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 584 |
} |