source: mainline/uspace/lib/pcut/src/os/generic.c@ 2bff9860

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2bff9860 was 01579ad, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Start work on PCUT integration

PCUT is a simple library for (hopefully) easier unit testing.
See https://github.com/vhotspur/pcut for more details.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[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>
35#include <sys/types.h>
36#include <errno.h>
37#include <assert.h>
38#include <string.h>
39#include "../internal.h"
40
41/** Maximum command-line length. */
42#define PCUT_COMMAND_LINE_BUFFER_SIZE 256
43
44/** Maximum length of a temporary file name. */
45#define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
46
47/** Maximum size of stdout we are able to capture. */
48#define OUTPUT_BUFFER_SIZE 8192
49
50/* Format the command to launch a test according to OS we are running on. */
51
52#if defined(__WIN64) || defined(__WIN32)
53#include <process.h>
54
55#define FORMAT_COMMAND(buffer, buffer_size, self_path, test_id, temp_file) \
56 snprintf(buffer, buffer_size, "\"\"%s\" -t%d >%s\"", self_path, test_id, temp_file)
57#define FORMAT_TEMP_FILENAME(buffer, buffer_size) \
58 snprintf(buffer, buffer_size, "pcut_%d.tmp", _getpid())
59
60#elif defined(__unix)
61#include <unistd.h>
62
63#define FORMAT_COMMAND(buffer, buffer_size, self_path, test_id, temp_file) \
64 snprintf(buffer, buffer_size, "%s -t%d &>%s", self_path, test_id, temp_file)
65#define FORMAT_TEMP_FILENAME(buffer, buffer_size) \
66 snprintf(buffer, buffer_size, "pcut_%d.tmp", getpid())
67
68#else
69#error "Unknown operating system."
70#endif
71
72/** Buffer for assertion and other error messages. */
73static char error_message_buffer[OUTPUT_BUFFER_SIZE];
74
75/** Buffer for stdout from the test. */
76static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
77
78/** Prepare for a new test. */
79static void before_test_start(pcut_item_t *test) {
80 pcut_report_test_start(test);
81
82 memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
83 memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
84}
85
86/** Convert program exit code to test outcome.
87 *
88 * @param status Return value from the system() function.
89 * @return Test outcome code.
90 */
91static int convert_wait_status_to_outcome(int status) {
92 if (status < 0) {
93 return TEST_OUTCOME_ERROR;
94 } else if (status == 0) {
95 return TEST_OUTCOME_PASS;
96 } else {
97 return TEST_OUTCOME_FAIL;
98 }
99}
100
101/** Run the test as a new process and report the result.
102 *
103 * @param self_path Path to itself, that is to current binary.
104 * @param test Test to be run.
105 */
106void pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
107 before_test_start(test);
108
109 char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
110 FORMAT_TEMP_FILENAME(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1);
111
112 char command[PCUT_COMMAND_LINE_BUFFER_SIZE];
113 FORMAT_COMMAND(command, PCUT_COMMAND_LINE_BUFFER_SIZE - 1,
114 self_path, (test)->id, tempfile_name);
115
116 int rc = system(command);
117 rc = convert_wait_status_to_outcome(rc);
118
119 FILE *tempfile = fopen(tempfile_name, "rb");
120 if (tempfile == NULL) {
121 pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to open temporary file.", NULL, NULL);
122 return;
123 }
124
125 fread(extra_output_buffer, 1, OUTPUT_BUFFER_SIZE, tempfile);
126 fclose(tempfile);
127 remove(tempfile_name);
128
129 pcut_report_test_done_unparsed(test, rc, extra_output_buffer, OUTPUT_BUFFER_SIZE);
130}
131
Note: See TracBrowser for help on using the repository browser.