View Javadoc
1   /*
2    * Copyright 2009-2009 CommonsSSH Project.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.commons.ssh;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * @author Sergey Vidyuk (svidyuk at gmail dot com)
29   * 
30   */
31  public abstract class AbstractSSHConnectionTest extends TestCase {
32      private String host, user, pass, publickeyfile, publickeyphase;
33  
34      /***
35       * Logger
36       */
37      protected final Log log = LogFactory.getLog(getClass());
38  
39      /***
40       * Obtains new connection factory
41       * 
42       * @return new connection factory
43       * @throws Exception
44       */
45      protected abstract ConnectionFactory newFactory() throws Exception;
46  
47      protected void setUp() throws Exception {
48  	super.setUp();
49  
50  	this.host = System.getProperty("net.sf.commons.ssh.test.host");
51  	this.user = System.getProperty("net.sf.commons.ssh.test.user");
52  	this.pass = System.getProperty("net.sf.commons.ssh.test.pass");
53  	this.publickeyfile = System
54  		.getProperty("net.sf.commons.ssh.test.publickeyfile");
55  	this.publickeyphase = System
56  		.getProperty("net.sf.commons.ssh.test.publickeyphase");
57      }
58  
59      private void testConnect(ConnectionFactory factory,
60  	    AuthenticationOptions authOptions) throws Exception, IOException {
61  	Connection connection = factory.openConnection(host, authOptions);
62  	try {
63  	    ShellSession shellSession = connection
64  		    .openShellSession(new ShellSessionOptions());
65  	    shellSession.close();
66  
67  	    assertTrue(shellSession.isClosed());
68  	} finally {
69  	    connection.close();
70  	}
71  
72  	assertTrue(connection.isClosed());
73      }
74  
75      /***
76       * Test that it is possible to connect to test host using specified
77       * parameters from system properties and execute "echo" command.
78       * 
79       * @throws Exception
80       */
81      public void testConnectAndEchoByExec() throws Exception {
82  	log.trace("testConnectAndEchoByExec()");
83  
84  	ConnectionFactory factory = newFactory();
85  
86  	if (!factory.isFeatureSupported(Features.SESSION_EXEC)) {
87  	    log.warn("COnnection factory " + factory + " doesn't support '"
88  		    + Features.SESSION_EXEC + "' feature");
89  	    return;
90  	}
91  
92  	AuthenticationOptions authOptions = new PasswordAuthenticationOptions(
93  		user, pass);
94  	Connection connection = factory.openConnection(host, authOptions);
95  
96  	final ExecSession execSession = connection
97  		.openExecSession(new ExecSessionOptions("echo 123"));
98  
99  	final InputStream inputStream = execSession.getInputStream();
100 
101 	final StringBuffer stringBuilder = new StringBuffer();
102 
103 	final Thread second = new StreamDataToBufferThread(inputStream,
104 		stringBuilder);
105 	second.start();
106 
107 	Thread.sleep(2000);
108 
109 	execSession.close();
110 	connection.close();
111 
112 	second.interrupt();
113 
114 	assertEquals("123 not found: " + stringBuilder, 0, stringBuilder
115 		.indexOf("123"));
116     }
117 
118     /***
119      * Test SDTP feature
120      * 
121      * @throws Exception
122      */
123     public void testSFTP() throws Exception {
124 	log.trace("testSFTP()");
125 
126 	ConnectionFactory factory = newFactory();
127 
128 	if (!factory.isFeatureSupported(Features.SESSION_SFTP)) {
129 	    log.warn("COnnection factory " + factory + " doesn't support '"
130 		    + Features.SESSION_SFTP + "' feature");
131 	    return;
132 	}
133 
134 	AuthenticationOptions authOptions = new PasswordAuthenticationOptions(
135 		user, pass);
136 	Connection connection = factory.openConnection(host, authOptions);
137 
138 	SftpSessionOptions sftpSessionOptions = new SftpSessionOptions();
139 	SftpSession sftpSession = connection
140 		.openSftpSession(sftpSessionOptions);
141 
142 	sftpSession.put(AbstractSSHConnectionTest.class
143 		.getResourceAsStream("somefile.bin"), "somefile.bin");
144 
145 	SftpFileAttributes fileAttributes = sftpSession.stat("somefile.bin");
146 	assertEquals(13, fileAttributes.getSize());
147 
148 	sftpSession.rm("somefile.bin");
149 
150 	sftpSession.close();
151 	connection.close();
152     }
153 
154     /***
155      * Test that it is possible to connect to test host using specified
156      * parameters from system properties and execute "echo" command.
157      * 
158      * @throws Exception
159      */
160     public void testConnectAndEchoByShell() throws Exception {
161 	log.trace("testConnectAndEchoByShell()");
162 
163 	ConnectionFactory factory = newFactory();
164 
165 	if (!factory.isFeatureSupported(Features.SESSION_SHELL)) {
166 	    log.warn("Connection factory " + factory + " doesn't support '"
167 		    + Features.SESSION_SHELL + "' feature");
168 	    return;
169 	}
170 
171 	AuthenticationOptions authOptions = new PasswordAuthenticationOptions(
172 		user, pass);
173 	Connection connection = factory.openConnection(host, authOptions);
174 
175 	final ShellSession shellSession = connection
176 		.openShellSession(new ShellSessionOptions());
177 
178 	final InputStream inputStream = shellSession.getInputStream();
179 	final OutputStream outputStream = shellSession.getOutputStream();
180 
181 	final StringBuffer stringBuilder = new StringBuffer();
182 
183 	final Thread second = new StreamDataToBufferThread(inputStream,
184 		stringBuilder);
185 	second.start();
186 
187 	Thread.sleep(1000);
188 
189 	outputStream.write("echo 123\n\r".getBytes("utf-8"));
190 	outputStream.flush();
191 
192 	Thread.sleep(2000);
193 
194 	shellSession.close();
195 	connection.close();
196 
197 	second.interrupt();
198 
199 	assertTrue("123 not found: " + stringBuilder, stringBuilder.toString()
200 		.indexOf("\n123") != -1);
201     }
202 
203     /***
204      * Test that it is possible to connect to test host using credentials
205      * authentication
206      */
207     public void testConnectWithPassword() throws Exception {
208 	log.trace("testConnectWithPassword()");
209 	ConnectionFactory factory = newFactory();
210 
211 	if (!factory.isFeatureSupported(Features.AUTH_CREDENTIALS)) {
212 	    log.warn("Connection factory " + factory + " doesn't support '"
213 		    + Features.AUTH_CREDENTIALS + "' feature");
214 	    return;
215 	}
216 
217 	AuthenticationOptions authOptions = new PasswordAuthenticationOptions(
218 		user, pass);
219 	testConnect(factory, authOptions);
220     }
221 
222     /***
223      * Test that it is possible to connect to test host using public key
224      * authentication
225      */
226     public void testConnectWithPublicKey() throws Exception {
227 	log.trace("testConnectWithPublicKey()");
228 	ConnectionFactory factory = newFactory();
229 
230 	if (!factory.isFeatureSupported(Features.AUTH_PUBLICKEY)) {
231 	    log.warn("Connection factory " + factory + " doesn't support '"
232 		    + Features.AUTH_PUBLICKEY + "' feature");
233 	    return;
234 	}
235 
236 	AuthenticationOptions authOptions = new PublicKeyAuthenticationOptions(
237 		user, publickeyfile, publickeyphase);
238 	testConnect(factory, authOptions);
239     }
240 
241     /***
242      * Tests ability to obtain connection factory
243      * 
244      * @throws Exception
245      */
246     public void testObtain() throws Exception {
247 	newFactory();
248     }
249 }