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    protected int getTestRepositoryPort()
069    {
070        return sshServerEmbedded.getPort();
071    }
072
073    public String getTestRepositoryUrl()
074    {
075        return TestData.getTestRepositoryUrl( sshServerEmbedded.getPort() );
076    }
077
078    protected AuthenticationInfo getAuthInfo()
079    {
080        AuthenticationInfo authInfo = super.getAuthInfo();
081        // user : guest/guest123 -  passphrase : toto01
082        authInfo.setUserName( "guest" );
083        //authInfo.setPassword( TestData.getUserPassword() );
084        authInfo.setPrivateKey( new File( "src/test/ssh-keys/id_rsa" ).getPath() );
085
086        return authInfo;
087    }
088
089    protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
090    {
091        return new File( repository.getBasedir(), resource.getName() ).lastModified();
092    }
093
094    public void testConnect()
095        throws Exception
096    {
097        getWagon().connect( new Repository( "foo", getTestRepositoryUrl() ), getAuthInfo() );
098        assertTrue( true );
099    }
100
101
102    @Override
103    protected boolean supportsGetIfNewer()
104    {
105        return false;
106    }
107
108    public void testWithSpaces()
109        throws Exception
110    {
111        String dir = "foo   test";
112        File spaceDirectory = new File( TestData.getRepoPath(), dir );
113        if ( spaceDirectory.exists() )
114        {
115            FileUtils.deleteDirectory( spaceDirectory );
116        }
117        spaceDirectory.mkdirs();
118
119        String subDir = "foo bar";
120        File sub = new File( spaceDirectory, subDir );
121        if ( sub.exists() )
122        {
123            FileUtils.deleteDirectory( sub );
124        }
125        sub.mkdirs();
126
127        File dummy = new File( "src/test/resources/dummy.txt" );
128        FileUtils.copyFileToDirectory( dummy, sub );
129
130        String url = getTestRepositoryUrl() + "/" + dir;
131        Repository repo = new Repository( "foo", url );
132        Wagon wagon = getWagon();
133        wagon.connect( repo, getAuthInfo() );
134        List<String> files = wagon.getFileList( subDir );
135        assertNotNull( files );
136        assertEquals( 1, files.size() );
137        assertTrue( files.contains( "dummy.txt" ) );
138
139        wagon.put( new File( "src/test/resources/dummy.txt" ), subDir + "/newdummy.txt" );
140
141        files = wagon.getFileList( subDir );
142        assertNotNull( files );
143        assertEquals( 2, files.size() );
144        assertTrue( files.contains( "dummy.txt" ) );
145        assertTrue( files.contains( "newdummy.txt" ) );
146
147        File sourceWithSpace = new File( "target/directory with spaces" );
148        if ( sourceWithSpace.exists() )
149        {
150            FileUtils.deleteDirectory( sourceWithSpace );
151        }
152        File resources = new File( "src/test/resources" );
153
154        FileUtils.copyDirectory( resources, sourceWithSpace );
155
156        wagon.putDirectory( sourceWithSpace, "target with spaces" );
157
158        files = wagon.getFileList( "target with spaces" );
159
160        assertNotNull( files );
161        assertTrue( files.contains( "dummy.txt" ) );
162        assertFalse( files.contains( "newdummy.txt" ) );
163        assertTrue( files.contains( "log4j.xml" ) );
164    }
165
166}