1515#include <fcntl.h>
1616#include <dirent.h>
1717
18- #define SECRET_LEN 10
1918
20- int main () {
21- int fd ;
22- int fd2 ;
23- int rtn ;
19+ void test_no_exist () {
2420 struct stat st ;
25- struct stat st2 ;
26- char buf [100 ];
27- char secret2 [] = SECRET2 ;
28- int len2 = SECRET_LEN / 2 ;
29-
30- rtn = stat ("/work/notexist.txt" , & st );
21+ int rtn = stat ("/work/notexist.txt" , & st );
3122 assert (rtn == -1 && errno == ENOENT );
23+ }
3224
33- rtn = stat ("/work/blob.txt" , & st );
25+ void test_blob_txt () {
26+ // Check that /work/blob.txt contains the expected data.
27+ const char content [] = "hello blob" ;
28+ size_t file_len = sizeof (content ) - 1 ;
29+
30+ struct stat st ;
31+ char buf [100 ];
32+ int rtn = stat ("/work/blob.txt" , & st );
3433 assert (rtn == 0 );
3534
36- fd = open ("/work/blob.txt" , O_RDWR , 0666 );
35+ int fd = open ("/work/blob.txt" , O_RDWR , 0666 );
3736 assert (fd >= 0 );
3837
39- rtn = read (fd , buf , 1000 );
40- assert (rtn == SECRET_LEN );
41- assert (strncmp (buf , SECRET , SECRET_LEN ) == 0 );
38+ ssize_t cnt = read (fd , buf , 1000 );
39+ printf ("read blob.txt: %ld, expecting: %lu\n" , cnt , file_len );
40+ assert (cnt == file_len );
41+ assert (strcmp (buf , content ) == 0 );
42+ }
43+
44+ void test_file_txt () {
45+ // Seek half way through /work/file.txt then verify the data read from that point on.
46+ const char content [] = "hello file" ;
47+ size_t file_len = sizeof (content ) - 1 ;
48+ char buf [100 ];
49+ off_t offset = file_len / 2 ;
4250
43- fd2 = open ("/work/file.txt" , O_RDONLY , 0666 );
44- assert (fd2 != -1 );
51+ int fd = open ("/work/file.txt" , O_RDONLY , 0666 );
52+ assert (fd != -1 );
4553
46- rtn = lseek (fd2 , len2 , SEEK_SET );
47- assert (rtn == len2 );
54+ off_t rtn = lseek (fd , offset , SEEK_SET );
55+ assert (rtn == offset );
4856
49- rtn = read (fd2 , buf , len2 );
50- assert (rtn == len2 );
51- assert (strncmp (buf , secret2 + len2 , len2 ) == 0 );
57+ ssize_t cnt = read (fd , buf , 1000 );
58+ printf ("read file.txt: %ld, expecting: %llu\n" , cnt , file_len - offset );
59+ assert (cnt == file_len - offset );
60+ assert (strncmp (buf , content + offset , file_len - offset ) == 0 );
61+ }
5262
63+ void test_chmod () {
64+ struct stat st , st2 ;
5365 stat ("/work/file.txt" , & st );
5466 chmod ("/work/file.txt" , 0640 );
5567 stat ("/work/file.txt" , & st2 );
5668 assert (st .st_mode == (0777 | S_IFREG ) && st2 .st_mode == (0640 | S_IFREG ));
69+ }
5770
71+ void test_readdir () {
5872 DIR * pDir = opendir ("/work/" );
5973 assert (pDir );
6074
@@ -72,12 +86,23 @@ int main() {
7286
7387 assert (blobFileExists );
7488 assert (fileTxtExists );
89+ }
7590
91+ void test_readlink () {
7692 // attemping to read a worker node as a link should result in EINVAL
93+ char buf [100 ];
7794 buf [0 ] = '\0' ;
7895 assert (readlink ("/work/blob.txt" , buf , sizeof (buf )) == -1 );
7996 assert (buf [0 ] == '\0' );
8097 assert (errno == EINVAL );
98+ }
8199
100+ int main () {
101+ test_no_exist ();
102+ test_blob_txt ();
103+ test_file_txt ();
104+ test_chmod ();
105+ test_readdir ();
106+ test_readlink ();
82107 return 0 ;
83108}
0 commit comments