1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "IoTest.h" 17 #include <string.h> 18 19 using namespace testing::ext; 20 21 /** 22 * @tc.number SUB_KERNEL_IO_STRING_0100 23 * @tc.name strverscmp basic function test 24 * @tc.desc [C- SOFTWARE -0200] 25 */ 26 HWTEST_F(IoTest, testStrverscmp, Function | MediumTest | Level1) 27 { 28 int ret = strverscmp("hello world !", "HELLO WORLD"); 29 EXPECT_GT(ret, 0); 30 31 ret = strverscmp("HELLO WORLD", "hello world !"); 32 EXPECT_LT(ret, 0); 33 34 ret = strverscmp("hello world !", "hello world !"); 35 EXPECT_EQ(ret, 0); 36 } 37 38 /** 39 * @tc.number SUB_KERNEL_IO_STRING_0200 40 * @tc.name strcoll basic function test 41 * @tc.desc [C- SOFTWARE -0200] 42 */ 43 HWTEST_F(IoTest, testStrcoll, Function | MediumTest | Level1) 44 { 45 int ret = strcoll("AbC", "ABC"); 46 EXPECT_GT(ret, 0); 47 48 ret = strcoll("ABC", "abc"); 49 EXPECT_LT(ret, 0); 50 51 ret = strcoll("ABC", "ABC"); 52 EXPECT_EQ(ret, 0); 53 } 54 55 /** 56 * @tc.number SUB_KERNEL_IO_STRING_0300 57 * @tc.name strcmp basic function test 58 * @tc.desc [C- SOFTWARE -0200] 59 */ 60 HWTEST_F(IoTest, testStrcmp, Function | MediumTest | Level1) 61 { 62 int ret = strcmp("abcdef", "ABCDEF"); 63 EXPECT_GT(ret, 0); 64 65 ret = strcmp("123456", "654321"); 66 EXPECT_LT(ret, 0); 67 68 EXPECT_EQ(strcmp("~!@#$%^&*()_+", "~!@#$%^&*()_+"), 0); 69 } 70 71 /** 72 * @tc.number SUB_KERNEL_IO_STRING_0400 73 * @tc.name strncmp basic function test 74 * @tc.desc [C- SOFTWARE -0200] 75 */ 76 HWTEST_F(IoTest, testStrncmp, Function | MediumTest | Level1) 77 { 78 const char dest[] = "abcdef"; 79 const char src[] = "ABCDEF"; 80 int ret = strncmp(dest, src, 1); 81 EXPECT_GT(ret, 0); 82 83 ret = strncmp("123456", "654321", 6); 84 EXPECT_LT(ret, 0); 85 86 ret = strncmp("~!@#$%^&*()_+", "~!@#$%^&*()_+", 8); 87 EXPECT_EQ(ret, 0); 88 } 89 90 /** 91 * @tc.number SUB_KERNEL_IO_STRING_0500 92 * @tc.name strxfrm basic function test 93 * @tc.desc [C- SOFTWARE -0200] 94 */ 95 HWTEST_F(IoTest, testStrxfrm, Function | MediumTest | Level1) 96 { 97 char dest[50] = {0}; 98 size_t ret = strxfrm(dest, "hello world !", sizeof(dest)); 99 EXPECT_EQ(ret, 13U); 100 EXPECT_STREQ(dest, "hello world !"); 101 102 char destT[15] = {0}; 103 ret = strxfrm(destT, "this is string", sizeof(destT)); 104 EXPECT_EQ(ret, 14UL); 105 EXPECT_STREQ(destT, "this is string"); 106 } 107 108 /** 109 * @tc.number SUB_KERNEL_IO_STRING_0600 110 * @tc.name strsignal basic function test 111 * @tc.desc [C- SOFTWARE -0200] 112 */ 113 HWTEST_F(IoTest, testStrsignal, Function | MediumTest | Level1) 114 { 115 char *ret = strsignal(14); 116 EXPECT_STREQ(ret, "Alarm clock"); 117 118 ret = strsignal(-1); 119 EXPECT_STREQ(ret, "Unknown signal"); 120 } 121 122 /** 123 * @tc.number SUB_KERNEL_IO_STRING_0700 124 * @tc.name stpcpy basic function test 125 * @tc.desc [C- SOFTWARE -0200] 126 */ 127 HWTEST_F(IoTest, testStpcpy, Function | MediumTest | Level1) 128 { 129 const char *src = "hello world !"; 130 char dest[50] = {0}; 131 char *ret = stpcpy(dest, src); 132 ASSERT_NE(ret, nullptr); 133 EXPECT_STREQ(dest, src); 134 135 const char *srcT = "~!@#$%%^&*()_+"; 136 char destT[50] = {0}; 137 ret = stpcpy(destT, srcT); 138 ASSERT_NE(ret, nullptr); 139 EXPECT_STREQ(destT, "~!@#$%%^&*()_+"); 140 } 141 142 /** 143 * @tc.number SUB_KERNEL_IO_STRING_0800 144 * @tc.name stpncpy basic function test 145 * @tc.desc [C- SOFTWARE -0200] 146 */ 147 HWTEST_F(IoTest, testStpncpy, Function | MediumTest | Level1) 148 { 149 char src[] = "hello world !"; 150 char dest[50] = {0}; 151 char *ret = stpncpy(dest, src, 5); 152 EXPECT_STREQ(ret, ""); 153 EXPECT_STREQ(dest, "hello"); 154 155 char srcT[] = "~!@#$%%^&*()_+"; 156 char destT[50] = {0}; 157 ret = stpncpy(destT, srcT, 15); 158 EXPECT_STREQ(ret, ""); 159 EXPECT_STREQ(destT, "~!@#$%%^&*()_+"); 160 } 161 162 /** 163 * @tc.number SUB_KERNEL_IO_STRING_0900 164 * @tc.name strlcpy basic function test 165 * @tc.desc [C- SOFTWARE -0200] 166 */ 167 HWTEST_F(IoTest, testStrlcpy, Function | MediumTest | Level1) 168 { 169 char dest[50] = {0}; 170 const char *src = "123456789abc"; 171 size_t ret = strlcpy(dest, src, sizeof(dest)); 172 EXPECT_EQ(ret, 12U); 173 EXPECT_STREQ(dest, src); 174 175 char destT[20] = {0}; 176 const char *srcT = " ~!@#$%^&*()_+"; 177 ret = strlcpy(destT, srcT, 15); 178 EXPECT_EQ(ret, 14U); 179 EXPECT_STREQ(destT, srcT); 180 } 181 182 /** 183 * @tc.number SUB_KERNEL_IO_STRING_1000 184 * @tc.name strcpy basic function test 185 * @tc.desc [C- SOFTWARE -0200] 186 */ 187 HWTEST_F(IoTest, testStrcpy, Function | MediumTest | Level1) 188 { 189 char src[] = "hello world !"; 190 char dest[20] = {0}; 191 char *ret = strcpy(dest, src); 192 EXPECT_STREQ(ret, dest); 193 194 char srcT[] = "This is String1"; 195 char destT[20] = {0}; 196 ret = strcpy(destT, srcT); 197 EXPECT_STREQ(ret, destT); 198 } 199 200 /** 201 * @tc.number SUB_KERNEL_IO_STRING_1100 202 * @tc.name strncpy basic function test 203 * @tc.desc [C- SOFTWARE -0200] 204 */ 205 HWTEST_F(IoTest, testStrncpy, Function | MediumTest | Level1) 206 { 207 char src[] = "hello world !"; 208 char dest[] = {0}; 209 char *ret = strncpy(dest, src, 6); 210 EXPECT_STREQ(ret, dest); 211 212 char destT[] = "~!@#$%^&*()_+"; 213 ret = strncpy(destT, "hello world", 0); 214 EXPECT_STREQ(ret, destT); 215 } 216 /** 217 * @tc.number SUB_KERNEL_IO_STRING_1200 218 * @tc.name strcasestr basic function test 219 * @tc.desc [C- SOFTWARE -0200] 220 */ 221 HWTEST_F(IoTest, testStrcasestr, Function | MediumTest | Level1) 222 { 223 char *ret = strcasestr("hello world !", " "); 224 EXPECT_STREQ(ret, " world !"); 225 226 ret = strcasestr("This is String1", "Str"); 227 EXPECT_STREQ(ret, "String1"); 228 229 ret = strcasestr("hello world !", "ABC"); 230 EXPECT_STREQ(ret, nullptr); 231 } 232 233 /** 234 * @tc.number SUB_KERNEL_IO_STRING_1300 235 * @tc.name strlcat basic function test 236 * @tc.desc [C- SOFTWARE -0200] 237 */ 238 HWTEST_F(IoTest, testStrlcat, Function | MediumTest | Level1) 239 { 240 char src[] = "ABCDE"; 241 char dest[] = "abcdefgh"; 242 size_t ret = strlcat(dest, src, 8); 243 EXPECT_EQ(ret, 13U); 244 EXPECT_STREQ(dest, "abcdefgh"); 245 246 char srcT[] = "|/*"; 247 char destT[20] = "~!@#$%^&*()_+"; 248 ret = strlcat(destT, srcT, 11); 249 EXPECT_EQ(ret, 14U); 250 EXPECT_STREQ(destT, "~!@#$%^&*()_+"); 251 } 252 253 /** 254 * @tc.number SUB_KERNEL_IO_STRING_1400 255 * @tc.name strcat basic function test 256 * @tc.desc [C- SOFTWARE -0200] 257 */ 258 HWTEST_F(IoTest, testStrcat, Function | MediumTest | Level1) 259 { 260 char dest[50] = {0}; 261 char *ret = strcat(dest, "hello world !"); 262 EXPECT_STREQ(ret, dest); 263 264 char destT[50] = "hello world ! || "; 265 ret = strcat(destT, "This is String1"); 266 EXPECT_STREQ(ret, destT); 267 } 268 269 /** 270 * @tc.number SUB_KERNEL_IO_STRING_1500 271 * @tc.name strncat basic function test 272 * @tc.desc [C- SOFTWARE -0200] 273 */ 274 HWTEST_F(IoTest, testStrncat, Function | MediumTest | Level1) 275 { 276 char dest[50] = "hello world || "; 277 char *ret = strncat(dest, "This is String1", 7); 278 EXPECT_STREQ(ret, "hello world || This is"); 279 280 char destT[20] = "|/*"; 281 ret = strncat(destT, "~!@#$%^&*()_+", 13); 282 EXPECT_STREQ(ret, destT); 283 } 284 285 /** 286 * @tc.number SUB_KERNEL_IO_STRING_1600 287 * @tc.name strchr basic function test 288 * @tc.desc [C- SOFTWARE -0200] 289 */ 290 HWTEST_F(IoTest, testStrchr, Function | MediumTest | Level1) 291 { 292 char src[] = "hello !! world"; 293 char *ret = strchr(src, '!'); 294 EXPECT_STREQ(ret, "!! world"); 295 296 ret = strchr(src, '?'); 297 EXPECT_STREQ(ret, nullptr); 298 } 299 300 /** 301 * @tc.number SUB_KERNEL_IO_STRING_1700 302 * @tc.name strrchr basic function test 303 * @tc.desc [C- SOFTWARE -0200] 304 */ 305 HWTEST_F(IoTest, testStrrchr, Function | MediumTest | Level1) 306 { 307 char src[] = "hello world & HEELO & WORLD"; 308 char *ret = strrchr(src, '&'); 309 EXPECT_STREQ(ret, "& WORLD"); 310 311 ret = strrchr(src, '?'); 312 EXPECT_STREQ(ret, nullptr); 313 } 314 315 /** 316 * @tc.number SUB_KERNEL_IO_STRING_1800 317 * @tc.name strnlen basic function test 318 * @tc.desc [C- SOFTWARE -0200] 319 */ 320 HWTEST_F(IoTest, testStrnlen, Function | MediumTest | Level1) 321 { 322 char src[] = "hello world !"; 323 unsigned int ltSrcSize = strlen(src) - 2; 324 unsigned int gtSrcSize = strlen(src) + 2; 325 326 size_t ret = strnlen(src, ltSrcSize); 327 EXPECT_EQ(ret, ltSrcSize); 328 329 ret = strnlen(src, gtSrcSize); 330 EXPECT_EQ(ret, strlen(src)); 331 } 332 333 /** 334 * @tc.number SUB_KERNEL_IO_STRING_1900 335 * @tc.name strtok basic function test 336 * @tc.desc [C- SOFTWARE -0200] 337 */ 338 HWTEST_F(IoTest, testStrtok, Function | MediumTest | Level1) 339 { 340 char src[] = "hello world & This is-String1"; 341 char *ret = strtok(src, "&"); 342 EXPECT_STREQ(ret, src); 343 344 ret = strtok(nullptr, "-"); 345 EXPECT_STREQ(ret, " This is"); 346 347 char srcS[] = "hello world !"; 348 ret = strtok(srcS, "?"); 349 EXPECT_STREQ(ret, srcS); 350 351 char srcT[50] = {0}; 352 ret = strtok(srcT, "~"); 353 EXPECT_STREQ(ret, nullptr); 354 } 355 356 /** 357 * @tc.number SUB_KERNEL_IO_STRING_2000 358 * @tc.name strtok_r basic function test 359 * @tc.desc [C- SOFTWARE -0200] 360 */ 361 HWTEST_F(IoTest, testStrtok_r, Function | MediumTest | Level1) 362 { 363 char str[] = "hello world & This is-String1"; 364 char *endPtr = nullptr; 365 char *ret = strtok_r(str, "&", &endPtr); 366 EXPECT_STREQ(ret, str); 367 EXPECT_STREQ(endPtr, " This is-String1"); 368 369 ret = strtok_r(nullptr, "-", &endPtr); 370 EXPECT_STREQ(ret, " This is"); 371 EXPECT_STREQ(endPtr, "String1"); 372 373 char strS[] = "hello world !"; 374 ret = strtok_r(strS, "?", &endPtr); 375 EXPECT_STREQ(ret, strS); 376 EXPECT_STREQ(endPtr, nullptr); 377 378 char strT[] = {0}; 379 ret = strtok_r(strT, "!", &endPtr); 380 EXPECT_STREQ(ret, nullptr); 381 EXPECT_STREQ(endPtr, nullptr); 382 } 383 384 /** 385 * @tc.number SUB_KERNEL_IO_STRING_2100 386 * @tc.name strsep basic function test 387 * @tc.desc [C- SOFTWARE -0200] 388 */ 389 HWTEST_F(IoTest, testStrsep, Function | MediumTest | Level1) 390 { 391 char src[] = "123===ABC"; 392 char *str = src; 393 char *ret = strsep(&str, "="); 394 EXPECT_STREQ(ret, "123"); 395 396 char srcS[] = "hello world !"; 397 char *strS = srcS; 398 ret = strsep(&strS, "?"); 399 EXPECT_STREQ(ret, "hello world !"); 400 EXPECT_EQ(strS, nullptr); 401 402 char srcT[] = {0}; 403 char *strT = srcT; 404 ret = strsep(&strT, "?"); 405 EXPECT_STREQ(ret, ""); 406 } 407 408 /** 409 * @tc.number SUB_KERNEL_IO_STRING_2200 410 * @tc.name strdup basic function test 411 * @tc.desc [C- SOFTWARE -0200] 412 */ 413 HWTEST_F(IoTest, testStrdup, Function | MediumTest | Level1) 414 { 415 char src[] = "hello world !"; 416 char *ret = strdup(src); 417 EXPECT_STREQ(ret, src) << "> strdup fail, errno = " << errno; 418 419 char srcS[] = "This is String1"; 420 ret = strdup(srcS); 421 EXPECT_STREQ(ret, "This is String1") << "> strdup fail, errno = " << errno; 422 free(ret); 423 } 424 425 /** 426 * @tc.number SUB_KERNEL_IO_STRING_2300 427 * @tc.name strndup basic function test 428 * @tc.desc [C- SOFTWARE -0200] 429 */ 430 HWTEST_F(IoTest, testStrndup, Function | MediumTest | Level1) 431 { 432 char src[] = "hello world !"; 433 char *ret = strndup(src, 5); 434 EXPECT_STREQ(ret, "hello") << "> strndup fail, errno = " << errno; 435 436 char srcS[] = "This is String1"; 437 ret = strndup(srcS, 20); 438 EXPECT_STREQ(ret, "This is String1") << "> strndup fail, errno = " << errno; 439 free(ret); 440 } 441 442 /** 443 * @tc.number SUB_KERNEL_IO_STRING_2400 444 * @tc.name strpbrk basic function test 445 * @tc.desc [C- SOFTWARE -0200] 446 */ 447 HWTEST_F(IoTest, testStrpbrk, Function | MediumTest | Level1) 448 { 449 const char dest[] = "hello !! world"; 450 const char src[] = "!!"; 451 const char *ret = strpbrk(dest, src); 452 EXPECT_STREQ(ret, "!! world"); 453 454 const char srcS[] = "??"; 455 ret = strpbrk(dest, srcS); 456 EXPECT_STREQ(ret, nullptr); 457 } 458 459 /** 460 * @tc.number SUB_KERNEL_IO_STRING_2500 461 * @tc.name strcspn basic function test 462 * @tc.desc [C- SOFTWARE -0200] 463 */ 464 HWTEST_F(IoTest, testStrcspn, Function | MediumTest | Level1) 465 { 466 const char dest[] = "hello world !"; 467 const char src[] = "!"; 468 size_t ret = strcspn(dest, src); 469 EXPECT_EQ(ret, 12U); 470 471 const char srcS[] = "a"; 472 ret = strcspn(dest, srcS); 473 EXPECT_EQ(ret, 13U); 474 } 475 476 /** 477 * @tc.number SUB_KERNEL_IO_STRING_2600 478 * @tc.name strspn basic function test 479 * @tc.desc [C- SOFTWARE -0200] 480 */ 481 HWTEST_F(IoTest, testStrspn, Function | MediumTest | Level1) 482 { 483 const char dest[] = "hello world !"; 484 const char src[] = "heAlo"; 485 size_t ret = strspn(dest, src); 486 EXPECT_EQ(ret, 5U); 487 EXPECT_EQ(ret, 5U); 488 489 const char destS[] = "this is string"; 490 const char srcS[] = "abc"; 491 ret = strspn(destS, srcS); 492 EXPECT_EQ(ret, 0U); 493 494 const char srcT[] = "helo\0 wal"; 495 ret = strspn(dest, srcT); 496 EXPECT_EQ(ret, 5U); 497 } 498 499 /** 500 * @tc.number SUB_KERNEL_IO_STRING_2700 501 * @tc.name strstr basic function test 502 * @tc.desc [C- SOFTWARE -0200] 503 */ 504 HWTEST_F(IoTest, testStrstr, Function | MediumTest | Level1) 505 { 506 const char dest[] = "hello world !"; 507 const char src[] = "heAlo"; 508 const char *ret = strstr(dest, src); 509 EXPECT_STREQ(ret, nullptr); 510 511 const char destS[] = "string this is string"; 512 const char srcS[] = "string"; 513 ret = strstr(destS, srcS); 514 EXPECT_STREQ(ret, "string this is string"); 515 516 const char srcT[] = "\0hello"; 517 ret = strstr(dest, srcT); 518 EXPECT_STREQ(ret, "hello world !"); 519 } 520 521 /** 522 * @tc.number SUB_KERNEL_IO_STRING_2800 523 * @tc.name strcoll_l basic function testj 524 * @tc.desc [C- SOFTWARE -0200] 525 */ 526 HWTEST_F(IoTest, testStrcoll_l, Function | MediumTest | Level1) 527 { 528 locale_t locale = nullptr; 529 int ret = strcoll_l("AbC", "ABC", locale); 530 EXPECT_GT(ret, 0); 531 532 ret = strcoll_l("ABC", "abc", locale); 533 EXPECT_LT(ret, 0); 534 535 ret = strcoll_l("ABC", "ABC", locale); 536 EXPECT_EQ(ret, 0); 537 } 538 539 /** 540 * @tc.number SUB_KERNEL_IO_STRING_2900 541 * @tc.name strxfrm_l basic function test 542 * @tc.desc [C- SOFTWARE -0200] 543 */ 544 HWTEST_F(IoTest, testStrxfrm_l, Function | MediumTest | Level1) 545 { 546 char dest[50] = {0}; 547 locale_t locale = nullptr; 548 size_t ret = strxfrm_l(dest, "hello world !", sizeof(dest), locale); 549 EXPECT_EQ(ret, 13UL); 550 EXPECT_STREQ(dest, "hello world !"); 551 552 char destT[15] = {0}; 553 ret = strxfrm_l(destT, "this is string", sizeof(destT), locale); 554 EXPECT_EQ(ret, 14UL); 555 EXPECT_STREQ(destT, "this is string"); 556 }