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 * 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>
41 #if !defined(UCLINUX) && !defined(__UCLIBC__)
42 #include <fmtmsg.h> /* interface definition */
43 #endif
44 #include <string.h>
45
46 /***** LTP Port *****/
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <errno.h>
50 #include "test.h"
51 #define FAILED 0
52 #define PASSED 1
53
54 char *TCID = "fmtms01";
55
56 int local_flag = PASSED;
57 int block_number;
58 FILE *temp;
59 int TST_TOTAL = 1;
60
61 int anyfail();
62 int blenter();
63 int blexit();
64 void setup();
65
66 /***** ** ** *****/
67 //char progname[]= "fmtmsg1()";
68
69 char ch;
70 char buf[80];
71 char *str1 = "LTP:fmtmsg: INFO: LTP fmtmsg() test1 message, NOT an error";
72 char *str2 = "TO FIX: This is correct output, no action needed LTP:msg:001";
73 char *str3 = "LTP:fmtmsg: LTP_TEST: LTP fmtmsg() test2 message, NOT an error";
74 char *str4 = "TO FIX: This is correct output, no action needed LTP:msg:002";
75
clearbuf(void)76 void clearbuf(void)
77 {
78 int i;
79 for (i = 0; i < 80; i++)
80 buf[i] = '\0';
81 }
82
83 #if !defined(UCLINUX) && !defined(__UCLIBC__)
84
85 /*--------------------------------------------------------------*/
main(int argc,char * argv[])86 int main(int argc, char *argv[])
87 {
88 int fd, ret_val;
89 FILE *fp;
90
91 setup(); /* temp file is now open */
92 /*--------------------------------------------------------------*/
93 blenter();
94
95 /* Check that system SEV_LEVEL output is correct */
96
97 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");
103 close(fd);
104
105 if (ret_val != 0) {
106 fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val);
107 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);
122 while (isspace(ch))
123 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 /*--------------------------------------------------------------*/
138 blenter();
139
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",
145 ret_val);
146 local_flag = FAILED;
147 }
148
149 blexit();
150 /*--------------------------------------------------------------*/
151 blenter();
152
153 /* Check that we can define our own */
154 /* SEV_LEVEL and output is correct */
155
156 ret_val = addseverity(5, "LTP_TEST");
157 if (ret_val != MM_OK) {
158 fprintf(temp, "addseverity returned %d, expected MM_OK\n",
159 ret_val);
160 local_flag = FAILED;
161 }
162
163 close(2); /* redirect stderr to file */
164 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");
169 close(fd);
170
171 if (ret_val != 0) {
172 fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val);
173 local_flag = FAILED;
174 }
175
176 fp = fopen("fmtfile", "r");
177 clearbuf();
178 fread(buf, sizeof(buf[0]), strlen(str3), fp);
179 if (strcmp(str3, buf) != 0) {
180 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 }
185
186 /* Read past spaces in output */
187 fread(&ch, sizeof(ch), 1, fp);
188 while (isspace(ch))
189 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) {
195 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 }
200
201 fclose(fp);
202 remove("fmtfile");
203
204 blexit();
205 /*--------------------------------------------------------------*/
206 blenter();
207
208 /* Test result of writing to /dev/console */
209
210 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");
214 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
224 anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */
225 tst_exit();
226 }
227
228 /*--------------------------------------------------------------*/
229
230 /***** LTP Port *****/
231 /* FUNCTIONS GO HERE */
232
anyfail(void)233 int anyfail(void)
234 {
235 (local_flag == FAILED) ? tst_resm(TFAIL,
236 "Test failed") : tst_resm(TPASS,
237 "Test passed");
238 tst_rmdir();
239 tst_exit();
240 }
241
setup(void)242 void setup(void)
243 {
244 temp = stderr;
245 tst_tmpdir();
246 }
247
blenter(void)248 int blenter(void)
249 {
250 //tst_resm(TINFO, "Enter block %d", block_number);
251 local_flag = PASSED;
252 return 0;
253 }
254
blexit(void)255 int blexit(void)
256 {
257 //tst_resm(TINFO, "Exitng test");
258 (local_flag == FAILED) ? tst_resm(TFAIL,
259 "Test failed") : tst_resm(TPASS,
260 "Test passed");
261 return 0;
262 }
263
264 #else
265
main(void)266 int main(void)
267 {
268 tst_resm(TINFO, "test is not available on uClinux");
269 tst_exit();
270 }
271
272 #endif /* if !defined(UCLINUX) */
273
274 /***** ** ** *****/
275