From a98ba507c70c1397cf817579c246e74408701298 Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Wed, 8 Nov 2023 14:31:37 +0100 Subject: [PATCH] Add schema packages tests --- schema/credential/credential.go | 32 ++----- schema/credential/credential_test.go | 65 +++++++++++++++ schema/csr/csr_test.go | 65 +++++++++++++++ schema/doxm/doxm_test.go | 40 +++++++++ schema/plgdtime/plgdtime_test.go | 111 +++++++++++++++++++++++++ schema/softwareupdate/swupdate_test.go | 68 +++++++++++++++ 6 files changed, 358 insertions(+), 23 deletions(-) create mode 100644 schema/csr/csr_test.go create mode 100644 schema/doxm/doxm_test.go create mode 100644 schema/plgdtime/plgdtime_test.go create mode 100644 schema/softwareupdate/swupdate_test.go diff --git a/schema/credential/credential.go b/schema/credential/credential.go index d38cec04..40b09985 100644 --- a/schema/credential/credential.go +++ b/schema/credential/credential.go @@ -122,11 +122,11 @@ type CredentialOptionalData struct { IsRevoked bool `json:"revstat"` } -func (c CredentialOptionalData) Data() []byte { - if c.DataInternal == nil { +func toByte(v interface{}) []byte { + if v == nil { return nil } - switch v := c.DataInternal.(type) { + switch v := v.(type) { case string: return []byte(v) case []byte: @@ -135,6 +135,10 @@ func (c CredentialOptionalData) Data() []byte { return nil } +func (c CredentialOptionalData) Data() []byte { + return toByte(c.DataInternal) +} + const ( dataEncoding_JWT string = "oic.sec.encoding.jwt" dataEncoding_CWT string = "oic.sec.encoding.cwt" @@ -162,16 +166,7 @@ type CredentialPrivateData struct { } func (c CredentialPrivateData) Data() []byte { - if c.DataInternal == nil { - return nil - } - switch v := c.DataInternal.(type) { - case string: - return []byte(v) - case []byte: - return v - } - return nil + return toByte(c.DataInternal) } type CredentialPrivateDataEncoding string @@ -191,16 +186,7 @@ type CredentialPublicData struct { } func (c CredentialPublicData) Data() []byte { - if c.DataInternal == nil { - return nil - } - switch v := c.DataInternal.(type) { - case string: - return []byte(v) - case []byte: - return v - } - return nil + return toByte(c.DataInternal) } type CredentialPublicDataEncoding string diff --git a/schema/credential/credential_test.go b/schema/credential/credential_test.go index 725d0f98..8f2558fc 100644 --- a/schema/credential/credential_test.go +++ b/schema/credential/credential_test.go @@ -23,6 +23,71 @@ import ( "github.com/stretchr/testify/require" ) +func testCredentialData(t *testing.T, checkData func(data interface{}, expected []byte)) { + type args struct { + data interface{} + } + tests := []struct { + name string + args args + want []byte + }{ + { + name: "Nil", + args: args{data: nil}, + want: nil, + }, + { + name: "String", + args: args{data: "test"}, + want: []byte("test"), + }, + { + name: "Bytes", + args: args{data: []byte("test")}, + want: []byte("test"), + }, + { + name: "Invalid", + args: args{data: 42}, + want: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + checkData(tt.args.data, tt.want) + }) + } +} + +func TestCredentialOptionalData(t *testing.T) { + testCredentialData(t, func(data interface{}, expected []byte) { + c := credential.CredentialOptionalData{ + DataInternal: data, + } + require.Equal(t, expected, c.Data()) + }) +} + +func TestCredentialPrivateData(t *testing.T) { + testCredentialData(t, func(data interface{}, expected []byte) { + c := credential.CredentialPrivateData{ + DataInternal: data, + } + require.Equal(t, expected, c.Data()) + }) +} + +func TestCredentialPublicData(t *testing.T) { + testCredentialData(t, func(data interface{}, expected []byte) { + c := credential.CredentialPublicData{ + DataInternal: data, + } + require.Equal(t, expected, c.Data()) + }) +} + func TestCredentialTypeString(t *testing.T) { tests := []struct { name string diff --git a/schema/csr/csr_test.go b/schema/csr/csr_test.go new file mode 100644 index 00000000..6594379e --- /dev/null +++ b/schema/csr/csr_test.go @@ -0,0 +1,65 @@ +// ************************************************************************ +// Copyright (C) 2022 plgd.dev, s.r.o. +// +// 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 csr_test + +import ( + "testing" + + "github.com/plgd-dev/device/v2/schema/csr" + "github.com/stretchr/testify/require" +) + +func TestCSR(t *testing.T) { + type args struct { + request interface{} + } + tests := []struct { + name string + args args + want []byte + }{ + { + name: "Nil", + args: args{request: nil}, + want: nil, + }, + { + name: "String", + args: args{request: "test"}, + want: []byte("test"), + }, + { + name: "Bytes", + args: args{request: []byte("test")}, + want: []byte("test"), + }, + { + name: "Invalid", + args: args{request: 42}, + want: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := csr.CertificateSigningRequestResponse{ + CertificateSigningRequest: tt.args.request, + } + require.Equal(t, tt.want, c.CSR()) + }) + } +} diff --git a/schema/doxm/doxm_test.go b/schema/doxm/doxm_test.go new file mode 100644 index 00000000..4d65b7ba --- /dev/null +++ b/schema/doxm/doxm_test.go @@ -0,0 +1,40 @@ +// ************************************************************************ +// Copyright (C) 2023 plgd.dev, s.r.o. +// +// 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 doxm_test + +import ( + "testing" + + "github.com/plgd-dev/device/v2/schema/doxm" + "github.com/stretchr/testify/require" +) + +func TestOwnerTransferMethodToString(t *testing.T) { + methodToStr := map[doxm.OwnerTransferMethod]string{ + doxm.JustWorks: "JustWorks", + doxm.SharedPin: "SharedPin", + doxm.ManufacturerCertificate: "ManufacturerCertificate", + doxm.Self: "Self", + } + + for method, str := range methodToStr { + require.Equal(t, str, method.String()) + } + + invalid := doxm.OwnerTransferMethod(42) + require.Equal(t, "unknown 42", invalid.String()) +} diff --git a/schema/plgdtime/plgdtime_test.go b/schema/plgdtime/plgdtime_test.go new file mode 100644 index 00000000..19dbdffb --- /dev/null +++ b/schema/plgdtime/plgdtime_test.go @@ -0,0 +1,111 @@ +// ************************************************************************ +// Copyright (C) 2023 plgd.dev, s.r.o. +// +// 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 plgdtime_test + +import ( + "testing" + "time" + + "github.com/plgd-dev/device/v2/schema/plgdtime" + "github.com/stretchr/testify/require" +) + +func TestPlgdTimeGetTime(t *testing.T) { + type args struct { + time string + } + tests := []struct { + name string + args args + wantTime time.Time + wantErr bool + }{ + { + name: "Empty string", + args: args{time: ""}, + wantErr: true, + }, + { + name: "Invalid string", + args: args{time: "This is not a time string"}, + wantErr: true, + }, + { + name: "Valid string", + args: args{time: "2023-01-15T15:04:05.000000000Z"}, + wantTime: time.Date(2023, time.January, 15, 15, 4, 5, 0, time.UTC), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pt := plgdtime.PlgdTime{ + Time: tt.args.time, + } + actualTime, err := pt.GetTime() + if tt.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + require.Equal(t, tt.wantTime, actualTime) + }) + } +} + +func TestPlgdTimeGetLastSyncedTime(t *testing.T) { + type args struct { + time string + } + tests := []struct { + name string + args args + wantTime time.Time + wantErr bool + }{ + { + name: "Invalid string", + args: args{time: "This is not a time string"}, + wantErr: true, + }, + { + name: "Empty string", + args: args{time: ""}, + wantTime: time.Time{}, + }, + { + name: "Valid string", + args: args{time: "2023-01-15T15:04:05.000000000Z"}, + wantTime: time.Date(2023, time.January, 15, 15, 4, 5, 0, time.UTC), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pt := plgdtime.PlgdTime{ + LastSyncedTime: tt.args.time, + } + lastSyncedTime, err := pt.GetLastSyncedTime() + if tt.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + require.Equal(t, tt.wantTime, lastSyncedTime) + }) + } +} diff --git a/schema/softwareupdate/swupdate_test.go b/schema/softwareupdate/swupdate_test.go new file mode 100644 index 00000000..5fbe89fb --- /dev/null +++ b/schema/softwareupdate/swupdate_test.go @@ -0,0 +1,68 @@ +// ************************************************************************ +// Copyright (C) 2023 plgd.dev, s.r.o. +// +// 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 softwareupdate_test + +import ( + "testing" + + "github.com/plgd-dev/device/v2/schema/softwareupdate" + "github.com/stretchr/testify/require" +) + +func TestSoftwareUpdateGetUpdateResultNilReceiver(t *testing.T) { + var sw *softwareupdate.SoftwareUpdate + require.Equal(t, -1, sw.GetUpdateResult()) +} + +func TestSoftwareUpdateGetUpdateResult(t *testing.T) { + type args struct { + result *int + } + tests := []struct { + name string + args args + want int + }{ + { + name: "Nil", + args: args{result: nil}, + want: -1, + }, + { + name: "Zero", + args: args{result: new(int)}, + want: 0, + }, + { + name: "Valid", + args: args{result: func() *int { + i := 42 + return &i + }()}, + want: 42, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sw := &softwareupdate.SoftwareUpdate{ + UpdateResult: tt.args.result, + } + require.Equal(t, tt.want, sw.GetUpdateResult()) + }) + } +}