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