31
31
import com .jcraft .jsch .JSch ;
32
32
import com .jcraft .jsch .UserInfo ;
33
33
import hudson .model .Items ;
34
- import org .apache .sshd .server .SshServer ;
35
- import org .apache .sshd .common .NamedFactory ;
36
- import org .apache .sshd .server .auth .password .PasswordAuthenticator ;
37
- import org .apache .sshd .server .auth .UserAuth ;
38
- import org .apache .sshd .server .auth .password .UserAuthPasswordFactory ;
39
- import org .apache .sshd .server .keyprovider .SimpleGeneratorHostKeyProvider ;
40
- import org .apache .sshd .server .session .ServerSession ;
41
34
import org .junit .After ;
42
35
import org .junit .Before ;
43
36
import org .junit .Rule ;
44
37
import org .junit .Test ;
45
38
import org .jvnet .hudson .test .JenkinsRule ;
46
39
40
+ import java .lang .reflect .InvocationTargetException ;
47
41
import java .util .Arrays ;
42
+ import java .util .List ;
48
43
import java .util .concurrent .TimeUnit ;
49
44
import java .util .logging .Level ;
50
45
import java .util .logging .Logger ;
@@ -96,18 +91,21 @@ public void setUp() throws Exception {
96
91
97
92
@ Test
98
93
public void testPassword () throws Exception {
99
- SshServer sshd = SshServer .setUpDefaultServer ();
100
- sshd .setPort (0 );
101
- sshd .setKeyPairProvider (new SimpleGeneratorHostKeyProvider ());
102
- sshd .setPasswordAuthenticator (new PasswordAuthenticator () {
103
- public boolean authenticate (String username , String password , ServerSession session ) {
104
- return "foomanchu" .equals (password );
105
- }
106
- });
107
- sshd .setUserAuthFactories (Arrays .<NamedFactory <UserAuth >>asList (new UserAuthPasswordFactory ()));
94
+ Object sshd = newDefaultSshServer ();
95
+ Class keyPairProviderClass = newKeyPairProviderClass ();
96
+ Object provider = newProvider ();
97
+ Class authenticatorClass = newAuthenticatorClass ();
98
+ Object authenticator = newAuthenticator (authenticatorClass );
99
+ Object factory = newFactory ();
100
+
101
+ invoke (sshd , "setPort" , new Class [] {Integer .TYPE }, new Object [] {0 });
102
+ invoke (sshd , "setKeyPairProvider" , new Class [] {keyPairProviderClass }, new Object [] {provider });
103
+ invoke (sshd , "setPasswordAuthenticator" , new Class [] {authenticatorClass }, new Object [] {authenticator });
104
+ invoke (sshd , "setUserAuthFactories" , new Class [] {List .class }, new Object [] {Arrays .asList (factory )});
108
105
try {
109
- sshd .start ();
110
- connector = new JSchConnector (user .getUsername (),"localhost" , sshd .getPort ());
106
+ invoke (sshd , "start" , null , null );
107
+ int port = (Integer )invoke (sshd , "getPort" , null , null );
108
+ connector = new JSchConnector (user .getUsername (),"localhost" , port );
111
109
JSchSSHPasswordAuthenticator instance = new JSchSSHPasswordAuthenticator (connector , user );
112
110
assertThat (instance .getAuthenticationMode (), is (SSHAuthenticator .Mode .BEFORE_CONNECT ));
113
111
assertThat (instance .canAuthenticate (), is (true ));
@@ -119,7 +117,7 @@ public boolean authenticate(String username, String password, ServerSession sess
119
117
assertThat (connector .getSession ().isConnected (), is (true ));
120
118
} finally {
121
119
try {
122
- sshd . stop ( true );
120
+ invoke ( sshd , " stop" , new Class [] { Boolean . TYPE }, new Object [] { true } );
123
121
} catch (Throwable t ) {
124
122
Logger .getLogger (getClass ().getName ()).log (Level .WARNING , "Problems shutting down ssh server" , t );
125
123
}
@@ -128,18 +126,21 @@ public boolean authenticate(String username, String password, ServerSession sess
128
126
129
127
@ Test
130
128
public void testFactory () throws Exception {
131
- SshServer sshd = SshServer .setUpDefaultServer ();
132
- sshd .setPort (0 );
133
- sshd .setKeyPairProvider (new SimpleGeneratorHostKeyProvider ());
134
- sshd .setPasswordAuthenticator (new PasswordAuthenticator () {
135
- public boolean authenticate (String username , String password , ServerSession session ) {
136
- return "foomanchu" .equals (password );
137
- }
138
- });
139
- sshd .setUserAuthFactories (Arrays .<NamedFactory <UserAuth >>asList (new UserAuthPasswordFactory ()));
129
+ Object sshd = newDefaultSshServer ();
130
+ Class keyPairProviderClass = newKeyPairProviderClass ();
131
+ Object provider = newProvider ();
132
+ Class authenticatorClass = newAuthenticatorClass ();
133
+ Object authenticator = newAuthenticator (authenticatorClass );
134
+ Object factory = newFactory ();
135
+
136
+ invoke (sshd , "setPort" , new Class [] {Integer .TYPE }, new Object [] {0 });
137
+ invoke (sshd , "setKeyPairProvider" , new Class [] {keyPairProviderClass }, new Object [] {provider });
138
+ invoke (sshd , "setPasswordAuthenticator" , new Class [] {authenticatorClass }, new Object [] {authenticator });
139
+ invoke (sshd , "setUserAuthFactories" , new Class [] {List .class }, new Object [] {Arrays .asList (factory )});
140
140
try {
141
- sshd .start ();
142
- connector = new JSchConnector (user .getUsername (),"localhost" , sshd .getPort ());
141
+ invoke (sshd , "start" , null , null );
142
+ int port = (Integer )invoke (sshd , "getPort" , null , null );
143
+ connector = new JSchConnector (user .getUsername (),"localhost" , port );
143
144
SSHAuthenticator instance = SSHAuthenticator .newInstance (connector , user );
144
145
assertThat (instance .getAuthenticationMode (), is (SSHAuthenticator .Mode .BEFORE_CONNECT ));
145
146
assertThat (instance .canAuthenticate (), is (true ));
@@ -151,7 +152,7 @@ public boolean authenticate(String username, String password, ServerSession sess
151
152
assertThat (connector .getSession ().isConnected (), is (true ));
152
153
} finally {
153
154
try {
154
- sshd . stop ( true );
155
+ invoke ( sshd , " stop" , new Class [] { Boolean . TYPE }, new Object [] { true } );
155
156
} catch (Throwable t ) {
156
157
Logger .getLogger (getClass ().getName ()).log (Level .WARNING , "Problems shutting down ssh server" , t );
157
158
}
@@ -185,4 +186,88 @@ public HostKey[] getHostKey(String host, String type) {
185
186
return new HostKey [0 ];
186
187
}
187
188
}
189
+
190
+
191
+
192
+ private Object invoke (Object target , String methodName , Class [] parameterTypes , Object [] args ) throws NoSuchMethodException , InvocationTargetException , IllegalAccessException {
193
+ return target .getClass ().getMethod (methodName , parameterTypes ).invoke (target , args );
194
+ }
195
+
196
+ private Object newDefaultSshServer () throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , IllegalAccessException {
197
+ Object sshd = null ;
198
+ Class sshdClass ;
199
+ try {
200
+ sshdClass = Class .forName ("org.apache.sshd.SshServer" );
201
+ } catch (ClassNotFoundException e ) {
202
+ sshdClass = Class .forName ("org.apache.sshd.server.SshServer" );
203
+ }
204
+
205
+ sshd = sshdClass .getDeclaredMethod ("setUpDefaultServer" , null ).invoke (null );
206
+ assertNotNull (sshd );
207
+
208
+ return sshd ;
209
+ }
210
+
211
+ private Class newKeyPairProviderClass () throws ClassNotFoundException {
212
+ Class keyPairProviderClass ;
213
+ try {
214
+ keyPairProviderClass = Class .forName ("org.apache.sshd.common.KeyPairProvider" );
215
+ } catch (ClassNotFoundException e ) {
216
+ keyPairProviderClass = Class .forName ("org.apache.sshd.common.keyprovider.KeyPairProvider" );
217
+ }
218
+
219
+ return keyPairProviderClass ;
220
+ }
221
+
222
+ private Object newProvider () throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
223
+ Class providerClass = Class .forName ("org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider" );
224
+ Object provider = providerClass .getConstructor ().newInstance ();
225
+ assertNotNull (provider );
226
+
227
+ return provider ;
228
+ }
229
+ private Class newAuthenticatorClass () throws ClassNotFoundException {
230
+ Class authenticatorClass ;
231
+ try {
232
+ authenticatorClass = Class .forName ("org.apache.sshd.server.auth.password.PasswordAuthenticator" );
233
+ } catch (ClassNotFoundException e ) {
234
+ authenticatorClass = Class .forName ("org.apache.sshd.server.PasswordAuthenticator" );
235
+ }
236
+
237
+ return authenticatorClass ;
238
+ }
239
+
240
+ private Object newAuthenticator (Class authenticatorClass ) throws ClassNotFoundException , IllegalArgumentException {
241
+ Object authenticator = java .lang .reflect .Proxy .newProxyInstance (
242
+ authenticatorClass .getClassLoader (),
243
+ new java .lang .Class []{authenticatorClass },
244
+ new java .lang .reflect .InvocationHandler () {
245
+
246
+ @ Override
247
+ public Object invoke (Object proxy , java .lang .reflect .Method method , Object [] args ) throws java .lang .Throwable {
248
+ if (method .getName ().equals ("authenticate" )) {
249
+ return "foomanchu" .equals (args [1 ]);
250
+ }
251
+
252
+ return null ;
253
+ }
254
+ });
255
+ assertNotNull (authenticator );
256
+ return authenticator ;
257
+ }
258
+
259
+ private Object newFactory () throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
260
+ Object factory = null ;
261
+ Class factoryClass ;
262
+ try {
263
+ factoryClass = Class .forName ("org.apache.sshd.server.auth.UserAuthPassword$Factory" );
264
+ } catch (ClassNotFoundException e ) {
265
+ factoryClass = Class .forName ("org.apache.sshd.server.auth.password.UserAuthPasswordFactory" );
266
+ }
267
+
268
+ factory = factoryClass .getConstructor ().newInstance ();
269
+
270
+ assertNotNull (factory );
271
+ return factory ;
272
+ }
188
273
}
0 commit comments