From 918e4ffaa6c4336466268508431640e26ff5098d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 17 Aug 2020 15:21:43 +0200 Subject: [PATCH] Add a sanity check to extraJSONMember MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... to ensure we are testing what we want to be testing. Signed-off-by: Miloslav Trmač --- signature/policy_config_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/signature/policy_config_test.go b/signature/policy_config_test.go index 93c72ada48..990230e0aa 100644 --- a/signature/policy_config_test.go +++ b/signature/policy_config_test.go @@ -250,7 +250,7 @@ func testInvalidJSONInput(t *testing.T, dest json.Unmarshaler) { assert.Error(t, err) } -// addExtraJSONMember adds adds an additional member "$name": $extra, +// addExtraJSONMember adds an additional member "$name": $extra, // possibly with a duplicate name, to encoded. // Errors, if any, are reported through t. func addExtraJSONMember(t *testing.T, encoded []byte, name string, extra interface{}) []byte { @@ -260,7 +260,13 @@ func addExtraJSONMember(t *testing.T, encoded []byte, name string, extra interfa require.True(t, bytes.HasSuffix(encoded, []byte("}"))) preservedLen := len(encoded) - 1 - return bytes.Join([][]byte{encoded[:preservedLen], []byte(`,"`), []byte(name), []byte(`":`), extraJSON, []byte("}")}, nil) + res := bytes.Join([][]byte{encoded[:preservedLen], []byte(`,"`), []byte(name), []byte(`":`), extraJSON, []byte("}")}, nil) + // Verify that the result is valid JSON, as a sanity check that we are actually triggering + // the “duplicate member” case in the caller. + var raw map[string]interface{} + err = json.Unmarshal(res, &raw) + require.NoError(t, err) + return res } // policyJSONUnmarshallerTests formalizes the repeated structure of the JSON unmasrhaller