c223d1cc0dbeea4010f5184b892a92fa298f9e78
[nestedvm.git] / src / tests / Test.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/fcntl.h>
7 #include <sys/unistd.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <sys/signal.h>
11 #include <time.h>
12 #include <dirent.h>
13 #include <wchar.h>
14 #include <math.h>
15
16 char *user_info[1024];
17
18 extern void _pause();
19 extern int _call_java(int a, int b, int c, int d);
20
21 void suckram();
22
23 int main(int argc, char **argv) {
24     int i,n,fd;
25     time_t now;
26     DIR *dir;
27     struct dirent *dent;
28     char buf[1024];
29     unsigned char ubuf[1024];
30     unsigned short sbuf[1024];
31     char *s;
32     
33     printf("Entered main()\n");
34     
35     if(argc > 1 && strcmp(argv[1],"calltest")==0)  {
36         printf("pausing for call test\n");
37         _pause();
38         printf("unpaused from call test\n");
39         
40         for(i=1;i<=3;i++) {
41             char *s = (char*)_call_java(i,0,0,0);
42             printf("_call_java(%d,0,0,0) = \"%s\" (%d chars)\n",i,s,strlen(s));
43             free(s);
44         }
45         fd = _call_java(4,0,0,0);
46         if(fd != -1) {
47             FILE *fp;
48             fprintf(stderr,"fd: %i\n",fd);
49             fp = fdopen(fd,"w");
50             if(fp != NULL) {
51                 fprintf(fp,"It worked! fp is %p - Hello, Java!\n",fp);
52                 fclose(fp);
53             } else {
54                 fprintf(stderr,"fdopen failed\n");
55                 close(fd);
56             }
57         } else {
58             fprintf(stderr,"fd == -1\n");
59         }
60         
61         printf("In main() in MIPS\n");
62         _call_java(5,0,0,0);
63         printf("Back in main() in MIPS\n");
64     } else if(argc > 2 && strcmp(argv[1],"fdtest")==0)  {
65         printf("opening %s\n",argv[2]);
66         fd = open(argv[2],O_RDONLY);
67         if(fd < 0) { perror("open"); exit(1); }
68         
69         printf("reading up to 64 bytes\n");
70         n = read(fd,buf,64);
71         if(n < 0) {perror("read"); exit(1); }
72         printf("read %d bytes\n",n);
73         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
74         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
75         
76         printf("seeking back to pos 4...\n");
77         if(lseek(fd,4,SEEK_SET) < 0) { perror("lseek"); exit(1); }
78         
79         printf("reading up to 64 bytes\n");
80         n = read(fd,buf,64);
81         if(n < 0) {perror("read"); exit(1); }
82         printf("read %d bytes\n",n);
83         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
84         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
85
86         printf("reading up to 64 bytes\n");
87         n = read(fd,buf,64);
88         if(n < 0) {perror("read"); exit(1); }
89         printf("read %d bytes\n",n);
90         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
91         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
92     } else if(argc > 1 && strcmp(argv[1],"fptest")==0)  {
93         double d = 0.0;
94         while(d != 10.0) {
95             printf("d: %f\n",d);
96             d += 2.5;
97         }
98     } else if(argc > 1 && strcmp(argv[1],"nullderef")==0) {
99         volatile int *mem = 0;
100         *mem = 1;
101     } else if(argc > 2 && strcmp(argv[1],"crashme") == 0) {
102         volatile int *mem = (int*) atoi(argv[2]);
103         *mem = 1;
104     } else if(argc > 2 && strcmp(argv[1],"get") == 0) {
105         extern int _open_socket(const char *host, int port);
106         fd = _open_socket(argv[2],80);
107         if(fd == -1) { perror("open_socket"); exit(EXIT_FAILURE); }
108 #define REQ "GET / HTTP/1.0\r\n\r\n"
109         n = write(fd,REQ,strlen(REQ));
110         if(n != strlen(REQ)) { perror("write"); exit(EXIT_FAILURE); }
111         for(;;) {
112             n = read(fd,buf,sizeof(buf));
113             if(n < 0) { perror("read"); exit(EXIT_FAILURE); }
114             if(n == 0) break;
115             write(1,buf,n);
116         }
117     } else if(argc > 1 && strcmp(argv[1],"server") == 0) {
118         extern int _listen_socket(int port);
119         extern int _accept(int fd);
120         
121         int server = _listen_socket(2000);
122         if(server< 0) { perror("server_socket"); exit(EXIT_FAILURE); }
123         while((fd = _accept(server)) >= 0) {
124             char buf2[1024];
125             int n = read(fd,buf,sizeof(buf));
126             if(n < 0) { perror("read"); continue; }
127             if(n == 0) continue;
128             while(n > 0 && (buf[n-1] == '\r' || buf[n-1] == '\n')) n--;
129             buf[n] = '\0';
130             snprintf(buf2,sizeof(buf2),"Hello, %s from nestedvm's socket support\r\n",buf);
131             write(fd,buf2,strlen(buf2));
132             close(fd);
133         }
134     } else { 
135         printf("%d\n", 0xffffff);
136         printf("%u\n", 0xffffffU);
137         printf("%li\n",0xffffffL);
138         printf("%lu\n",0xffffffUL);
139
140         
141         for(i=0;i<argc;i++)
142             printf("argv[%d] = \"%s\"\n",i,argv[i]);
143         for(i=0;user_info[i];i++)
144             printf("user_info[%d] = \"%s\"\n",i,user_info[i]);
145         
146         printf("getenv(\"USER\") = \"%s\"\n",getenv("USER"));
147         printf("getenv(\"HOME\") = \"%s\"\n",getenv("HOME"));
148         printf("getenv(\"TZ\") = \"%s\"\n",getenv("TZ"));
149     
150         time(&now);
151         tzset();
152         printf("%s %s %d\n",tzname[0],tzname[1],(int)_timezone);
153         
154         printf("Running ctime\n");
155         s = ctime(&now);
156         printf("ctime returned: %p\n",s);
157         printf("Current time: %s",s);
158         
159         printf("Trying to open /nonexistent\n");
160         fd = open("/nonexistent",O_RDONLY);
161         if(fd < 0) perror("open");
162         else close(fd);
163         
164         printf("Tyring to mkdir .mkdirtest\n");
165         if(mkdir(".mkdirtest",0700) < 0) perror("mkdir");
166         
167         printf("Trying to opendir .\n");
168         dir = opendir(".");
169         if(dir) {
170             printf("Success!\n");
171             while((dent=readdir(dir))!=NULL) {
172                 struct stat statbuf;
173                 stat(dent->d_name,&statbuf);
174                 printf("\t[%s] %lu %i %i\n",dent->d_name,dent->d_ino,statbuf.st_ino,statbuf.st_dev);
175             }
176             if(errno != 0) { fprintf(stderr,"readdir errno: %d\n",errno); perror("readdir"); }
177             closedir(dir);
178         } else {
179             perror("opendir");
180         }
181                 
182     
183 #if 0
184         printf("Sleeping...\n");
185         sleep(1);
186         printf("Done\n");
187 #endif
188         
189         fd = open("test.txt",O_RDONLY);
190         if(fd != -1) {
191             printf("Opened test.txt\n");
192             n = read(fd,sbuf,sizeof(sbuf));
193             printf("n: %d\n",n);
194             if(n < 0) perror("read");
195             ubuf[n] = '\0';
196             printf("buf: %s\n",buf);
197             for(i=0;i<n/2;i++) {
198                 printf("Char %d: [%x]\n",i,sbuf[i]);
199             }
200         }
201         
202         {
203             static double f = 1.574;
204             int n;
205             printf("%e\n",f);
206             f += 20.001;
207             f *= -2.0;
208             n = (int) f;
209             printf("%el\n",f);
210             printf("%d\n",n);
211             printf("%e\n",f);
212             printf("%e\n",fabs(f));
213         }
214     }
215         
216     {
217         char buf[1024];
218         memcpy(buf,"Hello, World",sizeof("Hello, World"));
219         printf("%s\n",buf);
220     }
221     
222     {
223         
224 #define HOST_BITS_PER_WIDE_INT 64
225 #define HOST_WIDE_INT long long
226         
227         extern int ri(int n);
228         int precision = ri(8);
229         long long l;
230         
231         l = (precision - HOST_BITS_PER_WIDE_INT > 0
232              ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
233             (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
234              ? (((HOST_WIDE_INT) 1
235                  << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
236              : 0);
237         
238         printf("%llX\n",l);
239     }
240     
241     //printf("cwd: %s\n",getcwd(NULL,0));
242     //printf("isatty(0): %d\n",isatty(0));
243     //printf("exiting\n");
244     return 0;
245 }
246
247 long long zero = 0;
248 int izero = 0;
249 long long rl(long long n) { return n + zero; }
250 int ri(int n) { return n + izero; }
251
252 void suckram() {
253     int total = 0;
254     fprintf(stderr,"Eating up all available memory\n");
255     while(malloc(1024*1024) != NULL) total ++;
256     fprintf(stderr,"Ate up %d megs\n",total);
257 }
258
259 __attribute__((constructor)) static void my_ctor()  { printf("Constructor!\n"); }
260 __attribute__((destructor)) static void my_dtor()  { printf("Destructor!\n"); }
261
262 int callme(int a1,int a2, int a3, int a4, int a5, int a6)  __attribute__((section(".text")));
263 int callme(int a1,int a2, int a3, int a4, int a5, int a6) {
264     printf("You said: %d %d %d %d %d %d\n",a1,a2,a3,a4,a5,a6);
265     return a1+a2+a3+a4+a5+a6;
266 }
267
268 void echo(const char *string, int count)  __attribute__((section(".text")));
269 void echo(const char *string, int count) {
270     int i;
271     for(i=0;i<count;i++)
272         printf("%d: %s\n",i,string);
273 }
274
275 void backinmips()  __attribute__((section(".text")));
276 void backinmips() {
277     fprintf(stderr,"In backinmips() in mips\n");
278 }