VirtualBox

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

Last change on this file since 34527 was 33656, checked in by vboxsync, 14 years ago

*: rebrand Sun (L)GPL disclaimers

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