blob: b41f6b244b6ca750f90682d52bb31fb42cb784e9 [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"
robbiewdc44d2e2002-12-30 21:32:36 +000051#define FAILED 0
52#define PASSED 1
53
54char *TCID = "fmtms01";
55
56int local_flag = PASSED;
57int block_number;
58FILE *temp;
59int TST_TOTAL = 1;
robbiewdc44d2e2002-12-30 21:32:36 +000060
61int anyfail();
62int blenter();
63int blexit();
64void setup();
65
66/***** ** ** *****/
67//char progname[]= "fmtmsg1()";
68
69char ch;
70char buf[80];
subrata_modak56207ce2009-03-23 13:35:39 +000071char *str1 = "LTP:fmtmsg: INFO: LTP fmtmsg() test1 message, NOT an error";
72char *str2 = "TO FIX: This is correct output, no action needed LTP:msg:001";
73char *str3 = "LTP:fmtmsg: LTP_TEST: LTP fmtmsg() test2 message, NOT an error";
74char *str4 = "TO FIX: This is correct output, no action needed LTP:msg:002";
robbiewdc44d2e2002-12-30 21:32:36 +000075
Mike Frysingerc57fba52014-04-09 18:56:30 -040076void clearbuf(void)
robbiewdc44d2e2002-12-30 21:32:36 +000077{
78 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000079 for (i = 0; i < 80; i++)
subrata_modakb15aafd2008-10-20 06:30:32 +000080 buf[i] = '\0';
robbiewdc44d2e2002-12-30 21:32:36 +000081}
82
robbiewe93a3242005-08-31 20:27:12 +000083#if !defined(UCLINUX) && !defined(__UCLIBC__)
robbiewd34d5812005-07-11 22:28:09 +000084
robbiewdc44d2e2002-12-30 21:32:36 +000085/*--------------------------------------------------------------*/
robbiew678a2702003-01-06 20:32:39 +000086int main(int argc, char *argv[])
robbiewdc44d2e2002-12-30 21:32:36 +000087{
88 int fd, ret_val;
89 FILE *fp;
90
91 setup(); /* temp file is now open */
92/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +000093 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +000094
95 /* Check that system SEV_LEVEL output is correct */
96
subrata_modak56207ce2009-03-23 13:35:39 +000097 close(2); /* redirect stderr to file */
98 fd = creat("fmtfile", 0644);
99 ret_val = fmtmsg(MM_PRINT | MM_SOFT, "LTP:fmtmsg", MM_INFO,
100 "LTP fmtmsg() test1 message, NOT an error",
101 "This is correct output, no action needed",
102 "LTP:msg:001");
robbiewdc44d2e2002-12-30 21:32:36 +0000103 close(fd);
104
105 if (ret_val != 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000106 fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000107 local_flag = FAILED;
108 }
109
110 fp = fopen("fmtfile", "r");
111 clearbuf();
112 fread(buf, sizeof(buf[0]), strlen(str1), fp);
113 if (strcmp(str1, buf) != 0) {
114 fprintf(temp, "Expected string: %s\n", str1);
115 fprintf(temp, "does not match\n");
116 fprintf(temp, "received string: %s\n\n", buf);
117 local_flag = FAILED;
118 }
119
120 /* Read past spaces in output */
121 fread(&ch, sizeof(ch), 1, fp);
subrata_modak56207ce2009-03-23 13:35:39 +0000122 while (isspace(ch))
robbiewdc44d2e2002-12-30 21:32:36 +0000123 fread(&ch, sizeof(ch), 1, fp);
124 ungetc(ch, fp);
125
126 clearbuf();
127 fread(buf, sizeof(buf[0]), strlen(str2), fp);
128 fclose(fp);
129 if (strcmp(str2, buf) != 0) {
130 fprintf(temp, "Expected string: %s\n", str2);
131 fprintf(temp, "does not match\n");
132 fprintf(temp, "received string: %s\n\n", buf);
133 local_flag = FAILED;
134 }
135
136 blexit();
137/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000138 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +0000139
140 /* Check that a system defined SEV_LEVEL cannot get redefined */
141
142 ret_val = addseverity(3, "INVALID");
143 if (ret_val != MM_NOTOK) {
144 fprintf(temp, "addseverity returned %d, expected MM_NOTOK\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000145 ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000146 local_flag = FAILED;
147 }
148
149 blexit();
150/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000151 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +0000152
153 /* Check that we can define our own */
154 /* SEV_LEVEL and output is correct */
155
robbiew678a2702003-01-06 20:32:39 +0000156 ret_val = addseverity(5, "LTP_TEST");
robbiewdc44d2e2002-12-30 21:32:36 +0000157 if (ret_val != MM_OK) {
158 fprintf(temp, "addseverity returned %d, expected MM_OK\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000159 ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000160 local_flag = FAILED;
subrata_modak56207ce2009-03-23 13:35:39 +0000161 }
robbiewdc44d2e2002-12-30 21:32:36 +0000162
163 close(2); /* redirect stderr to file */
subrata_modak56207ce2009-03-23 13:35:39 +0000164 fd = creat("fmtfile", 0644);
165 ret_val = fmtmsg(MM_PRINT | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
166 "LTP fmtmsg() test2 message, NOT an error",
167 "This is correct output, no action needed",
168 "LTP:msg:002");
robbiewdc44d2e2002-12-30 21:32:36 +0000169 close(fd);
170
171 if (ret_val != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000172 fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val);
robbiewdc44d2e2002-12-30 21:32:36 +0000173 local_flag = FAILED;
subrata_modak56207ce2009-03-23 13:35:39 +0000174 }
robbiewdc44d2e2002-12-30 21:32:36 +0000175
176 fp = fopen("fmtfile", "r");
177 clearbuf();
178 fread(buf, sizeof(buf[0]), strlen(str3), fp);
179 if (strcmp(str3, buf) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000180 fprintf(temp, "Expected string: %s\n", str3);
181 fprintf(temp, "does not match\n");
182 fprintf(temp, "received string: %s\n\n", buf);
183 local_flag = FAILED;
184 }
robbiewdc44d2e2002-12-30 21:32:36 +0000185
186 /* Read past spaces in output */
187 fread(&ch, sizeof(ch), 1, fp);
subrata_modak56207ce2009-03-23 13:35:39 +0000188 while (isspace(ch))
robbiewdc44d2e2002-12-30 21:32:36 +0000189 fread(&ch, sizeof(ch), 1, fp);
190 ungetc(ch, fp);
191
192 clearbuf();
193 fread(buf, sizeof(buf[0]), strlen(str4), fp);
194 if (strcmp(str4, buf) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000195 fprintf(temp, "Expected string: %s\n", str4);
196 fprintf(temp, "does not match\n");
197 fprintf(temp, "received string: %s\n\n", buf);
198 local_flag = FAILED;
199 }
robbiewdc44d2e2002-12-30 21:32:36 +0000200
201 fclose(fp);
202 remove("fmtfile");
203
204 blexit();
205/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000206 blenter();
robbiewdc44d2e2002-12-30 21:32:36 +0000207
subrata_modak56207ce2009-03-23 13:35:39 +0000208 /* Test result of writing to /dev/console */
robbiewdc44d2e2002-12-30 21:32:36 +0000209
subrata_modak56207ce2009-03-23 13:35:39 +0000210 ret_val = fmtmsg(MM_CONSOLE | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
211 "LTP fmtmsg() test3 message, NOT an error",
212 "This is correct output, no action needed",
213 "LTP:msg:003");
robbiewdc44d2e2002-12-30 21:32:36 +0000214 if (ret_val != MM_OK) {
215 fprintf(temp, "fmtmsg returned %d, expected MM_OK\n", ret_val);
216 fprintf(temp, "failed to write to console\n\n");
217 local_flag = FAILED;
218 }
219
220 blexit();
221/*--------------------------------------------------------------*/
222/* Clean up any files created by test before call to anyfail. */
223
subrata_modak56207ce2009-03-23 13:35:39 +0000224 anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */
Garrett Cooper2c282152010-12-16 00:55:50 -0800225 tst_exit();
robbiewdc44d2e2002-12-30 21:32:36 +0000226}
subrata_modak56207ce2009-03-23 13:35:39 +0000227
robbiewdc44d2e2002-12-30 21:32:36 +0000228/*--------------------------------------------------------------*/
229
230/***** LTP Port *****/
231/* FUNCTIONS GO HERE */
232
Mike Frysingerc57fba52014-04-09 18:56:30 -0400233int anyfail(void)
robbiewdc44d2e2002-12-30 21:32:36 +0000234{
subrata_modak56207ce2009-03-23 13:35:39 +0000235 (local_flag == FAILED) ? tst_resm(TFAIL,
236 "Test failed") : tst_resm(TPASS,
237 "Test passed");
238 tst_rmdir();
239 tst_exit();
robbiewdc44d2e2002-12-30 21:32:36 +0000240}
241
Mike Frysingerc57fba52014-04-09 18:56:30 -0400242void setup(void)
robbiewdc44d2e2002-12-30 21:32:36 +0000243{
subrata_modak56207ce2009-03-23 13:35:39 +0000244 temp = stderr;
245 tst_tmpdir();
robbiewdc44d2e2002-12-30 21:32:36 +0000246}
247
Mike Frysingerc57fba52014-04-09 18:56:30 -0400248int blenter(void)
robbiewdc44d2e2002-12-30 21:32:36 +0000249{
subrata_modak56207ce2009-03-23 13:35:39 +0000250 //tst_resm(TINFO, "Enter block %d", block_number);
251 local_flag = PASSED;
252 return 0;
robbiewdc44d2e2002-12-30 21:32:36 +0000253}
254
Mike Frysingerc57fba52014-04-09 18:56:30 -0400255int blexit(void)
robbiewdc44d2e2002-12-30 21:32:36 +0000256{
subrata_modak56207ce2009-03-23 13:35:39 +0000257 //tst_resm(TINFO, "Exitng test");
258 (local_flag == FAILED) ? tst_resm(TFAIL,
259 "Test failed") : tst_resm(TPASS,
260 "Test passed");
261 return 0;
robbiewdc44d2e2002-12-30 21:32:36 +0000262}
263
robbiewd34d5812005-07-11 22:28:09 +0000264#else
265
Mike Frysingerc57fba52014-04-09 18:56:30 -0400266int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000267{
vapier81a63072006-02-27 04:29:21 +0000268 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800269 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000270}
271
272#endif /* if !defined(UCLINUX) */
273
Chris Dearmanec6edca2012-10-17 19:54:01 -0700274/***** ** ** *****/