View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.io.File;
24  
25  import org.apache.commons.vfs2.provider.sftp.IdentityInfo;
26  import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Check FileSystemOptions.
31   */
32  public class FileSystemOptionsTest {
33  
34      public static class JUnitConfigBuilder extends FileSystemConfigBuilder {
35          private abstract static class JUnitFS implements FileSystem {
36          }
37  
38          private static final JUnitConfigBuilder BUILDER = new JUnitConfigBuilder();
39  
40          public static JUnitConfigBuilder getInstance() {
41              return BUILDER;
42          }
43  
44          @Override
45          protected Class<? extends FileSystem> getConfigClass() {
46              return JUnitFS.class;
47          }
48  
49          public void setId(final FileSystemOptions opts, final String id) {
50              setParam(opts, "id", id);
51          }
52  
53          public void setNames(final FileSystemOptions opts, final String[] names) {
54              setParam(opts, "names", names);
55          }
56      }
57  
58      private static void assertSftpOptionsEquals(final File privKey, final File pubKey, final byte[] passphrase) {
59          final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
60  
61          final FileSystemOptions expected = new FileSystemOptions();
62          final IdentityInfo info1 = new IdentityInfo(privKey, pubKey, passphrase);
63          builder.setIdentityProvider(expected, info1);
64  
65          final FileSystemOptions actual = new FileSystemOptions();
66          final IdentityInfo info2 = new IdentityInfo(privKey, pubKey, passphrase);
67          builder.setIdentityProvider(actual, info2);
68  
69          assertEquals(0, expected.compareTo(actual));
70          assertEquals(expected.hashCode(), actual.hashCode());
71      }
72  
73      private static void assertSftpOptionsNotEquals(final File privKey1, final File pubKey1, final byte[] passphrase1,
74          final File privKey2, final File pubKey2, final byte[] passphrase2) {
75          final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
76  
77          final FileSystemOptions expected = new FileSystemOptions();
78          final IdentityInfo info1 = new IdentityInfo(privKey1, pubKey1, passphrase1);
79          builder.setIdentityProvider(expected, info1);
80  
81          final FileSystemOptions actual = new FileSystemOptions();
82          final IdentityInfo info2 = new IdentityInfo(privKey2, pubKey2, passphrase2);
83          builder.setIdentityProvider(actual, info2);
84  
85          assertNotEquals(0, expected.compareTo(actual));
86          assertNotEquals(expected.hashCode(), actual.hashCode());
87      }
88  
89      @Test
90      public void testClone() {
91          final FileSystemOptions fileSystemOptions = new FileSystemOptions();
92          assertEquals(fileSystemOptions.getClass(), fileSystemOptions.clone().getClass());
93          assertEquals(0, ((FileSystemOptions) fileSystemOptions.clone()).size());
94          fileSystemOptions.setOption(FileSystem.class, "key1", "value1");
95          assertEquals(1, ((FileSystemOptions) fileSystemOptions.clone()).size());
96          final FileSystemOptions clone = (FileSystemOptions) fileSystemOptions.clone();
97          assertEquals("value1", clone.getOption(FileSystem.class, "key1"));
98          fileSystemOptions.setOption(FileSystem.class, "key2", "value2");
99          assertNull(clone.getOption(FileSystem.class, "key2"));
100     }
101 
102     @Test
103     public void testEqualsHashCodeAndCompareTo() {
104         final JUnitConfigBuilder builder = JUnitConfigBuilder.getInstance();
105         final FileSystemOptions expected = new FileSystemOptions();
106         builder.setId(expected, "Test");
107 
108         final FileSystemOptions actual = new FileSystemOptions();
109         builder.setId(actual, "Test");
110 
111         assertEquals(expected, actual);
112         assertEquals(0, actual.compareTo(expected));
113         assertEquals(expected.hashCode(), actual.hashCode());
114 
115         builder.setNames(expected, new String[] {"A", "B", "C"});
116 
117         assertNotEquals(expected, actual);
118         assertEquals(-1, actual.compareTo(expected));
119         assertNotEquals(expected.hashCode(), actual.hashCode());
120 
121         builder.setNames(actual, new String[] {"A", "B", "C"});
122 
123         assertEquals(expected, actual);
124         assertEquals(0, actual.compareTo(expected));
125         assertEquals(expected.hashCode(), actual.hashCode());
126     }
127 
128     @Test
129     public void testEqualsHashCodeAndCompareToWithSftpIdentityProviderMatch() {
130         for (int mask = 0; mask < 8; mask++) {
131             assertSftpOptionsEquals(
132                 (mask & 1) == 1 ? new File("/tmp/test.priv") : null,
133                 (mask & 2) == 2 ? new File("/tmp/test.pub") : null,
134                 (mask & 4) == 4 ? new byte[] {1, 2, 3} : null
135             );
136         }
137     }
138 
139     @Test
140     public void testEqualsHashCodeAndCompareToWithSftpIdentityProviderMismatch() {
141         final String pubKey1 = "/tmp/test.pub";
142         final String pubKey2 = "/tmp/test1.pub";
143 
144         final String privKey1 = "/tmp/test.priv";
145         final String privKey2 = "/tmp/test1.priv";
146 
147         assertSftpOptionsNotEquals(
148             new File(privKey1), new File(pubKey1), new byte[] {1, 2, 3},
149             new File(privKey2), new File(pubKey1), new byte[] {1, 2, 3}
150         );
151 
152         assertSftpOptionsNotEquals(
153             new File(privKey1), new File(pubKey1), new byte[] {1, 2, 3},
154             new File(privKey1), new File(pubKey2), new byte[] {1, 2, 3}
155         );
156 
157         assertSftpOptionsNotEquals(
158             new File(privKey1), new File(pubKey1), new byte[] {1, 2, 3},
159             new File(privKey1), new File(pubKey1), new byte[] {1, 2, 4}
160         );
161     }
162 }