2023-05-25 12:43:44 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-02 03:19:23 -04:00
|
|
|
package config
|
2023-05-25 12:43:44 -04:00
|
|
|
|
|
|
|
import (
|
2023-08-09 09:11:14 -04:00
|
|
|
"encoding/json"
|
2023-05-25 12:43:44 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVersionMarshalYAML(t *testing.T) {
|
2023-08-09 09:11:14 -04:00
|
|
|
tests := map[string]struct {
|
2023-05-25 12:43:44 -04:00
|
|
|
sut AttestationVersion
|
|
|
|
want string
|
|
|
|
}{
|
2023-08-09 09:11:14 -04:00
|
|
|
"isLatest resolves to latest": {
|
2023-05-25 12:43:44 -04:00
|
|
|
sut: AttestationVersion{
|
2023-06-14 08:17:52 -04:00
|
|
|
Value: 1,
|
|
|
|
WantLatest: true,
|
2023-05-25 12:43:44 -04:00
|
|
|
},
|
|
|
|
want: "latest\n",
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"value 5 resolves to 5": {
|
2023-05-25 12:43:44 -04:00
|
|
|
sut: AttestationVersion{
|
2023-06-14 08:17:52 -04:00
|
|
|
Value: 5,
|
|
|
|
WantLatest: false,
|
2023-05-25 12:43:44 -04:00
|
|
|
},
|
|
|
|
want: "5\n",
|
|
|
|
},
|
|
|
|
}
|
2023-08-09 09:11:14 -04:00
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
bt, err := yaml.Marshal(tc.sut)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(tc.want, string(bt))
|
2023-05-25 12:43:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-09 05:41:04 -04:00
|
|
|
|
|
|
|
func TestVersionUnmarshalYAML(t *testing.T) {
|
2023-08-09 09:11:14 -04:00
|
|
|
tests := map[string]struct {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut string
|
|
|
|
want AttestationVersion
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2023-08-09 09:11:14 -04:00
|
|
|
"latest resolves to isLatest": {
|
|
|
|
sut: "latest",
|
2023-08-09 05:41:04 -04:00
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 0,
|
|
|
|
WantLatest: true,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"1 resolves to value 1": {
|
|
|
|
sut: "1",
|
2023-08-09 05:41:04 -04:00
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 1,
|
|
|
|
WantLatest: false,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"max uint8+1 errors": {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut: "256",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"-1 errors": {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut: "-1",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"2.6 errors": {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut: "2.6",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"2.0 errors": {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut: "2.0",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"hex format is invalid": {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut: "0x10",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"octal format is invalid": {
|
2023-08-09 05:41:04 -04:00
|
|
|
sut: "010",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-09 09:11:14 -04:00
|
|
|
"0 resolves to value 0": {
|
|
|
|
sut: "0",
|
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 0,
|
|
|
|
WantLatest: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"00 errors": {
|
|
|
|
sut: "00",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
var sut AttestationVersion
|
|
|
|
err := yaml.Unmarshal([]byte(tc.sut), &sut)
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(tc.want, sut)
|
|
|
|
})
|
2023-08-09 05:41:04 -04:00
|
|
|
}
|
2023-08-09 09:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionUnmarshalJSON(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
sut string
|
|
|
|
want AttestationVersion
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"latest resolves to isLatest": {
|
|
|
|
sut: `"latest"`,
|
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 0,
|
|
|
|
WantLatest: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"1 resolves to value 1": {
|
|
|
|
sut: "1",
|
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 1,
|
|
|
|
WantLatest: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"quoted number resolves to value": {
|
|
|
|
sut: `"1"`,
|
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 1,
|
|
|
|
WantLatest: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"quoted float errors": {
|
|
|
|
sut: `"1.0"`,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"max uint8+1 errors": {
|
|
|
|
sut: "256",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"-1 errors": {
|
|
|
|
sut: "-1",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"2.6 errors": {
|
|
|
|
sut: "2.6",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"2.0 errors": {
|
|
|
|
sut: "2.0",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"hex format is invalid": {
|
|
|
|
sut: "0x10",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"octal format is invalid": {
|
|
|
|
sut: "010",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"0 resolves to value 0": {
|
|
|
|
sut: "0",
|
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 0,
|
|
|
|
WantLatest: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"quoted 0 resolves to value 0": {
|
|
|
|
sut: `"0"`,
|
|
|
|
want: AttestationVersion{
|
|
|
|
Value: 0,
|
|
|
|
WantLatest: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"00 errors": {
|
|
|
|
sut: "00",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-08-09 05:41:04 -04:00
|
|
|
var sut AttestationVersion
|
2023-08-09 09:11:14 -04:00
|
|
|
err := json.Unmarshal([]byte(tc.sut), &sut)
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
2023-08-09 05:41:04 -04:00
|
|
|
return
|
|
|
|
}
|
2023-08-09 09:11:14 -04:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(tc.want, sut)
|
2023-08-09 05:41:04 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|