2023-06-02 06:10:22 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2023-09-25 05:53:02 -04:00
|
|
|
package attestationconfigapi
|
2023-06-02 06:10:22 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsInputNewerThanLatestAPI(t *testing.T) {
|
2023-11-14 04:03:01 -05:00
|
|
|
newTestCfg := func() SEVSNPVersion {
|
|
|
|
return SEVSNPVersion{
|
2023-08-10 03:45:46 -04:00
|
|
|
Microcode: 93,
|
|
|
|
TEE: 0,
|
|
|
|
SNP: 6,
|
|
|
|
Bootloader: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 06:10:22 -04:00
|
|
|
testCases := map[string]struct {
|
2023-11-14 04:03:01 -05:00
|
|
|
latest SEVSNPVersion
|
|
|
|
input SEVSNPVersion
|
2023-06-02 06:10:22 -04:00
|
|
|
expect bool
|
|
|
|
errMsg string
|
|
|
|
}{
|
|
|
|
"input is older than latest": {
|
2023-11-14 04:03:01 -05:00
|
|
|
input: func(c SEVSNPVersion) SEVSNPVersion {
|
2023-06-02 06:10:22 -04:00
|
|
|
c.Microcode--
|
|
|
|
return c
|
2023-08-10 03:45:46 -04:00
|
|
|
}(newTestCfg()),
|
|
|
|
latest: newTestCfg(),
|
2023-06-02 06:10:22 -04:00
|
|
|
expect: false,
|
|
|
|
errMsg: "input Microcode version: 92 is older than latest API version: 93",
|
|
|
|
},
|
|
|
|
"input has greater and smaller version field than latest": {
|
2023-11-14 04:03:01 -05:00
|
|
|
input: func(c SEVSNPVersion) SEVSNPVersion {
|
2023-06-02 06:10:22 -04:00
|
|
|
c.Microcode++
|
|
|
|
c.Bootloader--
|
|
|
|
return c
|
2023-08-10 03:45:46 -04:00
|
|
|
}(newTestCfg()),
|
|
|
|
latest: newTestCfg(),
|
2023-06-02 06:10:22 -04:00
|
|
|
expect: false,
|
|
|
|
errMsg: "input Bootloader version: 1 is older than latest API version: 2",
|
|
|
|
},
|
|
|
|
"input is newer than latest": {
|
2023-11-14 04:03:01 -05:00
|
|
|
input: func(c SEVSNPVersion) SEVSNPVersion {
|
2023-06-02 06:10:22 -04:00
|
|
|
c.TEE++
|
|
|
|
return c
|
2023-08-10 03:45:46 -04:00
|
|
|
}(newTestCfg()),
|
|
|
|
latest: newTestCfg(),
|
2023-06-02 06:10:22 -04:00
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
"input is equal to latest": {
|
2023-08-10 03:45:46 -04:00
|
|
|
input: newTestCfg(),
|
|
|
|
latest: newTestCfg(),
|
2023-06-02 06:10:22 -04:00
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-09-25 05:53:02 -04:00
|
|
|
isNewer, err := isInputNewerThanOtherVersion(tc.input, tc.latest)
|
2023-06-02 06:10:22 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
if tc.errMsg != "" {
|
|
|
|
assert.EqualError(err, tc.errMsg)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.expect, isNewer)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|