aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/file_syscall_tests.c
blob: 53358eb20bbef5ca12853dbab2aa4aac399647d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* klaar@ida

   pintos -v -k --fs-disk=2 -p ../examples/file_syscall_tests -a fst -- -f -q run 'fst testing one two three'

   NOTE:
        "file_syscall_tests" is too long a filename for Pintos, it
        must be shortened. The simplest way is to rename it to a
        shorter name using the "-a" flag as in the command line above.

   Test program for basic system calls. Inspired by previous versions
   by various IDA-employees / lab-assistanst.
   
   Emergency exit QEMU bu pressing C-a x (release Ctrl before pressing x)
 
   DONE: Warn when '\r' instead of '\n'
   DONE: Check result for JUNK
   DONE: Compare/verify content of buffer after reads.
   DONE: Make sure content of buffer is known/reset before filling it.
 */

#include <stdio.h>
#include <string.h>
#include <syscall.h>

#define SIZE 1024
#define JUNK 0xCCCC /* 52428 */
#define CRAP 0xDDDD

#define msg( comment ) \
  write ( STDOUT_FILENO, "\n" comment "\n", strlen (comment) + 2 )

#define end( comment ) \
  write ( STDOUT_FILENO, "\n" comment "\n", strlen (comment) + 2 );\
  skip_line ()

static void skip_line( void );
static int  get_line( char* buf, int size );
static void verify(int test);
static void init_buffer(char*, int);

int main(int argc, char* argv[])
{
  int id = JUNK;
  int i, j;
  
  msg ( "* ------------------ write screen test ------------------ *" );
  {
    char* msg = "Now displaying the arguments to main\n";
    int length = strlen (msg);
    int result = JUNK;
    
    result = write (STDOUT_FILENO, msg, length);

    for ( i = 0; i < argc; ++i )
    {
      write (STDOUT_FILENO, argv[i], strlen (argv[i]) );
      write (STDOUT_FILENO, "\n", 1);
    }
    
    verify (length == result);
  }
  end ( "* -------------------- press enter ---------------------- *" );
  
  printf ("To emergency exit QEMU at any time:\n");
  printf ("Hold 'Control' and press 'a' and then \n");
  printf ("release 'Control' and press 'x'\n");
    
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ----------------- read keyboard test ------------------ *" );
  {
    char* input = "qwerty1234";
    char buffer[10];
    int length = CRAP;
    int result = JUNK;
    char yes;

    init_buffer(buffer, 10);
    
    printf ("Will now try to read 10 characters.\n");
    printf ("Please write \"%s\": ", input);
    result = read ( STDIN_FILENO, buffer, 10 );
    length = strlen( buffer );
    printf ("\nThe following characters was read: '%s'\n", buffer);
    
    verify ( length == result && memcmp(buffer, input, 10) == 0 );
    
    printf ("\nDid you see each character as you typed it? (y/n) ");
    result = read ( STDIN_FILENO, &yes, 1 );
    printf ("\n");
    
    verify ( result == 1 && yes == 'y');
  }
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ------------------ create file test ------------------- *" );
  {
    int success = JUNK;
    
    printf ("Will try to create 'test.txt'\n");
    success = create("test.txt", SIZE);    
    verify ( success != JUNK && success );
  }
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ------------------ open file test --------------------- *" );
  {
    printf ("Will try to open 'non_existent_file'\n");
    id = open("non_existent_file");    
    verify ( id == -1 );
    
    printf ("Will try to open 'test.txt'\n");
    id = open("test.txt");
    verify ( id > 1 );
  }
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ------------------ write file test -------------------- *" );
  {
    char buffer[8];
    int result = JUNK;
    bool success = true;

    init_buffer(buffer, 8);
    
    printf ("Will try to write a sequence to '%d'\n", id);
    for ( i = 0; i < 16; ++i)
    {
      for ( j = 0; j < 16; ++j)
      {
        snprintf(buffer, 8, "%4d", i*16+j);
        result = write(STDOUT_FILENO, buffer, 4);
        result = write(id, buffer, 4);
        success = success && (result == 4);
      }
      result = write(STDOUT_FILENO, "\n", 1);
    }
    verify ( success );

    printf ("Will try to write 8 characters to '%d'\n", JUNK);
    result = write(JUNK, buffer, 8);
    verify ( result == -1 );
  }
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ------------------ read file test --------------------- *" );
  {
    char buffer[8];
    char verify_buffer[8];
    int result = JUNK;
    bool success = true;
    
    init_buffer(buffer, 8);
    init_buffer(verify_buffer, 8);
    
    printf ("Will try to reopen 'test.txt'\n");
    close(id);
    id = open("test.txt");
    
    printf ("Will try to read a sequence from '%d'\n", id);
    for ( i = 0; i < 16; ++i)
    {
      for ( j = 0; j < 16; ++j)
      {
        snprintf(verify_buffer, 8, "%4d", i*16+j);
        result = read(id, buffer, 4);
        success = success && (result == 4);
        result = write(STDOUT_FILENO, buffer, 4);
        
        success = success && (memcmp(buffer, verify_buffer, 4) == 0);
      }
      result = write(STDOUT_FILENO, "\n", 1);
    }
    verify ( success );

    printf ("Will try to read 8 characters from '%d'\n", JUNK);
    result = read(JUNK, buffer, 8);
    verify ( result == -1 );
  }
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ------------------ close file test -------------------- *" );
  {
    char buffer[128];
    int result = JUNK;
    
    init_buffer(buffer, 128);
    
    printf ("Will try to close 'STDIN_FILENO' and then read from it.\n");
    printf ("Write some input please: ");
    close(STDIN_FILENO);
    result = get_line(buffer, 128);
    verify ( result < 128 && result == (int)strlen(buffer) );
    
    printf ("Will try to close 'STDOUT_FILENO' and then write to it.\n");
    close(STDOUT_FILENO);
    result = write(STDOUT_FILENO, buffer, strlen(buffer));
    printf ("\n");
    verify ( result == (int)strlen(buffer) );
    
    printf ("Will try to close id '%d' and then read from it\n", id);
    close(id);
    result = read(id, buffer, 128);
    verify ( result == -1 );
    
    printf ("Will try to close id '%d' and then read from it\n", id);
    close(JUNK);
    result = read(JUNK, buffer, 128);
    verify ( result == -1 );
  }
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ------------------ remove file test ------------------- *" );
  {
    int success = JUNK;
    
    printf ("Will try to remove 'test.txt' and then open it\n");
    success = remove("test.txt");
    id = open("test.txt");    
    verify ( success && id == -1 );
    
    printf ("Will try to remove 'non_existent_file'\n");
    success = remove("non_existent_file");
    verify ( ! success );
  }
  end ( "* -------------------- press enter ---------------------- *" );
  
  printf ("To emergency exit QEMU at any time:\n");
  printf ("Hold 'Control' and press 'a' and then \n");
  printf ("release 'Control' and press 'x'\n");
    
  end ( "* -------------------- press enter ---------------------- *" );

  
  msg ( "* ---------------- seek/tell file test ------------------ *" );
  {
    char buffer[8];
    char verify_buffer[8];
    int result = JUNK;
    int success = CRAP;
    
    init_buffer(buffer, 8);
    init_buffer(verify_buffer, 8);
    
    printf ("Will try to create and open 'test.txt'\n");
    success = create("test.txt", SIZE);
    id = open("test.txt");    
    verify ( success != CRAP && success && id > 1 );

    printf ("Will try to write a sequence to '%d'\n", id);
    for ( i = 0; i < 16; ++i)
    {
      for ( j = 0; j < 16; ++j)
      {
        snprintf (buffer, 8, "%4d", j*16+i);
        seek (id, (j*16+i)*4);
        result = write (STDOUT_FILENO, buffer, 4);
        result = write (id, buffer, 4);
        success = success && (result == 4);
        result = tell (id);
        success = success && (result == (j*16+i+1)*4);
      }
      result = write(STDOUT_FILENO, "\n", 1);
    }
    verify ( success );
    
    printf ("Will try to read the sequence from '%d'\n", id);
    seek (id, 0);
    for ( i = 0; i < 16; ++i)
    {
      for ( j = 0; j < 16; ++j)
      {
        snprintf (verify_buffer, 8, "%4d", i*16+j);        
        result = read(id, buffer, 4);
        success = success && (result == 4);
        result = write(STDOUT_FILENO, buffer, 4);
        
        success = success && (memcmp(buffer, verify_buffer, 4) == 0);
      }
      result = write(STDOUT_FILENO, "\n", 1);
    }
    result = tell (id);
    verify ( success && result == 256*4 );

    printf ("Will try to determine filesize and seek past it\n");
    result = filesize (id);
    seek (id, result*2);
    verify ( result == SIZE );
  }
  end ( "* -------------------- press enter ---------------------- *" );
  return 0;
}


