Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silcsftp / tests / sftp_server.c
1 /*
2
3   sprp_server.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include "silcsftp.h"
22
23 typedef struct ServerSessionStruct {
24   SilcStream stream;
25   SilcSFTP sftp;
26 } *ServerSession;
27
28 typedef struct {
29   SilcSchedule schedule;
30   SilcNetListener listener;
31   SilcSFTPFilesystem fs;
32 } *Server;
33
34 static void error_cb(SilcSFTP sftp, SilcSFTPStatus status, void *context)
35 {
36   ServerSession session = context;
37
38   if (status == SILC_SFTP_STATUS_EOF) {
39     SILC_LOG_DEBUG(("Eof"));
40     silc_stream_destroy(session->stream);
41     silc_free(session);
42   }
43
44   SILC_LOG_DEBUG(("Error %d", status));
45 }
46
47 static void net_callback(SilcNetStatus status, SilcStream stream,
48                          void *context)
49 {
50   Server server = context;
51   ServerSession session;
52
53   SILC_LOG_DEBUG(("New connection"));
54
55   session = silc_calloc(1, sizeof(*session));
56   if (!session)
57     return;
58   session->stream = stream;
59   session->sftp = silc_sftp_server_start(stream, server->schedule, error_cb,
60                                          session, server->fs);
61
62 }
63
64 int main()
65 {
66   Server server = silc_calloc(1, sizeof(*server));
67   void *dir;
68   const char *ip = "127.0.0.1";
69
70   silc_log_debug(TRUE);
71   silc_log_debug_hexdump(TRUE);
72   silc_log_set_debug_string("*sftp*");
73
74   server->schedule = silc_schedule_init(0, NULL, NULL);
75   if (!server->schedule)
76     return -1;
77
78   server->listener = silc_net_tcp_create_listener(&ip, 1, 5000, FALSE,
79                                                   FALSE, server->schedule,
80                                                   net_callback, server);
81   if (!server->listener)
82     return -1;
83
84   /* Make test filesystem hierarchy */
85
86   server->fs = silc_sftp_fs_memory_alloc((SILC_SFTP_FS_PERM_READ |
87                                           SILC_SFTP_FS_PERM_WRITE));
88   dir =
89     silc_sftp_fs_memory_add_dir(server->fs, NULL, (SILC_SFTP_FS_PERM_READ |
90                                                    SILC_SFTP_FS_PERM_WRITE |
91                                                    SILC_SFTP_FS_PERM_EXEC),
92                                 "sftp");
93   silc_sftp_fs_memory_add_file(server->fs, NULL, SILC_SFTP_FS_PERM_READ,
94                                "passwd", "file:///etc/passwd");
95   silc_sftp_fs_memory_add_file(server->fs, NULL, (SILC_SFTP_FS_PERM_READ |
96                                                   SILC_SFTP_FS_PERM_WRITE),
97                                "writeme", "file://./writeme-test");
98   silc_sftp_fs_memory_add_file(server->fs, dir, SILC_SFTP_FS_PERM_READ,
99                                "shadow", "file:///etc/shadow");
100   silc_sftp_fs_memory_add_file(server->fs, dir, SILC_SFTP_FS_PERM_READ,
101                                "sftp_server.c", "file://sftp_server.c");
102   silc_sftp_fs_memory_add_dir(server->fs, dir, (SILC_SFTP_FS_PERM_READ |
103                                                 SILC_SFTP_FS_PERM_WRITE |
104                                                 SILC_SFTP_FS_PERM_EXEC),
105                                "Mail");
106   silc_sftp_fs_memory_add_file(server->fs, NULL, SILC_SFTP_FS_PERM_EXEC,
107                                "testi", "file://sftp_client.c");
108
109   silc_schedule(server->schedule);
110
111   return 0;
112 }