From 0ff9d3ca746ae309a98a26631085b0a3e339a1de Mon Sep 17 00:00:00 2001 From: Junjie Gao Date: Mon, 8 Jan 2024 16:18:37 +0800 Subject: [PATCH] test: add unit test Signed-off-by: Junjie Gao --- internal/crypto/cms/encoding/ber/ber.go | 2 +- internal/crypto/cms/encoding/ber/ber_test.go | 32 +++++++++++ internal/crypto/cms/encoding/ber/common.go | 6 ++ .../crypto/cms/encoding/ber/constructed.go | 4 +- .../cms/encoding/ber/constructed_test.go | 56 +++++++++++++++++++ internal/crypto/cms/encoding/ber/primitive.go | 4 +- .../crypto/cms/encoding/ber/primitive_test.go | 44 +++++++++++++++ 7 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 internal/crypto/cms/encoding/ber/constructed_test.go create mode 100644 internal/crypto/cms/encoding/ber/primitive_test.go diff --git a/internal/crypto/cms/encoding/ber/ber.go b/internal/crypto/cms/encoding/ber/ber.go index 99824c34..bfe5d716 100644 --- a/internal/crypto/cms/encoding/ber/ber.go +++ b/internal/crypto/cms/encoding/ber/ber.go @@ -32,7 +32,7 @@ import ( // value is the interface for an ASN.1 value node. type value interface { // EncodeMetadata encodes the identifier and length in DER to the buffer. - EncodeMetadata(*bytes.Buffer) error + EncodeMetadata(Writer) error // EncodedLen returns the length in bytes of the data when encoding in DER. EncodedLen() int diff --git a/internal/crypto/cms/encoding/ber/ber_test.go b/internal/crypto/cms/encoding/ber/ber_test.go index 62bff2b5..0004c367 100644 --- a/internal/crypto/cms/encoding/ber/ber_test.go +++ b/internal/crypto/cms/encoding/ber/ber_test.go @@ -300,6 +300,20 @@ func TestConvertToDER(t *testing.T) { der: []byte{}, expectError: true, }, + { + name: "long form length greather than subsequent octets length ", + ber: []byte{ + // Primitive value + // identifier + 0x1f, 0xa0, 0x20, + // length + 0x81, 0x09, + // content + 0x01, + }, + der: []byte{}, + expectError: true, + }, } for _, tt := range testData { @@ -317,3 +331,21 @@ func TestConvertToDER(t *testing.T) { } } } + +func TestDecodeIdentifier(t *testing.T) { + t.Run("identifier is empty", func(t *testing.T) { + _, _, err := decodeIdentifier([]byte{}) + if err == nil { + t.Errorf("decodeIdentifier() error = nil, but expect error") + } + }) +} + +func TestDecodeLength(t *testing.T) { + t.Run("length is empty", func(t *testing.T) { + _, _, err := decodeLength([]byte{}) + if err == nil { + t.Errorf("decodeLength() error = nil, but expect error") + } + }) +} diff --git a/internal/crypto/cms/encoding/ber/common.go b/internal/crypto/cms/encoding/ber/common.go index 8d2ecd87..60b5cc5d 100644 --- a/internal/crypto/cms/encoding/ber/common.go +++ b/internal/crypto/cms/encoding/ber/common.go @@ -17,6 +17,12 @@ import ( "io" ) +// Writer is the interface that wraps the basic Write and WriteByte methods. +type Writer interface { + io.Writer + io.ByteWriter +} + // encodeLength encodes length octets in DER. // Reference: ISO/IEC 8825-1: 10.1 func encodeLength(w io.ByteWriter, length int) error { diff --git a/internal/crypto/cms/encoding/ber/constructed.go b/internal/crypto/cms/encoding/ber/constructed.go index be8126a6..cd9ef012 100644 --- a/internal/crypto/cms/encoding/ber/constructed.go +++ b/internal/crypto/cms/encoding/ber/constructed.go @@ -13,8 +13,6 @@ package ber -import "bytes" - // constructed represents a value in constructed. type constructed struct { identifier []byte @@ -25,7 +23,7 @@ type constructed struct { // EncodeMetadata encodes the identifier and length octets of constructed // to the value writer in DER. -func (v *constructed) EncodeMetadata(w *bytes.Buffer) error { +func (v *constructed) EncodeMetadata(w Writer) error { _, err := w.Write(v.identifier) if err != nil { return err diff --git a/internal/crypto/cms/encoding/ber/constructed_test.go b/internal/crypto/cms/encoding/ber/constructed_test.go new file mode 100644 index 00000000..c98fa5ec --- /dev/null +++ b/internal/crypto/cms/encoding/ber/constructed_test.go @@ -0,0 +1,56 @@ +// Copyright The Notary Project Authors. +// 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 ber + +import ( + "errors" + "testing" +) + +type errorWriter struct{} + +func (ew *errorWriter) Write(p []byte) (n int, err error) { + return 0, errors.New("write error") +} + +func (ew *errorWriter) WriteByte(c byte) error { + return errors.New("write error") +} + +func TestConstructedEncodeMetadata(t *testing.T) { + tests := []struct { + name string + v constructed + wantError bool + }{ + { + name: "Error case", + v: constructed{ + identifier: []byte{0x30}, + length: 5, + }, + wantError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := &errorWriter{} + err := tt.v.EncodeMetadata(w) + if (err != nil) != tt.wantError { + t.Errorf("EncodeMetadata() error = %v, wantError %v", err, tt.wantError) + } + }) + } +} diff --git a/internal/crypto/cms/encoding/ber/primitive.go b/internal/crypto/cms/encoding/ber/primitive.go index de746487..1043d863 100644 --- a/internal/crypto/cms/encoding/ber/primitive.go +++ b/internal/crypto/cms/encoding/ber/primitive.go @@ -13,8 +13,6 @@ package ber -import "bytes" - // primitive represents a value in primitive encoding. type primitive struct { identifier []byte @@ -23,7 +21,7 @@ type primitive struct { // EncodeMetadata encodes the identifier and length octets of primitive to // the value writer in DER. -func (v *primitive) EncodeMetadata(w *bytes.Buffer) error { +func (v *primitive) EncodeMetadata(w Writer) error { _, err := w.Write(v.identifier) if err != nil { return err diff --git a/internal/crypto/cms/encoding/ber/primitive_test.go b/internal/crypto/cms/encoding/ber/primitive_test.go new file mode 100644 index 00000000..5cd55265 --- /dev/null +++ b/internal/crypto/cms/encoding/ber/primitive_test.go @@ -0,0 +1,44 @@ +// Copyright The Notary Project Authors. +// 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 ber + +import ( + "testing" +) + +func TestPrimitiveEncodeMetadata(t *testing.T) { + tests := []struct { + name string + v primitive + wantError bool + }{ + { + name: "Error case", + v: primitive{ + identifier: []byte{0x30}, + }, + wantError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := &errorWriter{} + err := tt.v.EncodeMetadata(w) + if (err != nil) != tt.wantError { + t.Errorf("EncodeMetadata() error = %v, wantError %v", err, tt.wantError) + } + }) + } +}