[01579ad] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file
|
---|
| 30 | *
|
---|
| 31 | * Platform-dependent test execution function when system() is available.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <stdlib.h>
|
---|
[9b20126] | 35 | #include <stdio.h>
|
---|
[9eb1ff5] | 36 | #include <sys/types.h>
|
---|
[01579ad] | 37 | #include <errno.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <string.h>
|
---|
| 40 | #include "../internal.h"
|
---|
| 41 |
|
---|
| 42 | /** Maximum command-line length. */
|
---|
| 43 | #define PCUT_COMMAND_LINE_BUFFER_SIZE 256
|
---|
| 44 |
|
---|
| 45 | /** Maximum length of a temporary file name. */
|
---|
| 46 | #define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
|
---|
| 47 |
|
---|
| 48 | /** Maximum size of stdout we are able to capture. */
|
---|
| 49 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
| 50 |
|
---|
| 51 | /* Format the command to launch a test according to OS we are running on. */
|
---|
| 52 |
|
---|
[9b20126] | 53 | #if defined(__WIN64) || defined(__WIN32) || defined(_WIN32)
|
---|
[01579ad] | 54 | #include <process.h>
|
---|
| 55 |
|
---|
| 56 | #define FORMAT_COMMAND(buffer, buffer_size, self_path, test_id, temp_file) \
|
---|
| 57 | snprintf(buffer, buffer_size, "\"\"%s\" -t%d >%s\"", self_path, test_id, temp_file)
|
---|
| 58 | #define FORMAT_TEMP_FILENAME(buffer, buffer_size) \
|
---|
| 59 | snprintf(buffer, buffer_size, "pcut_%d.tmp", _getpid())
|
---|
| 60 |
|
---|
| 61 | #elif defined(__unix)
|
---|
| 62 | #include <unistd.h>
|
---|
| 63 |
|
---|
| 64 | #define FORMAT_COMMAND(buffer, buffer_size, self_path, test_id, temp_file) \
|
---|
| 65 | snprintf(buffer, buffer_size, "%s -t%d &>%s", self_path, test_id, temp_file)
|
---|
| 66 | #define FORMAT_TEMP_FILENAME(buffer, buffer_size) \
|
---|
| 67 | snprintf(buffer, buffer_size, "pcut_%d.tmp", getpid())
|
---|
| 68 |
|
---|
| 69 | #else
|
---|
| 70 | #error "Unknown operating system."
|
---|
| 71 | #endif
|
---|
| 72 |
|
---|
| 73 | /** Buffer for assertion and other error messages. */
|
---|
| 74 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 75 |
|
---|
| 76 | /** Buffer for stdout from the test. */
|
---|
| 77 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 78 |
|
---|
[134ac5d] | 79 | /** Prepare for a new test.
|
---|
| 80 | *
|
---|
| 81 | * @param test Test that is about to start.
|
---|
| 82 | */
|
---|
[01579ad] | 83 | static void before_test_start(pcut_item_t *test) {
|
---|
| 84 | pcut_report_test_start(test);
|
---|
| 85 |
|
---|
| 86 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 87 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /** Convert program exit code to test outcome.
|
---|
| 91 | *
|
---|
| 92 | * @param status Return value from the system() function.
|
---|
| 93 | * @return Test outcome code.
|
---|
| 94 | */
|
---|
| 95 | static int convert_wait_status_to_outcome(int status) {
|
---|
| 96 | if (status < 0) {
|
---|
[9eb1ff5] | 97 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 98 | } else if (status == 0) {
|
---|
[9eb1ff5] | 99 | return PCUT_OUTCOME_PASS;
|
---|
[01579ad] | 100 | } else {
|
---|
[9eb1ff5] | 101 | return PCUT_OUTCOME_FAIL;
|
---|
[01579ad] | 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Run the test as a new process and report the result.
|
---|
| 106 | *
|
---|
| 107 | * @param self_path Path to itself, that is to current binary.
|
---|
| 108 | * @param test Test to be run.
|
---|
| 109 | */
|
---|
[9eb1ff5] | 110 | int pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
|
---|
| 111 | int rc, outcome;
|
---|
[9b20126] | 112 | FILE *tempfile;
|
---|
| 113 | char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
|
---|
| 114 | char command[PCUT_COMMAND_LINE_BUFFER_SIZE];
|
---|
| 115 |
|
---|
[01579ad] | 116 | before_test_start(test);
|
---|
| 117 |
|
---|
| 118 | FORMAT_TEMP_FILENAME(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1);
|
---|
| 119 | FORMAT_COMMAND(command, PCUT_COMMAND_LINE_BUFFER_SIZE - 1,
|
---|
| 120 | self_path, (test)->id, tempfile_name);
|
---|
[a35b458] | 121 |
|
---|
[9b20126] | 122 | PCUT_DEBUG("Will execute <%s> (temp file <%s>) with system().",
|
---|
| 123 | command, tempfile_name);
|
---|
| 124 |
|
---|
| 125 | rc = system(command);
|
---|
| 126 |
|
---|
| 127 | PCUT_DEBUG("system() returned 0x%04X", rc);
|
---|
[01579ad] | 128 |
|
---|
[9eb1ff5] | 129 | outcome = convert_wait_status_to_outcome(rc);
|
---|
[01579ad] | 130 |
|
---|
[9b20126] | 131 | tempfile = fopen(tempfile_name, "rb");
|
---|
[01579ad] | 132 | if (tempfile == NULL) {
|
---|
| 133 | pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to open temporary file.", NULL, NULL);
|
---|
[9eb1ff5] | 134 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 135 | }
|
---|
| 136 |
|
---|
| 137 | fread(extra_output_buffer, 1, OUTPUT_BUFFER_SIZE, tempfile);
|
---|
| 138 | fclose(tempfile);
|
---|
[9eb1ff5] | 139 | remove(tempfile_name);
|
---|
[01579ad] | 140 |
|
---|
[9eb1ff5] | 141 | pcut_report_test_done_unparsed(test, outcome, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
| 142 |
|
---|
| 143 | return outcome;
|
---|
[01579ad] | 144 | }
|
---|
| 145 |
|
---|
[9b20126] | 146 | void pcut_hook_before_test(pcut_item_t *test) {
|
---|
| 147 | PCUT_UNUSED(test);
|
---|
| 148 |
|
---|
| 149 | /* Do nothing. */
|
---|
| 150 | }
|
---|
| 151 |
|
---|