| 1 | /* Copyright (c) 2006, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include "unittest/mytap/tap.h" |
| 26 | |
| 27 | #include "my_config.h" |
| 28 | |
| 29 | #include <signal.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "my_stacktrace.h" |
| 36 | #ifdef HAVE_UNISTD_H |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
| 40 | /* |
| 41 | Visual Studio 2003 does not know vsnprintf but knows _vsnprintf. |
| 42 | We don't put this #define elsewhere because we prefer my_vsnprintf |
| 43 | everywhere instead, except when linking with libmysys is not |
| 44 | desirable - the case here. |
| 45 | */ |
| 46 | #if defined(_MSC_VER) && (_MSC_VER == 1310) |
| 47 | #define vsnprintf _vsnprintf |
| 48 | #endif |
| 49 | |
| 50 | [[noreturn]] static void handle_core_signal(int signo); |
| 51 | static void vemit_tap(int pass, char const *fmt, va_list ap) |
| 52 | MY_ATTRIBUTE((format(printf, 2, 0))); |
| 53 | |
| 54 | /** |
| 55 | @defgroup MyTAP_Internal MyTAP Internals |
| 56 | |
| 57 | Internal functions and data structures for the MyTAP implementation. |
| 58 | */ |
| 59 | |
| 60 | /** |
| 61 | Test data structure. |
| 62 | |
| 63 | Data structure containing all information about the test suite. |
| 64 | |
| 65 | @ingroup MyTAP_Internal |
| 66 | */ |
| 67 | static TEST_DATA g_test = {NO_PLAN, 0, 0, ""}; |
| 68 | |
| 69 | /** |
| 70 | Output stream for test report message. |
| 71 | |
| 72 | The macro is just a temporary solution. |
| 73 | |
| 74 | @ingroup MyTAP_Internal |
| 75 | */ |
| 76 | #define tapout stdout |
| 77 | |
| 78 | /** |
| 79 | Emit the beginning of a test line, that is: "(not) ok", test number, |
| 80 | and description. |
| 81 | |
| 82 | To emit the directive, use the emit_dir() function |
| 83 | |
| 84 | @ingroup MyTAP_Internal |
| 85 | |
| 86 | @see emit_dir |
| 87 | |
| 88 | @param pass 'true' if test passed, 'false' otherwise |
| 89 | @param fmt Description of test in printf() format. |
| 90 | @param ap Vararg list for the description string above. |
| 91 | */ |
| 92 | static void vemit_tap(int pass, char const *fmt, va_list ap) { |
| 93 | fprintf(tapout, "%sok %d%s", pass ? "" : "not ", ++g_test.last, |
| 94 | (fmt && *fmt) ? " - " : ""); |
| 95 | if (fmt && *fmt) vfprintf(tapout, fmt, ap); |
| 96 | fflush(tapout); |
| 97 | } |
| 98 | |
| 99 | static void vemit_tap1(int pass) { |
| 100 | fprintf(tapout, "%sok %d%s", pass ? "" : "not ", ++g_test.last, ""); |
| 101 | fflush(tapout); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | Emit a TAP directive. |
| 106 | |
| 107 | TAP directives are comments after that have the form: |
| 108 | |
| 109 | @code |
| 110 | ok 1 # skip reason for skipping |
| 111 | not ok 2 # todo some text explaining what remains |
| 112 | @endcode |
| 113 | |
| 114 | @ingroup MyTAP_Internal |
| 115 | |
| 116 | @param dir Directive as a string |
| 117 | @param why Explanation string |
| 118 | */ |
| 119 | static void emit_dir(const char *dir, const char *why) { |
| 120 | fprintf(tapout, " # %s %s", dir, why); |
| 121 | fflush(tapout); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | Emit a newline to the TAP output stream. |
| 126 | |
| 127 | @ingroup MyTAP_Internal |
| 128 | */ |
| 129 | static void emit_endl() { |
| 130 | fprintf(tapout, "\n"); |
| 131 | fflush(tapout); |
| 132 | } |
| 133 | |
| 134 | static void handle_core_signal(int signo) { |
| 135 | /* BAIL_OUT("Signal %d thrown", signo); */ |
| 136 | #ifdef HAVE_STACKTRACE |
| 137 | fprintf(stderr, "Signal %d thrown, attempting backtrace.\n", signo); |
| 138 | my_print_stacktrace(nullptr, 0); |
| 139 | #endif |
| 140 | signal(signo, SIG_DFL); |
| 141 | raise(signo); |
| 142 | _exit(EXIT_FAILURE); |
| 143 | } |
| 144 | |
| 145 | void BAIL_OUT(char const *fmt, ...) { |
| 146 | va_list ap; |
| 147 | va_start(ap, fmt); |
| 148 | fprintf(tapout, "Bail out! "); |
| 149 | vfprintf(tapout, fmt, ap); |
| 150 | emit_endl(); |
| 151 | va_end(ap); |
| 152 | exit(255); |
| 153 | } |
| 154 | |
| 155 | void diag(char const *fmt, ...) { |
| 156 | va_list ap; |
| 157 | va_start(ap, fmt); |
| 158 | fprintf(tapout, "# "); |
| 159 | vfprintf(tapout, fmt, ap); |
| 160 | emit_endl(); |
| 161 | va_end(ap); |
| 162 | } |
| 163 | |
| 164 | typedef struct signal_entry { |
| 165 | int signo; |
| 166 | void (*handler)(int); |
| 167 | } signal_entry; |
| 168 | |
| 169 | static signal_entry install_signal[] = { |
| 170 | #ifdef _WIN32 |
| 171 | {SIGTERM, handle_core_signal}, |
| 172 | #else |
| 173 | {SIGQUIT, handle_core_signal}, |
| 174 | #endif |
| 175 | {SIGILL, handle_core_signal}, |
| 176 | {SIGABRT, handle_core_signal}, |
| 177 | {SIGFPE, handle_core_signal}, |
| 178 | {SIGSEGV, handle_core_signal} |
| 179 | #ifdef SIGBUS |
| 180 | , |
| 181 | {SIGBUS, handle_core_signal} |
| 182 | #endif |
| 183 | #ifdef SIGXCPU |
| 184 | , |
| 185 | {SIGXCPU, handle_core_signal} |
| 186 | #endif |
| 187 | #ifdef SIGXCPU |
| 188 | , |
| 189 | {SIGXFSZ, handle_core_signal} |
| 190 | #endif |
| 191 | #ifdef SIGXCPU |
| 192 | , |
| 193 | {SIGSYS, handle_core_signal} |
| 194 | #endif |
| 195 | #ifdef SIGXCPU |
| 196 | , |
| 197 | {SIGTRAP, handle_core_signal} |
| 198 | #endif |
| 199 | }; |
| 200 | |
| 201 | int skip_big_tests = 1; |
| 202 | |
| 203 | void plan(int const count) { |
| 204 | char *config = getenv("MYTAP_CONFIG"); |
| 205 | size_t i; |
| 206 | |
| 207 | if (config) skip_big_tests = strcmp(config, "big"); |
| 208 | |
| 209 | /* |
| 210 | Install signal handler |
| 211 | */ |
| 212 | |
| 213 | for (i = 0; i < sizeof(install_signal) / sizeof(*install_signal); ++i) |
| 214 | signal(install_signal[i].signo, install_signal[i].handler); |
| 215 | |
| 216 | g_test.plan = count; |
| 217 | switch (count) { |
| 218 | case NO_PLAN: |
| 219 | break; |
| 220 | default: |
| 221 | if (count > 0) { |
| 222 | fprintf(tapout, "1..%d\n", count); |
| 223 | fflush(tapout); |
| 224 | } |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void skip_all(char const *reason, ...) { |
| 230 | va_list ap; |
| 231 | va_start(ap, reason); |
| 232 | fprintf(tapout, "1..0 # skip "); |
| 233 | vfprintf(tapout, reason, ap); |
| 234 | fflush(tapout); |
| 235 | va_end(ap); |
| 236 | exit(0); |
| 237 | } |
| 238 | |
| 239 | void ok(int const pass, char const *fmt, ...) { |
| 240 | va_list ap; |
| 241 | va_start(ap, fmt); |
| 242 | |
| 243 | if (!pass && *g_test.todo == '\0') ++g_test.failed; |
| 244 | |
| 245 | vemit_tap(pass, fmt, ap); |
| 246 | va_end(ap); |
| 247 | if (*g_test.todo != '\0') emit_dir("todo", g_test.todo); |
| 248 | emit_endl(); |
| 249 | } |
| 250 | |
| 251 | void ok1(int const pass) { |
| 252 | va_list ap; |
| 253 | |
| 254 | memset(&ap, 0, sizeof(ap)); |
| 255 | |
| 256 | if (!pass && *g_test.todo == '\0') ++g_test.failed; |
| 257 | |
| 258 | vemit_tap1(pass); |
| 259 | |
| 260 | if (*g_test.todo != '\0') emit_dir("todo", g_test.todo); |
| 261 | |
| 262 | emit_endl(); |
| 263 | } |
| 264 | |
| 265 | void skip(int how_many, char const *fmt, ...) { |
| 266 | char reason[80]; |
| 267 | if (fmt && *fmt) { |
| 268 | va_list ap; |
| 269 | va_start(ap, fmt); |
| 270 | vsnprintf(reason, sizeof(reason), fmt, ap); |
| 271 | va_end(ap); |
| 272 | } else |
| 273 | reason[0] = '\0'; |
| 274 | |
| 275 | while (how_many-- > 0) { |
| 276 | va_list ap; |
| 277 | memset((char *)&ap, 0, sizeof(ap)); /* Keep compiler happy */ |
| 278 | vemit_tap1(1); |
| 279 | emit_dir("skip", reason); |
| 280 | emit_endl(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void todo_start(char const *message, ...) { |
| 285 | va_list ap; |
| 286 | va_start(ap, message); |
| 287 | vsnprintf(g_test.todo, sizeof(g_test.todo), message, ap); |
| 288 | va_end(ap); |
| 289 | } |
| 290 | |
| 291 | void todo_end() { *g_test.todo = '\0'; } |
| 292 | |
| 293 | int exit_status() { |
| 294 | /* |
| 295 | If there were no plan, we write one last instead. |
| 296 | */ |
| 297 | if (g_test.plan == NO_PLAN) plan(g_test.last); |
| 298 | |
| 299 | if (g_test.plan != g_test.last) { |
| 300 | diag("%d tests planned but%s %d executed", g_test.plan, |
| 301 | (g_test.plan > g_test.last ? " only" : ""), g_test.last); |
| 302 | return EXIT_FAILURE; |
| 303 | } |
| 304 | |
| 305 | if (g_test.failed > 0) { |
| 306 | diag("Failed %d tests!", g_test.failed); |
| 307 | return EXIT_FAILURE; |
| 308 | } |
| 309 | |
| 310 | return EXIT_SUCCESS; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | @mainpage Testing C and C++ using MyTAP |
| 315 | |
| 316 | @section IntroSec Introduction |
| 317 | |
| 318 | Unit tests are used to test individual components of a system. In |
| 319 | contrast, functional tests usually test the entire system. The |
| 320 | rationale is that each component should be correct if the system is |
| 321 | to be correct. Unit tests are usually small pieces of code that |
| 322 | tests an individual function, class, a module, or other unit of the |
| 323 | code. |
| 324 | |
| 325 | Observe that a correctly functioning system can be built from |
| 326 | "faulty" components. The problem with this approach is that as the |
| 327 | system evolves, the bugs surface in unexpected ways, making |
| 328 | maintenance harder. |
| 329 | |
| 330 | The advantages of using unit tests to test components of the system |
| 331 | are several: |
| 332 | |
| 333 | - The unit tests can make a more thorough testing than the |
| 334 | functional tests by testing correctness even for pathological use |
| 335 | (which shouldn't be present in the system). This increases the |
| 336 | overall robustness of the system and makes maintenance easier. |
| 337 | |
| 338 | - It is easier and faster to find problems with a malfunctioning |
| 339 | component than to find problems in a malfunctioning system. This |
| 340 | shortens the compile-run-edit cycle and therefore improves the |
| 341 | overall performance of development. |
| 342 | |
| 343 | - The component has to support at least two uses: in the system and |
| 344 | in a unit test. This leads to more generic and stable interfaces |
| 345 | and in addition promotes the development of reusable components. |
| 346 | |
| 347 | For example, the following are typical functional tests: |
| 348 | - Does transactions work according to specifications? |
| 349 | - Can we connect a client to the server and execute statements? |
| 350 | |
| 351 | In contrast, the following are typical unit tests: |
| 352 | |
| 353 | - Can the 'String' class handle a specified list of character sets? |
| 354 | - Does all operations for 'my_bitmap' produce the correct result? |
| 355 | - Does all the NIST test vectors for the AES implementation encrypt |
| 356 | correctly? |
| 357 | |
| 358 | |
| 359 | @section UnitTest Writing unit tests |
| 360 | |
| 361 | The purpose of writing unit tests is to use them to drive component |
| 362 | development towards a solution that passes the tests. This means that the |
| 363 | unit tests has to be as complete as possible, testing at least: |
| 364 | |
| 365 | - Normal input |
| 366 | - Borderline cases |
| 367 | - Faulty input |
| 368 | - Error handling |
| 369 | - Bad environment |
| 370 | |
| 371 | @subsection NormalSubSec Normal input |
| 372 | |
| 373 | This is to test that the component have the expected behaviour. |
| 374 | This is just plain simple: test that it works. For example, test |
| 375 | that you can unpack what you packed, adding gives the sum, pincing |
| 376 | the duck makes it quack. |
| 377 | |
| 378 | This is what everybody does when they write tests. |
| 379 | |
| 380 | |
| 381 | @subsection BorderlineTests Borderline cases |
| 382 | |
| 383 | If you have a size anywhere for your component, does it work for |
| 384 | size 1? Size 0? Sizes close to <code>UINT_MAX</code>? |
| 385 | |
| 386 | It might not be sensible to have a size 0, so in this case it is |
| 387 | not a borderline case, but rather a faulty input (see @ref |
| 388 | FaultyInputTests). |
| 389 | |
| 390 | |
| 391 | @subsection FaultyInputTests Faulty input |
| 392 | |
| 393 | Does your bitmap handle 0 bits size? Well, it might not be designed |
| 394 | for it, but is should <em>not</em> crash the application, but |
| 395 | rather produce an error. This is called defensive programming. |
| 396 | |
| 397 | Unfortunately, adding checks for values that should just not be |
| 398 | entered at all is not always practical: the checks cost cycles and |
| 399 | might cost more than it's worth. For example, some functions are |
| 400 | designed so that you may not give it a null pointer. In those |
| 401 | cases it's not sensible to pass it <code>NULL</code> just to see it |
| 402 | crash. |
| 403 | |
| 404 | Since every experienced programmer add an <code>assert()</code> to |
| 405 | ensure that you get a proper failure for the debug builds when a |
| 406 | null pointer passed (you add asserts too, right?), you will in this |
| 407 | case instead have a controlled (early) crash in the debug build. |
| 408 | |
| 409 | |
| 410 | @subsection ErrorHandlingTests Error handling |
| 411 | |
| 412 | This is testing that the errors your component is designed to give |
| 413 | actually are produced. For example, testing that trying to open a |
| 414 | non-existing file produces a sensible error code. |
| 415 | |
| 416 | |
| 417 | @subsection BadEnvironmentTests Environment |
| 418 | |
| 419 | Sometimes, modules has to behave well even when the environment |
| 420 | fails to work correctly. Typical examples are when the computer is |
| 421 | out of dynamic memory or when the disk is full. You can emulate |
| 422 | this by replacing, e.g., <code>malloc()</code> with your own |
| 423 | version that will work for a while, but then fail. Some things are |
| 424 | worth to keep in mind here: |
| 425 | |
| 426 | - Make sure to make the function fail deterministically, so that |
| 427 | you really can repeat the test. |
| 428 | |
| 429 | - Make sure that it doesn't just fail immediately. The unit might |
| 430 | have checks for the first case, but might actually fail some time |
| 431 | in the near future. |
| 432 | |
| 433 | |
| 434 | @section UnitTest How to structure a unit test |
| 435 | |
| 436 | In this section we will give some advice on how to structure the |
| 437 | unit tests to make the development run smoothly. The basic |
| 438 | structure of a test is: |
| 439 | |
| 440 | - Plan |
| 441 | - Test |
| 442 | - Report |
| 443 | |
| 444 | |
| 445 | @subsection TestPlanning Plan the test |
| 446 | |
| 447 | Planning the test means telling how many tests there are. In the |
| 448 | event that one of the tests causes a crash, it is then possible to |
| 449 | see that there are fewer tests than expected, and print a proper |
| 450 | error message. |
| 451 | |
| 452 | To plan a test, use the @c plan() function in the following manner: |
| 453 | |
| 454 | @code |
| 455 | int main(int argc, char *argv[]) |
| 456 | { |
| 457 | plan(5); |
| 458 | . |
| 459 | . |
| 460 | . |
| 461 | } |
| 462 | @endcode |
| 463 | |
| 464 | If you don't call the @c plan() function, the number of tests |
| 465 | executed will be printed at the end. This is intended to be used |
| 466 | while developing the unit and you are constantly adding tests. It |
| 467 | is not indented to be used after the unit has been released. |
| 468 | |
| 469 | |
| 470 | @subsection TestRunning Execute the test |
| 471 | |
| 472 | To report the status of a test, the @c ok() function is used in the |
| 473 | following manner: |
| 474 | |
| 475 | @code |
| 476 | int main(int argc, char *argv[]) |
| 477 | { |
| 478 | plan(5); |
| 479 | ok(ducks == paddling_ducks, |
| 480 | "%d ducks did not paddle", ducks - paddling_ducks); |
| 481 | . |
| 482 | . |
| 483 | . |
| 484 | } |
| 485 | @endcode |
| 486 | |
| 487 | This will print a test result line on the standard output in TAP |
| 488 | format, which allows TAP handling frameworks (like Test::Harness) |
| 489 | to parse the status of the test. |
| 490 | |
| 491 | @subsection TestReport Report the result of the test |
| 492 | |
| 493 | At the end, a complete test report should be written, with some |
| 494 | statistics. If the test returns EXIT_SUCCESS, all tests were |
| 495 | successful, otherwise at least one test failed. |
| 496 | |
| 497 | To get a TAP complient output and exit status, report the exit |
| 498 | status in the following manner: |
| 499 | |
| 500 | @code |
| 501 | int main(int argc, char *argv[]) |
| 502 | { |
| 503 | plan(5); |
| 504 | ok(ducks == paddling_ducks, |
| 505 | "%d ducks did not paddle", ducks - paddling_ducks); |
| 506 | . |
| 507 | . |
| 508 | . |
| 509 | return exit_status(); |
| 510 | } |
| 511 | @endcode |
| 512 | |
| 513 | @section DontDoThis Ways to not do unit testing |
| 514 | |
| 515 | In this section, we'll go through some quite common ways to write |
| 516 | tests that are <em>not</em> a good idea. |
| 517 | |
| 518 | @subsection BreadthFirstTests Doing breadth-first testing |
| 519 | |
| 520 | If you're writing a library with several functions, don't test all |
| 521 | functions using size 1, then all functions using size 2, etc. If a |
| 522 | test for size 42 fails, you have no easy way of tracking down why |
| 523 | it failed. |
| 524 | |
| 525 | It is better to concentrate on getting one function to work at a |
| 526 | time, which means that you test each function for all sizes that |
| 527 | you think is reasonable. Then you continue with the next function, |
| 528 | doing the same. This is usually also the way that a library is |
| 529 | developed (one function at a time) so stick to testing that is |
| 530 | appropriate for now the unit is developed. |
| 531 | |
| 532 | @subsection JustToBeSafeTest Writing unnecessarily large tests |
| 533 | |
| 534 | Don't write tests that use parameters in the range 1-1024 unless |
| 535 | you have a very good reason to belive that the component will |
| 536 | succeed for 562 but fail for 564 (the numbers picked are just |
| 537 | examples). |
| 538 | |
| 539 | It is very common to write extensive tests "just to be safe." |
| 540 | Having a test suite with a lot of values might give you a warm |
| 541 | fuzzy feeling, but it doesn't really help you find the bugs. Good |
| 542 | tests fail; seriously, if you write a test that you expect to |
| 543 | succeed, you don't need to write it. If you think that it |
| 544 | <em>might</em> fail, <em>then</em> you should write it. |
| 545 | |
| 546 | Don't take this as an excuse to avoid writing any tests at all |
| 547 | "since I make no mistakes" (when it comes to this, there are two |
| 548 | kinds of people: those who admit they make mistakes, and those who |
| 549 | don't); rather, this means that there is no reason to test that |
| 550 | using a buffer with size 100 works when you have a test for buffer |
| 551 | size 96. |
| 552 | |
| 553 | The drawback is that the test suite takes longer to run, for little |
| 554 | or no benefit. It is acceptable to do a exhaustive test if it |
| 555 | doesn't take too long to run and it is quite common to do an |
| 556 | exhaustive test of a function for a small set of values. |
| 557 | Use your judgment to decide what is excessive: your milage may |
| 558 | vary. |
| 559 | */ |
| 560 | |
| 561 | /** |
| 562 | @example simple.t.c |
| 563 | |
| 564 | This is an simple example of how to write a test using the |
| 565 | library. The output of this program is: |
| 566 | |
| 567 | @code |
| 568 | 1..1 |
| 569 | # Testing basic functions |
| 570 | ok 1 - Testing gcs() |
| 571 | @endcode |
| 572 | |
| 573 | The basic structure is: plan the number of test points using the |
| 574 | plan() function, perform the test and write out the result of each |
| 575 | test point using the ok() function, print out a diagnostics message |
| 576 | using diag(), and report the result of the test by calling the |
| 577 | exit_status() function. Observe that this test does excessive |
| 578 | testing (see @ref JustToBeSafeTest), but the test point doesn't |
| 579 | take very long time. |
| 580 | */ |
| 581 | |
| 582 | /** |
| 583 | @example todo.t.c |
| 584 | |
| 585 | This example demonstrates how to use the <code>todo_start()</code> |
| 586 | and <code>todo_end()</code> function to mark a sequence of tests to |
| 587 | be done. Observe that the tests are assumed to fail: if any test |
| 588 | succeeds, it is considered a "bonus". |
| 589 | */ |
| 590 | |
| 591 | /** |
| 592 | @example skip.t.c |
| 593 | |
| 594 | This is an example of how the <code>SKIP_BLOCK_IF</code> can be |
| 595 | used to skip a predetermined number of tests. Observe that the |
| 596 | macro actually skips the following statement, but it's not sensible |
| 597 | to use anything than a block. |
| 598 | */ |
| 599 | |
| 600 | /** |
| 601 | @example skip_all.t.c |
| 602 | |
| 603 | Sometimes, you skip an entire test because it's testing a feature |
| 604 | that doesn't exist on the system that you're testing. To skip an |
| 605 | entire test, use the <code>skip_all()</code> function according to |
| 606 | this example. |
| 607 | */ |