diff --git a/pgpainless-core/src/main/java/org/pgpainless/algorithm/AlgorithmSuite.java b/pgpainless-core/src/main/java/org/pgpainless/algorithm/AlgorithmSuite.java index e5f8e2c4..17445df5 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/algorithm/AlgorithmSuite.java +++ b/pgpainless-core/src/main/java/org/pgpainless/algorithm/AlgorithmSuite.java @@ -22,6 +22,10 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +/** + * The {@link AlgorithmSuite} class is consulted when new OpenPGP keys are being generated to set + * preferred algorithms on the key. + */ public class AlgorithmSuite { private static AlgorithmSuite defaultAlgorithmSuite = new AlgorithmSuite( @@ -63,7 +67,7 @@ public class AlgorithmSuite { public int[] getSymmetricKeyAlgorithmIds() { int[] array = new int[symmetricKeyAlgorithms.size()]; - List list = new ArrayList<>(symmetricKeyAlgorithms); + List list = new ArrayList<>(getSymmetricKeyAlgorithms()); for (int i = 0; i < array.length; i++) { array[i] = list.get(i).getAlgorithmId(); } @@ -80,7 +84,7 @@ public class AlgorithmSuite { public int[] getHashAlgorithmIds() { int[] array = new int[hashAlgorithms.size()]; - List list = new ArrayList<>(hashAlgorithms); + List list = new ArrayList<>(getHashAlgorithms()); for (int i = 0; i < array.length; i++) { array[i] = list.get(i).getAlgorithmId(); } @@ -97,7 +101,7 @@ public class AlgorithmSuite { public int[] getCompressionAlgorithmIds() { int[] array = new int[compressionAlgorithms.size()]; - List list = new ArrayList<>(compressionAlgorithms); + List list = new ArrayList<>(getCompressionAlgorithms()); for (int i = 0; i < array.length; i++) { array[i] = list.get(i).getAlgorithmId(); } diff --git a/pgpainless-core/src/test/java/org/pgpainless/algorithm/AlgorithmSuiteTest.java b/pgpainless-core/src/test/java/org/pgpainless/algorithm/AlgorithmSuiteTest.java new file mode 100644 index 00000000..89ac5a2a --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/algorithm/AlgorithmSuiteTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2021 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.pgpainless.algorithm; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class AlgorithmSuiteTest { + + private AlgorithmSuite suite; + + @BeforeEach + public void resetEmptyAlgorithmSuite() { + suite = new AlgorithmSuite( + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList() + ); + } + + @Test + public void setSymmetricAlgorithmsTest() { + List algorithmList = Arrays.asList( + SymmetricKeyAlgorithm.AES_128, SymmetricKeyAlgorithm.AES_192, SymmetricKeyAlgorithm.AES_256 + ); + + suite.setSymmetricKeyAlgorithms(algorithmList); + + assertEquals(algorithmList, new ArrayList<>(suite.getSymmetricKeyAlgorithms())); + } + + @Test + public void setHashAlgorithmsTest() { + List algorithmList = Arrays.asList( + HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512 + ); + + suite.setHashAlgorithms(algorithmList); + + assertEquals(algorithmList, new ArrayList<>(suite.getHashAlgorithms())); + } + + @Test + public void setCompressionAlgorithmsTest() { + List algorithmList = Arrays.asList( + CompressionAlgorithm.ZLIB, CompressionAlgorithm.ZIP, CompressionAlgorithm.BZIP2 + ); + + suite.setCompressionAlgorithms(algorithmList); + + assertEquals(algorithmList, new ArrayList<>(suite.getCompressionAlgorithms())); + } +}