~bzr-pqm/bzr/bzr.dev

1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""
18
A stub SFTP server for loopback SFTP testing.
19
Adapted from the one in paramiko's unit tests.
20
"""
21
22
import os
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
    SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
25
26
27
class StubServer (ServerInterface):
1185.49.10 by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp).
28
    def __init__(self, test_case):
29
        ServerInterface.__init__(self)
30
        self._test_case = test_case
31
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
32
    def check_auth_password(self, username, password):
33
        # all are allowed
1185.49.10 by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp).
34
        self._test_case.log('sftpserver - authorizing: %s' % (username,))
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
35
        return AUTH_SUCCESSFUL
36
37
    def check_channel_request(self, kind, chanid):
1185.49.10 by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp).
38
        self._test_case.log('sftpserver - channel request: %s, %s' % (kind, chanid))
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
39
        return OPEN_SUCCEEDED
40
41
42
class StubSFTPHandle (SFTPHandle):
43
    def stat(self):
44
        try:
45
            return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno()))
46
        except OSError, e:
47
            return SFTPServer.convert_errno(e.errno)
48
49
    def chattr(self, attr):
50
        # python doesn't have equivalents to fchown or fchmod, so we have to
51
        # use the stored filename
52
        try:
53
            SFTPServer.set_file_attr(self.filename, attr)
54
        except OSError, e:
55
            return SFTPServer.convert_errno(e.errno)
56
57
58
class StubSFTPServer (SFTPServerInterface):
59
    def __init__(self, server, root):
60
        SFTPServerInterface.__init__(self, server)
61
        self.root = root
62
        
63
    def _realpath(self, path):
64
        return self.root + self.canonicalize(path)
65
66
    def list_folder(self, path):
67
        path = self._realpath(path)
68
        try:
69
            out = [ ]
70
            flist = os.listdir(path)
71
            for fname in flist:
72
                attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname)))
73
                attr.filename = fname
74
                out.append(attr)
75
            return out
76
        except OSError, e:
77
            return SFTPServer.convert_errno(e.errno)
78
79
    def stat(self, path):
80
        path = self._realpath(path)
81
        try:
82
            return SFTPAttributes.from_stat(os.stat(path))
83
        except OSError, e:
84
            return SFTPServer.convert_errno(e.errno)
85
86
    def lstat(self, path):
87
        path = self._realpath(path)
88
        try:
89
            return SFTPAttributes.from_stat(os.lstat(path))
90
        except OSError, e:
91
            return SFTPServer.convert_errno(e.errno)
92
93
    def open(self, path, flags, attr):
94
        path = self._realpath(path)
95
        try:
96
            fd = os.open(path, flags)
97
        except OSError, e:
98
            return SFTPServer.convert_errno(e.errno)
99
        if (flags & os.O_CREAT) and (attr is not None):
100
            SFTPServer.set_file_attr(path, attr)
101
        if flags & os.O_WRONLY:
102
            fstr = 'w'
103
        elif flags & os.O_RDWR:
104
            fstr = 'r+'
105
        else:
106
            # O_RDONLY (== 0)
107
            fstr = 'r'
108
        try:
109
            f = os.fdopen(fd, fstr)
110
        except OSError, e:
111
            return SFTPServer.convert_errno(e.errno)
112
        fobj = StubSFTPHandle()
113
        fobj.filename = path
114
        fobj.readfile = f
115
        fobj.writefile = f
116
        return fobj
117
118
    def remove(self, path):
119
        path = self._realpath(path)
120
        try:
121
            os.remove(path)
122
        except OSError, e:
123
            return SFTPServer.convert_errno(e.errno)
124
        return SFTP_OK
125
126
    def rename(self, oldpath, newpath):
127
        oldpath = self._realpath(oldpath)
128
        newpath = self._realpath(newpath)
129
        try:
130
            os.rename(oldpath, newpath)
131
        except OSError, e:
132
            return SFTPServer.convert_errno(e.errno)
133
        return SFTP_OK
134
135
    def mkdir(self, path, attr):
136
        path = self._realpath(path)
137
        try:
138
            os.mkdir(path)
139
            if attr is not None:
140
                SFTPServer.set_file_attr(path, attr)
141
        except OSError, e:
142
            return SFTPServer.convert_errno(e.errno)
143
        return SFTP_OK
144
145
    def rmdir(self, path):
146
        path = self._realpath(path)
147
        try:
148
            os.rmdir(path)
149
        except OSError, e:
150
            return SFTPServer.convert_errno(e.errno)
151
        return SFTP_OK
152
153
    # removed: chattr, symlink, readlink
154
    # (nothing in bzr's sftp transport uses those)