View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.command.checkout;
20  
21  import java.nio.file.Path;
22  import java.security.GeneralSecurityException;
23  
24  import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
25  import org.apache.maven.scm.provider.git.GitScmTestUtils;
26  import org.apache.maven.scm.provider.git.GitSshServer;
27  import org.apache.maven.scm.repository.ScmRepository;
28  import org.apache.maven.scm.tck.command.checkout.CheckOutCommandTckTest;
29  import org.junit.Assume;
30  import org.junit.Rule;
31  import org.junit.Test;
32  import org.junit.rules.TemporaryFolder;
33  
34  public abstract class GitSshCheckOutCommandTckTest extends CheckOutCommandTckTest {
35      protected final GitSshServer gitSshServer;
36  
37      @Rule
38      public TemporaryFolder tmpDirectory = new TemporaryFolder();
39  
40      protected GitSshCheckOutCommandTckTest() throws GeneralSecurityException {
41          gitSshServer = new GitSshServer();
42      }
43  
44      protected abstract String getScmProvider();
45  
46      /**
47       * {@inheritDoc}
48       */
49      public String getScmUrl() throws Exception {
50          return "scm:" + getScmProvider() + ":ssh://localhost:" + gitSshServer.getPort() + "/repository";
51      }
52  
53      protected void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
54          ScmProviderRepositoryWithHost providerRepository =
55                  ScmProviderRepositoryWithHost.class.cast(repository.getProviderRepository());
56          // store as file
57          Path privateKeyFile = tmpDirectory.newFile().toPath();
58          gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
59          providerRepository.setPrivateKey(privateKeyFile.toString());
60          providerRepository.setPassphrase(passphrase); // may be null
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      public void initRepo() throws Exception {
67          GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
68          gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
69  
70          // as checkout also already happens in setup() make sure to configure credentials here as well
71          configureCredentials(getScmRepository(), null);
72      }
73  
74      @Override
75      public void removeRepo() throws Exception {
76          gitSshServer.stop();
77          super.removeRepo();
78      }
79  
80      @Override
81      @Test
82      public void testCheckOutCommandTest() throws Exception {
83          configureCredentials(getScmRepository(), null);
84          super.testCheckOutCommandTest();
85      }
86  
87      @Test
88      public void testCheckOutCommandWithPassphraseTest() throws Exception {
89          // TODO: currently no easy way to pass passphrase in gitexe
90          Assume.assumeTrue(
91                  "Ignore test with passphrase for provider " + getScmProvider(), "jgit".equals(getScmProvider()));
92          configureCredentials(getScmRepository(), "mySecret");
93          super.testCheckOutCommandTest();
94      }
95  }