001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.command.checkout;
020
021import java.nio.file.Path;
022import java.security.GeneralSecurityException;
023
024import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
025import org.apache.maven.scm.provider.git.GitScmTestUtils;
026import org.apache.maven.scm.provider.git.GitSshServer;
027import org.apache.maven.scm.repository.ScmRepository;
028import org.apache.maven.scm.tck.command.checkout.CheckOutCommandTckTest;
029import org.junit.Assume;
030import org.junit.Rule;
031import org.junit.Test;
032import org.junit.rules.TemporaryFolder;
033
034/**
035 *
036 */
037public abstract class GitSshCheckOutCommandTckTest extends CheckOutCommandTckTest {
038    protected final GitSshServer gitSshServer;
039
040    @Rule
041    public TemporaryFolder tmpDirectory = new TemporaryFolder();
042
043    protected GitSshCheckOutCommandTckTest() throws GeneralSecurityException {
044        gitSshServer = new GitSshServer();
045    }
046
047    protected abstract String getScmProvider();
048
049    /** {@inheritDoc} */
050    public String getScmUrl() throws Exception {
051        return "scm:" + getScmProvider() + ":ssh://localhost:" + gitSshServer.getPort() + "/repository";
052    }
053
054    protected void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
055        ScmProviderRepositoryWithHost providerRepository =
056                ScmProviderRepositoryWithHost.class.cast(repository.getProviderRepository());
057        // store as file
058        Path privateKeyFile = tmpDirectory.newFile().toPath();
059        gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
060        providerRepository.setPrivateKey(privateKeyFile.toString());
061        providerRepository.setPassphrase(passphrase); // may be null
062    }
063
064    /** {@inheritDoc} */
065    public void initRepo() throws Exception {
066        GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
067        gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
068
069        // as checkout also already happens in setup() make sure to configure credentials here as well
070        configureCredentials(getScmRepository(), null);
071    }
072
073    @Override
074    public void removeRepo() throws Exception {
075        gitSshServer.stop();
076        super.removeRepo();
077    }
078
079    @Override
080    @Test
081    public void testCheckOutCommandTest() throws Exception {
082        configureCredentials(getScmRepository(), null);
083        super.testCheckOutCommandTest();
084    }
085
086    @Test
087    public void testCheckOutCommandWithPassphraseTest() throws Exception {
088        // TODO: currently no easy way to pass passphrase in gitexe
089        Assume.assumeTrue(
090                "Ignore test with passphrase for provider " + getScmProvider(), "jgit".equals(getScmProvider()));
091        configureCredentials(getScmRepository(), "mySecret");
092        super.testCheckOutCommandTest();
093    }
094}