File size: 11,426 Bytes
78d2150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
/* The following globals/functions are defined in the program source (paste.c)
and are available here since this file is included into the same TU:
- static bool paste_serial(size_t, char **)
- static int collapse_escapes(const char *)
- static bool have_read_stdin;
- static unsigned char line_delim;
- static char *delims;
- static char const *delim_end;
*/
static char *create_temp_file_with_bytes(const void *data, size_t len)
{
char tmpl[] = "/tmp/paste_serial_test_XXXXXX";
int fd = mkstemp(tmpl);
if (fd < 0)
return NULL;
ssize_t written = 0;
const char *p = (const char *)data;
while ((size_t)written < len)
{
ssize_t r = write(fd, p + written, len - (size_t)written);
if (r < 0)
{
int saved = errno;
close(fd);
unlink(tmpl);
errno = saved;
return NULL;
}
written += r;
}
if (close(fd) != 0)
{
int saved = errno;
unlink(tmpl);
errno = saved;
return NULL;
}
/* Caller must free the returned path and unlink when done. */
return strdup(tmpl);
}
static char *create_temp_file_with_str(const char *s)
{
return create_temp_file_with_bytes(s, strlen(s));
}
/* Capture stdout while invoking paste_serial(nfiles, files).
Returns 0 on success; nonzero on failure to set up capturing.
The out_buf is malloc'ed and must be free()'d by caller.
Important: This function does not perform any Unity assertions while
stdout is redirected. */
static int run_and_capture(char **files, size_t nfiles,
char **out_buf, size_t *out_len,
bool *ok_ret)
{
if (!out_buf || !out_len || !ok_ret)
return -1;
fflush(stdout);
int saved_stdout = dup(STDOUT_FILENO);
if (saved_stdout < 0)
return -1;
FILE *tmp = tmpfile();
if (!tmp)
{
int saved = errno;
close(saved_stdout);
errno = saved;
return -1;
}
if (dup2(fileno(tmp), STDOUT_FILENO) < 0)
{
int saved = errno;
fclose(tmp);
close(saved_stdout);
errno = saved;
return -1;
}
/* Call the function under test. */
bool ok = paste_serial(nfiles, files);
/* Flush and read captured output. */
fflush(stdout);
long endpos = ftell(tmp);
if (endpos < 0)
{
/* Restore stdout before returning. */
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
fclose(tmp);
return -1;
}
size_t len = (size_t)endpos;
if (fseek(tmp, 0, SEEK_SET) != 0)
{
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
fclose(tmp);
return -1;
}
char *buf = NULL;
if (len > 0)
{
buf = (char *)malloc(len);
if (!buf)
{
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
fclose(tmp);
return -1;
}
size_t rd = fread(buf, 1, len, tmp);
if (rd != len)
{
free(buf);
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
fclose(tmp);
return -1;
}
}
else
{
/* Ensure we return a non-NULL pointer even for zero length? Not necessary.
We'll return NULL with out_len==0. */
}
/* Restore stdout. */
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
fclose(tmp);
*out_buf = buf;
*out_len = len;
*ok_ret = ok;
return 0;
}
static void assert_bytes_equal(const void *expected, size_t expected_len,
const void *actual, size_t actual_len)
{
TEST_ASSERT_EQUAL_size_t_MESSAGE(expected_len, actual_len, "Output length mismatch");
if (expected_len > 0)
{
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, expected_len, "Output content mismatch");
}
}
void setUp(void) {
/* Reset global state for each test. */
have_read_stdin = false;
line_delim = '\n';
/* Default delimiter to TAB, as per paste. */
collapse_escapes("\t");
}
void tearDown(void) {
/* Nothing to clean specifically; test cases unlink their temp files. */
}
/* 1) Basic serial paste with default TAB delimiter. */
void test_paste_serial_basic_tab(void)
{
char *f = create_temp_file_with_str("a\nb\nc\n");
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "a\tb\tc\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 2) File without trailing newline still ends with newline in output. */
void test_paste_serial_no_trailing_newline(void)
{
char *f = create_temp_file_with_str("a\nb\nc");
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "a\tb\tc\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 3) Empty file should produce just a newline. */
void test_paste_serial_empty_file_outputs_newline(void)
{
char *f = create_temp_file_with_str("");
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 4) Multiple files processed serially, outputs concatenated with newline after each file. */
void test_paste_serial_multiple_files(void)
{
char *f1 = create_temp_file_with_str("x\ny\n");
char *f2 = create_temp_file_with_str("1\n2\n");
TEST_ASSERT_NOT_NULL(f1);
TEST_ASSERT_NOT_NULL(f2);
char *files[] = { f1, f2 };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 2, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "x\ty\n1\t2\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f1); unlink(f2);
free(f1); free(f2);
}
/* 5) Custom delimiters cycle correctly. */
void test_paste_serial_custom_delims_cycle(void)
{
/* Use ",;" cycling: a,b;c,d\n for four lines. */
collapse_escapes(",;");
char *f = create_temp_file_with_str("a\nb\nc\nd\n");
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "a,b;c,d\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 6) EMPTY_DELIM (\0) means no delimiter inserted between lines. */
void test_paste_serial_empty_delimiter(void)
{
collapse_escapes("\\0");
char *f = create_temp_file_with_str("a\nb\nc\n");
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "abc\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 7) Zero-terminated records without trailing NUL: outputs newline at end. */
void test_paste_serial_zero_terminated_no_trailing_nul(void)
{
line_delim = '\0';
collapse_escapes("\t");
const char data[] = { 'a', '\0', 'b', '\0', 'c' }; /* no trailing NUL */
char *f = create_temp_file_with_bytes(data, sizeof(data));
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char expected[] = { 'a', '\t', 'b', '\t', 'c', '\n' };
assert_bytes_equal(expected, sizeof(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 8) Zero-terminated records with trailing NUL: no final newline (last char equals line_delim). */
void test_paste_serial_zero_terminated_with_trailing_nul(void)
{
line_delim = '\0';
collapse_escapes("\t");
const char data[] = { 'a', '\0', 'b', '\0' }; /* trailing NUL */
char *f = create_temp_file_with_bytes(data, sizeof(data));
TEST_ASSERT_NOT_NULL(f);
char *files[] = { f };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char expected[] = { 'a', '\t', 'b', '\0' }; /* no final newline */
assert_bytes_equal(expected, sizeof(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 9) Reading from standard input via "-" works. */
void test_paste_serial_stdin_input(void)
{
/* Reset line delimiter and delimiters. */
line_delim = '\n';
collapse_escapes("\t");
char *f = create_temp_file_with_str("p\nq\n");
TEST_ASSERT_NOT_NULL(f);
/* Redirect stdin from file f. */
int saved_stdin = dup(STDIN_FILENO);
TEST_ASSERT_TRUE_MESSAGE(saved_stdin >= 0, "Failed to save stdin");
int fd = open(f, O_RDONLY);
TEST_ASSERT_TRUE_MESSAGE(fd >= 0, "Failed to open temp file for stdin");
TEST_ASSERT_TRUE_MESSAGE(dup2(fd, STDIN_FILENO) >= 0, "Failed to redirect stdin");
close(fd);
char *dash = "-";
char *files[] = { dash };
char *out = NULL;
size_t out_len = 0;
bool ok = false;
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
/* Restore stdin before any assertions that may output. */
dup2(saved_stdin, STDIN_FILENO);
close(saved_stdin);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(ok);
const char *expected = "p\tq\n";
assert_bytes_equal(expected, strlen(expected), out, out_len);
free(out);
unlink(f);
free(f);
}
/* 10) Nonexistent file: returns false and writes nothing to stdout. */
void test_paste_serial_open_error_returns_false(void)
{
char *missing = (char *)"__definitely_missing_paste_serial_test__";
char *files[] = { missing };
char *out = NULL;
size_t out_len = 0;
bool ok = true; /* expect false */
int rc = run_and_capture(files, 1, &out, &out_len, &ok);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_FALSE(ok);
TEST_ASSERT_EQUAL_size_t(0, out_len);
/* out may be NULL when out_len==0; that's fine. */
free(out);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_paste_serial_basic_tab);
RUN_TEST(test_paste_serial_no_trailing_newline);
RUN_TEST(test_paste_serial_empty_file_outputs_newline);
RUN_TEST(test_paste_serial_multiple_files);
RUN_TEST(test_paste_serial_custom_delims_cycle);
RUN_TEST(test_paste_serial_empty_delimiter);
RUN_TEST(test_paste_serial_zero_terminated_no_trailing_nul);
RUN_TEST(test_paste_serial_zero_terminated_with_trailing_nul);
RUN_TEST(test_paste_serial_stdin_input);
RUN_TEST(test_paste_serial_open_error_returns_false);
return UNITY_END();
} |