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.branch;
020
021import java.io.File;
022import java.nio.file.Path;
023import java.security.GeneralSecurityException;
024
025import org.apache.maven.scm.command.checkout.CheckOutScmResult;
026import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
027import org.apache.maven.scm.provider.git.GitScmTestUtils;
028import org.apache.maven.scm.provider.git.GitSshServer;
029import org.apache.maven.scm.repository.ScmRepository;
030import org.apache.maven.scm.tck.command.branch.BranchCommandTckTest;
031import org.junit.Assume;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.rules.TemporaryFolder;
035
036/**
037 *
038 */
039public abstract class GitSshBranchCommandTckTest extends BranchCommandTckTest {
040    protected final GitSshServer gitSshServer;
041
042    @Rule
043    public TemporaryFolder tmpDirectory = new TemporaryFolder();
044
045    protected GitSshBranchCommandTckTest() throws GeneralSecurityException {
046        gitSshServer = new GitSshServer();
047    }
048
049    protected abstract String getScmProvider();
050
051    /** {@inheritDoc} */
052    public String getScmUrl() throws Exception {
053        return "scm:" + getScmProvider() + ":ssh://localhost:" + gitSshServer.getPort() + "/repository";
054    }
055
056    public void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
057        ScmProviderRepositoryWithHost providerRepository =
058                ScmProviderRepositoryWithHost.class.cast(repository.getProviderRepository());
059        // store as file
060        Path privateKeyFile = tmpDirectory.newFile().toPath();
061        gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
062        providerRepository.setPrivateKey(privateKeyFile.toString());
063        providerRepository.setPassphrase(passphrase); // may be null
064    }
065
066    /** {@inheritDoc} */
067    public void initRepo() throws Exception {
068        GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
069        gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
070
071        // as checkout also already happens in setup() make sure to configure credentials here as well
072        configureCredentials(getScmRepository(), null);
073    }
074
075    @Override
076    public void removeRepo() throws Exception {
077        gitSshServer.stop();
078        super.removeRepo();
079    }
080
081    @Override
082    protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
083        try {
084            return super.checkOut(workingDirectory, repository);
085        } finally {
086            GitScmTestUtils.setDefaultGitConfig(workingDirectory);
087        }
088    }
089
090    @Test
091    public void testBranchCommandTestWithPush() throws Exception {
092        configureCredentials(getScmRepository(), null);
093        getScmRepository().getProviderRepository().setPushChanges(true);
094        super.testBranchCommandTest();
095    }
096
097    @Test
098    public void testBranchCommandWithPassphraseAndPushTest() throws Exception {
099        // TODO: currently no easy way to pass passphrase in gitexe
100        Assume.assumeTrue(
101                "Ignore test with passphrase for provider " + getScmProvider(), "jgit".equals(getScmProvider()));
102        configureCredentials(getScmRepository(), "mySecret");
103        getScmRepository().getProviderRepository().setPushChanges(true);
104        super.testBranchCommandTest();
105    }
106}