1 | // |
2 | // FTPStreamFactoryTest.cpp |
3 | // |
4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "FTPStreamFactoryTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "DialogServer.h" |
15 | #include "Poco/Net/FTPStreamFactory.h" |
16 | #include "Poco/Net/DialogSocket.h" |
17 | #include "Poco/Net/SocketAddress.h" |
18 | #include "Poco/Net/NetException.h" |
19 | #include "Poco/URI.h" |
20 | #include "Poco/StreamCopier.h" |
21 | #include <sstream> |
22 | #include <memory> |
23 | |
24 | |
25 | using Poco::Net::FTPStreamFactory; |
26 | using Poco::Net::FTPPasswordProvider; |
27 | using Poco::Net::DialogSocket; |
28 | using Poco::Net::SocketAddress; |
29 | using Poco::Net::FTPException; |
30 | using Poco::URI; |
31 | using Poco::StreamCopier; |
32 | |
33 | |
34 | namespace |
35 | { |
36 | class TestPasswordProvider: public FTPPasswordProvider |
37 | { |
38 | public: |
39 | std::string password(const std::string& username, const std::string& host) |
40 | { |
41 | return "secret" ; |
42 | } |
43 | }; |
44 | } |
45 | |
46 | |
47 | FTPStreamFactoryTest::FTPStreamFactoryTest(const std::string& name): CppUnit::TestCase(name) |
48 | { |
49 | } |
50 | |
51 | |
52 | FTPStreamFactoryTest::~FTPStreamFactoryTest() |
53 | { |
54 | } |
55 | |
56 | |
57 | void FTPStreamFactoryTest::testDownload() |
58 | { |
59 | FTPStreamFactory::setPasswordProvider(0); |
60 | |
61 | DialogServer server; |
62 | server.addResponse("220 localhost FTP ready" ); |
63 | server.addResponse("331 Password required" ); |
64 | server.addResponse("230 Welcome" ); |
65 | server.addResponse("200 Type set to I" ); |
66 | server.addResponse("200 Type set to A" ); |
67 | |
68 | DialogServer dataServer(false); |
69 | dataServer.addResponse("line1\r\nline2" ); |
70 | std::ostringstream epsv; |
71 | epsv << "229 Entering Extended Passive Mode (|||" << dataServer.port() << "|)" ; |
72 | server.addResponse(epsv.str()); |
73 | server.addResponse("150 sending data\r\n226 Transfer complete" ); |
74 | server.addResponse("221 Good bye" ); |
75 | |
76 | URI uri; |
77 | uri.setScheme("ftp" ); |
78 | uri.setHost("127.0.0.1" ); |
79 | uri.setPort(server.port()); |
80 | uri.setPath("/test.txt;type=a" ); |
81 | FTPStreamFactory sf; |
82 | std::unique_ptr<std::istream> pStr(sf.open(uri)); |
83 | |
84 | std::ostringstream dataStr; |
85 | StreamCopier::copyStream(*pStr.get(), dataStr); |
86 | |
87 | pStr.reset(); |
88 | |
89 | std::string s(dataStr.str()); |
90 | assertTrue (s == "line1\r\nline2\r\n" ); |
91 | } |
92 | |
93 | |
94 | void FTPStreamFactoryTest::testList() |
95 | { |
96 | FTPStreamFactory::setPasswordProvider(0); |
97 | |
98 | DialogServer server; |
99 | server.addResponse("220 localhost FTP ready" ); |
100 | server.addResponse("331 Password required" ); |
101 | server.addResponse("230 Welcome" ); |
102 | server.addResponse("200 Type set to I" ); |
103 | server.addResponse("250 CWD OK" ); |
104 | server.addResponse("250 CWD OK" ); |
105 | |
106 | DialogServer dataServer(false); |
107 | dataServer.addResponse("file1\r\nfile2" ); |
108 | std::ostringstream epsv; |
109 | epsv << "229 Entering Extended Passive Mode (|||" << dataServer.port() << "|)" ; |
110 | server.addResponse(epsv.str()); |
111 | server.addResponse("150 sending data\r\n226 Transfer complete" ); |
112 | server.addResponse("221 Good bye" ); |
113 | |
114 | URI uri; |
115 | uri.setScheme("ftp" ); |
116 | uri.setHost("127.0.0.1" ); |
117 | uri.setPort(server.port()); |
118 | uri.setPath("/usr/guest/data;type=d" ); |
119 | FTPStreamFactory sf; |
120 | std::unique_ptr<std::istream> pStr(sf.open(uri)); |
121 | |
122 | std::ostringstream dataStr; |
123 | StreamCopier::copyStream(*pStr.get(), dataStr); |
124 | |
125 | pStr.reset(); |
126 | |
127 | std::string s(dataStr.str()); |
128 | assertTrue (s == "file1\r\nfile2\r\n" ); |
129 | } |
130 | |
131 | |
132 | void FTPStreamFactoryTest::testUserInfo() |
133 | { |
134 | FTPStreamFactory::setPasswordProvider(0); |
135 | |
136 | DialogServer server; |
137 | server.addResponse("220 localhost FTP ready" ); |
138 | server.addResponse("331 Password required" ); |
139 | server.addResponse("230 Welcome" ); |
140 | server.addResponse("200 Type set to I" ); |
141 | server.addResponse("200 Type set to A" ); |
142 | |
143 | DialogServer dataServer(false); |
144 | dataServer.addResponse("line1\r\nline2" ); |
145 | std::ostringstream epsv; |
146 | epsv << "229 Entering Extended Passive Mode (|||" << dataServer.port() << "|)" ; |
147 | server.addResponse(epsv.str()); |
148 | server.addResponse("150 sending data\r\n226 Transfer complete" ); |
149 | server.addResponse("221 Good bye" ); |
150 | |
151 | URI uri; |
152 | uri.setScheme("ftp" ); |
153 | uri.setHost("127.0.0.1" ); |
154 | uri.setPort(server.port()); |
155 | uri.setPath("/test.txt;type=a" ); |
156 | uri.setUserInfo("user:secret" ); |
157 | FTPStreamFactory sf; |
158 | std::unique_ptr<std::istream> pStr(sf.open(uri)); |
159 | |
160 | std::ostringstream dataStr; |
161 | StreamCopier::copyStream(*pStr.get(), dataStr); |
162 | |
163 | pStr.reset(); |
164 | |
165 | std::string s(dataStr.str()); |
166 | assertTrue (s == "line1\r\nline2\r\n" ); |
167 | } |
168 | |
169 | |
170 | void FTPStreamFactoryTest::testPasswordProvider() |
171 | { |
172 | static TestPasswordProvider tpp; |
173 | FTPStreamFactory::setPasswordProvider(&tpp); |
174 | |
175 | DialogServer server; |
176 | server.addResponse("220 localhost FTP ready" ); |
177 | server.addResponse("331 Password required" ); |
178 | server.addResponse("230 Welcome" ); |
179 | server.addResponse("200 Type set to I" ); |
180 | server.addResponse("200 Type set to A" ); |
181 | |
182 | DialogServer dataServer(false); |
183 | dataServer.addResponse("line1\r\nline2" ); |
184 | std::ostringstream epsv; |
185 | epsv << "229 Entering Extended Passive Mode (|||" << dataServer.port() << "|)" ; |
186 | server.addResponse(epsv.str()); |
187 | server.addResponse("150 sending data\r\n226 Transfer complete" ); |
188 | server.addResponse("221 Good bye" ); |
189 | |
190 | URI uri; |
191 | uri.setScheme("ftp" ); |
192 | uri.setHost("127.0.0.1" ); |
193 | uri.setPort(server.port()); |
194 | uri.setPath("/test.txt;type=a" ); |
195 | uri.setUserInfo("user" ); |
196 | FTPStreamFactory sf; |
197 | std::unique_ptr<std::istream> pStr(sf.open(uri)); |
198 | |
199 | std::ostringstream dataStr; |
200 | StreamCopier::copyStream(*pStr.get(), dataStr); |
201 | |
202 | pStr.reset(); |
203 | |
204 | std::string s(dataStr.str()); |
205 | assertTrue (s == "line1\r\nline2\r\n" ); |
206 | } |
207 | |
208 | |
209 | void FTPStreamFactoryTest::testMissingPasswordProvider() |
210 | { |
211 | FTPStreamFactory::setPasswordProvider(0); |
212 | |
213 | DialogServer server; |
214 | server.addResponse("220 localhost FTP ready" ); |
215 | server.addResponse("221 Good bye" ); |
216 | |
217 | URI uri; |
218 | uri.setScheme("ftp" ); |
219 | uri.setHost("127.0.0.1" ); |
220 | uri.setPort(server.port()); |
221 | uri.setPath("/test.txt;type=a" ); |
222 | uri.setUserInfo("user" ); |
223 | |
224 | try |
225 | { |
226 | FTPStreamFactory sf; |
227 | std::unique_ptr<std::istream> pStr(sf.open(uri)); |
228 | fail("no password provider - must throw" ); |
229 | } |
230 | catch (FTPException&) |
231 | { |
232 | } |
233 | } |
234 | |
235 | |
236 | void FTPStreamFactoryTest::setUp() |
237 | { |
238 | } |
239 | |
240 | |
241 | void FTPStreamFactoryTest::tearDown() |
242 | { |
243 | } |
244 | |
245 | |
246 | CppUnit::Test* FTPStreamFactoryTest::suite() |
247 | { |
248 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FTPStreamFactoryTest" ); |
249 | |
250 | CppUnit_addTest(pSuite, FTPStreamFactoryTest, testDownload); |
251 | CppUnit_addTest(pSuite, FTPStreamFactoryTest, testList); |
252 | CppUnit_addTest(pSuite, FTPStreamFactoryTest, testUserInfo); |
253 | CppUnit_addTest(pSuite, FTPStreamFactoryTest, testPasswordProvider); |
254 | CppUnit_addTest(pSuite, FTPStreamFactoryTest, testMissingPasswordProvider); |
255 | |
256 | return pSuite; |
257 | } |
258 | |