aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/popen.c
blob: 87bbde274e7371a42b14277d2663b4fc233f7369 (plain)
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>

#ifdef USE_HOST_LIBC
#define TEST_FILE "popen-host-libc.tmp"
#else
#define TEST_FILE "popen.tmp"
#endif

#define TEST_STRING1 "the quick brown fox jumps over the lazy dog"
#define TEST_STRING1_LEN 43
#define TEST_STRING2 "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
#define TEST_STRING2_LEN 55

int main() {
	// Test reading
	FILE *f1 = popen("echo -n '" TEST_STRING1 "'", "r");
	assert(f1);
	char buf1[TEST_STRING1_LEN];
	assert(fread(buf1, 1, TEST_STRING1_LEN, f1) == TEST_STRING1_LEN);
	assert(memcmp(buf1, TEST_STRING1, TEST_STRING1_LEN) == 0);
	pclose(f1);

	// Test writing
	FILE *f2 = popen("cat - > " TEST_FILE, "w");
	assert(f2);
	assert(fwrite(TEST_STRING2, 1, TEST_STRING2_LEN, f2) == TEST_STRING2_LEN);
	pclose(f2);

	// Read test file back
	FILE *test_file = fopen(TEST_FILE, "r");
	assert(test_file);
	char buf2[TEST_STRING2_LEN];
	assert(fread(buf2, 1, TEST_STRING2_LEN, test_file) == TEST_STRING2_LEN);
	assert(memcmp(buf2, TEST_STRING2, TEST_STRING2_LEN) == 0);
	fclose(test_file);
	assert(unlink(TEST_FILE) == 0);

	return 0;
}