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