1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.sftp;
18
19 import java.io.InputStream;
20
21 import org.apache.commons.vfs2.Capability;
22 import org.apache.commons.vfs2.FileObject;
23 import org.apache.commons.vfs2.FileSystemException;
24 import org.apache.mina.core.session.IoSession;
25 import org.apache.sshd.common.FactoryManager;
26 import org.apache.sshd.common.session.AbstractSession;
27 import org.apache.sshd.server.session.ServerSession;
28 import org.apache.sshd.server.session.SessionFactory;
29 import org.junit.Test;
30 import org.junit.jupiter.api.Assertions;
31
32
33
34
35 public class SftpPutChannelTestCase extends AbstractSftpProviderTestCase {
36
37
38
39
40 private static class CustomServerSession extends ServerSession {
41 public CustomServerSession(final FactoryManager server, final IoSession ioSession) throws Exception {
42 super(server, ioSession);
43 }
44
45 public int getChannelsCount() {
46 return channels.size();
47 }
48 }
49
50 private static class CustomSessionFactory extends SessionFactory {
51 @Override
52 protected AbstractSession doCreateSession(final IoSession ioSession) throws Exception {
53 return new CustomServerSession(server, ioSession);
54 }
55 }
56
57 private static final Integer MAX_CHANNELS = 10;
58
59
60
61
62 public static junit.framework.Test suite() throws Exception {
63 return new SftpProviderTestSuite(new SftpPutChannelTestCase()) {
64 @Override
65 protected void addBaseTests() throws Exception {
66
67 addTests(SftpPutChannelTestCase.class);
68 }
69 };
70 }
71
72
73
74
75 @Override
76 protected Capability[] getRequiredCapabilities() {
77 return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
78 Capability.WRITE_CONTENT };
79 }
80
81 @Override
82 protected boolean isExecChannelClosed() {
83 return false;
84 }
85
86 @Override
87 protected SessionFactory sessionFactory() {
88 return new CustomSessionFactory();
89 }
90
91
92
93
94 @Test
95 public void testDoGetInputStream() throws Exception {
96 final FileObject readFolder = getReadFolder();
97
98
99 for (int i = 0; i < MAX_CHANNELS * 2; i++) {
100 try {
101 try (InputStream ignored = readFolder.resolveFile("not-exists.txt").getContent().getInputStream()) {
102 Assertions.fail("file should not be exists");
103 }
104 } catch (final FileSystemException e) {
105 final int channelsCount = ((CustomServerSession) server.getActiveSessions().get(0)).getChannelsCount();
106 Assertions.assertTrue(channelsCount < MAX_CHANNELS, "channels count expected less than " + MAX_CHANNELS);
107 }
108 }
109 }
110
111 }