source: rtems-libbsd/testsuite/ftpd02/test_main.c @ 60b1d40

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 60b1d40 was 68d406b, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 09:19:09

ftpfs: Import from RTEMS

RTEMS Git commit 251c94d3d3d27e0039f01b718e5c2eb06f39fdf7.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (c) 2011, 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <assert.h>
35#include <stdlib.h>
36
37#include <rtems.h>
38#include <rtems/ftpd.h>
39#include <rtems/ftpfs.h>
40#include <rtems/shell.h>
41#include <rtems/console.h>
42
43#include <machine/rtems-bsd-commands.h>
44
45#define TEST_NAME "LIBBSD FTPD 2"
46
47#define FTP_WORKER_TASK_COUNT 2
48
49#define FTP_WORKER_TASK_EXTRA_STACK (FTP_WORKER_TASK_COUNT * FTPD_STACKSIZE)
50
51struct rtems_ftpd_configuration rtems_ftpd_configuration = {
52        /* FTPD task priority */
53        .priority = 100,
54
55        /* Maximum buffersize for hooks */
56        .max_hook_filesize = 0,
57
58        /* Well-known port */
59        .port = 21,
60
61        /* List of hooks */
62        .hooks = NULL,
63
64        /* Root for FTPD or NULL for "/" */
65        .root = NULL,
66
67        /* Max. connections */
68        .tasks_count = 4,
69
70        /* Idle timeout in seconds  or 0 for no (infinite) timeout */
71        .idle = 5 * 60,
72
73        /* Access: 0 - r/w, 1 - read-only, 2 - write-only, 3 - browse-only */
74        .access = 0
75};
76
77static const char content[] =
78"                      LICENSE INFORMATION\n"
79"\n"
80"RTEMS is free software; you can redistribute it and/or modify it under\n"
81"terms of the GNU General Public License as published by the\n"
82"Free Software Foundation; either version 2, or (at your option) any\n"
83"later version.  RTEMS is distributed in the hope that it will be useful,\n"
84"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
85"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
86"General Public License for more details. You should have received\n"
87"a copy of the GNU General Public License along with RTEMS; see\n"
88"file COPYING. If not, write to the Free Software Foundation, 675\n"
89"Mass Ave, Cambridge, MA 02139, USA.\n"
90"\n"
91"As a special exception, including RTEMS header files in a file,\n"
92"instantiating RTEMS generics or templates, or linking other files\n"
93"with RTEMS objects to produce an executable application, does not\n"
94"by itself cause the resulting executable application to be covered\n"
95"by the GNU General Public License. This exception does not\n"
96"however invalidate any other reasons why the executable file might be\n"
97"covered by the GNU Public License.\n";
98
99static void
100initialize_ftpfs(void)
101{
102        rtems_status_code sc = RTEMS_SUCCESSFUL;
103        int rv = 0;
104        struct timeval to = {
105                .tv_sec = 10,
106                .tv_usec = 0
107        };
108        const char *target = RTEMS_FTPFS_MOUNT_POINT_DEFAULT;
109
110        rv = mount_and_make_target_path(
111                NULL,
112                target,
113                RTEMS_FILESYSTEM_TYPE_FTPFS,
114                RTEMS_FILESYSTEM_READ_WRITE,
115                NULL
116        );
117        assert(rv == 0);
118
119        sc = rtems_ftpfs_set_verbose(target, true);
120        assert(sc == RTEMS_SUCCESSFUL);
121
122        sc = rtems_ftpfs_set_timeout(target, &to);
123        assert(sc == RTEMS_SUCCESSFUL);
124}
125
126static void
127change_self_priority(void)
128{
129        rtems_status_code sc = RTEMS_SUCCESSFUL;
130        rtems_task_priority cur = 0;
131
132        sc = rtems_task_set_priority(RTEMS_SUCCESSFUL, 110, &cur);
133        assert(sc == RTEMS_SUCCESSFUL);
134}
135
136static void
137create_file(const char *path, const void *begin, size_t size)
138{
139        int rv = 0;
140        int fd = open(path, O_WRONLY);
141        ssize_t n = 0;
142
143        assert(fd >= 0);
144
145        n = write(fd, begin, size);
146        assert(n == (ssize_t) size);
147
148        rv = close(fd);
149        assert(rv == 0);
150}
151
152static void
153copy_file(const char *src_path, const char *dest_path)
154{
155        int rv = 0;
156        int in = open(src_path, O_RDONLY);
157        int out = open(dest_path, O_WRONLY);
158        ssize_t n_in = 0;
159        char buf [64];
160        struct stat st_in;
161        struct stat st_out;
162
163        memset(&st_in, 0xff, sizeof(st_in));
164        memset(&st_out, 0xff, sizeof(st_out));
165
166        assert(in >= 0);
167        assert(out >= 0);
168
169        rv = fstat(out, &st_out);
170        assert(rv == 0);
171
172        assert(st_out.st_size == 0);
173
174        while ((n_in = read(in, buf, sizeof(buf))) > 0) {
175                ssize_t n_out = write(out, buf, (size_t) n_in);
176                assert(n_out == n_in);
177        }
178
179        rv = fstat(out, &st_out);
180        assert(rv == 0);
181
182        rv = fstat(in, &st_in);
183        assert(rv == 0);
184
185        assert(st_in.st_size == st_out.st_size);
186
187        rv = close(out);
188        assert(rv == 0);
189
190        rv = close(in);
191        assert(rv == 0);
192}
193
194static void
195check_file_size(const char *path, size_t size)
196{
197        struct stat st;
198        int rv = lstat(path, &st);
199
200        assert(rv == 0);
201        assert(st.st_size == (off_t) size);
202}
203
204static void
205check_file(const char *path)
206{
207        int rv = 0;
208        int fd = open(path, O_RDONLY);
209        ssize_t n = 0;
210        char buf [64];
211        const char *current = &content [0];
212        size_t done = 0;
213
214        assert(fd >= 0);
215
216        while ((n = read(fd, buf, sizeof(buf))) > 0) {
217                done += (size_t) n;
218                assert(done <= sizeof(content));
219                assert(memcmp(current, buf, (size_t) n) == 0);
220                current += (size_t) n;
221        }
222
223        assert(done == sizeof(content));
224
225        rv = close(fd);
226        assert(rv == 0);
227}
228
229static void
230test_main(void)
231{
232        const char file_a[] = "/FTP/127.0.0.1/a.txt";
233        const char file_b[] = "/FTP/127.0.0.1/b.txt";
234        char *lo0[] = {
235                "ifconfig",
236                "lo0",
237                "inet",
238                "127.0.0.1",
239                "netmask",
240                "255.255.255.0",
241                NULL
242        };
243        int rv;
244        int exit_code;
245
246        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0), lo0);
247        assert(exit_code == EXIT_SUCCESS);
248
249        rv = rtems_initialize_ftpd();
250        assert(rv == 0);
251
252        initialize_ftpfs();
253        change_self_priority();
254        create_file(file_a, &content [0], sizeof(content));
255        copy_file(file_a, file_b);
256        check_file(file_b);
257        check_file_size(file_a, sizeof(content));
258        check_file_size(file_b, sizeof(content));
259
260        exit(0);
261}
262
263#define CONFIGURE_FILESYSTEM_FTPFS
264
265#include <rtems/bsd/test/default-init.h>
Note: See TracBrowser for help on using the repository browser.