You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2011/09/21 15:42:53 UTC
svn commit: r1173638 - in /maven/wagon/trunk/wagon-providers: ./
wagon-ssh-common-test/
wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/
wagon-ssh-external/ wagon-ssh/
wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ss...
Author: olamy
Date: Wed Sep 21 13:42:52 2011
New Revision: 1173638
URL: http://svn.apache.org/viewvc?rev=1173638&view=rev
Log:
[WAGON-350] add unit test for wagon ssh with an embedded ssh server
refactor with moving embedded server class to ssh-common-test for reuse it with ssh-external test
Added:
maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommand.java
- copied, changed from r1173637, maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommand.java
maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommandFactory.java
- copied, changed from r1173637, maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommandFactory.java
maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ShellCommand.java
maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/SshServerEmbedded.java
Removed:
maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommand.java
maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommandFactory.java
Modified:
maven/wagon/trunk/wagon-providers/pom.xml
maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/pom.xml
maven/wagon/trunk/wagon-providers/wagon-ssh-external/pom.xml
maven/wagon/trunk/wagon-providers/wagon-ssh/pom.xml
maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpWagonTest.java
Modified: maven/wagon/trunk/wagon-providers/pom.xml
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/pom.xml?rev=1173638&r1=1173637&r2=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/pom.xml (original)
+++ maven/wagon/trunk/wagon-providers/pom.xml Wed Sep 21 13:42:52 2011
@@ -58,6 +58,12 @@ under the License.
<artifactId>httpcore</artifactId>
<version>4.1.2</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.sshd</groupId>
+ <artifactId>sshd-core</artifactId>
+ <version>0.6.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</dependencyManagement>
Modified: maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/pom.xml
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/pom.xml?rev=1173638&r1=1173637&r2=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/pom.xml (original)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/pom.xml Wed Sep 21 13:42:52 2011
@@ -49,5 +49,10 @@ under the License.
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sshd</groupId>
+ <artifactId>sshd-core</artifactId>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
Copied: maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommand.java (from r1173637, maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommand.java)
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommand.java?p2=maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommand.java&p1=maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommand.java&r1=1173637&r2=1173638&rev=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommand.java (original)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommand.java Wed Sep 21 13:42:52 2011
@@ -1,4 +1,4 @@
-package org.apache.maven.wagon.providers.ssh.jsch;
+package org.apache.maven.wagon.providers.ssh;
/*
* Licensed to the Apache Software Foundation (ASF) under one
Copied: maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommandFactory.java (from r1173637, maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommandFactory.java)
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommandFactory.java?p2=maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommandFactory.java&p1=maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommandFactory.java&r1=1173637&r2=1173638&rev=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpCommandFactory.java (original)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ScpCommandFactory.java Wed Sep 21 13:42:52 2011
@@ -1,4 +1,4 @@
-package org.apache.maven.wagon.providers.ssh.jsch;
+package org.apache.maven.wagon.providers.ssh;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,12 +18,12 @@ package org.apache.maven.wagon.providers
* under the License.
*/
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* This <code>CommandFactory</code> can be used as a standalone command factory
* or can be used to augment another <code>CommandFactory</code> and provides
@@ -33,24 +33,25 @@ import org.apache.sshd.server.CommandFac
*
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
*/
-public class ScpCommandFactory implements CommandFactory {
+public class ScpCommandFactory
+ implements CommandFactory {
private CommandFactory delegate;
public ScpCommandFactory() {
}
- public ScpCommandFactory(CommandFactory delegate) {
+ public ScpCommandFactory( CommandFactory delegate ) {
this.delegate = delegate;
}
/**
* Parses a command string and verifies that the basic syntax is
* correct. If parsing fails the responsibility is delegated to
- * the configured {@link CommandFactory} instance; if one exist.
+ * the configured {@link org.apache.sshd.server.CommandFactory} instance; if one exist.
*
- * @param command command to parse
- * @return configured {@link Command} instance
+ * @param command command to parse
+ * @return configured {@link org.apache.sshd.server.Command} instance
* @throws IllegalArgumentException
*/
public Command createCommand(String command) {
Added: maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ShellCommand.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ShellCommand.java?rev=1173638&view=auto
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ShellCommand.java (added)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/ShellCommand.java Wed Sep 21 13:42:52 2011
@@ -0,0 +1,170 @@
+package org.apache.maven.wagon.providers.ssh;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.sshd.server.Command;
+import org.apache.sshd.server.Environment;
+import org.apache.sshd.server.ExitCallback;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @author Olivier Lamy
+ */
+public class ShellCommand implements Command
+{
+protected static final int OK = 0;
+
+ protected static final int WARNING = 1;
+
+ protected static final int ERROR = 2;
+
+ private InputStream in;
+
+ private OutputStream out;
+
+ private OutputStream err;
+
+ private ExitCallback callback;
+
+ private Thread thread;
+
+ private String commandLine;
+
+ public ShellCommand( String commandLine )
+ {
+ this.commandLine = commandLine;
+ }
+
+ public void setInputStream( InputStream in )
+ {
+ this.in = in;
+ }
+
+ public void setOutputStream( OutputStream out )
+ {
+ this.out = out;
+ }
+
+ public void setErrorStream( OutputStream err )
+ {
+ this.err = err;
+ }
+
+ public void setExitCallback( ExitCallback callback )
+ {
+ this.callback = callback;
+ }
+
+ public void start( Environment env )
+ throws IOException
+ {
+ File tmpFile = File.createTempFile( "wagon", "test-sh" );
+ tmpFile.deleteOnExit();
+ int exitValue = 0;
+ CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
+ CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
+ try
+ {
+
+ // hackhish defaut commandline tools not support ; or && so write a file with the script
+ // and "/bin/sh -e " + tmpFile.getPath();
+ FileUtils.fileWrite( tmpFile, commandLine );
+
+ Commandline cl = new Commandline();
+ cl.setExecutable( "/bin/sh" );
+ //cl.createArg().setValue( "-e" );
+ //cl.createArg().setValue( tmpFile.getPath() );
+ cl.createArg().setFile( tmpFile );
+
+ exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
+ System.out.println( "exit value " + exitValue );
+ /*
+ if ( exitValue == 0 )
+ {
+ out.write( stdout.getOutput().getBytes() );
+ out.write( '\n' );
+ out.flush();
+
+ }
+ else
+ {
+ out.write( stderr.getOutput().getBytes() );
+ out.write( '\n' );
+ out.flush();
+
+ }*/
+
+ }
+ catch ( Exception e )
+ {
+ exitValue = ERROR;
+ e.printStackTrace();
+ }
+ finally
+ {
+ deleteQuietly( tmpFile );
+ if ( exitValue != 0 )
+ {
+ err.write( stderr.getOutput().getBytes() );
+ err.write( '\n' );
+ err.flush();
+ callback.onExit( exitValue, stderr.getOutput() );
+ }
+ else
+ {
+ out.write( stdout.getOutput().getBytes() );
+ out.write( '\n' );
+ out.flush();
+ callback.onExit( exitValue, stdout.getOutput() );
+ }
+
+ }
+ /*
+ out.write( exitValue );
+ out.write( '\n' );
+
+ */
+ out.flush();
+ }
+
+ public void destroy()
+ {
+
+ }
+
+ private void deleteQuietly( File f )
+ {
+
+ try
+ {
+ f.delete();
+ }
+ catch ( Exception e )
+ {
+ // ignore
+ }
+ }
+}
Added: maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/SshServerEmbedded.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/SshServerEmbedded.java?rev=1173638&view=auto
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/SshServerEmbedded.java (added)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/SshServerEmbedded.java Wed Sep 21 13:42:52 2011
@@ -0,0 +1,180 @@
+package org.apache.maven.wagon.providers.ssh;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.mina.core.session.IoSession;
+import org.apache.sshd.SshServer;
+import org.apache.sshd.common.Session;
+import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
+import org.apache.sshd.common.session.AbstractSession;
+import org.apache.sshd.server.Command;
+import org.apache.sshd.server.CommandFactory;
+import org.apache.sshd.server.FileSystemFactory;
+import org.apache.sshd.server.FileSystemView;
+import org.apache.sshd.server.PublickeyAuthenticator;
+import org.apache.sshd.server.SshFile;
+import org.apache.sshd.server.auth.UserAuthPassword;
+import org.apache.sshd.server.auth.UserAuthPublicKey;
+import org.apache.sshd.server.filesystem.NativeSshFile;
+import org.apache.sshd.server.session.ServerSession;
+import org.apache.sshd.server.session.SessionFactory;
+import org.apache.sshd.server.shell.ProcessShellFactory;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.PublicKey;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ */
+public class SshServerEmbedded
+{
+ private String wagonProtocol;
+
+ private int port;
+
+ private SshServer sshd;
+
+ private List<String> sshKeysFiles = new ArrayList<String>();
+
+ public SshServerEmbedded( String wagonProtocol, List<String> sshKeysFiles )
+ {
+ this.wagonProtocol = wagonProtocol;
+
+ this.sshKeysFiles = sshKeysFiles;
+
+ this.sshd = SshServer.setUpDefaultServer();
+
+
+ }
+
+ /**
+ * @return random port used
+ */
+ public int start()
+ throws IOException
+ {
+ sshd.setPort( 0 );
+
+ sshd.setUserAuthFactories( Arrays.asList( new UserAuthPublicKey.Factory(), new UserAuthPassword.Factory() ) );
+
+ sshd.setPublickeyAuthenticator( new PublickeyAuthenticator()
+ {
+ public boolean authenticate( String s, PublicKey publicKey, ServerSession serverSession )
+ {
+ return true;
+ }
+ } );
+
+ FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider();
+
+ fileKeyPairProvider.setFiles( sshKeysFiles.toArray( new String[sshKeysFiles.size()] ) );
+
+ sshd.setKeyPairProvider( fileKeyPairProvider );
+ SessionFactory sessionFactory = new SessionFactory()
+ {
+ @Override
+ protected AbstractSession doCreateSession( IoSession ioSession )
+ throws Exception
+ {
+ System.out.println( "doCreateSession" );
+ return super.doCreateSession( ioSession );
+ }
+ };
+ sshd.setSessionFactory( sessionFactory );
+
+ //sshd.setFileSystemFactory( );
+
+ final ProcessShellFactory processShellFactory =
+ new ProcessShellFactory( new String[]{ "/bin/sh", "-i", "-l" } );
+ sshd.setShellFactory( processShellFactory );
+
+ CommandFactory delegateCommandFactory = new CommandFactory()
+ {
+ public Command createCommand( String command )
+ {
+ return new ShellCommand( command );
+ }
+ };
+
+ ScpCommandFactory commandFactory = new ScpCommandFactory( delegateCommandFactory );
+ sshd.setCommandFactory( commandFactory );
+
+ FileSystemFactory fileSystemFactory = new FileSystemFactory()
+ {
+ public FileSystemView createFileSystemView( Session session )
+ throws IOException
+ {
+ return new FileSystemView()
+ {
+ public SshFile getFile( String file )
+ {
+ file = file.replace( "\\", "" );
+ file = file.replace( "\"", "" );
+ File f = new File( FileUtils.normalize( file ) );
+
+ return new SshServerEmbedded.TestSshFile( f.getAbsolutePath(), f,
+ System.getProperty( "user.name" ) );
+ }
+
+ public SshFile getFile( SshFile baseDir, String file )
+ {
+ file = file.replace( "\\", "" );
+ file = file.replace( "\"", "" );
+ File f = new File( FileUtils.normalize( file ) );
+ return new SshServerEmbedded.TestSshFile( f.getAbsolutePath(), f,
+ System.getProperty( "user.name" ) );
+ }
+ };
+ }
+ };
+ sshd.setNioWorkers( 0 );
+ //sshd.setScheduledExecutorService( );
+ sshd.setFileSystemFactory( fileSystemFactory );
+ sshd.start();
+ this.port = sshd.getPort();
+ return this.port;
+ }
+
+
+ public void stop( boolean immediately )
+ throws InterruptedException
+ {
+ sshd.stop( immediately );
+ }
+
+ public int getPort()
+ {
+ return port;
+ }
+
+ public static class TestSshFile
+ extends NativeSshFile
+ {
+ public TestSshFile( String fileName, File file, String userName )
+ {
+
+ super( FileUtils.normalize( fileName ), file, userName );
+ }
+ }
+}
Modified: maven/wagon/trunk/wagon-providers/wagon-ssh-external/pom.xml
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh-external/pom.xml?rev=1173638&r1=1173637&r2=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh-external/pom.xml (original)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh-external/pom.xml Wed Sep 21 13:42:52 2011
@@ -47,6 +47,11 @@ under the License.
<artifactId>wagon-ssh-common-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sshd</groupId>
+ <artifactId>sshd-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<profiles>
Modified: maven/wagon/trunk/wagon-providers/wagon-ssh/pom.xml
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh/pom.xml?rev=1173638&r1=1173637&r2=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh/pom.xml (original)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh/pom.xml Wed Sep 21 13:42:52 2011
@@ -58,7 +58,6 @@ under the License.
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
- <version>0.6.0</version>
<scope>test</scope>
</dependency>
@@ -76,13 +75,6 @@ under the License.
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-exec</artifactId>
- <version>1.1</version>
- <scope>test</scope>
- </dependency>
-
</dependencies>
<profiles>
Modified: maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpWagonTest.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpWagonTest.java?rev=1173638&r1=1173637&r2=1173638&view=diff
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpWagonTest.java (original)
+++ maven/wagon/trunk/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/jsch/ScpWagonTest.java Wed Sep 21 13:42:52 2011
@@ -19,13 +19,11 @@ package org.apache.maven.wagon.providers
* under the License.
*/
-import org.apache.commons.exec.DefaultExecutor;
-import org.apache.commons.exec.Executor;
-import org.apache.commons.exec.LogOutputStream;
-import org.apache.commons.exec.PumpStreamHandler;
import org.apache.maven.wagon.StreamingWagonTestCase;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.providers.ssh.ShellCommand;
+import org.apache.maven.wagon.providers.ssh.SshServerEmbedded;
import org.apache.maven.wagon.providers.ssh.TestData;
import org.apache.maven.wagon.providers.ssh.knownhost.KnownHostsProvider;
import org.apache.maven.wagon.repository.Repository;
@@ -37,8 +35,6 @@ import org.apache.sshd.common.keyprovide
import org.apache.sshd.common.session.AbstractSession;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
-import org.apache.sshd.server.Environment;
-import org.apache.sshd.server.ExitCallback;
import org.apache.sshd.server.FileSystemFactory;
import org.apache.sshd.server.FileSystemView;
import org.apache.sshd.server.PublickeyAuthenticator;
@@ -50,17 +46,11 @@ import org.apache.sshd.server.session.Se
import org.apache.sshd.server.session.SessionFactory;
import org.apache.sshd.server.shell.ProcessShellFactory;
import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.cli.CommandLineUtils;
-import org.codehaus.plexus.util.cli.Commandline;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.security.PublicKey;
-import java.util.ArrayList;
import java.util.Arrays;
-import java.util.List;
/**
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
@@ -70,7 +60,7 @@ public class ScpWagonTest
extends StreamingWagonTestCase
{
- final SshServer sshd = SshServer.setUpDefaultServer();
+ SshServerEmbedded sshServerEmbedded;
@Override
protected Wagon getWagon()
@@ -109,91 +99,19 @@ public class ScpWagonTest
{
super.setUp();
- sshd.setPort( 0 );
-
- sshd.setUserAuthFactories( Arrays.asList( new UserAuthPublicKey.Factory(), new UserAuthPassword.Factory() ) );
-
- sshd.setPublickeyAuthenticator( new PublickeyAuthenticator()
- {
- public boolean authenticate( String s, PublicKey publicKey, ServerSession serverSession )
- {
- return true;
- }
- } );
-
- FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider();
File sshKey = new File( System.getProperty( "sshKeysPath", "src/test/ssh-keys" ), "id_rsa" );
- fileKeyPairProvider.setFiles( Arrays.asList( sshKey.getPath() ).toArray( new String[1] ) );
-
- sshd.setKeyPairProvider( fileKeyPairProvider );
- SessionFactory sessionFactory = new SessionFactory()
- {
- @Override
- protected AbstractSession doCreateSession( IoSession ioSession )
- throws Exception
- {
- System.out.println( "doCreateSession" );
- return super.doCreateSession( ioSession );
- }
- };
- sshd.setSessionFactory( sessionFactory );
-
- //sshd.setFileSystemFactory( );
- final ProcessShellFactory processShellFactory =
- new ProcessShellFactory( new String[]{ "/bin/sh", "-i", "-l" } );
- sshd.setShellFactory( processShellFactory );
+ sshServerEmbedded = new SshServerEmbedded( getProtocol(), Arrays.asList( sshKey.getPath() ) );
- CommandFactory delegateCommandFactory = new CommandFactory()
- {
- public Command createCommand( String command )
- {
- return new ShellCommand( command );
- }
- };
-
- ScpCommandFactory commandFactory = new ScpCommandFactory( delegateCommandFactory );
- sshd.setCommandFactory( commandFactory );
-
- FileSystemFactory fileSystemFactory = new FileSystemFactory()
- {
- public FileSystemView createFileSystemView( Session session )
- throws IOException
- {
- return new FileSystemView()
- {
- // Executing command: scp -t "/Users/olamy/dev/sources/maven/maven-wagon/wagon-providers/wagon-ssh/target/classes/wagon-ssh-test/olamy/test-resource"
- public SshFile getFile( String file )
- {
- file = file.replace( "\\", "" );
- file = file.replace( "\"", "" );
- File f = new File( FileUtils.normalize( file ) );
-
- return new TestSshFile( f.getAbsolutePath(), f, System.getProperty( "user.name" ) );
- }
-
- public SshFile getFile( SshFile baseDir, String file )
- {
- file = file.replace( "\\", "" );
- file = file.replace( "\"", "" );
- File f = new File( FileUtils.normalize( file ) );
- return new TestSshFile( f.getAbsolutePath(), f, System.getProperty( "user.name" ) );
- }
- };
- }
- };
- sshd.setNioWorkers( 0 );
- //sshd.setScheduledExecutorService( );
- sshd.setFileSystemFactory( fileSystemFactory );
- sshd.start();
- System.out.println( "sshd on port " + sshd.getPort() );
+ sshServerEmbedded.start();
+ System.out.println( "sshd on port " + sshServerEmbedded.getPort() );
}
@Override
protected void tearDownWagonTestingFixtures()
throws Exception
{
- sshd.stop( true );
+ sshServerEmbedded.stop( true );
}
protected String getProtocol()
@@ -204,13 +122,13 @@ public class ScpWagonTest
@Override
protected int getTestRepositoryPort()
{
- return sshd.getPort();
+ return sshServerEmbedded.getPort();
}
public String getTestRepositoryUrl()
{
- return TestData.getTestRepositoryUrl( sshd.getPort() );
+ return TestData.getTestRepositoryUrl( sshServerEmbedded.getPort() );
}
protected AuthenticationInfo getAuthInfo()
@@ -237,187 +155,6 @@ public class ScpWagonTest
}
- protected static class ShellCommand
- implements Command
- {
-
- protected static final int OK = 0;
-
- protected static final int WARNING = 1;
-
- protected static final int ERROR = 2;
-
- private InputStream in;
-
- private OutputStream out;
-
- private OutputStream err;
-
- private ExitCallback callback;
-
- private Thread thread;
-
- private String commandLine;
-
- ShellCommand( String commandLine )
- {
- this.commandLine = commandLine;
- }
-
- public void setInputStream( InputStream in )
- {
- this.in = in;
- }
-
- public void setOutputStream( OutputStream out )
- {
- this.out = out;
- }
-
- public void setErrorStream( OutputStream err )
- {
- this.err = err;
- }
-
- public void setExitCallback( ExitCallback callback )
- {
- this.callback = callback;
- }
-
- public void start( Environment env )
- throws IOException
- {
- File tmpFile = File.createTempFile( "wagon", "test-sh" );
- tmpFile.deleteOnExit();
- int exitValue = 0;
- SystemLogOutputStream systemOut = new SystemLogOutputStream( 1 );
- SystemLogOutputStream errOut = new SystemLogOutputStream( 1 );
- CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
- CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
- try
- {
-
- Executor exec = new DefaultExecutor();
- exec.setStreamHandler( new PumpStreamHandler( systemOut, errOut ) );
- // hackhish defaut commandline tools not support ; or && so write a file with the script
- // and "/bin/sh -e " + tmpFile.getPath();
-
- FileUtils.fileWrite( tmpFile, commandLine );
-
- Commandline cl = new Commandline();
- cl.setExecutable( "/bin/sh" );
- //cl.createArg().setValue( "-e" );
- //cl.createArg().setValue( tmpFile.getPath() );
- cl.createArg().setFile( tmpFile );
-
- exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
- System.out.println( "exit value " + exitValue );
- /*
- if ( exitValue == 0 )
- {
- out.write( stdout.getOutput().getBytes() );
- out.write( '\n' );
- out.flush();
-
- }
- else
- {
- out.write( stderr.getOutput().getBytes() );
- out.write( '\n' );
- out.flush();
-
- }*/
-
- }
- catch ( Exception e )
- {
- exitValue = ERROR;
- e.printStackTrace();
- }
- finally
- {
- deleteQuietly( tmpFile );
- if ( exitValue != 0 )
- {
- err.write( stderr.getOutput().getBytes() );
- err.write( '\n' );
- err.flush();
- callback.onExit( exitValue, stderr.getOutput() );
- }
- else
- {
- out.write( stdout.getOutput().getBytes() );
- out.write( '\n' );
- out.flush();
- callback.onExit( exitValue, stdout.getOutput() );
- }
-
- }
- /*
- out.write( exitValue );
- out.write( '\n' );
-
- */
- out.flush();
- }
-
- public void destroy()
- {
-
- }
-
- private void deleteQuietly( File f )
- {
-
- try
- {
- f.delete();
- }
- catch ( Exception e )
- {
- // ignore
- }
- }
- }
-
-
- static class TestSshFile
- extends NativeSshFile
- {
- public TestSshFile( String fileName, File file, String userName )
- {
-
- super( FileUtils.normalize( fileName ), file, userName );
- }
- }
-
- static class SystemLogOutputStream
- extends LogOutputStream
- {
-
- private List<String> lines = new ArrayList<String>();
-
- private SystemLogOutputStream( int level )
- {
- super( level );
- }
-
- protected void processLine( String line, int level )
- {
- lines.add( line );
- }
- }
-
- // file lastModified not return so don't support
- protected boolean supportsGetIfNewer()
- {
- return false;
- }
-
- public void testWagon() throws Exception
- {
- super.testWagon();
- }
}