support >2TB SCSI disks (Rik van Riel)
[qemu/mini2440/sniper_sniper_test.git] / tests / linux-test.c
blob19c0e1abb9b20c12fb29990b4c7de418e21b1438
1 /*
2 * linux and CPU test
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <errno.h>
32 #include <utime.h>
33 #include <time.h>
34 #include <sys/time.h>
35 #include <sys/uio.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <sched.h>
40 #include <dirent.h>
41 #include <setjmp.h>
42 #include <sys/shm.h>
44 #define TESTPATH "/tmp/linux-test.tmp"
45 #define TESTPORT 7654
46 #define STACK_SIZE 16384
48 void error1(const char *filename, int line, const char *fmt, ...)
50 va_list ap;
51 va_start(ap, fmt);
52 fprintf(stderr, "%s:%d: ", filename, line);
53 vfprintf(stderr, fmt, ap);
54 fprintf(stderr, "\n");
55 va_end(ap);
56 exit(1);
59 int __chk_error(const char *filename, int line, int ret)
61 if (ret < 0) {
62 error1(filename, line, "%m (ret=%d, errno=%d)",
63 ret, errno);
65 return ret;
68 #define error(fmt, args...) error1(__FILE__, __LINE__, fmt, ##args)
70 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
72 /*******************************************************/
74 #define FILE_BUF_SIZE 300
76 void test_file(void)
78 int fd, i, len, ret;
79 uint8_t buf[FILE_BUF_SIZE];
80 uint8_t buf2[FILE_BUF_SIZE];
81 uint8_t buf3[FILE_BUF_SIZE];
82 char cur_dir[1024];
83 struct stat st;
84 struct utimbuf tbuf;
85 struct iovec vecs[2];
86 DIR *dir;
87 struct dirent *de;
89 /* clean up, just in case */
90 unlink(TESTPATH "/file1");
91 unlink(TESTPATH "/file2");
92 unlink(TESTPATH "/file3");
93 rmdir(TESTPATH);
95 if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
96 error("getcwd");
98 chk_error(mkdir(TESTPATH, 0755));
100 chk_error(chdir(TESTPATH));
102 /* open/read/write/close/readv/writev/lseek */
104 fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644));
105 for(i=0;i < FILE_BUF_SIZE; i++)
106 buf[i] = i;
107 len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2));
108 if (len != (FILE_BUF_SIZE / 2))
109 error("write");
110 vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2);
111 vecs[0].iov_len = 16;
112 vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16;
113 vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16;
114 len = chk_error(writev(fd, vecs, 2));
115 if (len != (FILE_BUF_SIZE / 2))
116 error("writev");
117 chk_error(close(fd));
119 chk_error(rename("file1", "file2"));
121 fd = chk_error(open("file2", O_RDONLY));
123 len = chk_error(read(fd, buf2, FILE_BUF_SIZE));
124 if (len != FILE_BUF_SIZE)
125 error("read");
126 if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0)
127 error("memcmp");
129 #define FOFFSET 16
130 ret = chk_error(lseek(fd, FOFFSET, SEEK_SET));
131 if (ret != 16)
132 error("lseek");
133 vecs[0].iov_base = buf3;
134 vecs[0].iov_len = 32;
135 vecs[1].iov_base = buf3 + 32;
136 vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32;
137 len = chk_error(readv(fd, vecs, 2));
138 if (len != FILE_BUF_SIZE - FOFFSET)
139 error("readv");
140 if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0)
141 error("memcmp");
143 chk_error(close(fd));
145 /* access */
146 chk_error(access("file2", R_OK));
148 /* stat/chmod/utime/truncate */
150 chk_error(chmod("file2", 0600));
151 tbuf.actime = 1001;
152 tbuf.modtime = 1000;
153 chk_error(truncate("file2", 100));
154 chk_error(utime("file2", &tbuf));
155 chk_error(stat("file2", &st));
156 if (st.st_size != 100)
157 error("stat size");
158 if (!S_ISREG(st.st_mode))
159 error("stat mode");
160 if ((st.st_mode & 0777) != 0600)
161 error("stat mode2");
162 if (st.st_atime != 1001 ||
163 st.st_mtime != 1000)
164 error("stat time");
166 chk_error(stat(TESTPATH, &st));
167 if (!S_ISDIR(st.st_mode))
168 error("stat mode");
170 /* fstat */
171 fd = chk_error(open("file2", O_RDWR));
172 chk_error(ftruncate(fd, 50));
173 chk_error(fstat(fd, &st));
174 chk_error(close(fd));
176 if (st.st_size != 50)
177 error("stat size");
178 if (!S_ISREG(st.st_mode))
179 error("stat mode");
181 /* symlink/lstat */
182 chk_error(symlink("file2", "file3"));
183 chk_error(lstat("file3", &st));
184 if (!S_ISLNK(st.st_mode))
185 error("stat mode");
187 /* getdents */
188 dir = opendir(TESTPATH);
189 if (!dir)
190 error("opendir");
191 len = 0;
192 for(;;) {
193 de = readdir(dir);
194 if (!de)
195 break;
196 if (strcmp(de->d_name, ".") != 0 &&
197 strcmp(de->d_name, "..") != 0 &&
198 strcmp(de->d_name, "file2") != 0 &&
199 strcmp(de->d_name, "file3") != 0)
200 error("readdir");
201 len++;
203 closedir(dir);
204 if (len != 4)
205 error("readdir");
207 chk_error(unlink("file3"));
208 chk_error(unlink("file2"));
209 chk_error(chdir(cur_dir));
210 chk_error(rmdir(TESTPATH));
213 void test_fork(void)
215 int pid, status;
217 pid = chk_error(fork());
218 if (pid == 0) {
219 /* child */
220 exit(2);
222 chk_error(waitpid(pid, &status, 0));
223 if (!WIFEXITED(status) || WEXITSTATUS(status) != 2)
224 error("waitpid status=0x%x", status);
227 void test_time(void)
229 struct timeval tv, tv2;
230 struct timespec ts, rem;
231 struct rusage rusg1, rusg2;
232 int ti, i;
234 chk_error(gettimeofday(&tv, NULL));
235 rem.tv_sec = 1;
236 ts.tv_sec = 0;
237 ts.tv_nsec = 20 * 1000000;
238 chk_error(nanosleep(&ts, &rem));
239 if (rem.tv_sec != 1)
240 error("nanosleep");
241 chk_error(gettimeofday(&tv2, NULL));
242 ti = tv2.tv_sec - tv.tv_sec;
243 if (ti >= 2)
244 error("gettimeofday");
246 chk_error(getrusage(RUSAGE_SELF, &rusg1));
247 for(i = 0;i < 10000; i++);
248 chk_error(getrusage(RUSAGE_SELF, &rusg2));
249 if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 ||
250 (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0)
251 error("getrusage");
254 void pstrcpy(char *buf, int buf_size, const char *str)
256 int c;
257 char *q = buf;
259 if (buf_size <= 0)
260 return;
262 for(;;) {
263 c = *str++;
264 if (c == 0 || q >= buf + buf_size - 1)
265 break;
266 *q++ = c;
268 *q = '\0';
271 /* strcat and truncate. */
272 char *pstrcat(char *buf, int buf_size, const char *s)
274 int len;
275 len = strlen(buf);
276 if (len < buf_size)
277 pstrcpy(buf + len, buf_size - len, s);
278 return buf;
281 int server_socket(void)
283 int val, fd;
284 struct sockaddr_in sockaddr;
286 /* server socket */
287 fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
289 val = 1;
290 chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)));
292 sockaddr.sin_family = AF_INET;
293 sockaddr.sin_port = htons(TESTPORT);
294 sockaddr.sin_addr.s_addr = 0;
295 chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
296 chk_error(listen(fd, 0));
297 return fd;
301 int client_socket(void)
303 int fd;
304 struct sockaddr_in sockaddr;
306 /* server socket */
307 fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
308 sockaddr.sin_family = AF_INET;
309 sockaddr.sin_port = htons(TESTPORT);
310 inet_aton("127.0.0.1", &sockaddr.sin_addr);
311 chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
312 return fd;
315 const char socket_msg[] = "hello socket\n";
317 void test_socket(void)
319 int server_fd, client_fd, fd, pid, ret, val;
320 struct sockaddr_in sockaddr;
321 socklen_t len;
322 char buf[512];
324 server_fd = server_socket();
326 /* test a few socket options */
327 len = sizeof(val);
328 chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len));
329 if (val != SOCK_STREAM)
330 error("getsockopt");
332 pid = chk_error(fork());
333 if (pid == 0) {
334 client_fd = client_socket();
335 send(client_fd, socket_msg, sizeof(socket_msg), 0);
336 close(client_fd);
337 exit(0);
339 len = sizeof(sockaddr);
340 fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len));
342 ret = chk_error(recv(fd, buf, sizeof(buf), 0));
343 if (ret != sizeof(socket_msg))
344 error("recv");
345 if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0)
346 error("socket_msg");
347 chk_error(close(fd));
348 chk_error(close(server_fd));
351 #define WCOUNT_MAX 512
353 void test_pipe(void)
355 fd_set rfds, wfds;
356 int fds[2], fd_max, ret;
357 uint8_t ch;
358 int wcount, rcount;
360 chk_error(pipe(fds));
361 chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK));
362 chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK));
363 wcount = 0;
364 rcount = 0;
365 for(;;) {
366 FD_ZERO(&rfds);
367 fd_max = fds[0];
368 FD_SET(fds[0], &rfds);
370 FD_ZERO(&wfds);
371 FD_SET(fds[1], &wfds);
372 if (fds[1] > fd_max)
373 fd_max = fds[1];
375 ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL));
376 if (ret > 0) {
377 if (FD_ISSET(fds[0], &rfds)) {
378 chk_error(read(fds[0], &ch, 1));
379 rcount++;
380 if (rcount >= WCOUNT_MAX)
381 break;
383 if (FD_ISSET(fds[1], &wfds)) {
384 ch = 'a';
385 chk_error(write(fds[0], &ch, 1));
386 wcount++;
390 chk_error(close(fds[0]));
391 chk_error(close(fds[1]));
394 int thread1_res;
395 int thread2_res;
397 int thread1_func(void *arg)
399 int i;
400 for(i=0;i<5;i++) {
401 thread1_res++;
402 usleep(10 * 1000);
404 return 0;
407 int thread2_func(void *arg)
409 int i;
410 for(i=0;i<6;i++) {
411 thread2_res++;
412 usleep(10 * 1000);
414 return 0;
417 void test_clone(void)
419 uint8_t *stack1, *stack2;
420 int pid1, pid2, status1, status2;
422 stack1 = malloc(STACK_SIZE);
423 pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE,
424 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello1"));
426 stack2 = malloc(STACK_SIZE);
427 pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE,
428 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello2"));
430 while (waitpid(pid1, &status1, 0) != pid1);
431 while (waitpid(pid2, &status2, 0) != pid2);
432 if (thread1_res != 5 ||
433 thread2_res != 6)
434 error("clone");
437 /***********************************/
439 volatile int alarm_count;
440 jmp_buf jmp_env;
442 void sig_alarm(int sig)
444 if (sig != SIGALRM)
445 error("signal");
446 alarm_count++;
449 void sig_segv(int sig, siginfo_t *info, void *puc)
451 if (sig != SIGSEGV)
452 error("signal");
453 longjmp(jmp_env, 1);
456 void test_signal(void)
458 struct sigaction act;
459 struct itimerval it, oit;
461 /* timer test */
463 alarm_count = 0;
465 act.sa_handler = sig_alarm;
466 sigemptyset(&act.sa_mask);
467 act.sa_flags = 0;
468 chk_error(sigaction(SIGALRM, &act, NULL));
470 it.it_interval.tv_sec = 0;
471 it.it_interval.tv_usec = 10 * 1000;
472 it.it_value.tv_sec = 0;
473 it.it_value.tv_usec = 10 * 1000;
474 chk_error(setitimer(ITIMER_REAL, &it, NULL));
475 chk_error(getitimer(ITIMER_REAL, &oit));
476 if (oit.it_value.tv_sec != it.it_value.tv_sec ||
477 oit.it_value.tv_usec != it.it_value.tv_usec)
478 error("itimer");
480 while (alarm_count < 5) {
481 usleep(10 * 1000);
484 it.it_interval.tv_sec = 0;
485 it.it_interval.tv_usec = 0;
486 it.it_value.tv_sec = 0;
487 it.it_value.tv_usec = 0;
488 memset(&oit, 0xff, sizeof(oit));
489 chk_error(setitimer(ITIMER_REAL, &it, &oit));
490 if (oit.it_value.tv_sec != 0 ||
491 oit.it_value.tv_usec != 10 * 1000)
492 error("setitimer");
494 /* SIGSEGV test */
495 act.sa_sigaction = sig_segv;
496 sigemptyset(&act.sa_mask);
497 act.sa_flags = SA_SIGINFO;
498 chk_error(sigaction(SIGSEGV, &act, NULL));
499 if (setjmp(jmp_env) == 0) {
500 *(uint8_t *)0 = 0;
503 act.sa_handler = SIG_DFL;
504 sigemptyset(&act.sa_mask);
505 act.sa_flags = 0;
506 chk_error(sigaction(SIGSEGV, &act, NULL));
509 #define SHM_SIZE 32768
511 void test_shm(void)
513 void *ptr;
514 int shmid;
516 shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
517 ptr = shmat(shmid, NULL, 0);
518 if (!ptr)
519 error("shmat");
521 memset(ptr, 0, SHM_SIZE);
523 chk_error(shmctl(shmid, IPC_RMID, 0));
524 chk_error(shmdt(ptr));
527 int main(int argc, char **argv)
529 test_file();
530 test_fork();
531 test_time();
532 test_socket();
533 // test_clone();
534 test_signal();
535 test_shm();
536 return 0;