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 | |
---|
29 | const char rtems_test_name[] = "TAR 1"; |
---|
30 | |
---|
31 | /* forward declarations to avoid warnings */ |
---|
32 | rtems_task Init(rtems_task_argument argument); |
---|
33 | void test_untar_from_memory(void); |
---|
34 | void test_untar_from_file(void); |
---|
35 | void test_untar_chunks_from_memory(void); |
---|
36 | void test_untar_unzip_tgz(void); |
---|
37 | |
---|
38 | #define TARFILE_START initial_filesystem_tar |
---|
39 | #define TARFILE_SIZE initial_filesystem_tar_size |
---|
40 | #define TARFILE_GZ_START initial_filesystem_tar_gz |
---|
41 | #define TARFILE_GZ_SIZE initial_filesystem_tar_gz_size |
---|
42 | |
---|
43 | void test_cat( |
---|
44 | char *file, |
---|
45 | int offset_arg, |
---|
46 | int length |
---|
47 | ); |
---|
48 | |
---|
49 | static void test_untar_check_mode(const char* file, int mode) |
---|
50 | { |
---|
51 | struct stat sb; |
---|
52 | int fmode; |
---|
53 | rtems_test_assert(stat(file, &sb) == 0); |
---|
54 | fmode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
---|
55 | printf(" %s: mode: %04o want: %04o\n", file, fmode, mode); |
---|
56 | rtems_test_assert(fmode == mode); |
---|
57 | } |
---|
58 | |
---|
59 | void test_untar_from_memory(void) |
---|
60 | { |
---|
61 | rtems_status_code sc; |
---|
62 | rtems_printer printer; |
---|
63 | |
---|
64 | rtems_print_printer_printf(&printer); |
---|
65 | |
---|
66 | printf("Untaring from memory - "); |
---|
67 | sc = Untar_FromMemory_Print((void *)TARFILE_START, TARFILE_SIZE, &printer); |
---|
68 | if (sc != RTEMS_SUCCESSFUL) { |
---|
69 | printf ("error: untar failed: %s\n", rtems_status_text (sc)); |
---|
70 | exit(1); |
---|
71 | } |
---|
72 | printf ("successful\n"); |
---|
73 | |
---|
74 | /******************/ |
---|
75 | printf( "========= /home/test_file =========\n" ); |
---|
76 | test_cat( "/home/test_file", 0, 0 ); |
---|
77 | |
---|
78 | /******************/ |
---|
79 | printf( "========= /home/test_script =========\n" ); |
---|
80 | test_cat( "/home/test_script", 0, 0 ); |
---|
81 | test_untar_check_mode("/home/test_script", 0755); |
---|
82 | |
---|
83 | /******************/ |
---|
84 | printf( "========= /symlink =========\n" ); |
---|
85 | test_cat( "/symlink", 0, 0 ); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | void test_untar_from_file(void) |
---|
90 | { |
---|
91 | int fd; |
---|
92 | int rv; |
---|
93 | ssize_t n; |
---|
94 | |
---|
95 | puts( "Copy tar image to test.tar" ); |
---|
96 | /* Copy tar image from object to file in IMFS */ |
---|
97 | fd = open( "/test.tar", O_CREAT|O_TRUNC|O_WRONLY, 0777 ); |
---|
98 | rtems_test_assert( fd != -1 ); |
---|
99 | |
---|
100 | n = write( fd, TARFILE_START, TARFILE_SIZE ); |
---|
101 | rtems_test_assert( n == TARFILE_SIZE ); |
---|
102 | close( fd ); |
---|
103 | |
---|
104 | /* make a directory to untar it into */ |
---|
105 | rv = mkdir( "/dest", 0777 ); |
---|
106 | rtems_test_assert( rv == 0 ); |
---|
107 | |
---|
108 | rv = chdir( "/dest" ); |
---|
109 | rtems_test_assert( rv == 0 ); |
---|
110 | |
---|
111 | /* Untar it */ |
---|
112 | rv = Untar_FromFile( "/test.tar" ); |
---|
113 | printf("Untaring from file - "); |
---|
114 | if (rv != UNTAR_SUCCESSFUL) { |
---|
115 | printf ("error: untar failed: %i\n", rv); |
---|
116 | exit(1); |
---|
117 | } |
---|
118 | printf ("successful\n"); |
---|
119 | |
---|
120 | /******************/ |
---|
121 | printf( "========= /dest/home/test_file =========\n" ); |
---|
122 | test_cat( "/dest/home/test_file", 0, 0 ); |
---|
123 | |
---|
124 | /******************/ |
---|
125 | printf( "========= /dest/home/test_script =========\n" ); |
---|
126 | test_cat( "/dest/home/test_script", 0, 0 ); |
---|
127 | test_untar_check_mode("/dest/home/test_script", 0755); |
---|
128 | |
---|
129 | /******************/ |
---|
130 | printf( "========= /dest/symlink =========\n" ); |
---|
131 | test_cat( "/dest/symlink", 0, 0 ); |
---|
132 | } |
---|
133 | |
---|
134 | void test_untar_chunks_from_memory(void) |
---|
135 | { |
---|
136 | rtems_status_code sc; |
---|
137 | rtems_printer printer; |
---|
138 | int rv; |
---|
139 | Untar_ChunkContext ctx; |
---|
140 | unsigned long counter = 0; |
---|
141 | char *buffer = (char *)TARFILE_START; |
---|
142 | size_t buflen = TARFILE_SIZE; |
---|
143 | |
---|
144 | rtems_print_printer_printf(&printer); |
---|
145 | |
---|
146 | /* make a directory to untar it into */ |
---|
147 | rv = mkdir( "/dest2", 0777 ); |
---|
148 | rtems_test_assert( rv == 0 ); |
---|
149 | |
---|
150 | rv = chdir( "/dest2" ); |
---|
151 | rtems_test_assert( rv == 0 ); |
---|
152 | |
---|
153 | printf( "Untaring chunks from memory - " ); |
---|
154 | Untar_ChunkContext_Init(&ctx); |
---|
155 | do { |
---|
156 | sc = Untar_FromChunk_Print(&ctx, &buffer[counter], (size_t)1 , &printer); |
---|
157 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
158 | counter ++; |
---|
159 | } while (counter < buflen); |
---|
160 | printf("successful\n"); |
---|
161 | |
---|
162 | /******************/ |
---|
163 | printf( "========= /dest2/home/test_file =========\n" ); |
---|
164 | test_cat( "/dest2/home/test_file", 0, 0 ); |
---|
165 | |
---|
166 | /******************/ |
---|
167 | printf( "========= /dest2/home/test_script =========\n" ); |
---|
168 | test_cat( "/dest2/home/test_script", 0, 0 ); |
---|
169 | test_untar_check_mode("/dest2/home/test_script", 0755); |
---|
170 | |
---|
171 | /******************/ |
---|
172 | printf( "========= /dest2/symlink =========\n" ); |
---|
173 | test_cat( "/dest2/symlink", 0, 0 ); |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | void test_untar_unzip_tgz(void) |
---|
178 | { |
---|
179 | int status; |
---|
180 | rtems_printer printer; |
---|
181 | int rv; |
---|
182 | Untar_GzChunkContext ctx; |
---|
183 | size_t i = 0; |
---|
184 | char *buffer = (char *)TARFILE_GZ_START; |
---|
185 | size_t buflen = TARFILE_GZ_SIZE; |
---|
186 | char inflate_buffer; |
---|
187 | |
---|
188 | rtems_print_printer_printf(&printer); |
---|
189 | |
---|
190 | /* make a directory to untar it into */ |
---|
191 | rv = mkdir( "/dest3", 0777 ); |
---|
192 | rtems_test_assert( rv == 0 ); |
---|
193 | |
---|
194 | rv = chdir( "/dest3" ); |
---|
195 | rtems_test_assert( rv == 0 ); |
---|
196 | |
---|
197 | printf( "Untaring chunks from tgz - " ); |
---|
198 | |
---|
199 | status = Untar_GzChunkContext_Init(&ctx, &inflate_buffer, 1); |
---|
200 | rtems_test_assert(status == UNTAR_SUCCESSFUL); |
---|
201 | for(i = 0; i < buflen; i++) { |
---|
202 | status = Untar_FromGzChunk_Print(&ctx, &buffer[i], 1, &printer); |
---|
203 | rtems_test_assert(status == UNTAR_SUCCESSFUL); |
---|
204 | } |
---|
205 | printf( "successful\n" ); |
---|
206 | |
---|
207 | /******************/ |
---|
208 | printf( "========= /dest3/home/test_file =========\n" ); |
---|
209 | test_cat( "/dest3/home/test_file", 0, 0 ); |
---|
210 | |
---|
211 | /******************/ |
---|
212 | printf( "========= /dest3/home/test_script =========\n" ); |
---|
213 | test_cat( "/dest3/home/test_script", 0, 0 ); |
---|
214 | test_untar_check_mode("/dest3/home/test_script", 0755); |
---|
215 | |
---|
216 | /******************/ |
---|
217 | printf( "========= /dest3/symlink =========\n" ); |
---|
218 | test_cat( "/dest3/symlink", 0, 0 ); |
---|
219 | } |
---|
220 | |
---|
221 | rtems_task Init( |
---|
222 | rtems_task_argument ignored |
---|
223 | ) |
---|
224 | { |
---|
225 | TEST_BEGIN(); |
---|
226 | |
---|
227 | test_untar_from_memory(); |
---|
228 | puts( "" ); |
---|
229 | test_untar_from_file(); |
---|
230 | puts( "" ); |
---|
231 | test_untar_chunks_from_memory(); |
---|
232 | puts( "" ); |
---|
233 | test_untar_unzip_tgz(); |
---|
234 | |
---|
235 | TEST_END(); |
---|
236 | exit( 0 ); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | /* NOTICE: the clock driver is explicitly disabled */ |
---|
241 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
242 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
243 | |
---|
244 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
245 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5 |
---|
246 | |
---|
247 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
248 | |
---|
249 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
250 | |
---|
251 | #define CONFIGURE_INIT |
---|
252 | #include <rtems/confdefs.h> |
---|