xref: /freebsd/contrib/sendmail/libsm/t-fget.c (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
1552d4955SGregory Neil Shapiro /*
25dd76dd0SGregory Neil Shapiro  * Copyright (c) 2013 Proofpoint, Inc. and its suppliers.
3552d4955SGregory Neil Shapiro  *	All rights reserved.
4552d4955SGregory Neil Shapiro  *
5552d4955SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
6552d4955SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
7552d4955SGregory Neil Shapiro  * the sendmail distribution.
8552d4955SGregory Neil Shapiro  */
9552d4955SGregory Neil Shapiro 
10552d4955SGregory Neil Shapiro #include <sm/gen.h>
11*4313cc83SGregory Neil Shapiro SM_IDSTR(id, "@(#)$Id: t-fget.c,v 1.2 2013-11-22 20:51:43 ca Exp $")
12552d4955SGregory Neil Shapiro 
13552d4955SGregory Neil Shapiro #include <sm/io.h>
14552d4955SGregory Neil Shapiro #include <sm/string.h>
15552d4955SGregory Neil Shapiro #include <sm/test.h>
16552d4955SGregory Neil Shapiro #include <errno.h>
17552d4955SGregory Neil Shapiro 
18552d4955SGregory Neil Shapiro void
check(char * msg,int l)19552d4955SGregory Neil Shapiro check(char *msg, int l)
20552d4955SGregory Neil Shapiro {
21552d4955SGregory Neil Shapiro 	SM_FILE_T *wfp, *rfp;
22552d4955SGregory Neil Shapiro 	char buf[256];
23552d4955SGregory Neil Shapiro 	size_t n;
24552d4955SGregory Neil Shapiro 	int r, i;
25552d4955SGregory Neil Shapiro 	static char fn[] = "tfget";
26552d4955SGregory Neil Shapiro 
27552d4955SGregory Neil Shapiro 	wfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
28552d4955SGregory Neil Shapiro 			   SM_IO_WRONLY_B, NULL);
29552d4955SGregory Neil Shapiro 	SM_TEST(wfp != NULL);
30552d4955SGregory Neil Shapiro 	for (i = 0; i < l; i++)
31552d4955SGregory Neil Shapiro 	{
32552d4955SGregory Neil Shapiro 		r = sm_io_putc(wfp, SM_TIME_DEFAULT, msg[i]);
33552d4955SGregory Neil Shapiro 		SM_TEST(r >= 0);
34552d4955SGregory Neil Shapiro 	}
35552d4955SGregory Neil Shapiro 	r = sm_io_close(wfp, SM_TIME_DEFAULT);
36552d4955SGregory Neil Shapiro 	SM_TEST(r == 0);
37552d4955SGregory Neil Shapiro 
38552d4955SGregory Neil Shapiro 	rfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fn,
39552d4955SGregory Neil Shapiro 			   SM_IO_RDONLY_B, NULL);
40552d4955SGregory Neil Shapiro 	SM_TEST(rfp != NULL);
41552d4955SGregory Neil Shapiro 	n = sizeof(buf);
42552d4955SGregory Neil Shapiro 	r = sm_io_fgets(rfp, SM_TIME_DEFAULT, buf, n);
43552d4955SGregory Neil Shapiro 	if (l == 0)
44552d4955SGregory Neil Shapiro 	{
45552d4955SGregory Neil Shapiro 		SM_TEST(r == -1);
46552d4955SGregory Neil Shapiro 		SM_TEST(errno == 0);
47552d4955SGregory Neil Shapiro 	}
48552d4955SGregory Neil Shapiro 	else
49552d4955SGregory Neil Shapiro 	{
50552d4955SGregory Neil Shapiro 		SM_TEST(r == l);
51552d4955SGregory Neil Shapiro 		if (r != l)
52552d4955SGregory Neil Shapiro 			fprintf(stderr, "buf='%s', in='%s', r=%d, l=%d\n",
53552d4955SGregory Neil Shapiro 				buf, msg, r, l);
54552d4955SGregory Neil Shapiro 	}
55552d4955SGregory Neil Shapiro 	SM_TEST(memcmp(buf, msg, l) == 0);
56552d4955SGregory Neil Shapiro 	r = sm_io_close(rfp, SM_TIME_DEFAULT);
57552d4955SGregory Neil Shapiro 	SM_TEST(r == 0);
58552d4955SGregory Neil Shapiro }
59552d4955SGregory Neil Shapiro 
60552d4955SGregory Neil Shapiro 
61552d4955SGregory Neil Shapiro int
main(argc,argv)62552d4955SGregory Neil Shapiro main(argc, argv)
63552d4955SGregory Neil Shapiro 	int argc;
64552d4955SGregory Neil Shapiro 	char **argv;
65552d4955SGregory Neil Shapiro {
66552d4955SGregory Neil Shapiro 	char res[256];
67552d4955SGregory Neil Shapiro 	int l;
68552d4955SGregory Neil Shapiro 
69552d4955SGregory Neil Shapiro 	sm_test_begin(argc, argv, "test fget");
70552d4955SGregory Neil Shapiro 
71552d4955SGregory Neil Shapiro 	check("", strlen(""));
72552d4955SGregory Neil Shapiro 	check("\n", strlen("\n"));
73552d4955SGregory Neil Shapiro 	check("test\n", strlen("test\n"));
74552d4955SGregory Neil Shapiro 
75552d4955SGregory Neil Shapiro 	l = snprintf(res, sizeof(res), "%c%s\n", '\0', "test ing");
76552d4955SGregory Neil Shapiro 	check(res, l);
77552d4955SGregory Neil Shapiro 
78552d4955SGregory Neil Shapiro 	l = snprintf(res, sizeof(res), "%c%s%c\n", '\0', "test ing", '\0');
79552d4955SGregory Neil Shapiro 	check(res, l);
80552d4955SGregory Neil Shapiro 
81552d4955SGregory Neil Shapiro 	l = snprintf(res, sizeof(res), "%c%s%c%s\n",
82552d4955SGregory Neil Shapiro 		'\0', "test ing", '\0', "eol");
83552d4955SGregory Neil Shapiro 	check(res, l);
84552d4955SGregory Neil Shapiro 
85552d4955SGregory Neil Shapiro 	return sm_test_end();
86552d4955SGregory Neil Shapiro }
87