VirtualBox

source: vbox/trunk/src/recompiler/tests/linux-test.c@ 67439

Last change on this file since 67439 was 36175, checked in by vboxsync, 14 years ago

rem: Synced up to v0.11.1 (35bfc7324e2e6946c4113ada5db30553a1a7c40b) from git://git.savannah.nongnu.org/qemu.git.

  • Property svn:eol-style set to native
File size: 13.2 KB
Line 
1/*
2 * linux and CPU test
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
22 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
23 * the General Public License version 2 (GPLv2) at this time for any software where
24 * a choice of GPL license versions is made available with the language indicating
25 * that GPLv2 or any later version may be used, or where a choice of which version
26 * of the GPL is applied is otherwise unspecified.
27 */
28
29#include <stdarg.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <inttypes.h>
35#include <string.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39#include <errno.h>
40#include <utime.h>
41#include <time.h>
42#include <sys/time.h>
43#include <sys/uio.h>
44#include <sys/socket.h>
45#include <netinet/in.h>
46#include <arpa/inet.h>
47#include <sched.h>
48#include <dirent.h>
49#include <setjmp.h>
50#include <sys/shm.h>
51
52#define TESTPATH "/tmp/linux-test.tmp"
53#define TESTPORT 7654
54#define STACK_SIZE 16384
55
56void error1(const char *filename, int line, const char *fmt, ...)
57{
58 va_list ap;
59 va_start(ap, fmt);
60 fprintf(stderr, "%s:%d: ", filename, line);
61 vfprintf(stderr, fmt, ap);
62 fprintf(stderr, "\n");
63 va_end(ap);
64 exit(1);
65}
66
67int __chk_error(const char *filename, int line, int ret)
68{
69 if (ret < 0) {
70 error1(filename, line, "%m (ret=%d, errno=%d)",
71 ret, errno);
72 }
73 return ret;
74}
75
76#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
77
78#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
79
80/*******************************************************/
81
82#define FILE_BUF_SIZE 300
83
84void test_file(void)
85{
86 int fd, i, len, ret;
87 uint8_t buf[FILE_BUF_SIZE];
88 uint8_t buf2[FILE_BUF_SIZE];
89 uint8_t buf3[FILE_BUF_SIZE];
90 char cur_dir[1024];
91 struct stat st;
92 struct utimbuf tbuf;
93 struct iovec vecs[2];
94 DIR *dir;
95 struct dirent *de;
96
97 /* clean up, just in case */
98 unlink(TESTPATH "/file1");
99 unlink(TESTPATH "/file2");
100 unlink(TESTPATH "/file3");
101 rmdir(TESTPATH);
102
103 if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
104 error("getcwd");
105
106 chk_error(mkdir(TESTPATH, 0755));
107
108 chk_error(chdir(TESTPATH));
109
110 /* open/read/write/close/readv/writev/lseek */
111
112 fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644));
113 for(i=0;i < FILE_BUF_SIZE; i++)
114 buf[i] = i;
115 len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2));
116 if (len != (FILE_BUF_SIZE / 2))
117 error("write");
118 vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2);
119 vecs[0].iov_len = 16;
120 vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16;
121 vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16;
122 len = chk_error(writev(fd, vecs, 2));
123 if (len != (FILE_BUF_SIZE / 2))
124 error("writev");
125 chk_error(close(fd));
126
127 chk_error(rename("file1", "file2"));
128
129 fd = chk_error(open("file2", O_RDONLY));
130
131 len = chk_error(read(fd, buf2, FILE_BUF_SIZE));
132 if (len != FILE_BUF_SIZE)
133 error("read");
134 if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0)
135 error("memcmp");
136
137#define FOFFSET 16
138 ret = chk_error(lseek(fd, FOFFSET, SEEK_SET));
139 if (ret != 16)
140 error("lseek");
141 vecs[0].iov_base = buf3;
142 vecs[0].iov_len = 32;
143 vecs[1].iov_base = buf3 + 32;
144 vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32;
145 len = chk_error(readv(fd, vecs, 2));
146 if (len != FILE_BUF_SIZE - FOFFSET)
147 error("readv");
148 if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0)
149 error("memcmp");
150
151 chk_error(close(fd));
152
153 /* access */
154 chk_error(access("file2", R_OK));
155
156 /* stat/chmod/utime/truncate */
157
158 chk_error(chmod("file2", 0600));
159 tbuf.actime = 1001;
160 tbuf.modtime = 1000;
161 chk_error(truncate("file2", 100));
162 chk_error(utime("file2", &tbuf));
163 chk_error(stat("file2", &st));
164 if (st.st_size != 100)
165 error("stat size");
166 if (!S_ISREG(st.st_mode))
167 error("stat mode");
168 if ((st.st_mode & 0777) != 0600)
169 error("stat mode2");
170 if (st.st_atime != 1001 ||
171 st.st_mtime != 1000)
172 error("stat time");
173
174 chk_error(stat(TESTPATH, &st));
175 if (!S_ISDIR(st.st_mode))
176 error("stat mode");
177
178 /* fstat */
179 fd = chk_error(open("file2", O_RDWR));
180 chk_error(ftruncate(fd, 50));
181 chk_error(fstat(fd, &st));
182 chk_error(close(fd));
183
184 if (st.st_size != 50)
185 error("stat size");
186 if (!S_ISREG(st.st_mode))
187 error("stat mode");
188
189 /* symlink/lstat */
190 chk_error(symlink("file2", "file3"));
191 chk_error(lstat("file3", &st));
192 if (!S_ISLNK(st.st_mode))
193 error("stat mode");
194
195 /* getdents */
196 dir = opendir(TESTPATH);
197 if (!dir)
198 error("opendir");
199 len = 0;
200 for(;;) {
201 de = readdir(dir);
202 if (!de)
203 break;
204 if (strcmp(de->d_name, ".") != 0 &&
205 strcmp(de->d_name, "..") != 0 &&
206 strcmp(de->d_name, "file2") != 0 &&
207 strcmp(de->d_name, "file3") != 0)
208 error("readdir");
209 len++;
210 }
211 closedir(dir);
212 if (len != 4)
213 error("readdir");
214
215 chk_error(unlink("file3"));
216 chk_error(unlink("file2"));
217 chk_error(chdir(cur_dir));
218 chk_error(rmdir(TESTPATH));
219}
220
221void test_fork(void)
222{
223 int pid, status;
224
225 pid = chk_error(fork());
226 if (pid == 0) {
227 /* child */
228 exit(2);
229 }
230 chk_error(waitpid(pid, &status, 0));
231 if (!WIFEXITED(status) || WEXITSTATUS(status) != 2)
232 error("waitpid status=0x%x", status);
233}
234
235void test_time(void)
236{
237 struct timeval tv, tv2;
238 struct timespec ts, rem;
239 struct rusage rusg1, rusg2;
240 int ti, i;
241
242 chk_error(gettimeofday(&tv, NULL));
243 rem.tv_sec = 1;
244 ts.tv_sec = 0;
245 ts.tv_nsec = 20 * 1000000;
246 chk_error(nanosleep(&ts, &rem));
247 if (rem.tv_sec != 1)
248 error("nanosleep");
249 chk_error(gettimeofday(&tv2, NULL));
250 ti = tv2.tv_sec - tv.tv_sec;
251 if (ti >= 2)
252 error("gettimeofday");
253
254 chk_error(getrusage(RUSAGE_SELF, &rusg1));
255 for(i = 0;i < 10000; i++);
256 chk_error(getrusage(RUSAGE_SELF, &rusg2));
257 if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 ||
258 (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0)
259 error("getrusage");
260}
261
262void pstrcpy(char *buf, int buf_size, const char *str)
263{
264 int c;
265 char *q = buf;
266
267 if (buf_size <= 0)
268 return;
269
270 for(;;) {
271 c = *str++;
272 if (c == 0 || q >= buf + buf_size - 1)
273 break;
274 *q++ = c;
275 }
276 *q = '\0';
277}
278
279/* strcat and truncate. */
280char *pstrcat(char *buf, int buf_size, const char *s)
281{
282 int len;
283 len = strlen(buf);
284 if (len < buf_size)
285 pstrcpy(buf + len, buf_size - len, s);
286 return buf;
287}
288
289int server_socket(void)
290{
291 int val, fd;
292 struct sockaddr_in sockaddr;
293
294 /* server socket */
295 fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
296
297 val = 1;
298 chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)));
299
300 sockaddr.sin_family = AF_INET;
301 sockaddr.sin_port = htons(TESTPORT);
302 sockaddr.sin_addr.s_addr = 0;
303 chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
304 chk_error(listen(fd, 0));
305 return fd;
306
307}
308
309int client_socket(void)
310{
311 int fd;
312 struct sockaddr_in sockaddr;
313
314 /* server socket */
315 fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
316 sockaddr.sin_family = AF_INET;
317 sockaddr.sin_port = htons(TESTPORT);
318 inet_aton("127.0.0.1", &sockaddr.sin_addr);
319 chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
320 return fd;
321}
322
323const char socket_msg[] = "hello socket\n";
324
325void test_socket(void)
326{
327 int server_fd, client_fd, fd, pid, ret, val;
328 struct sockaddr_in sockaddr;
329 socklen_t len;
330 char buf[512];
331
332 server_fd = server_socket();
333
334 /* test a few socket options */
335 len = sizeof(val);
336 chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len));
337 if (val != SOCK_STREAM)
338 error("getsockopt");
339
340 pid = chk_error(fork());
341 if (pid == 0) {
342 client_fd = client_socket();
343 send(client_fd, socket_msg, sizeof(socket_msg), 0);
344 close(client_fd);
345 exit(0);
346 }
347 len = sizeof(sockaddr);
348 fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len));
349
350 ret = chk_error(recv(fd, buf, sizeof(buf), 0));
351 if (ret != sizeof(socket_msg))
352 error("recv");
353 if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0)
354 error("socket_msg");
355 chk_error(close(fd));
356 chk_error(close(server_fd));
357}
358
359#define WCOUNT_MAX 512
360
361void test_pipe(void)
362{
363 fd_set rfds, wfds;
364 int fds[2], fd_max, ret;
365 uint8_t ch;
366 int wcount, rcount;
367
368 chk_error(pipe(fds));
369 chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK));
370 chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK));
371 wcount = 0;
372 rcount = 0;
373 for(;;) {
374 FD_ZERO(&rfds);
375 fd_max = fds[0];
376 FD_SET(fds[0], &rfds);
377
378 FD_ZERO(&wfds);
379 FD_SET(fds[1], &wfds);
380 if (fds[1] > fd_max)
381 fd_max = fds[1];
382
383 ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL));
384 if (ret > 0) {
385 if (FD_ISSET(fds[0], &rfds)) {
386 chk_error(read(fds[0], &ch, 1));
387 rcount++;
388 if (rcount >= WCOUNT_MAX)
389 break;
390 }
391 if (FD_ISSET(fds[1], &wfds)) {
392 ch = 'a';
393 chk_error(write(fds[0], &ch, 1));
394 wcount++;
395 }
396 }
397 }
398 chk_error(close(fds[0]));
399 chk_error(close(fds[1]));
400}
401
402int thread1_res;
403int thread2_res;
404
405int thread1_func(void *arg)
406{
407 int i;
408 for(i=0;i<5;i++) {
409 thread1_res++;
410 usleep(10 * 1000);
411 }
412 return 0;
413}
414
415int thread2_func(void *arg)
416{
417 int i;
418 for(i=0;i<6;i++) {
419 thread2_res++;
420 usleep(10 * 1000);
421 }
422 return 0;
423}
424
425void test_clone(void)
426{
427 uint8_t *stack1, *stack2;
428 int pid1, pid2, status1, status2;
429
430 stack1 = malloc(STACK_SIZE);
431 pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE,
432 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello1"));
433
434 stack2 = malloc(STACK_SIZE);
435 pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE,
436 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello2"));
437
438 while (waitpid(pid1, &status1, 0) != pid1);
439 while (waitpid(pid2, &status2, 0) != pid2);
440 if (thread1_res != 5 ||
441 thread2_res != 6)
442 error("clone");
443}
444
445/***********************************/
446
447volatile int alarm_count;
448jmp_buf jmp_env;
449
450void sig_alarm(int sig)
451{
452 if (sig != SIGALRM)
453 error("signal");
454 alarm_count++;
455}
456
457void sig_segv(int sig, siginfo_t *info, void *puc)
458{
459 if (sig != SIGSEGV)
460 error("signal");
461 longjmp(jmp_env, 1);
462}
463
464void test_signal(void)
465{
466 struct sigaction act;
467 struct itimerval it, oit;
468
469 /* timer test */
470
471 alarm_count = 0;
472
473 act.sa_handler = sig_alarm;
474 sigemptyset(&act.sa_mask);
475 act.sa_flags = 0;
476 chk_error(sigaction(SIGALRM, &act, NULL));
477
478 it.it_interval.tv_sec = 0;
479 it.it_interval.tv_usec = 10 * 1000;
480 it.it_value.tv_sec = 0;
481 it.it_value.tv_usec = 10 * 1000;
482 chk_error(setitimer(ITIMER_REAL, &it, NULL));
483 chk_error(getitimer(ITIMER_REAL, &oit));
484 if (oit.it_value.tv_sec != it.it_value.tv_sec ||
485 oit.it_value.tv_usec != it.it_value.tv_usec)
486 error("itimer");
487
488 while (alarm_count < 5) {
489 usleep(10 * 1000);
490 }
491
492 it.it_interval.tv_sec = 0;
493 it.it_interval.tv_usec = 0;
494 it.it_value.tv_sec = 0;
495 it.it_value.tv_usec = 0;
496 memset(&oit, 0xff, sizeof(oit));
497 chk_error(setitimer(ITIMER_REAL, &it, &oit));
498 if (oit.it_value.tv_sec != 0 ||
499 oit.it_value.tv_usec != 10 * 1000)
500 error("setitimer");
501
502 /* SIGSEGV test */
503 act.sa_sigaction = sig_segv;
504 sigemptyset(&act.sa_mask);
505 act.sa_flags = SA_SIGINFO;
506 chk_error(sigaction(SIGSEGV, &act, NULL));
507 if (setjmp(jmp_env) == 0) {
508 *(uint8_t *)0 = 0;
509 }
510
511 act.sa_handler = SIG_DFL;
512 sigemptyset(&act.sa_mask);
513 act.sa_flags = 0;
514 chk_error(sigaction(SIGSEGV, &act, NULL));
515}
516
517#define SHM_SIZE 32768
518
519void test_shm(void)
520{
521 void *ptr;
522 int shmid;
523
524 shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
525 ptr = shmat(shmid, NULL, 0);
526 if (!ptr)
527 error("shmat");
528
529 memset(ptr, 0, SHM_SIZE);
530
531 chk_error(shmctl(shmid, IPC_RMID, 0));
532 chk_error(shmdt(ptr));
533}
534
535int main(int argc, char **argv)
536{
537 test_file();
538 test_fork();
539 test_time();
540 test_socket();
541 // test_clone();
542 test_signal();
543 test_shm();
544 return 0;
545}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette