diff options
author | juerg <juerg@google.com> | 2023-03-28 06:32:16 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-28 06:34:16 -0700 |
commit | 0764a056e718097212b40152d5e8d30be8ca0c46 (patch) | |
tree | 2e82cbeb341399031b232768bef8f9119113a97c /tools | |
parent | 57c858c945eee79e78621a53ab323e61f2fe4041 (diff) | |
download | tink-0764a056e718097212b40152d5e8d30be8ca0c46.tar.gz |
Remove unused CompareKeysets.java.
PiperOrigin-RevId: 520011525
Diffstat (limited to 'tools')
4 files changed, 0 insertions, 438 deletions
diff --git a/tools/testing/BUILD.bazel b/tools/testing/BUILD.bazel index 5ba5f41f3..236926e51 100644 --- a/tools/testing/BUILD.bazel +++ b/tools/testing/BUILD.bazel @@ -22,16 +22,6 @@ java_library( ], ) -java_library( - name = "compare_keysets", - testonly = 1, - srcs = ["java/com/google/crypto/tink/testing/CompareKeysets.java"], - deps = [ - "@tink_java//proto:tink_java_proto", - "@tink_java//src/main/java/com/google/crypto/tink:privileged_registry", - ], -) - java_binary( name = "aead_cli_java", testonly = 1, diff --git a/tools/testing/java/com/google/crypto/tink/testing/CompareKeysets.java b/tools/testing/java/com/google/crypto/tink/testing/CompareKeysets.java deleted file mode 100644 index c19c8deff..000000000 --- a/tools/testing/java/com/google/crypto/tink/testing/CompareKeysets.java +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2019 Google LLC -// -// 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 com.google.crypto.tink.testing; - -import com.google.crypto.tink.PrivilegedRegistry; -import com.google.crypto.tink.proto.Keyset; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Provides a function {@link compareKeysets} which can compare two keysets. - */ -final class CompareKeysets { - // Compares two keys of a keyset. We parse the KeyData in each keyset in order to ensure we do not - // depend strongly on the serialization order of the tags; note that keysets may be serialized - // in different languages, and assuming that the fields are ordered in the same way when generated - // in each language seems relatively strong. - private static boolean equalKeys(Keyset.Key key1, Keyset.Key key2) throws Exception { - if (!key1.getStatus().equals(key2.getStatus())) { - return false; - } - if (key1.getKeyId() != key2.getKeyId()) { - return false; - } - if (!key1.getOutputPrefixType().equals(key2.getOutputPrefixType())) { - return false; - } - if (!key1.getKeyData().getKeyMaterialType().equals(key2.getKeyData().getKeyMaterialType())) { - return false; - } - if (!PrivilegedRegistry.parseKeyData(key1.getKeyData()) - .equals(PrivilegedRegistry.parseKeyData(key2.getKeyData()))) { - return false; - } - return true; - } - - /** - * Finds a key in {@code keyList} for which {@link equalKeys} returns true, removes it, and - * returns true. If no such key exists, returns false. - */ - private static boolean findAndRemove(Keyset.Key key, List<Keyset.Key> keyList) throws Exception { - for (Keyset.Key key2 : keyList) { - if (equalKeys(key, key2)) { - keyList.remove(key2); - return true; - } - } - return false; - } - - private static void compareKeysetLists(List<Keyset.Key> keyList1, List<Keyset.Key> keyList2) - throws Exception { - for (Keyset.Key key1 : keyList1) { - if (!findAndRemove(key1, keyList2)) { - throw new IllegalArgumentException("Key " + key1 + " not found in second keyset."); - } - } - } - - /** - * Collects all keys with a fixed key id in a {@link List}, returning the result in a map from - * the key-id to this list. - */ - private static Map<Integer, List<Keyset.Key>> getKeyDataMap(Keyset keyset) { - Map<Integer, List<Keyset.Key>> result = new HashMap<>(); - for (int i = 0; i < keyset.getKeyCount(); ++i) { - Keyset.Key key = keyset.getKey(i); - if (!result.containsKey(key.getKeyId())) { - result.put(key.getKeyId(), new ArrayList<Keyset.Key>()); - } - result.get(key.getKeyId()).add(key); - } - return result; - } - - /** - * Compares two keysets, throws some exception if the keyset are different. - * - * <p>If the keysets are different, this is guaranteed to throw an exception. If they are the same - * there are several possibilities why this will still throw an exception: - * - * <ul> - * <li>There is no key manager registered for one of the Keys used in the keyset - * <li> - * </ul> - */ - public static void compareKeysets(Keyset keyset1, Keyset keyset2) throws Exception { - if (keyset1.getPrimaryKeyId() != keyset2.getPrimaryKeyId()) { - throw new IllegalArgumentException( - "Given keysets contain different key ids. \n\nKeyset 1: " - + keyset1 - + "\n\nKeyset2: " - + keyset2); - } - if (keyset1.getKeyCount() != keyset2.getKeyCount()) { - throw new IllegalArgumentException( - "Given keysets contain different number of keys. \n\nKeyset 1: " - + keyset1 - + "\n\nKeyset2: " - + keyset2); - } - - // The order of the keys in the keyset is considered irrelevant. - Map<Integer, List<Keyset.Key>> keyset1Map = getKeyDataMap(keyset1); - Map<Integer, List<Keyset.Key>> keyset2Map = getKeyDataMap(keyset2); - for (Map.Entry<Integer, List<Keyset.Key>> idToList : keyset1Map.entrySet()) { - if (!keyset2Map.containsKey(idToList.getKey())) { - throw new IllegalArgumentException( - "Keysets differ; the second one contains no key with id " - + idToList.getKey() - + ", but the first one does. \n\nKeyset 1: " - + keyset1 - + "\n\nKeyset 2:" - + keyset2); - } - compareKeysetLists(keyset2Map.get(idToList.getKey()), idToList.getValue()); - } - } - - private CompareKeysets() { - } -} diff --git a/tools/testing/javatests/com/google/crypto/tink/testing/BUILD.bazel b/tools/testing/javatests/com/google/crypto/tink/testing/BUILD.bazel deleted file mode 100644 index 3ee9d5188..000000000 --- a/tools/testing/javatests/com/google/crypto/tink/testing/BUILD.bazel +++ /dev/null @@ -1,17 +0,0 @@ -licenses(["notice"]) - -package(default_visibility = ["//:__subpackages__"]) - -java_test( - name = "CompareKeysetsTest", - size = "small", - srcs = ["CompareKeysetsTest.java"], - deps = [ - "//testing:compare_keysets", - "@maven//:junit_junit", - "@tink_java//proto:tink_java_proto", - "@tink_java//src/main/java/com/google/crypto/tink/aead:aes_gcm_key_manager", - "@tink_java//src/main/java/com/google/crypto/tink/subtle:hex", - "@tink_java//src/main/java/com/google/crypto/tink/testing:test_util", - ], -) diff --git a/tools/testing/javatests/com/google/crypto/tink/testing/CompareKeysetsTest.java b/tools/testing/javatests/com/google/crypto/tink/testing/CompareKeysetsTest.java deleted file mode 100644 index 28f025918..000000000 --- a/tools/testing/javatests/com/google/crypto/tink/testing/CompareKeysetsTest.java +++ /dev/null @@ -1,272 +0,0 @@ -// Copyright 2019 Google LLC -// -// 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 com.google.crypto.tink.testing; - -import static org.junit.Assert.fail; - -import com.google.crypto.tink.aead.AesGcmKeyManager; -import com.google.crypto.tink.proto.KeyData; -import com.google.crypto.tink.proto.KeyData.KeyMaterialType; -import com.google.crypto.tink.proto.KeyStatusType; -import com.google.crypto.tink.proto.Keyset; -import com.google.crypto.tink.proto.OutputPrefixType; -import com.google.crypto.tink.subtle.Hex; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public final class CompareKeysetsTest { - @BeforeClass - public static void registerAesGcm() throws Exception { - AesGcmKeyManager.register(true); - } - - private static final byte[] KEY_0 = Hex.decode("000102030405060708090a0b0c0d0e0f"); - private static final byte[] KEY_1 = Hex.decode("100102030405060708090a0b0c0d0e0f"); - private static final byte[] KEY_2 = Hex.decode("200102030405060708090a0b0c0d0e0f"); - private static final byte[] KEY_3 = Hex.decode("300102030405060708090a0b0c0d0e0f"); - private static final byte[] KEY_4 = Hex.decode("400102030405060708090a0b0c0d0e0f"); - private static final byte[] KEY_5 = Hex.decode("500102030405060708090a0b0c0d0e0f"); - private static final byte[] KEY_6 = Hex.decode("600102030405060708090a0b0c0d0e0f"); - - private static Keyset.Key aesGcmKey( - byte[] keyValue, int keyId, KeyStatusType status, OutputPrefixType prefixType) - throws Exception { - return TestUtil.createKey(TestUtil.createAesGcmKeyData(keyValue), keyId, status, prefixType); - } - - @Test - public void testCompareKeysets_emptyKeysets_equal() throws Exception { - CompareKeysets.compareKeysets(Keyset.getDefaultInstance(), - Keyset.getDefaultInstance()); - } - - @Test - public void testCompareKeysets_singleKey_equal() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - CompareKeysets.compareKeysets(keyset1, keyset2); - } - - @Test - public void testCompareKeysets_twoKeys_equal() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - CompareKeysets.compareKeysets(keyset1, keyset2); - } - - @Test - public void testCompareKeysets_twoKeysDifferentOrder_equal() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - CompareKeysets.compareKeysets(keyset1, keyset2); - } - - @Test - public void testCompareKeysets_twoKeysDifferentPrimary_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(18) - .build(); - try { - CompareKeysets.compareKeysets(keyset1, keyset2); - fail(); - } catch (Exception e) { - // expected. - } - } - - @Test - public void testCompareKeysets_singleKeyDifferentStatus_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.DISABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - try { - CompareKeysets.compareKeysets(keyset1, keyset2); - fail(); - } catch (Exception e) { - // expected. - } - } - - @Test - public void testCompareKeysets_singleKeyDifferentOutputPrefix_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.RAW)) - .setPrimaryKeyId(17) - .build(); - try { - CompareKeysets.compareKeysets(keyset1, keyset2); - fail(); - } catch (Exception e) { - // expected. - } - } - - @Test - public void testCompareKeysets_singleKeyDifferentKeyMaterial_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset.Key key = keyset1.getKey(0); - Keyset keyset2 = - Keyset.newBuilder() - .addKey( - Keyset.Key.newBuilder(key) - .setKeyData( - KeyData.newBuilder(key.getKeyData()) - .setKeyMaterialType(KeyMaterialType.UNKNOWN_KEYMATERIAL))) - .setPrimaryKeyId(17) - .build(); - try { - CompareKeysets.compareKeysets(keyset1, keyset2); - fail(); - } catch (Exception e) { - // expected. - } - } - - @Test - public void testCompareKeysets_differentKeyIdButRawOutputPrefix_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 18, KeyStatusType.ENABLED, OutputPrefixType.RAW)) - .setPrimaryKeyId(18) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.RAW)) - .setPrimaryKeyId(17) - .build(); - try { - CompareKeysets.compareKeysets(keyset1, keyset2); - fail(); - } catch (Exception e) { - // expected. - } - } - - @Test - public void testCompareKeysets_sameKeysSameIdsDifferentOrder_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_2, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_3, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_4, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_5, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_6, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_3, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_2, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_5, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_4, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_6, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - CompareKeysets.compareKeysets(keyset1, keyset2); - } - - @Test - public void testCompareKeysets_differentKeysSameIdsSimlarOrder_throws() throws Exception { - Keyset keyset1 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_2, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_3, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_4, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_5, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - Keyset keyset2 = - Keyset.newBuilder() - .addKey(aesGcmKey(KEY_0, 17, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_1, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_2, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_6, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) // != KEY_3 - .addKey(aesGcmKey(KEY_4, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .addKey(aesGcmKey(KEY_5, 18, KeyStatusType.ENABLED, OutputPrefixType.TINK)) - .setPrimaryKeyId(17) - .build(); - try { - CompareKeysets.compareKeysets(keyset1, keyset2); - fail(); - } catch (Exception e) { - // expected. - } - } -} |