1 /*-
2  * Copyright (c) 2008 Hyogeol Lee <hyogeollee@gmail.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $Id: test_nm.c 2378 2012-01-03 08:59:56Z jkoshy $
27  */
28 
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <tet_api.h>
35 
36 static int	exec_cmd(const char *, const char *);
37 static void	startup();
38 static void	test_bsd();
39 static void	test_dynamic();
40 static void	test_external();
41 static void	test_hexa();
42 static bool	test_nm_out(const char *, const char *);
43 static void	test_no_sort();
44 static void	test_num_sort();
45 static void	test_octal();
46 static void	test_posix();
47 static void	test_print_filename();
48 static void	test_print_size();
49 static void	test_reverse_sort();
50 static void	test_size_sort();
51 static void	test_sysv();
52 static void	test_undef();
53 
54 struct tet_testlist tet_testlist[] = {
55 	{ test_dynamic, 1},
56 	{ test_external, 2},
57 	{ test_num_sort, 3},
58 	{ test_no_sort, 4},
59 	{ test_posix, 5},
60 	{ test_print_size, 6},
61 	{ test_undef, 7},
62 	{ test_size_sort, 8},
63 	{ test_sysv, 9},
64 	{ test_bsd, 10},
65 	{ test_print_filename, 11},
66 	{ test_octal, 12},
67 	{ test_hexa, 13},
68 	{ test_reverse_sort, 14},
69 	{ NULL, 0}
70 };
71 
72 #define	NM_CMD		NM " %s " TESTFILE " > test.out"
73 #define DIFF_CMD	"diff test.out " TC_DIR "/" TESTFILE "%s.txt > /dev/null"
74 
75 void (*tet_startup)() = startup;
76 void (*tet_cleanup)() = NULL;
77 
78 static int
exec_cmd(const char * cmd,const char * op)79 exec_cmd(const char *cmd, const char *op)
80 {
81 	char *this_cmd;
82 	int rtn;
83 	size_t cmd_len;
84 
85 	if (cmd == NULL || op == NULL)
86 		return (-1);
87 
88 	cmd_len = strlen(cmd) + strlen(op);
89 
90 	if ((this_cmd = malloc(sizeof(char) * cmd_len)) == NULL) {
91 		tet_infoline("cannot allocate memory");
92 
93 		return (-1);
94 	}
95 
96 	snprintf(this_cmd, cmd_len, cmd, op);
97 
98 	rtn = system(this_cmd);
99 
100 	free(this_cmd);
101 
102 	return (rtn);
103 }
104 
105 static void
startup()106 startup()
107 {
108 
109 	if (system("cp " TC_DIR "/" TESTFILE " .") < 0) {
110 		tet_infoline("cannot cp object");
111 
112 		exit(EXIT_FAILURE);
113 	}
114 }
115 
116 static void
test_bsd()117 test_bsd()
118 {
119 	bool rtn = true;
120 
121 	tet_infoline("OPTION -B, --format=bsd");
122 
123 	rtn |= test_nm_out("-B", "-B");
124 	rtn |= test_nm_out("--format=bsd", "-B");
125 
126 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
127 }
128 
129 static void
test_dynamic()130 test_dynamic()
131 {
132 	bool rtn = true;
133 
134 	tet_infoline("OPTION -D, --dynamic");
135 
136 	rtn |= test_nm_out("-D", "-D");
137 	rtn |= test_nm_out("--dynamic", "-D");
138 
139 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
140 }
141 
142 static void
test_external()143 test_external()
144 {
145 
146 	tet_infoline("OPTION -g");
147 
148 	if (test_nm_out("-g", "-g") == true)
149 		tet_result(TET_PASS);
150 	else
151 		tet_result(TET_FAIL);
152 }
153 
154 static void
test_hexa()155 test_hexa()
156 {
157 	bool rtn = true;
158 
159 	tet_infoline("OPTION -x, -t x");
160 
161 	rtn |= test_nm_out("-x", "-x");
162 	rtn |= test_nm_out("-t x", "-x");
163 	rtn |= test_nm_out("--radix=x", "-x");
164 
165 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
166 }
167 
168 static bool
test_nm_out(const char * op,const char * d_op)169 test_nm_out(const char *op, const char *d_op)
170 {
171 	int rtn;
172 
173 	if (op == NULL) {
174 		tet_result(TET_FAIL);
175 
176 		return (false);
177 	}
178 
179 	if ((rtn = exec_cmd(NM_CMD, op)) < 0) {
180 		tet_infoline("system function failed");
181 
182 		return (false);
183 	} else if (rtn == 127) {
184 		tet_infoline("execution shell failed");
185 
186 		return (false);
187 	}
188 
189 	if ((rtn = exec_cmd(DIFF_CMD, d_op)) < 0)
190 		tet_infoline("system function failed");
191 	else {
192 		switch (rtn) {
193 		case 127:
194 			tet_infoline("execution shell failed");
195 
196 			break;
197 		case 2:
198 			tet_infoline("diff has trouble");
199 
200 			break;
201 		case 1:
202 			tet_infoline("output is different");
203 
204 			break;
205 		case 0:
206 			return (true);
207 		}
208 	}
209 
210 	return (false);
211 }
212 
213 static void
test_no_sort()214 test_no_sort()
215 {
216 	bool rtn = true;
217 
218 	tet_infoline("OPTION -p");
219 
220 	rtn |= test_nm_out("-p", "-p");
221 	rtn |= test_nm_out("--no-sort", "-p");
222 
223 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
224 }
225 
226 static void
test_num_sort()227 test_num_sort()
228 {
229 	bool rtn = true;
230 
231 	tet_infoline("OPTION -n, --numeric-sort");
232 
233 	rtn |= test_nm_out("-n", "-n");
234 	rtn |= test_nm_out("--numeric-sort", "-n");
235 
236 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
237 }
238 
239 static void
test_octal()240 test_octal()
241 {
242 	bool rtn = true;
243 
244 	tet_infoline("OPTION --radix=o, -t o");
245 
246 	rtn |= test_nm_out("-t o", "-o");
247 	rtn |= test_nm_out("--radix=o", "-o");
248 
249 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
250 }
251 
252 static void
test_posix()253 test_posix()
254 {
255 	bool rtn = true;
256 
257 	tet_infoline("OPTION -P, --format=posix");
258 
259 	rtn |= test_nm_out("-P", "-P");
260 	rtn |= test_nm_out("--format=posix", "-P");
261 
262 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
263 }
264 
265 static void
test_print_filename()266 test_print_filename()
267 {
268 	bool rtn = true;
269 
270 	tet_infoline("OPTION -A, --print-file-name");
271 
272 	rtn |= test_nm_out("-A", "-A");
273 	rtn |= test_nm_out("--print-file-name", "-A");
274 
275 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
276 }
277 
278 static void
test_print_size()279 test_print_size()
280 {
281 	bool rtn = true;
282 
283 	tet_infoline("OPTION -S, --print-size");
284 
285 	rtn |= test_nm_out("-S", "-S");
286 	rtn |= test_nm_out("--print-size", "-S");
287 
288 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
289 }
290 
291 static void
test_reverse_sort()292 test_reverse_sort()
293 {
294 	bool rtn = true;
295 
296 	tet_infoline("OPTION -r, --reverse-sort");
297 
298 	rtn |= test_nm_out("-r", "-r");
299 	rtn |= test_nm_out("--reverse-sort", "-r");
300 
301 	rtn |= test_nm_out("-r -n", "-r-n");
302 	rtn |= test_nm_out("-r -p", "-r-p");
303 	rtn |= test_nm_out("-r --size-sort", "-r-size-sort");
304 
305 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
306 }
307 
308 static void
test_size_sort()309 test_size_sort()
310 {
311 
312 	tet_infoline("OPTION --size-sort");
313 
314 	if (test_nm_out("--size-sort", "-size-sort") == true)
315 		tet_result(TET_PASS);
316 	else
317 		tet_result(TET_FAIL);
318 }
319 
320 static void
test_sysv()321 test_sysv()
322 {
323 
324 	tet_infoline("OPTION --format=sysv");
325 
326 	if (test_nm_out("--format=sysv", "-sysv") == true)
327 		tet_result(TET_PASS);
328 	else
329 		tet_result(TET_FAIL);
330 }
331 
332 static void
test_undef()333 test_undef()
334 {
335 	bool rtn = true;
336 
337 	tet_infoline("OPTION -u, --undefined-only");
338 
339 	rtn |= test_nm_out("-u", "-u");
340 	rtn |= test_nm_out("--undefined-only", "-u");
341 
342 	tet_result(rtn == true ? TET_PASS : TET_FAIL);
343 }
344