Files
cocos/pkg/attestation/eat/eat_test.go
T
Sammy Kerata Oina c1cbcec851
CI / lint (push) Has been cancelled
CI / test (agent) (push) Has been cancelled
CI / test (cli) (push) Has been cancelled
CI / test (cmd) (push) Has been cancelled
CI / test (internal) (push) Has been cancelled
CI / test (manager, true) (push) Has been cancelled
CI / test (pkg) (push) Has been cancelled
CI / upload-coverage (push) Has been cancelled
COCOS-577 - Introduce Go-based CoRIM generation and deprecate Rust attestation policy scripts. (#578)
* feat: Introduce Go-based CoRIM generation and deprecate Rust attestation policy scripts.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Update dependencies and refactor attestation policy handling

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* refactor: Migrate attestation verification to use CoRIM and remove deprecated policy handling and EAT verification tests.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Removed the `tdx` and `sev-snp` attestation policy scripts and their build configurations, along with related build and installation steps from the main Makefile.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* chore: Remove Rust CI workflow and Cargo Dependabot configuration, and enhance Go test setup for attestation policy paths.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* refactor: Use WriteString instead of Write([]byte) for writing policy file content in test.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Refactor `ca-bundle` command to fetch bundles by product string using a configurable HTTP getter with improved error handling, and simplify `attestation_policy` command usage.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* fix: ignore return value of cmd.Help()

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Implement CoRIM generation for Azure and GCP attestation policies and add a CLI command to download and verify GCP OVMF files.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Upgrade Python virtual environment setup to include setuptools and wheel, append computation ID to Docker container names, and improve test robustness with error assertions and conditional skips for runtime tests.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* test: Enhance attestation verification tests, including CoRIM integration and specific platform types like Azure SNP, vTPM, TDX, and IGVM.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add comprehensive test cases for `VerifyWithCoRIM` including success and measurement mismatch, and refine reference value validation.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add Azure and TDX attestation verification tests and abstract external service dependencies for improved testability.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add new test cases for Azure measurement extraction, EAT platform types, IGVM measurement stopping, vTPM CoRIM verification, and GCP OVMF download CLI.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* test: enhance CLI CoRIM generation and ATLS certificate verification tests, and refactor the Azure MAA client to use an interface.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
2026-03-19 17:01:24 +01:00

205 lines
4.2 KiB
Go

// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
package eat
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/ultravioletrs/cocos/pkg/attestation"
)
func TestNewEATClaims(t *testing.T) {
tests := []struct {
name string
nonce []byte
expectedErr string
}{
{
name: "Valid nonce",
nonce: []byte("12345678"),
expectedErr: "",
},
{
name: "Nonce too short",
nonce: []byte("1234567"),
expectedErr: "eat_nonce must be at least 8 bytes long",
},
{
name: "Empty nonce",
nonce: []byte{},
expectedErr: "eat_nonce must be at least 8 bytes long",
},
{
name: "Nil nonce",
nonce: nil,
expectedErr: "eat_nonce must be at least 8 bytes long",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewEATClaims([]byte("dummy report"), tt.nonce, attestation.NoCC)
if tt.expectedErr != "" {
assert.EqualError(t, err, tt.expectedErr)
} else {
assert.NoError(t, err)
}
})
}
}
func TestSanitize(t *testing.T) {
tests := []struct {
name string
claims *EATClaims
expected *EATClaims
}{
{
name: "All dependencies present",
claims: &EATClaims{
OEMID: 123,
HWModel: []byte("ValidModel"),
HWVersion: "1.0",
},
expected: &EATClaims{
OEMID: 123,
HWModel: []byte("ValidModel"),
HWVersion: "1.0",
},
},
{
name: "Missing OEMID clears HWModel and HWVersion",
claims: &EATClaims{
OEMID: 0,
HWModel: []byte("ValidModel"),
HWVersion: "1.0",
},
expected: &EATClaims{
OEMID: 0,
HWModel: nil,
HWVersion: "",
},
},
{
name: "Missing HWModel clears HWVersion",
claims: &EATClaims{
OEMID: 123,
HWModel: nil,
HWVersion: "1.0",
},
expected: &EATClaims{
OEMID: 123,
HWModel: nil,
HWVersion: "",
},
},
{
name: "Missing HWModel (empty bytes) clears HWVersion",
claims: &EATClaims{
OEMID: 123,
HWModel: []byte{},
HWVersion: "1.0",
},
expected: &EATClaims{
OEMID: 123,
HWModel: []byte{}, // Should remain empty slice
HWVersion: "",
},
},
{
name: "Independent fields unaffected",
claims: &EATClaims{
OEMID: 0,
DebugStatus: DebugEnabled,
},
expected: &EATClaims{
OEMID: 0,
DebugStatus: DebugEnabled,
},
},
{
name: "Missing SWName clears SWVersion",
claims: &EATClaims{
SWName: "",
SWVersion: "1.0.0",
},
expected: &EATClaims{
SWName: "",
SWVersion: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.claims.Sanitize()
assert.Equal(t, tt.expected, tt.claims)
})
}
}
func TestNewEATClaims_Platforms(t *testing.T) {
nonce := []byte("12345678")
dummyReport := make([]byte, 1200) // Large enough for SNP
tests := []struct {
name string
platform attestation.PlatformType
expectError bool
expectedName string
}{
{
name: "SNP",
platform: attestation.SNP,
expectError: false,
expectedName: "SNP",
},
{
name: "vTPM",
platform: attestation.VTPM,
expectError: false,
expectedName: "vTPM",
},
{
name: "Azure",
platform: attestation.Azure,
expectError: false,
expectedName: "Azure",
},
{
name: "NoCC",
platform: attestation.NoCC,
expectError: false,
expectedName: "NoCC",
},
{
name: "Unknown",
platform: attestation.PlatformType(99),
expectError: false,
expectedName: "Unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
report := dummyReport
if tt.name == "SNP" {
report = make([]byte, 2000)
report[0] = 1 // Version
}
claims, err := NewEATClaims(report, nonce, tt.platform)
if tt.expectError {
assert.Error(t, err)
} else if err != nil {
// Special case for platforms that might fail with dummy data (like TDX)
t.Logf("Platform %s failed with error: %v (expected for dummy data)", tt.name, err)
} else {
assert.NotNil(t, claims)
assert.Equal(t, tt.expectedName, claims.PlatformType)
}
})
}
}