Skip to main content
summaryrefslogtreecommitdiffstats
blob: 07e04f712df8d15e79eca7c1297cae4e67b9eb6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package org.eclipse.ptp.core.tests;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import junit.framework.TestCase;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

public class SFTPTests extends TestCase {
	private static final String USERNAME = "user"; //$NON-NLS-1$
	private static final String PASSWORD = "password"; //$NON-NLS-1$
	private static final String HOST = "localhost"; //$NON-NLS-1$
	private static final String PATH1 = "/home/user/sftp_test"; //$NON-NLS-1$
	private static final String PATH2 = PATH1 + "/.file1"; //$NON-NLS-1$
	private static final String TEST_STRING = "a string containing fairly *()(*&^$%## random text"; //$NON-NLS-1$
	
	
	private class SSHUserInfo implements UserInfo, UIKeyboardInteractive {
		private SSHUserInfo() { }

		public String getPassword() {
			return PASSWORD;
		}
		
		public void setPassword(String password) {
		}

		public boolean promptYesNo(String str) {
			return true;
		}

		public String getPassphrase() {
			return "";
		}
		
		public void setPassphrase(String passphrase) {
		}

		public boolean promptPassphrase(String message) {
			return false;
		}

		public boolean promptPassword(String message) {
			return true;
		}
		
		public void setUsePassword(boolean usePassword) {
		}

		public void showMessage(String message) {
		}

		public String[] promptKeyboardInteractive(final String destination,
				final String name, final String instruction,
				final String[] prompt, final boolean[] echo) {
			if (prompt.length != 1
					|| echo[0] != false) {
				return null;
			}
			String[] response = new String[1];
			response[0] = PASSWORD;
			return response;
		}
	}
	
	private JSch jsch;
	private Session session;
	private ChannelSftp sftp;
	
	public void testSftp() {
		for (int i = 0; i < 5; i++) {
			System.out.print("starting test... ");
			
			// stat
			SftpATTRS attrs = null;
			try {
				attrs = sftp.stat(PATH1);
			} catch (SftpException e) {
				assertTrue(e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE);
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			assertNull(attrs);
			
			// mkdir
			
			try {
				sftp.mkdir(PATH1);
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			attrs = null;
			try {
				attrs = sftp.stat(PATH1);
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			assertNotNull(attrs);
			assertTrue(attrs.isDir());
			
			// test write
			attrs = null;
			try {
				attrs = sftp.stat(PATH2);
			} catch (SftpException e) {
				assertTrue(e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE);
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			assertNull(attrs);
			
			try {
				OutputStream stream = sftp.put(PATH2);
				assertNotNull(stream);
				BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(stream));
				buf.write(TEST_STRING);
				buf.close();
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			
			attrs = null;
			try {
				attrs = sftp.stat(PATH2);
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			assertNotNull(attrs);
	
			// read
			try {
				InputStream stream = sftp.get(PATH2);
				assertNotNull(stream);
				BufferedReader buf = new BufferedReader(new InputStreamReader(stream));
				String line = buf.readLine().trim();
				assertTrue(line.equals(TEST_STRING));
				buf.close();
			} catch (Exception e) {
				fail(e.getLocalizedMessage());
			}
			
			try {
				sftp.rm(PATH2);
			} catch (SftpException e) {
				fail(e.getLocalizedMessage());
			}
			
			try {
				sftp.rmdir(PATH1);
			} catch (SftpException e) {
				fail(e.getLocalizedMessage());
			}
			
			System.out.println("completed");
		}
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		jsch = new JSch();
		session = jsch.getSession(USERNAME, HOST);
		session.setUserInfo(new SSHUserInfo());
		session.connect();
		sftp = (ChannelSftp) session.openChannel("sftp");
		sftp.connect();
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		sftp.disconnect();
		session.disconnect();
	}
	
}

Back to the top