blob: f1abe4f7275e2dab8070f4d2a4ac293896f2fcba [file] [log] [blame]
robbiew38005ee2002-12-31 20:54:14 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew38005ee2002-12-31 20:54:14 +000018 */
19
robbiewdc44d2e2002-12-30 21:32:36 +000020/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
21/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
23/*
robbiewdc44d2e2002-12-30 21:32:36 +000024 * NAME
25 * fmtmsg -- test fmtmsg(3C) and addseverity(3C)
26 *
27 * CALLS
28 * fmtmsg(3), addseverity(3C)
29 *
30 * ALGORITHM
31 * Check basic functionality using various messages and severity levels.
32 *
33 * RESTRICTIONS
34 */
35
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <fcntl.h>
39#include <ctype.h>
40#include <stdio.h>
robbiewe93a3242005-08-31 20:27:12 +000041#if !defined(UCLINUX) && !defined(__UCLIBC__)
subrata_modak56207ce2009-03-23 13:35:39 +000042#include <fmtmsg.h> /* interface definition */
robbiewd34d5812005-07-11 22:28:09 +000043#endif
robbiewdc44d2e2002-12-30 21:32:36 +000044#include <string.h>
45
46/***** LTP Port *****/
47#include <stdlib.h>
48#include <unistd.h>
49#include <errno.h>
50#include "test.h"
51#include "usctest.h"
52#define FAILED 0
53#define PASSED 1
54
55char *TCID = "fmtms01";
56
57int local_flag = PASSED;
58int block_number;
59FILE *temp;
60int TST_TOTAL = 1;
robbiewdc44d2e2002-12-30 21:32:36 +000061
62int anyfail();
63int blenter();
64int blexit();
65void setup();
66
67/***** ** ** *****/
68//char progname[]= "fmtmsg1()";
69
70char ch;
71char buf[80];
subrata_modak56207ce2009-03-23 13:35:39 +000072char *str1 = "LTP:fmtmsg: INFO: LTP fmtmsg() test1 message, NOT an error";
73char *str2 = "TO FIX: This is correct output, no action needed LTP:msg:001";
74char *str3 = "LTP:fmtmsg: LTP_TEST: LTP fmtmsg() test2 message, NOT an error";
75char *str4 = "TO FIX: This is correct output, no action needed LTP:msg:002";
robbiewdc44d2e2002-12-30 21:32:36 +000076
robbiew678a2702003-01-06 20:32:39 +000077void clearbuf()
robbiewdc44d2e2002-12-30 21:32:36 +000078{
79 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000080 for (i = 0; i < 80; i++)
subrata_modakb15aafd2008-10-20 06:30:32 +000081 buf[i] = '\0';
robbiewdc44d2e2002-12-30 21:32:36 +000082}
83
robbiewe93a3242005-08-31 20:27:12 +000084#if !defined(UCLINUX) && !defined(__UCLIBC__)
robbiewd34d5812005-07-11 22:28:09 +000085
robbiewdc44d2e2002-12-30 21:32:36 +000086/*--------------------------------------------------------------*/
robbiew678a2702003-01-06 20:32:39 +000087int main(int argc, char *argv[])
robbiewdc44d2e2002-12-30 21:32:36 +000088{
89 int fd, ret_val;
90 FILE *fp;
91
92 setup(); /* temp file is now open */
93/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +000094 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +000095
96 /* Check that system SEV_LEVEL output is correct */
97
subrata_modak56207ce2009-03-23 13:35:39 +000098 close(2); /* redirect stderr to file */
99 fd = creat("fmtfile", 0644);
100 ret_val = fmtmsg(MM_PRINT | MM_SOFT, "LTP:fmtmsg", MM_INFO,
101 "LTP fmtmsg() test1 message, NOT an error",
102 "This is correct output, no action needed",
103 "LTP:msg:001");
robbiewdc44d2e2002-12-30 21:32:36 +0000104 close(fd);
105
106 if (ret_val != 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000107 fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000108 local_flag = FAILED;
109 }
110
111 fp = fopen("fmtfile", "r");
112 clearbuf();
113 fread(buf, sizeof(buf[0]), strlen(str1), fp);
114 if (strcmp(str1, buf) != 0) {
115 fprintf(temp, "Expected string: %s\n", str1);
116 fprintf(temp, "does not match\n");
117 fprintf(temp, "received string: %s\n\n", buf);
118 local_flag = FAILED;
119 }
120
121 /* Read past spaces in output */
122 fread(&ch, sizeof(ch), 1, fp);
subrata_modak56207ce2009-03-23 13:35:39 +0000123 while (isspace(ch))
robbiewdc44d2e2002-12-30 21:32:36 +0000124 fread(&ch, sizeof(ch), 1, fp);
125 ungetc(ch, fp);
126
127 clearbuf();
128 fread(buf, sizeof(buf[0]), strlen(str2), fp);
129 fclose(fp);
130 if (strcmp(str2, buf) != 0) {
131 fprintf(temp, "Expected string: %s\n", str2);
132 fprintf(temp, "does not match\n");
133 fprintf(temp, "received string: %s\n\n", buf);
134 local_flag = FAILED;
135 }
136
137 blexit();
138/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000139 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +0000140
141 /* Check that a system defined SEV_LEVEL cannot get redefined */
142
143 ret_val = addseverity(3, "INVALID");
144 if (ret_val != MM_NOTOK) {
145 fprintf(temp, "addseverity returned %d, expected MM_NOTOK\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000146 ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000147 local_flag = FAILED;
148 }
149
150 blexit();
151/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000152 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +0000153
154 /* Check that we can define our own */
155 /* SEV_LEVEL and output is correct */
156
robbiew678a2702003-01-06 20:32:39 +0000157 ret_val = addseverity(5, "LTP_TEST");
robbiewdc44d2e2002-12-30 21:32:36 +0000158 if (ret_val != MM_OK) {
159 fprintf(temp, "addseverity returned %d, expected MM_OK\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000160 ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000161 local_flag = FAILED;
subrata_modak56207ce2009-03-23 13:35:39 +0000162 }
robbiewdc44d2e2002-12-30 21:32:36 +0000163
164 close(2); /* redirect stderr to file */
subrata_modak56207ce2009-03-23 13:35:39 +0000165 fd = creat("fmtfile", 0644);
166 ret_val = fmtmsg(MM_PRINT | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
167 "LTP fmtmsg() test2 message, NOT an error",
168 "This is correct output, no action needed",
169 "LTP:msg:002");
robbiewdc44d2e2002-12-30 21:32:36 +0000170 close(fd);
171
172 if (ret_val != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000173 fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000174 local_flag = FAILED;
subrata_modak56207ce2009-03-23 13:35:39 +0000175 }
robbiewdc44d2e2002-12-30 21:32:36 +0000176
177 fp = fopen("fmtfile", "r");
178 clearbuf();
179 fread(buf, sizeof(buf[0]), strlen(str3), fp);
180 if (strcmp(str3, buf) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000181 fprintf(temp, "Expected string: %s\n", str3);
182 fprintf(temp, "does not match\n");
183 fprintf(temp, "received string: %s\n\n", buf);
184 local_flag = FAILED;
185 }
robbiewdc44d2e2002-12-30 21:32:36 +0000186
187 /* Read past spaces in output */
188 fread(&ch, sizeof(ch), 1, fp);
subrata_modak56207ce2009-03-23 13:35:39 +0000189 while (isspace(ch))
robbiewdc44d2e2002-12-30 21:32:36 +0000190 fread(&ch, sizeof(ch), 1, fp);
191 ungetc(ch, fp);
192
193 clearbuf();
194 fread(buf, sizeof(buf[0]), strlen(str4), fp);
195 if (strcmp(str4, buf) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000196 fprintf(temp, "Expected string: %s\n", str4);
197 fprintf(temp, "does not match\n");
198 fprintf(temp, "received string: %s\n\n", buf);
199 local_flag = FAILED;
200 }
robbiewdc44d2e2002-12-30 21:32:36 +0000201
202 fclose(fp);
203 remove("fmtfile");
204
205 blexit();
206/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000207 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +0000208
subrata_modak56207ce2009-03-23 13:35:39 +0000209 /* Test result of writing to /dev/console */
robbiewdc44d2e2002-12-30 21:32:36 +0000210
subrata_modak56207ce2009-03-23 13:35:39 +0000211 ret_val = fmtmsg(MM_CONSOLE | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
212 "LTP fmtmsg() test3 message, NOT an error",
213 "This is correct output, no action needed",
214 "LTP:msg:003");
robbiewdc44d2e2002-12-30 21:32:36 +0000215 if (ret_val != MM_OK) {
216 fprintf(temp, "fmtmsg returned %d, expected MM_OK\n", ret_val);
217 fprintf(temp, "failed to write to console\n\n");
218 local_flag = FAILED;
219 }
220
221 blexit();
222/*--------------------------------------------------------------*/
223/* Clean up any files created by test before call to anyfail. */
224
subrata_modak56207ce2009-03-23 13:35:39 +0000225 anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */
Garrett Cooper2c282152010-12-16 00:55:50 -0800226 tst_exit();
robbiewdc44d2e2002-12-30 21:32:36 +0000227}
subrata_modak56207ce2009-03-23 13:35:39 +0000228
robbiewdc44d2e2002-12-30 21:32:36 +0000229/*--------------------------------------------------------------*/
230
231/***** LTP Port *****/
232/* FUNCTIONS GO HERE */
233
234int anyfail()
235{
subrata_modak56207ce2009-03-23 13:35:39 +0000236 (local_flag == FAILED) ? tst_resm(TFAIL,
237 "Test failed") : tst_resm(TPASS,
238 "Test passed");
239 tst_rmdir();
240 tst_exit();
241 return 0;
robbiewdc44d2e2002-12-30 21:32:36 +0000242}
243
robbiewdc44d2e2002-12-30 21:32:36 +0000244void setup()
245{
subrata_modak56207ce2009-03-23 13:35:39 +0000246 temp = stderr;
247 tst_tmpdir();
robbiewdc44d2e2002-12-30 21:32:36 +0000248}
249
robbiewdc44d2e2002-12-30 21:32:36 +0000250int blenter()
251{
subrata_modak56207ce2009-03-23 13:35:39 +0000252 //tst_resm(TINFO, "Enter block %d", block_number);
253 local_flag = PASSED;
254 return 0;
robbiewdc44d2e2002-12-30 21:32:36 +0000255}
256
257int blexit()
258{
subrata_modak56207ce2009-03-23 13:35:39 +0000259 //tst_resm(TINFO, "Exitng test");
260 (local_flag == FAILED) ? tst_resm(TFAIL,
261 "Test failed") : tst_resm(TPASS,
262 "Test passed");
263 return 0;
robbiewdc44d2e2002-12-30 21:32:36 +0000264}
265
robbiewd34d5812005-07-11 22:28:09 +0000266#else
267
268int main()
269{
vapier81a63072006-02-27 04:29:21 +0000270 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800271 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000272}
273
274#endif /* if !defined(UCLINUX) */
275
Chris Dearmanec6edca2012-10-17 19:54:01 -0700276/***** ** ** *****/