1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2012. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <bsp.h> /* for device driver prototypes */ |
---|
15 | #include "tmacros.h" |
---|
16 | #include <rtems/untar.h> |
---|
17 | #include <rtems/error.h> |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <sys/types.h> |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <fcntl.h> |
---|
24 | #include <unistd.h> |
---|
25 | |
---|
26 | #include "initial_filesystem_tar.h" |
---|
27 | #include "initial_filesystem_tar_gz.h" |
---|
28 | #if HAVE_XZ |
---|
29 | #include "initial_filesystem_tar_xz.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | const char rtems_test_name[] = "TAR 1"; |
---|
33 | |
---|
34 | /* forward declarations to avoid warnings */ |
---|
35 | rtems_task Init(rtems_task_argument argument); |
---|
36 | void test_untar_from_memory(void); |
---|
37 | void test_untar_from_file(void); |
---|
38 | void test_untar_chunks_from_memory(void); |
---|
39 | void test_untar_unzip_tgz(void); |
---|
40 | void test_untar_unzip_txz(void); |
---|
41 | |
---|
42 | #define TARFILE_START initial_filesystem_tar |
---|
43 | #define TARFILE_SIZE initial_filesystem_tar_size |
---|
44 | #define TARFILE_GZ_START initial_filesystem_tar_gz |
---|
45 | #define TARFILE_GZ_SIZE initial_filesystem_tar_gz_size |
---|
46 | #if HAVE_XZ |
---|
47 | #define TARFILE_XZ_START initial_filesystem_tar_xz |
---|
48 | #define TARFILE_XZ_SIZE initial_filesystem_tar_xz_size |
---|
49 | #endif |
---|
50 | |
---|
51 | void test_cat( |
---|
52 | char *file, |
---|
53 | int offset_arg, |
---|
54 | int length |
---|
55 | ); |
---|
56 | |
---|
57 | static void test_untar_check_mode(const char* file, int mode) |
---|
58 | { |
---|
59 | struct stat sb; |
---|
60 | int fmode; |
---|
61 | rtems_test_assert(stat(file, &sb) == 0); |
---|
62 | fmode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
---|
63 | printf(" %s: mode: %04o want: %04o\n", file, fmode, mode); |
---|
64 | rtems_test_assert(fmode == mode); |
---|
65 | } |
---|
66 | |
---|
67 | void test_untar_from_memory(void) |
---|
68 | { |
---|
69 | rtems_status_code sc; |
---|
70 | rtems_printer printer; |
---|
71 | |
---|
72 | rtems_print_printer_printf(&printer); |
---|
73 | |
---|
74 | printf("Untaring from memory - "); |
---|
75 | sc = Untar_FromMemory_Print((void *)TARFILE_START, TARFILE_SIZE, &printer); |
---|
76 | if (sc != RTEMS_SUCCESSFUL) { |
---|
77 | printf ("error: untar failed: %s\n", rtems_status_text (sc)); |
---|
78 | exit(1); |
---|
79 | } |
---|
80 | printf ("successful\n"); |
---|
81 | |
---|
82 | /******************/ |
---|
83 | printf( "========= /home/test_file =========\n" ); |
---|
84 | test_cat( "/home/test_file", 0, 0 ); |
---|
85 | |
---|
86 | /******************/ |
---|
87 | printf( "========= /home/test_script =========\n" ); |
---|
88 | test_cat( "/home/test_script", 0, 0 ); |
---|
89 | test_untar_check_mode("/home/test_script", 0755); |
---|
90 | |
---|
91 | /******************/ |
---|
92 | printf( "========= /symlink =========\n" ); |
---|
93 | test_cat( "/symlink", 0, 0 ); |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | void test_untar_from_file(void) |
---|
98 | { |
---|
99 | int fd; |
---|
100 | int rv; |
---|
101 | ssize_t n; |
---|
102 | |
---|
103 | puts( "" ); |
---|
104 | |
---|
105 | puts( "Copy tar image to test.tar" ); |
---|
106 | /* Copy tar image from object to file in IMFS */ |
---|
107 | fd = open( "/test.tar", O_CREAT|O_TRUNC|O_WRONLY, 0777 ); |
---|
108 | rtems_test_assert( fd != -1 ); |
---|
109 | |
---|
110 | n = write( fd, TARFILE_START, TARFILE_SIZE ); |
---|
111 | rtems_test_assert( n == TARFILE_SIZE ); |
---|
112 | close( fd ); |
---|
113 | |
---|
114 | /* make a directory to untar it into */ |
---|
115 | rv = mkdir( "/dest", 0777 ); |
---|
116 | rtems_test_assert( rv == 0 ); |
---|
117 | |
---|
118 | rv = chdir( "/dest" ); |
---|
119 | rtems_test_assert( rv == 0 ); |
---|
120 | |
---|
121 | /* Untar it */ |
---|
122 | rv = Untar_FromFile( "/test.tar" ); |
---|
123 | printf("Untaring from file - "); |
---|
124 | if (rv != UNTAR_SUCCESSFUL) { |
---|
125 | printf ("error: untar failed: %i\n", rv); |
---|
126 | exit(1); |
---|
127 | } |
---|
128 | printf ("successful\n"); |
---|
129 | |
---|
130 | /******************/ |
---|
131 | printf( "========= /dest/home/test_file =========\n" ); |
---|
132 | test_cat( "/dest/home/test_file", 0, 0 ); |
---|
133 | |
---|
134 | /******************/ |
---|
135 | printf( "========= /dest/home/test_script =========\n" ); |
---|
136 | test_cat( "/dest/home/test_script", 0, 0 ); |
---|
137 | test_untar_check_mode("/dest/home/test_script", 0755); |
---|
138 | |
---|
139 | /******************/ |
---|
140 | printf( "========= /dest/symlink =========\n" ); |
---|
141 | test_cat( "/dest/symlink", 0, 0 ); |
---|
142 | } |
---|
143 | |
---|
144 | void test_untar_chunks_from_memory(void) |
---|
145 | { |
---|
146 | rtems_status_code sc; |
---|
147 | rtems_printer printer; |
---|
148 | int rv; |
---|
149 | Untar_ChunkContext ctx; |
---|
150 | unsigned long counter = 0; |
---|
151 | char *buffer = (char *)TARFILE_START; |
---|
152 | size_t buflen = TARFILE_SIZE; |
---|
153 | |
---|
154 | puts( "" ); |
---|
155 | |
---|
156 | rtems_print_printer_printf(&printer); |
---|
157 | |
---|
158 | /* make a directory to untar it into */ |
---|
159 | rv = mkdir( "/dest2", 0777 ); |
---|
160 | rtems_test_assert( rv == 0 ); |
---|
161 | |
---|
162 | rv = chdir( "/dest2" ); |
---|
163 | rtems_test_assert( rv == 0 ); |
---|
164 | |
---|
165 | printf( "Untaring chunks from memory - " ); |
---|
166 | Untar_ChunkContext_Init(&ctx); |
---|
167 | do { |
---|
168 | sc = Untar_FromChunk_Print(&ctx, &buffer[counter], (size_t)1 , &printer); |
---|
169 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
170 | counter ++; |
---|
171 | } while (counter < buflen); |
---|
172 | printf("successful\n"); |
---|
173 | |
---|
174 | /******************/ |
---|
175 | printf( "========= /dest2/home/test_file =========\n" ); |
---|
176 | test_cat( "/dest2/home/test_file", 0, 0 ); |
---|
177 | |
---|
178 | /******************/ |
---|
179 | printf( "========= /dest2/home/test_script =========\n" ); |
---|
180 | test_cat( "/dest2/home/test_script", 0, 0 ); |
---|
181 | test_untar_check_mode("/dest2/home/test_script", 0755); |
---|
182 | |
---|
183 | /******************/ |
---|
184 | printf( "========= /dest2/symlink =========\n" ); |
---|
185 | test_cat( "/dest2/symlink", 0, 0 ); |
---|
186 | |
---|
187 | } |
---|
188 | |
---|
189 | void test_untar_unzip_tgz(void) |
---|
190 | { |
---|
191 | int status; |
---|
192 | rtems_printer printer; |
---|
193 | int rv; |
---|
194 | Untar_GzChunkContext ctx; |
---|
195 | size_t i = 0; |
---|
196 | char *buffer = (char *)TARFILE_GZ_START; |
---|
197 | size_t buflen = TARFILE_GZ_SIZE; |
---|
198 | char inflate_buffer; |
---|
199 | |
---|
200 | puts( "" ); |
---|
201 | |
---|
202 | rtems_test_assert( buflen != 0 ); |
---|
203 | |
---|
204 | rtems_print_printer_printf(&printer); |
---|
205 | |
---|
206 | /* make a directory to untar it into */ |
---|
207 | rv = mkdir( "/dest3", 0777 ); |
---|
208 | rtems_test_assert( rv == 0 ); |
---|
209 | |
---|
210 | rv = chdir( "/dest3" ); |
---|
211 | rtems_test_assert( rv == 0 ); |
---|
212 | |
---|
213 | printf( "Untaring chunks from tgz - " ); |
---|
214 | |
---|
215 | status = Untar_GzChunkContext_Init(&ctx, &inflate_buffer, 1); |
---|
216 | rtems_test_assert(status == UNTAR_SUCCESSFUL); |
---|
217 | for(i = 0; i < buflen; i++) { |
---|
218 | status = Untar_FromGzChunk_Print(&ctx, &buffer[i], 1, &printer); |
---|
219 | rtems_test_assert(status == UNTAR_SUCCESSFUL); |
---|
220 | } |
---|
221 | printf( "successful\n" ); |
---|
222 | |
---|
223 | /******************/ |
---|
224 | printf( "========= /dest3/home/test_file =========\n" ); |
---|
225 | test_cat( "/dest3/home/test_file", 0, 0 ); |
---|
226 | |
---|
227 | /******************/ |
---|
228 | printf( "========= /dest3/home/test_script =========\n" ); |
---|
229 | test_cat( "/dest3/home/test_script", 0, 0 ); |
---|
230 | test_untar_check_mode("/dest3/home/test_script", 0755); |
---|
231 | |
---|
232 | /******************/ |
---|
233 | printf( "========= /dest3/symlink =========\n" ); |
---|
234 | test_cat( "/dest3/symlink", 0, 0 ); |
---|
235 | } |
---|
236 | |
---|
237 | void test_untar_unzip_txz(void) |
---|
238 | { |
---|
239 | #if HAVE_XZ |
---|
240 | int status; |
---|
241 | rtems_printer printer; |
---|
242 | int rv; |
---|
243 | Untar_XzChunkContext ctx; |
---|
244 | size_t i = 0; |
---|
245 | char *buffer = (char *)TARFILE_XZ_START; |
---|
246 | size_t buflen = TARFILE_XZ_SIZE; |
---|
247 | char inflate_buffer; |
---|
248 | |
---|
249 | puts( "" ); |
---|
250 | |
---|
251 | rtems_test_assert( buflen != 0 ); |
---|
252 | |
---|
253 | rtems_print_printer_printf(&printer); |
---|
254 | |
---|
255 | /* make a directory to untar it into */ |
---|
256 | rv = mkdir( "/dest4", 0777 ); |
---|
257 | rtems_test_assert( rv == 0 ); |
---|
258 | |
---|
259 | rv = chdir( "/dest4" ); |
---|
260 | rtems_test_assert( rv == 0 ); |
---|
261 | |
---|
262 | printf( "Untaring chunks from txz - " ); |
---|
263 | |
---|
264 | /* |
---|
265 | * Use 8K dict, this is set on the command line of xz when compressing. |
---|
266 | */ |
---|
267 | status = Untar_XzChunkContext_Init(&ctx, XZ_DYNALLOC, |
---|
268 | 8 * 1024, &inflate_buffer, 1); |
---|
269 | rtems_test_assert(status == UNTAR_SUCCESSFUL); |
---|
270 | for(i = 0; i < buflen; i++) { |
---|
271 | status = Untar_FromXzChunk_Print(&ctx, &buffer[i], 1, &printer); |
---|
272 | rtems_test_assert(status == UNTAR_SUCCESSFUL); |
---|
273 | } |
---|
274 | printf( "successful\n" ); |
---|
275 | |
---|
276 | /******************/ |
---|
277 | printf( "========= /dest4/home/test_file =========\n" ); |
---|
278 | test_cat( "/dest4/home/test_file", 0, 0 ); |
---|
279 | |
---|
280 | /******************/ |
---|
281 | printf( "========= /dest4/home/test_script =========\n" ); |
---|
282 | test_cat( "/dest4/home/test_script", 0, 0 ); |
---|
283 | test_untar_check_mode("/dest4/home/test_script", 0755); |
---|
284 | |
---|
285 | /******************/ |
---|
286 | printf( "========= /dest4/symlink =========\n" ); |
---|
287 | test_cat( "/dest4/symlink", 0, 0 ); |
---|
288 | #endif |
---|
289 | } |
---|
290 | |
---|
291 | rtems_task Init( |
---|
292 | rtems_task_argument ignored |
---|
293 | ) |
---|
294 | { |
---|
295 | TEST_BEGIN(); |
---|
296 | |
---|
297 | test_untar_from_memory(); |
---|
298 | test_untar_from_file(); |
---|
299 | test_untar_chunks_from_memory(); |
---|
300 | test_untar_unzip_tgz(); |
---|
301 | test_untar_unzip_txz(); |
---|
302 | |
---|
303 | TEST_END(); |
---|
304 | exit( 0 ); |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | /* NOTICE: the clock driver is explicitly disabled */ |
---|
309 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
310 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
311 | |
---|
312 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
313 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5 |
---|
314 | |
---|
315 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
316 | |
---|
317 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
318 | |
---|
319 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT |
---|
320 | |
---|
321 | #define CONFIGURE_INIT |
---|
322 | #include <rtems/confdefs.h> |
---|