VirtualBox

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

最後變更 在這個檔案從36170是 36170,由 vboxsync 提交於 14 年 前

rem: synced up to svn://svn.savannah.nongnu.org/qemu/trunk@6686 (repo UUID c046a42c-6fe2-441c-8c8c-71466251a162).

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

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