1 | /* |
---|
2 | * Copyright (c) 2011 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include <string.h> |
---|
22 | |
---|
23 | #include <rtems.h> |
---|
24 | #include <tmacros.h> |
---|
25 | |
---|
26 | #include <rtems/rtems_bsdnet.h> |
---|
27 | #include <rtems/ftpd.h> |
---|
28 | #include <rtems/ftpfs.h> |
---|
29 | |
---|
30 | const char rtems_test_name[] = "FTP 1"; |
---|
31 | |
---|
32 | /* forward declarations to avoid warnings */ |
---|
33 | static rtems_task Init(rtems_task_argument argument); |
---|
34 | |
---|
35 | struct rtems_bsdnet_config rtems_bsdnet_config; |
---|
36 | |
---|
37 | #define FTP_WORKER_TASK_COUNT 2 |
---|
38 | |
---|
39 | #define FTP_WORKER_TASK_EXTRA_STACK (FTP_WORKER_TASK_COUNT * FTPD_STACKSIZE) |
---|
40 | |
---|
41 | struct rtems_ftpd_configuration rtems_ftpd_configuration = { |
---|
42 | .priority = 90, |
---|
43 | .max_hook_filesize = 0, |
---|
44 | .port = 21, |
---|
45 | .hooks = NULL, |
---|
46 | .root = NULL, |
---|
47 | .tasks_count = FTP_WORKER_TASK_COUNT, |
---|
48 | .idle = 0, |
---|
49 | .access = 0 |
---|
50 | }; |
---|
51 | |
---|
52 | static const char content [] = |
---|
53 | " LICENSE INFORMATION\n" |
---|
54 | "\n" |
---|
55 | "RTEMS is free software; you can redistribute it and/or modify it under\n" |
---|
56 | "terms of the GNU General Public License as published by the\n" |
---|
57 | "Free Software Foundation; either version 2, or (at your option) any\n" |
---|
58 | "later version. RTEMS is distributed in the hope that it will be useful,\n" |
---|
59 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
---|
60 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" |
---|
61 | "General Public License for more details. You should have received\n" |
---|
62 | "a copy of the GNU General Public License along with RTEMS; see\n" |
---|
63 | "file COPYING. If not, write to the Free Software Foundation, 675\n" |
---|
64 | "Mass Ave, Cambridge, MA 02139, USA.\n" |
---|
65 | "\n" |
---|
66 | "As a special exception, including RTEMS header files in a file,\n" |
---|
67 | "instantiating RTEMS generics or templates, or linking other files\n" |
---|
68 | "with RTEMS objects to produce an executable application, does not\n" |
---|
69 | "by itself cause the resulting executable application to be covered\n" |
---|
70 | "by the GNU General Public License. This exception does not\n" |
---|
71 | "however invalidate any other reasons why the executable file might be\n" |
---|
72 | "covered by the GNU Public License.\n"; |
---|
73 | |
---|
74 | static void initialize_ftpfs(void) |
---|
75 | { |
---|
76 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
77 | int rv = 0; |
---|
78 | struct timeval to = { |
---|
79 | .tv_sec = 10, |
---|
80 | .tv_usec = 0 |
---|
81 | }; |
---|
82 | const char *target = RTEMS_FTPFS_MOUNT_POINT_DEFAULT; |
---|
83 | |
---|
84 | rv = mount_and_make_target_path( |
---|
85 | NULL, |
---|
86 | target, |
---|
87 | RTEMS_FILESYSTEM_TYPE_FTPFS, |
---|
88 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
89 | NULL |
---|
90 | ); |
---|
91 | rtems_test_assert(rv == 0); |
---|
92 | |
---|
93 | sc = rtems_ftpfs_set_verbose(target, true); |
---|
94 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
95 | |
---|
96 | sc = rtems_ftpfs_set_timeout(target, &to); |
---|
97 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
98 | } |
---|
99 | |
---|
100 | static void change_self_priority(void) |
---|
101 | { |
---|
102 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
103 | rtems_task_priority cur = 0; |
---|
104 | |
---|
105 | sc = rtems_task_set_priority(RTEMS_SUCCESSFUL, 110, &cur); |
---|
106 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
107 | } |
---|
108 | |
---|
109 | static void create_file(const char *path, const void *begin, size_t size) |
---|
110 | { |
---|
111 | int rv = 0; |
---|
112 | int fd = open(path, O_WRONLY); |
---|
113 | ssize_t n = 0; |
---|
114 | |
---|
115 | rtems_test_assert(fd >= 0); |
---|
116 | |
---|
117 | n = write(fd, begin, size); |
---|
118 | rtems_test_assert(n == (ssize_t) size); |
---|
119 | |
---|
120 | rv = close(fd); |
---|
121 | rtems_test_assert(rv == 0); |
---|
122 | } |
---|
123 | |
---|
124 | static void copy_file(const char *src_path, const char *dest_path) |
---|
125 | { |
---|
126 | int rv = 0; |
---|
127 | int in = open(src_path, O_RDONLY); |
---|
128 | int out = open(dest_path, O_WRONLY); |
---|
129 | ssize_t n_in = 0; |
---|
130 | char buf [64]; |
---|
131 | struct stat st_in; |
---|
132 | struct stat st_out; |
---|
133 | |
---|
134 | memset(&st_in, 0xff, sizeof(st_in)); |
---|
135 | memset(&st_out, 0xff, sizeof(st_out)); |
---|
136 | |
---|
137 | rtems_test_assert(in >= 0); |
---|
138 | rtems_test_assert(out >= 0); |
---|
139 | |
---|
140 | rv = fstat(out, &st_out); |
---|
141 | rtems_test_assert(rv == 0); |
---|
142 | |
---|
143 | rtems_test_assert(st_out.st_size == 0); |
---|
144 | |
---|
145 | while ((n_in = read(in, buf, sizeof(buf))) > 0) { |
---|
146 | ssize_t n_out = write(out, buf, (size_t) n_in); |
---|
147 | rtems_test_assert(n_out == n_in); |
---|
148 | } |
---|
149 | |
---|
150 | rv = fstat(out, &st_out); |
---|
151 | rtems_test_assert(rv == 0); |
---|
152 | |
---|
153 | rv = fstat(in, &st_in); |
---|
154 | rtems_test_assert(rv == 0); |
---|
155 | |
---|
156 | rtems_test_assert(st_in.st_size == st_out.st_size); |
---|
157 | |
---|
158 | rv = close(out); |
---|
159 | rtems_test_assert(rv == 0); |
---|
160 | |
---|
161 | rv = close(in); |
---|
162 | rtems_test_assert(rv == 0); |
---|
163 | } |
---|
164 | |
---|
165 | static void check_file_size(const char *path, size_t size) |
---|
166 | { |
---|
167 | struct stat st; |
---|
168 | int rv = lstat(path, &st); |
---|
169 | |
---|
170 | rtems_test_assert(rv == 0); |
---|
171 | rtems_test_assert(st.st_size == (off_t) size); |
---|
172 | } |
---|
173 | |
---|
174 | static void check_file(const char *path) |
---|
175 | { |
---|
176 | int rv = 0; |
---|
177 | int fd = open(path, O_RDONLY); |
---|
178 | ssize_t n = 0; |
---|
179 | char buf [64]; |
---|
180 | const char *current = &content [0]; |
---|
181 | size_t done = 0; |
---|
182 | |
---|
183 | rtems_test_assert(fd >= 0); |
---|
184 | |
---|
185 | while ((n = read(fd, buf, sizeof(buf))) > 0) { |
---|
186 | done += (size_t) n; |
---|
187 | rtems_test_assert(done <= sizeof(content)); |
---|
188 | rtems_test_assert(memcmp(current, buf, (size_t) n) == 0); |
---|
189 | current += (size_t) n; |
---|
190 | } |
---|
191 | |
---|
192 | rtems_test_assert(done == sizeof(content)); |
---|
193 | |
---|
194 | rv = close(fd); |
---|
195 | rtems_test_assert(rv == 0); |
---|
196 | } |
---|
197 | |
---|
198 | static void test(void) |
---|
199 | { |
---|
200 | int rv = 0; |
---|
201 | const char file_a [] = "/FTP/127.0.0.1/a.txt"; |
---|
202 | const char file_b [] = "/FTP/127.0.0.1/b.txt"; |
---|
203 | |
---|
204 | rv = rtems_bsdnet_initialize_network(); |
---|
205 | rtems_test_assert(rv == 0); |
---|
206 | |
---|
207 | rv = rtems_initialize_ftpd(); |
---|
208 | rtems_test_assert(rv == 0); |
---|
209 | |
---|
210 | initialize_ftpfs(); |
---|
211 | change_self_priority(); |
---|
212 | create_file(file_a, &content [0], sizeof(content)); |
---|
213 | copy_file(file_a, file_b); |
---|
214 | check_file(file_b); |
---|
215 | check_file_size(file_a, sizeof(content)); |
---|
216 | check_file_size(file_b, sizeof(content)); |
---|
217 | } |
---|
218 | |
---|
219 | static rtems_task Init(rtems_task_argument argument) |
---|
220 | { |
---|
221 | TEST_BEGIN(); |
---|
222 | test(); |
---|
223 | TEST_END(); |
---|
224 | |
---|
225 | rtems_test_exit(0); |
---|
226 | } |
---|
227 | |
---|
228 | #define CONFIGURE_INIT |
---|
229 | |
---|
230 | #define CONFIGURE_MICROSECONDS_PER_TICK 10000 |
---|
231 | |
---|
232 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
233 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
234 | |
---|
235 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 14 |
---|
236 | |
---|
237 | #define CONFIGURE_FILESYSTEM_FTPFS |
---|
238 | |
---|
239 | #define CONFIGURE_MAXIMUM_DRIVERS 2 |
---|
240 | |
---|
241 | #define CONFIGURE_MAXIMUM_TASKS (3 + FTP_WORKER_TASK_COUNT) |
---|
242 | #define CONFIGURE_MAXIMUM_SEMAPHORES 2 |
---|
243 | |
---|
244 | #define CONFIGURE_EXTRA_TASK_STACKS FTP_WORKER_TASK_EXTRA_STACK |
---|
245 | |
---|
246 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
247 | |
---|
248 | #define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 2 |
---|
249 | |
---|
250 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
251 | |
---|
252 | #include <rtems/confdefs.h> |
---|