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  /**
35   *
36   */
37  public abstract class GitSshCheckOutCommandTckTest extends CheckOutCommandTckTest {
38      protected final GitSshServer gitSshServer;
39  
40      @Rule
41      public TemporaryFolder tmpDirectory = new TemporaryFolder();
42  
43      protected GitSshCheckOutCommandTckTest() throws GeneralSecurityException {
44          gitSshServer = new GitSshServer();
45      }
46  
47      protected abstract String getScmProvider();
48  
49      /** {@inheritDoc} */
50      public String getScmUrl() throws Exception {
51          return "scm:" + getScmProvider() + ":ssh://localhost:" + gitSshServer.getPort() + "/repository";
52      }
53  
54      protected void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
55          ScmProviderRepositoryWithHost providerRepository =
56                  ScmProviderRepositoryWithHost.class.cast(repository.getProviderRepository());
57          // store as file
58          Path privateKeyFile = tmpDirectory.newFile().toPath();
59          gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
60          providerRepository.setPrivateKey(privateKeyFile.toString());
61          providerRepository.setPassphrase(passphrase); // may be null
62      }
63  
64      /** {@inheritDoc} */
65      public void initRepo() throws Exception {
66          GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
67          gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
68  
69          // as checkout also already happens in setup() make sure to configure credentials here as well
70          configureCredentials(getScmRepository(), null);
71      }
72  
73      @Override
74      public void removeRepo() throws Exception {
75          gitSshServer.stop();
76          super.removeRepo();
77      }
78  
79      @Override
80      @Test
81      public void testCheckOutCommandTest() throws Exception {
82          configureCredentials(getScmRepository(), null);
83          super.testCheckOutCommandTest();
84      }
85  
86      @Test
87      public void testCheckOutCommandWithPassphraseTest() throws Exception {
88          // TODO: currently no easy way to pass passphrase in gitexe
89          Assume.assumeTrue(
90                  "Ignore test with passphrase for provider " + getScmProvider(), "jgit".equals(getScmProvider()));
91          configureCredentials(getScmRepository(), "mySecret");
92          super.testCheckOutCommandTest();
93      }
94  }