View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.ssl.maven.plugin;
6   
7   import java.io.File;
8   import java.nio.charset.StandardCharsets;
9   import java.nio.file.Files;
10  import java.util.Properties;
11  import java.util.UUID;
12  import org.apache.commons.io.FileUtils;
13  import org.hamcrest.MatcherAssert;
14  import org.hamcrest.Matchers;
15  import org.junit.Assume;
16  import org.junit.Rule;
17  import org.junit.Test;
18  import org.junit.rules.TemporaryFolder;
19  
20  /**
21   * Test case for {@link Cacerts}.
22   *
23   * @since 0.5
24   */
25  public final class CacertsTest {
26  
27      /**
28       * Temporary folder.
29       * @checkstyle VisibilityModifier (3 lines)
30       */
31      @Rule
32      public transient TemporaryFolder temp = new TemporaryFolder();
33  
34      /**
35       * Cacerts can generate a keystore.
36       * @throws Exception If something is wrong
37       */
38      @Test
39      @SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
40      public void importsCertificatesFromKeystore() throws Exception {
41          final File keystore = this.temp.newFile("keystore.jks");
42          keystore.delete();
43          final File truststore = this.temp.newFile("cacerts.jks");
44          truststore.delete();
45          new Keystore("some-password").activate(keystore);
46          final Cacerts cacerts = new Cacerts(truststore);
47          cacerts.imprt();
48          MatcherAssert.assertThat(
49              new Keytool(truststore, "changeit").list(),
50              Matchers.containsString("localhost")
51          );
52          final Properties props = new Properties();
53          cacerts.populate(props);
54          MatcherAssert.assertThat(
55              truststore.getAbsolutePath(),
56              Matchers.equalTo(props.getProperty(Cacerts.TRUST))
57          );
58          MatcherAssert.assertThat(
59              Cacerts.STD_PWD,
60              Matchers.equalTo(props.getProperty(Cacerts.TRUST_PWD))
61          );
62      }
63  
64      /**
65       * Cacerts copies content from symlink correctly when cacerts is a symlink.
66       * @throws Exception If something is wrong
67       */
68      @Test
69      public void copiesFromSymlinkCorrectly() throws Exception {
70          final File original = this.temp.newFile(
71              UUID.randomUUID().toString()
72          );
73          FileUtils.writeStringToFile(
74              original,
75              UUID.randomUUID().toString().repeat(100),
76              StandardCharsets.UTF_8
77          );
78          final File link = new File(
79              this.temp.getRoot(),
80              UUID.randomUUID().toString()
81          );
82          Files.createSymbolicLink(link.toPath(), original.toPath());
83          Assume.assumeTrue(
84              "Symlinks not supported on this system",
85              Files.isSymbolicLink(link.toPath())
86          );
87          final File destination = this.temp.newFile(
88              UUID.randomUUID().toString()
89          );
90          destination.delete();
91          FileUtils.copyFile(
92              link.toPath().toRealPath().toFile(),
93              destination
94          );
95          MatcherAssert.assertThat(
96              "Copied file size must match original file size when copying from symlink",
97              destination.length(),
98              Matchers.equalTo(original.length())
99          );
100     }
101 }