const char* crlf_warning = ("\033[1;31;40mWARNING\033[1;0m: "
			    "You should convert '\\r' to '\\n'.\n");

static void skip_line( void )
{
  char c = '\0';
  
  while ( c != '\n' && c != '\r' )
    read ( STDIN_FILENO, &c, 1);

  if ( c == '\r' )
    write ( STDOUT_FILENO, crlf_warning, strlen ( crlf_warning ) );
}


static int get_line( char* buf, int size )
{
  int i;
  
  for ( i = 0; i < (size - 1); ++i)
  {
    buf[i] = '\0';
    
    if (read ( STDIN_FILENO, buf+i, 1) != 1)
      return -1;
    
    if ( buf[i] == '\r' )
      write ( STDOUT_FILENO, crlf_warning, strlen ( crlf_warning ) );

    if ( buf[i] == '\n' )
      break;
  }
  buf[i] = '\0';
  return i;
}


static void verify(int test)
{
  /* will only print nice in vt100 terminal */
  /* assuming black background */
  const char* result[3] = {"\033[1;31;40m WRONG RESULT \033[1;0m \n",
                           "\033[1;32;40m RESULT OK \033[1;0m \n",
                           "\033[1;31;40m BAD BOOLEAN \033[1;0m \n"};
  
  if (test < 0 || test > 1)
    test = 2;
  
  write ( STDOUT_FILENO, result[ test ], strlen (result[ test ]) );
}


static void init_buffer(char* buffer, int size)
{
  const char* data = "ERROR";
  const int S = strlen(data);
  int i;

  for (i = 0; i < (size - 1); ++i)
  {
    buffer[i] = data[i % S];
  }
  buffer[i] = '\0';
}