Mercurial > trustbridge
comparison ui/listutil.c @ 7:992c0ec57660
Add unit tests make CertificateList work.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Wed, 12 Feb 2014 16:52:27 +0000 |
parents | 9849250f50f2 |
children | 2ad9a96518e3 |
comparison
equal
deleted
inserted
replaced
6:1f6455d46b30 | 7:992c0ec57660 |
---|---|
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <string.h> | |
10 | 11 |
11 #define MAX_FILESIZE_KB 1024 | 12 #define MAX_FILESIZE_KB 1024 |
12 | 13 |
13 void handle_errno() | 14 void handle_errno() |
14 { | 15 { |
15 printf("Error: %s \n", strerror(errno)); | 16 printf("Error: %s \n", strerror(errno)); |
16 } | 17 } |
17 | 18 |
18 list_status_t readList(char *fileName, void *data, size_t *size) | 19 list_status_t readList(const char *fileName, char **data, size_t *size) |
19 { | 20 { |
20 int fd = -1; | 21 int fd = -1; |
21 struct stat fileStat; | 22 struct stat fileStat; |
22 int rc = 0; | 23 int rc = 0; |
23 ssize_t bRead = 0; | 24 ssize_t bRead = 0; |
25 | |
26 memset(&fileStat, 0, sizeof(fileStat)); | |
27 | |
24 list_status_t retval = UnknownError; | 28 list_status_t retval = UnknownError; |
25 | 29 |
26 fd = open(fileName, O_RDONLY); | 30 fd = open(fileName, O_RDONLY); |
27 if (fd == -1) { | 31 if (fd == -1) { |
28 handle_errno(); | 32 handle_errno(); |
29 retval = StatFailed; | 33 retval = StatFailed; |
30 goto failure; | 34 goto cleanup; |
31 } | 35 } |
32 | 36 |
33 rc = fstat(fd, &fileStat); | 37 rc = fstat(fd, &fileStat); |
34 if (rc < 0) { | 38 if (rc < 0) { |
35 printf ("Stat failed with rc: %i\n", rc); | 39 printf ("Stat failed with rc: %i\n", rc); |
36 retval = StatFailed; | 40 retval = StatFailed; |
37 goto failure; | 41 goto cleanup; |
38 } | 42 } |
39 | 43 |
40 // Check the size of the file | 44 // Check the size of the file |
41 if (fileStat.st_size) { | 45 if (!fileStat.st_size) { |
42 printf("Size zero\n"); | 46 printf("Size zero\n"); |
43 retval = StatFailed; | 47 retval = StatFailed; |
44 goto failure; | 48 goto cleanup; |
45 } | 49 } |
46 | 50 |
47 if (fileStat.st_size / 1024 > MAX_FILESIZE_KB || | 51 if (fileStat.st_size / 1024 > MAX_FILESIZE_KB && |
48 fileStat.st_size >= (size_t)-1) { | 52 fileStat.st_size > 0) { |
49 printf("File too large\n"); | 53 printf("File too large\n"); |
50 retval = TooLarge; | 54 retval = TooLarge; |
51 goto failure; | 55 goto cleanup; |
52 } | 56 } |
53 | 57 |
54 // We can cast here as we already checked | |
55 // that this size is not too large for size_t | |
56 *size = (size_t) fileStat.st_size; | 58 *size = (size_t) fileStat.st_size; |
57 | 59 |
58 data = malloc(*size); | 60 *data = (char*) malloc(*size); |
59 | 61 |
60 if (data == NULL) { | 62 if (data == NULL) { |
61 printf("Malloc failed\n"); | 63 printf("Malloc failed\n"); |
62 retval = UnknownError; | 64 retval = UnknownError; |
63 goto failure; | 65 goto cleanup; |
64 } | 66 } |
65 | 67 |
66 bRead = read(fd, data, *size); | 68 bRead = read(fd, *data, *size); |
67 | 69 |
68 if (bRead != *size) { | 70 if (bRead < 0 || (size_t) bRead != *size) { |
69 printf("Read failed"); | 71 printf("Read failed\n"); |
70 if (bRead == -1) { | 72 if (bRead == -1) { |
71 handle_errno(); | 73 handle_errno(); |
72 } | 74 } |
73 retval = UnknownError; | 75 retval = UnknownError; |
74 *size = 0; | 76 *size = 0; |
75 if (data) { | 77 if (*data) { |
76 free(data); | 78 free(*data); |
77 data = NULL; | 79 printf("Nulling data\n"); |
80 *data = NULL; | |
78 } | 81 } |
79 goto failure; | 82 goto cleanup; |
80 } | 83 } |
81 | 84 |
82 retval = Unknown; | 85 retval = Unknown; |
83 failure: | 86 cleanup: |
84 | 87 |
85 if (fd && fd != -1) { | 88 if (fd && fd != -1) { |
86 close(fd); | 89 close(fd); |
87 fd = -1; | 90 fd = -1; |
88 } | 91 } |
89 | 92 |
90 return retval; | 93 return retval; |
91 } | 94 } |
92 | 95 |
93 list_status_t readAndVerifyList(char *fileName, void *data, size_t *size) | 96 list_status_t readAndVerifyList(const char *fileName, char **data, size_t *size) |
94 { | 97 { |
95 int validSig = 0; | 98 // int validSig = 0; |
96 char * firstChar = NULL; | 99 char ** firstChar = NULL; |
97 int i = 0; | |
98 | 100 |
99 list_status_t retval = UnknownError; | 101 list_status_t retval = UnknownError; |
100 data = NULL; | 102 *data = NULL; |
101 size = NULL; | 103 *size = 0; |
102 | 104 |
103 retval = readList(fileName, data, size); | 105 retval = readList(fileName, data, size); |
104 | 106 |
105 if (retval != Unknown) { | 107 if (retval != Unknown) { |
108 printf ("Readlist failed\n"); | |
106 return retval; | 109 return retval; |
107 } | 110 } |
108 | 111 |
109 if (!data || !size) { | 112 if (!data || !*size) { |
110 // should not have happend if readList works as specified | 113 // should not have happend if readList works as specified |
114 printf ("No data or no size\n"); | |
115 printf ("%ld\n", (long)data); | |
111 return UnknownError; | 116 return UnknownError; |
112 } | 117 } |
113 | 118 |
114 firstChar = (char*) data; | 119 firstChar = (char**) data; |
115 | 120 |
116 if (*firstChar != 'S') { | 121 if (**firstChar != 'S') { |
117 printf("Does not start with S\n"); | 122 printf("Does not start with S\n"); |
118 retval = InvalidFormat; | 123 retval = InvalidFormat; |
124 goto cleanup; | |
119 } | 125 } |
120 | 126 |
121 for (i=0; i < *size; i++) { | 127 // TODO VERIFIY |
122 printf("%c", firstChar + i); | 128 retval = Valid; |
123 } | |
124 | 129 |
125 failure: | 130 cleanup: |
126 if (retval != Valid && data) { | 131 if (retval != Valid && *data) { |
127 free(data); | 132 free(*data); |
128 data = NULL; | 133 *data = NULL; |
129 *size = 0; | 134 *size = 0; |
130 } | 135 } |
131 return retval; | 136 return retval; |
132 } | 137 } |
133 | 138 |