Skip to content

Commit

Permalink
Add schema packages tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Nov 8, 2023
1 parent aa0424a commit a98ba50
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 23 deletions.
32 changes: 9 additions & 23 deletions schema/credential/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
65 changes: 65 additions & 0 deletions schema/credential/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions schema/csr/csr_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}
40 changes: 40 additions & 0 deletions schema/doxm/doxm_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
111 changes: 111 additions & 0 deletions schema/plgdtime/plgdtime_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading

0 comments on commit a98ba50

Please sign in to comment.