001package org.apache.maven.wagon.providers.ssh;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.wagon.StreamingWagonTestCase;
023import org.apache.maven.wagon.Wagon;
024import org.apache.maven.wagon.authentication.AuthenticationInfo;
025import org.apache.maven.wagon.repository.Repository;
026import org.apache.maven.wagon.resource.Resource;
027import org.codehaus.plexus.util.FileUtils;
028
029import java.io.File;
030import java.util.Arrays;
031import java.util.List;
032
033/**
034 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
035 *
036 */
037public abstract class AbstractEmbeddedScpWagonWithKeyTest
038    extends StreamingWagonTestCase
039{
040
041    SshServerEmbedded sshServerEmbedded;
042
043
044    @Override
045    protected void setUp()
046        throws Exception
047    {
048        super.setUp();
049
050        String sshKeyResource = "ssh-keys/id_rsa.pub";
051
052        sshServerEmbedded = new SshServerEmbedded( getProtocol(), Arrays.asList( sshKeyResource ), true );
053
054        sshServerEmbedded.start();
055        System.out.println( "sshd on port " + sshServerEmbedded.getPort() );
056    }
057
058    @Override
059    protected void tearDownWagonTestingFixtures()
060        throws Exception
061    {
062
063        sshServerEmbedded.stop();
064    }
065
066    protected abstract String getProtocol();
067
068    @Override
069    protected int getTestRepositoryPort()
070    {
071        return sshServerEmbedded.getPort();
072    }
073
074
075    public String getTestRepositoryUrl()
076    {
077        return TestData.getTestRepositoryUrl( sshServerEmbedded.getPort() );
078    }
079
080    protected AuthenticationInfo getAuthInfo()
081    {
082        AuthenticationInfo authInfo = super.getAuthInfo();
083        // user : guest/guest123 -  passphrase : toto01
084        authInfo.setUserName( "guest" );
085        //authInfo.setPassword( TestData.getUserPassword() );
086        authInfo.setPrivateKey( new File( "src/test/ssh-keys/id_rsa" ).getPath() );
087
088        return authInfo;
089    }
090
091    protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
092    {
093        return new File( repository.getBasedir(), resource.getName() ).lastModified();
094    }
095
096    public void testConnect()
097        throws Exception
098    {
099        getWagon().connect( new Repository( "foo", getTestRepositoryUrl() ), getAuthInfo() );
100        assertTrue( true );
101    }
102
103
104    @Override
105    protected boolean supportsGetIfNewer()
106    {
107        return false;
108    }
109
110    public void testWithSpaces()
111        throws Exception
112    {
113        String dir = "foo   test";
114        File spaceDirectory = new File( TestData.getRepoPath(), dir );
115        if ( spaceDirectory.exists() )
116        {
117            FileUtils.deleteDirectory( spaceDirectory );
118        }
119        spaceDirectory.mkdirs();
120
121        String subDir = "foo bar";
122        File sub = new File( spaceDirectory, subDir );
123        if ( sub.exists() )
124        {
125            FileUtils.deleteDirectory( sub );
126        }
127        sub.mkdirs();
128
129        File dummy = new File( "src/test/resources/dummy.txt" );
130        FileUtils.copyFileToDirectory( dummy, sub );
131
132        String url = getTestRepositoryUrl() + "/" + dir;
133        Repository repo = new Repository( "foo", url );
134        Wagon wagon = getWagon();
135        wagon.connect( repo, getAuthInfo() );
136        List<String> files = wagon.getFileList( subDir );
137        assertNotNull( files );
138        assertEquals( 1, files.size() );
139        assertTrue( files.contains( "dummy.txt" ) );
140
141        wagon.put( new File( "src/test/resources/dummy.txt" ), subDir + "/newdummy.txt" );
142
143        files = wagon.getFileList( subDir );
144        assertNotNull( files );
145        assertEquals( 2, files.size() );
146        assertTrue( files.contains( "dummy.txt" ) );
147        assertTrue( files.contains( "newdummy.txt" ) );
148
149        File sourceWithSpace = new File( "target/directory with spaces" );
150        if ( sourceWithSpace.exists() )
151        {
152            FileUtils.deleteDirectory( sourceWithSpace );
153        }
154        File resources = new File( "src/test/resources" );
155
156        FileUtils.copyDirectory( resources, sourceWithSpace );
157
158        wagon.putDirectory( sourceWithSpace, "target with spaces" );
159
160        files = wagon.getFileList( "target with spaces" );
161
162        assertNotNull( files );
163        assertTrue( files.contains( "dummy.txt" ) );
164        assertFalse( files.contains( "newdummy.txt" ) );
165        assertTrue( files.contains( "log4j.xml" ) );
166    }
167
168}