KeysetHandle { public static final KeysetHandle readNoSecret(KeysetReader reader) throws GeneralSecurityException, IOException { try { Keyset keyset = reader.read(); assertNoSecretKeyMaterial(keyset); return KeysetHandle.fromKeyset(keyset); } catch (@SuppressWarnings("UnusedException") InvalidProtocolBufferException e) { throw new GeneralSecurityException("invalid keyset"); } } private  KeysetHandle(Keyset keyset); KeysetInfo getKeysetInfo(); @Deprecated static final KeysetHandle generateNew(com.google.crypto.tink.proto.KeyTemplate keyTemplate); static final KeysetHandle generateNew(KeyTemplate keyTemplate); static final KeysetHandle read(KeysetReader reader, Aead masterKey); static final KeysetHandle readNoSecret(KeysetReader reader); static final KeysetHandle readNoSecret(final byte[] serialized); void write(KeysetWriter keysetWriter, Aead masterKey); void writeNoSecret(KeysetWriter writer); KeysetHandle getPublicKeysetHandle(); @Override String toString(); static void assertEnoughKeyMaterial(Keyset keyset); static void assertEnoughEncryptedKeyMaterial(EncryptedKeyset keyset); P getPrimitive(Class<P> targetClassObject); P getPrimitive(KeyManager<P> customKeyManager, Class<P> targetClassObject);  }
@Test public void readNoSecretFailWithTypeSymmetric() throws Exception { String keyValue = "01234567890123456"; Keyset keyset = TestUtil.createKeyset( TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK)); try { KeysetHandle unused = KeysetHandle.readNoSecret(keyset.toByteArray()); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset contains key material of type"); assertExceptionContains(e, " type.googleapis.com/google.crypto.tink.HmacKey"); } try { KeysetHandle unused = KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(keyset.toByteArray())); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset contains key material"); } }
@Test public void readNoSecretFailWithEmptyKeyset() throws Exception { try { KeysetHandle unused = KeysetHandle.readNoSecret(new byte[0]); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "empty keyset"); } }
@Test public void readNoSecretFailWithInvalidKeyset() throws Exception { byte[] proto = new byte[] {0x00, 0x01, 0x02}; try { KeysetHandle unused = KeysetHandle.readNoSecret(proto); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "invalid"); } }