Skip to content

Commit fb58b05

Browse files
authored
Change serialization tests for ED-DSA so that they don't hard-code the class path. (#1352)
* Add tests for serialization for Ed25519 private and public key. * Fix formatting. * Add missing imports. * Change serialization tests so that they don't hard-code the class path. * Fix format. * Fix import error. * Fix format.
1 parent 0d59c62 commit fb58b05

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

common/src/test/java/org/conscrypt/EdDsaTest.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.ObjectInputStream;
3333
import java.io.ObjectOutputStream;
3434
import java.nio.ByteBuffer;
35+
import java.nio.charset.StandardCharsets;
3536
import java.security.InvalidKeyException;
3637
import java.security.KeyFactory;
3738
import java.security.KeyPair;
@@ -325,14 +326,15 @@ public void serializePrivateKey_isEqualToTestVector() throws Exception {
325326
oos.writeObject(privateKey);
326327
}
327328

328-
String expectedHexEncoding = "aced000573720024"
329-
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
330-
+ "4f70656e53736c4564447361507269766174654b6579" // hex("OpenSslEdDsaPrivateKey")
329+
String classNameHex = TestUtils.encodeHex(
330+
privateKey.getClass().getName().getBytes(StandardCharsets.UTF_8));
331+
String expectedHexEncoding = "aced000573720024" + classNameHex
331332
+ "d479f95a133abadc" // serialVersionUID
332333
+ "0200015b000f"
333334
+ "707269766174654b65794279746573" // hex("privateKeyBytes")
334335
+ "7400025b427870757200025b42acf317f8060854e0020000787000000020"
335-
+ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"; // private key
336+
+ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"; // private
337+
// key
336338
assertEquals(expectedHexEncoding, TestUtils.encodeHex(baos.toByteArray()));
337339
}
338340

@@ -353,22 +355,26 @@ public void serializePublicKey_isEqualToTestVector() throws Exception {
353355
oos.writeObject(publicKey);
354356
}
355357

356-
String expectedHexEncoding = "aced000573720023"
357-
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
358-
+ "4f70656e53736c45644473615075626c69634b6579" // hex("OpenSslEdDsaPublicKey")
358+
String classNameHex = TestUtils.encodeHex(
359+
publicKey.getClass().getName().getBytes(StandardCharsets.UTF_8));
360+
String expectedHexEncoding = "aced000573720023" + classNameHex
359361
+ "064c7113d078e42d" // serialVersionUID
360362
+ "0200015b000e"
361363
+ "7075626c69634b65794279746573" // hex("publicKeyBytes")
362364
+ "7400025b427870757200025b42acf317f8060854e0020000787000000020"
363-
+ "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; // public key
365+
+ "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; // public
366+
// key
364367
assertEquals(expectedHexEncoding, TestUtils.encodeHex(baos.toByteArray()));
365368
}
366369

367370
@Test
368371
public void deserializeInvalidPrivateKey_fails() throws Exception {
369-
String invalidPrivateKeySerialized = "aced000573720024"
370-
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
371-
+ "4f70656e53736c4564447361507269766174654b6579" // hex("OpenSslEdDsaPrivateKey")
372+
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("Ed25519", conscryptProvider);
373+
KeyPair keyPair = keyGen.generateKeyPair();
374+
String classNameHex = TestUtils.encodeHex(
375+
keyPair.getPrivate().getClass().getName().getBytes(StandardCharsets.UTF_8));
376+
377+
String invalidPrivateKeySerialized = "aced000573720024" + classNameHex
372378
+ "d479f95a133abadc" // serialVersionUID
373379
+ "0200015b000f"
374380
+ "707269766174654b65794279746573" // hex("privateKeyBytes")
@@ -389,9 +395,12 @@ public void deserializeInvalidPrivateKey_fails() throws Exception {
389395

390396
@Test
391397
public void deserializeInvalidPublicKey_fails() throws Exception {
392-
String invalidPublicKeySerialized = "aced000573720023"
393-
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
394-
+ "4f70656e53736c45644473615075626c69634b6579" // hex("OpenSslEdDsaPublicKey")
398+
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("Ed25519", conscryptProvider);
399+
KeyPair keyPair = keyGen.generateKeyPair();
400+
String classNameHex = TestUtils.encodeHex(
401+
keyPair.getPublic().getClass().getName().getBytes(StandardCharsets.UTF_8));
402+
403+
String invalidPublicKeySerialized = "aced000573720023" + classNameHex
395404
+ "064c7113d078e42d" // serialVersionUID
396405
+ "0200015b000e"
397406
+ "7075626c69634b65794279746573" // hex("publicKeyBytes")

0 commit comments

Comments
 (0)