]> git.proxmox.com Git - qemu.git/blobdiff - tests/linux-test.c
Merge remote branch 'mst/for_anthony' into staging
[qemu.git] / tests / linux-test.c
index 3ced0dd69a5311516a63dc10329fa4ec3b085b01..9986e299b21b1de521a3f9bcdf65c59194033ecc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  linux and CPU test
- * 
+ *
  *  Copyright (c) 2003 Fabrice Bellard
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 #include <stdarg.h>
 #include <stdlib.h>
@@ -38,6 +37,7 @@
 #include <sched.h>
 #include <dirent.h>
 #include <setjmp.h>
+#include <sys/shm.h>
 
 #define TESTPATH "/tmp/linux-test.tmp"
 #define TESTPORT 7654
@@ -57,13 +57,13 @@ void error1(const char *filename, int line, const char *fmt, ...)
 int __chk_error(const char *filename, int line, int ret)
 {
     if (ret < 0) {
-        error1(filename, line, "%m (ret=%d, errno=%d)", 
+        error1(filename, line, "%m (ret=%d, errno=%d)",
                ret, errno);
     }
     return ret;
 }
 
-#define error(fmt, args...) error1(__FILE__, __LINE__, fmt, ##args)
+#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
 
 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
 
@@ -71,7 +71,7 @@ int __chk_error(const char *filename, int line, int ret)
 
 #define FILE_BUF_SIZE 300
 
-void file_test(void)
+void test_file(void)
 {
     int fd, i, len, ret;
     uint8_t buf[FILE_BUF_SIZE];
@@ -92,11 +92,11 @@ void file_test(void)
 
     if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
         error("getcwd");
-    
+
     chk_error(mkdir(TESTPATH, 0755));
-    
+
     chk_error(chdir(TESTPATH));
-    
+
     /* open/read/write/close/readv/writev/lseek */
 
     fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644));
@@ -123,7 +123,7 @@ void file_test(void)
         error("read");
     if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0)
         error("memcmp");
-    
+
 #define FOFFSET 16
     ret = chk_error(lseek(fd, FOFFSET, SEEK_SET));
     if (ret != 16)
@@ -137,7 +137,7 @@ void file_test(void)
         error("readv");
     if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0)
         error("memcmp");
-    
+
     chk_error(close(fd));
 
     /* access */
@@ -170,18 +170,18 @@ void file_test(void)
     chk_error(ftruncate(fd, 50));
     chk_error(fstat(fd, &st));
     chk_error(close(fd));
-    
+
     if (st.st_size != 50)
         error("stat size");
     if (!S_ISREG(st.st_mode))
         error("stat mode");
-    
+
     /* symlink/lstat */
     chk_error(symlink("file2", "file3"));
     chk_error(lstat("file3", &st));
     if (!S_ISLNK(st.st_mode))
         error("stat mode");
-    
+
     /* getdents */
     dir = opendir(TESTPATH);
     if (!dir)
@@ -240,7 +240,7 @@ void test_time(void)
     ti = tv2.tv_sec - tv.tv_sec;
     if (ti >= 2)
         error("gettimeofday");
-    
+
     chk_error(getrusage(RUSAGE_SELF, &rusg1));
     for(i = 0;i < 10000; i++);
     chk_error(getrusage(RUSAGE_SELF, &rusg2));
@@ -271,7 +271,7 @@ char *pstrcat(char *buf, int buf_size, const char *s)
 {
     int len;
     len = strlen(buf);
-    if (len < buf_size) 
+    if (len < buf_size)
         pstrcpy(buf + len, buf_size - len, s);
     return buf;
 }
@@ -314,12 +314,19 @@ const char socket_msg[] = "hello socket\n";
 
 void test_socket(void)
 {
-    int server_fd, client_fd, fd, pid, ret;
+    int server_fd, client_fd, fd, pid, ret, val;
     struct sockaddr_in sockaddr;
     socklen_t len;
     char buf[512];
 
     server_fd = server_socket();
+
+    /* test a few socket options */
+    len = sizeof(val);
+    chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len));
+    if (val != SOCK_STREAM)
+        error("getsockopt");
+
     pid = chk_error(fork());
     if (pid == 0) {
         client_fd = client_socket();
@@ -411,11 +418,11 @@ void test_clone(void)
     int pid1, pid2, status1, status2;
 
     stack1 = malloc(STACK_SIZE);
-    pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE, 
+    pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE,
                            CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello1"));
 
     stack2 = malloc(STACK_SIZE);
-    pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE, 
+    pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE,
                            CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, "hello2"));
 
     while (waitpid(pid1, &status1, 0) != pid1);
@@ -457,7 +464,7 @@ void test_signal(void)
     sigemptyset(&act.sa_mask);
     act.sa_flags = 0;
     chk_error(sigaction(SIGALRM, &act, NULL));
-    
+
     it.it_interval.tv_sec = 0;
     it.it_interval.tv_usec = 10 * 1000;
     it.it_value.tv_sec = 0;
@@ -467,7 +474,7 @@ void test_signal(void)
     if (oit.it_value.tv_sec != it.it_value.tv_sec ||
         oit.it_value.tv_usec != it.it_value.tv_usec)
         error("itimer");
-    
+
     while (alarm_count < 5) {
         usleep(10 * 1000);
     }
@@ -490,21 +497,39 @@ void test_signal(void)
     if (setjmp(jmp_env) == 0) {
         *(uint8_t *)0 = 0;
     }
-    
+
     act.sa_handler = SIG_DFL;
     sigemptyset(&act.sa_mask);
     act.sa_flags = 0;
     chk_error(sigaction(SIGSEGV, &act, NULL));
 }
 
+#define SHM_SIZE 32768
+
+void test_shm(void)
+{
+    void *ptr;
+    int shmid;
+
+    shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
+    ptr = shmat(shmid, NULL, 0);
+    if (!ptr)
+        error("shmat");
+
+    memset(ptr, 0, SHM_SIZE);
+
+    chk_error(shmctl(shmid, IPC_RMID, 0));
+    chk_error(shmdt(ptr));
+}
+
 int main(int argc, char **argv)
 {
-    file_test();
+    test_file();
     test_fork();
     test_time();
     test_socket();
-    test_clone();
+    //    test_clone();
     test_signal();
+    test_shm();
     return 0;
 }
-