From 84a80f856726e9baa64c178cb3d9e9057cf2b2be Mon Sep 17 00:00:00 2001 From: Emil Lundberg Date: Tue, 28 Sep 2021 17:36:45 +0200 Subject: [PATCH] Add tests for to/fromJson methods in PublicKeyCredentialCreationOptions and AssertionRequest --- .../com/yubico/webauthn/AssertionRequest.java | 8 +++ .../PublicKeyCredentialCreationOptions.java | 8 +++ .../com/yubico/webauthn/data/JsonIoSpec.scala | 62 +++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/webauthn-server-core/src/main/java/com/yubico/webauthn/AssertionRequest.java b/webauthn-server-core/src/main/java/com/yubico/webauthn/AssertionRequest.java index 1b89ef223..3dc0e0f60 100644 --- a/webauthn-server-core/src/main/java/com/yubico/webauthn/AssertionRequest.java +++ b/webauthn-server-core/src/main/java/com/yubico/webauthn/AssertionRequest.java @@ -101,6 +101,14 @@ public String toCredentialsGetJson() throws JsonProcessingException { return publicKeyCredentialRequestOptions.toCredentialsGetJson(); } + public String toJson() throws JsonProcessingException { + return ""; + } + + public static AssertionRequest fromJson(String json) throws JsonProcessingException { + return null; + } + public static AssertionRequestBuilder.MandatoryStages builder() { return new AssertionRequestBuilder.MandatoryStages(); } diff --git a/webauthn-server-core/src/main/java/com/yubico/webauthn/data/PublicKeyCredentialCreationOptions.java b/webauthn-server-core/src/main/java/com/yubico/webauthn/data/PublicKeyCredentialCreationOptions.java index 98a3d0dfa..284b00b88 100644 --- a/webauthn-server-core/src/main/java/com/yubico/webauthn/data/PublicKeyCredentialCreationOptions.java +++ b/webauthn-server-core/src/main/java/com/yubico/webauthn/data/PublicKeyCredentialCreationOptions.java @@ -169,6 +169,14 @@ public String toCredentialsCreateJson() throws JsonProcessingException { return json.writeValueAsString(result); } + public String toJson() throws JsonProcessingException { + return ""; + } + + public static PublicKeyCredentialCreationOptions fromJson(String json) throws JsonProcessingException { + return null; + } + public Optional getTimeout() { return Optional.ofNullable(timeout); } diff --git a/webauthn-server-core/src/test/scala/com/yubico/webauthn/data/JsonIoSpec.scala b/webauthn-server-core/src/test/scala/com/yubico/webauthn/data/JsonIoSpec.scala index 1ffa37b93..d17ffa725 100644 --- a/webauthn-server-core/src/test/scala/com/yubico/webauthn/data/JsonIoSpec.scala +++ b/webauthn-server-core/src/test/scala/com/yubico/webauthn/data/JsonIoSpec.scala @@ -384,6 +384,38 @@ class JsonIoSpec ) should equal(pkcco) } } + + describe("has a toJson() method and a fromJson(String) factory method") { + it("which behave like a Jackson ObjectMapper.") { + forAll { req: PublicKeyCredentialCreationOptions => + println(req) + val json1 = req.toJson + val json2 = JacksonCodecs.json.writeValueAsString(req) + json1 should equal(json2) + + val parsed1 = PublicKeyCredentialCreationOptions.fromJson(json1) + val parsed2 = JacksonCodecs.json.readValue( + json2, + classOf[PublicKeyCredentialCreationOptions], + ) + parsed1 should equal(parsed2) + } + } + + it("which are stable over multiple serialization round-trips.") { + forAll { req: PublicKeyCredentialCreationOptions => + println(req) + val encoded = req.toJson + val decoded = PublicKeyCredentialCreationOptions.fromJson(encoded) + val reencoded = decoded.toJson + val redecoded = PublicKeyCredentialCreationOptions.fromJson(reencoded) + + decoded should equal(req) + redecoded should equal(req) + encoded should equal(reencoded) + } + } + } } describe("The class PublicKeyCredentialRequestOptions") { @@ -413,6 +445,36 @@ class JsonIoSpec ) should equal(req.getPublicKeyCredentialRequestOptions) } } + + describe("has a toJson() method and a fromJson(String) factory method") { + it("which behave like a Jackson ObjectMapper.") { + forAll { req: AssertionRequest => + println(req) + val json1 = req.toJson + val json2 = JacksonCodecs.json.writeValueAsString(req) + + val parsed1 = AssertionRequest.fromJson(json1) + val parsed2 = + JacksonCodecs.json.readValue(json2, classOf[AssertionRequest]) + json1 should equal(json2) + parsed1 should equal(parsed2) + } + } + + it("which are stable over multiple serialization round-trips.") { + forAll { req: AssertionRequest => + println(req) + val encoded = req.toJson + val decoded = AssertionRequest.fromJson(encoded) + val reencoded = decoded.toJson + val redecoded = AssertionRequest.fromJson(reencoded) + + decoded should equal(req) + redecoded should equal(req) + encoded should equal(reencoded) + } + } + } } }