Merge branch 'maint-0.4.5' into release-0.4.5
[tor.git] / src / test / test-process.c
blobf5a1f1a54e7c2a74a87e8c1aae6121f1a96515cb
1 /* Copyright (c) 2011-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include <stdio.h>
6 #ifdef _WIN32
7 #define WINDOWS_LEAN_AND_MEAN
8 #include <windows.h>
9 #else
10 #include <unistd.h>
11 #endif /* defined(_WIN32) */
12 #include <string.h>
13 #include <stdlib.h>
15 #ifdef _WIN32
16 #define SLEEP(sec) Sleep((sec)*1000)
17 #else
18 #define SLEEP(sec) sleep(sec)
19 #endif
21 /* Trivial test program to test process_t. */
22 int
23 main(int argc, char **argv)
25 /* Does our process get the right arguments? */
26 for (int i = 0; i < argc; ++i) {
27 fprintf(stdout, "argv[%d] = '%s'\n", i, argv[i]);
28 fflush(stdout);
31 /* Make sure our process got our environment variable. */
32 fprintf(stdout, "Environment variable TOR_TEST_ENV = '%s'\n",
33 getenv("TOR_TEST_ENV"));
34 fflush(stdout);
36 /* Test line handling on stdout and stderr. */
37 fprintf(stdout, "Output on stdout\nThis is a new line\n");
38 fflush(stdout);
40 fprintf(stderr, "Output on stderr\nThis is a new line\n");
41 fflush(stderr);
43 fprintf(stdout, "Partial line on stdout ...");
44 fflush(stdout);
46 fprintf(stderr, "Partial line on stderr ...");
47 fflush(stderr);
49 SLEEP(2);
51 fprintf(stdout, "end of partial line on stdout\n");
52 fflush(stdout);
53 fprintf(stderr, "end of partial line on stderr\n");
54 fflush(stderr);
56 /* Echo input from stdin. */
57 char buffer[1024];
59 int count = 0;
61 while (fgets(buffer, sizeof(buffer), stdin)) {
62 /* Strip the newline. */
63 size_t size = strlen(buffer);
65 if (size >= 1 && buffer[size - 1] == '\n') {
66 buffer[size - 1] = '\0';
67 --size;
70 if (size >= 1 && buffer[size - 1] == '\r') {
71 buffer[size - 1] = '\0';
72 --size;
75 fprintf(stdout, "Read line from stdin: '%s'\n", buffer);
76 fflush(stdout);
78 if (++count == 3)
79 break;
82 fprintf(stdout, "We are done for here, thank you!\n");
84 return 0;