1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2002
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/* 01/02/2003	Port to LTP avenkat@us.ibm.com */
21/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
22
23/*
24 * fmtmsg(3C) and addseverity(3C)
25 *
26 * ALGORITHM
27 *      Check basic functionality using various messages and severity levels.
28 */
29
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <fcntl.h>
33#include <ctype.h>
34#include <stdio.h>
35#if !defined(__UCLIBC__)
36#include <fmtmsg.h>		/* interface definition */
37#endif
38#include <string.h>
39
40#include <stdlib.h>
41#include <unistd.h>
42#include <errno.h>
43#include "test.h"
44#define FAILED 0
45#define PASSED 1
46
47char *TCID = "fmtms01";
48
49int local_flag = PASSED;
50int block_number;
51FILE *temp;
52int TST_TOTAL = 1;
53
54int anyfail();
55int blenter();
56int blexit();
57void setup();
58
59char ch;
60char buf[80];
61char *str1 = "LTP:fmtmsg: INFO: LTP fmtmsg() test1 message, NOT an error";
62char *str2 = "TO FIX: This is correct output, no action needed  LTP:msg:001";
63char *str3 = "LTP:fmtmsg: LTP_TEST: LTP fmtmsg() test2 message, NOT an error";
64char *str4 = "TO FIX: This is correct output, no action needed  LTP:msg:002";
65
66void clearbuf(void)
67{
68	int i;
69	for (i = 0; i < 80; i++)
70		buf[i] = '\0';
71}
72
73#if !defined(__UCLIBC__)
74
75int main(int argc, char *argv[])
76{
77	int fd, ret_val;
78	FILE *fp;
79
80	tst_parse_opts(argc, argv, NULL, NULL);
81
82	setup();
83/*--------------------------------------------------------------*/
84	blenter();
85
86	/* Check that system SEV_LEVEL output is correct */
87
88	close(2);		/* redirect stderr to file */
89	fd = creat("fmtfile", 0644);
90	ret_val = fmtmsg(MM_PRINT | MM_SOFT, "LTP:fmtmsg", MM_INFO,
91			 "LTP fmtmsg() test1 message, NOT an error",
92			 "This is correct output, no action needed",
93			 "LTP:msg:001");
94	close(fd);
95
96	if (ret_val != 0) {
97		fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val);
98		local_flag = FAILED;
99	}
100
101	fp = fopen("fmtfile", "r");
102	clearbuf();
103	fread(buf, sizeof(buf[0]), strlen(str1), fp);
104	if (strcmp(str1, buf) != 0) {
105		fprintf(temp, "Expected string: %s\n", str1);
106		fprintf(temp, "does not match\n");
107		fprintf(temp, "received string: %s\n\n", buf);
108		local_flag = FAILED;
109	}
110
111	/* Read past spaces in output */
112	fread(&ch, sizeof(ch), 1, fp);
113	while (isspace(ch))
114		fread(&ch, sizeof(ch), 1, fp);
115	ungetc(ch, fp);
116
117	clearbuf();
118	fread(buf, sizeof(buf[0]), strlen(str2), fp);
119	fclose(fp);
120	if (strcmp(str2, buf) != 0) {
121		fprintf(temp, "Expected string: %s\n", str2);
122		fprintf(temp, "does not match\n");
123		fprintf(temp, "received string: %s\n\n", buf);
124		local_flag = FAILED;
125	}
126
127	blexit();
128/*--------------------------------------------------------------*/
129	blenter();
130
131	/* Check that a system defined SEV_LEVEL cannot get redefined */
132
133	ret_val = addseverity(3, "INVALID");
134	if (ret_val != MM_NOTOK) {
135		fprintf(temp, "addseverity returned %d, expected MM_NOTOK\n",
136			ret_val);
137		local_flag = FAILED;
138	}
139
140	blexit();
141/*--------------------------------------------------------------*/
142	blenter();
143
144	/* Check that we can define our own */
145	/* SEV_LEVEL and output is correct  */
146
147	ret_val = addseverity(5, "LTP_TEST");
148	if (ret_val != MM_OK) {
149		fprintf(temp, "addseverity returned %d, expected MM_OK\n",
150			ret_val);
151		local_flag = FAILED;
152	}
153
154	close(2);		/* redirect stderr to file */
155	fd = creat("fmtfile", 0644);
156	ret_val = fmtmsg(MM_PRINT | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
157			 "LTP fmtmsg() test2 message, NOT an error",
158			 "This is correct output, no action needed",
159			 "LTP:msg:002");
160	close(fd);
161
162	if (ret_val != 0) {
163		fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val);
164		local_flag = FAILED;
165	}
166
167	fp = fopen("fmtfile", "r");
168	clearbuf();
169	fread(buf, sizeof(buf[0]), strlen(str3), fp);
170	if (strcmp(str3, buf) != 0) {
171		fprintf(temp, "Expected string: %s\n", str3);
172		fprintf(temp, "does not match\n");
173		fprintf(temp, "received string: %s\n\n", buf);
174		local_flag = FAILED;
175	}
176
177	/* Read past spaces in output */
178	fread(&ch, sizeof(ch), 1, fp);
179	while (isspace(ch))
180		fread(&ch, sizeof(ch), 1, fp);
181	ungetc(ch, fp);
182
183	clearbuf();
184	fread(buf, sizeof(buf[0]), strlen(str4), fp);
185	if (strcmp(str4, buf) != 0) {
186		fprintf(temp, "Expected string: %s\n", str4);
187		fprintf(temp, "does not match\n");
188		fprintf(temp, "received string: %s\n\n", buf);
189		local_flag = FAILED;
190	}
191
192	fclose(fp);
193	remove("fmtfile");
194
195	blexit();
196/*--------------------------------------------------------------*/
197	blenter();
198
199	/* Test result of writing to /dev/console */
200
201	ret_val = fmtmsg(MM_CONSOLE | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
202			 "LTP fmtmsg() test3 message, NOT an error",
203			 "This is correct output, no action needed",
204			 "LTP:msg:003");
205	if (ret_val != MM_OK) {
206		fprintf(temp, "fmtmsg returned %d, expected MM_OK\n", ret_val);
207		fprintf(temp, "failed to write to console\n\n");
208		local_flag = FAILED;
209	}
210
211	blexit();
212	anyfail();
213	tst_exit();
214}
215
216int anyfail(void)
217{
218	(local_flag == FAILED) ? tst_resm(TFAIL,
219					  "Test failed") : tst_resm(TPASS,
220								    "Test passed");
221	tst_rmdir();
222	tst_exit();
223}
224
225void setup(void)
226{
227	temp = stderr;
228	tst_tmpdir();
229}
230
231int blenter(void)
232{
233	local_flag = PASSED;
234	return 0;
235}
236
237int blexit(void)
238{
239	(local_flag == FAILED) ? tst_resm(TFAIL,
240					  "Test failed") : tst_resm(TPASS,
241								    "Test passed");
242	return 0;
243}
244
245#else
246
247int main(void)
248{
249	tst_brkm(TCONF, NULL, "test is not available on uClibc");
250}
251
252#endif /* if !defined(__UCLIBC__) */
253