mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
4e8057f481
CI / ci (push) Has been cancelled
* Implement IMAMeasurements method in agentSDK and add corresponding unit tests Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add unit tests for NewIMAMeasurements command in CLI Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add error assertion for command execution in NewIMAMeasurements test Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Fix nil pointer dereference in Close method and update NewCreateVMCmd logic for manager client initialization Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor file permission settings to use octal notation and improve cleanup handling in NewCreateVMCmd test Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive unit tests for state machine functionality Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add mock implementation for Algorithm interface and corresponding test cases Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor file permission settings to use octal notation in TestStopComputationIntegration Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Remove redundant reset test cases from TestStateMachine_Reset Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Fix race condition in action call verification in TestStateMachine_HandleEvent Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Enhance state machine with reset functionality and improve thread safety in event handling Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Improve error handling in state machine start function during tests Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Remove concurrent reset and send event test from state machine tests Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Remove error logging for Start function in transition tests Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add mock implementations for AgentService_IMAMeasurementsClient and Service Shutdown method; enhance progress tests for IMA measurements handling Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for FileStorage functionality including loading, saving, and concurrent access Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Enhance tests by adding dataset and algorithm hashes in handleRunReqChunks; improve error handling in TestFileStorage_ErrorHandling cleanup Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Enhance TestManagerClient_Process by adding new test cases for Agent state and Disconnect requests; update setupMocks to include grpcClient Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Fix graceful shutdown in gRPC server by adding nil checks for health and server instances Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Enhance TestAttestation by adding mock expectations for VTpmAttestation and Attestation methods; update service call to include platform parameter Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Enhance gRPC Server by adding synchronization for start/stop methods; prevent multiple starts and ensure graceful shutdown Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add unit tests for gRPC server methods including VM creation, removal, and info retrieval Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add tests for SEVSNP and TDX host capabilities; remove unused vsock code Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add a newline for better readability in vm_test.go Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add integration tests for gRPC client in cvm_test.go Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Remove unused vsock dependencies and add comprehensive unit tests for GCP attestation functions Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Skip GCP tests if credentials are not set Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add tests for error handling in attestation configuration and GCP commands Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Improve error handling in Azure VM test response writing Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Skip tests in GCP functions if credentials are not set Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive unit tests for Azure attestation provider and verifier Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add unit tests for TPM functionality and improve error handling Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for attestation functionality and improve error handling Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add validation for teeNonce in TeeAttestation and implement comprehensive tests for provider methods Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor error messages in TDX attestation tests for clarity Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Fix error message in TeeAttestation test for valid nonce case Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add MeasurementProvider mock and update mockery configuration Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add logging for product in parseUints and rename test functions for clarity Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor TestSevsnpverify to reset configuration and improve error logging Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: Sammy Oina <sammyoina@gmail.com>
379 lines
11 KiB
Go
379 lines
11 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/google/go-sev-guest/proto/check"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/ultravioletrs/cocos/pkg/attestation"
|
|
"github.com/ultravioletrs/cocos/pkg/attestation/vtpm"
|
|
)
|
|
|
|
func TestChangeAttestationConfiguration(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "attestation_policy.json")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
initialConfig := attestation.Config{Config: &check.Config{RootOfTrust: &check.RootOfTrust{}, Policy: &check.Policy{}}, PcrConfig: &attestation.PcrConfig{}}
|
|
|
|
initialJSON, err := json.Marshal(initialConfig)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(tmpfile.Name(), initialJSON, 0o644)
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
name string
|
|
base64Data string
|
|
expectedLength int
|
|
field fieldType
|
|
expectError bool
|
|
errorType error
|
|
}{
|
|
{
|
|
name: "Valid Measurement",
|
|
base64Data: base64.StdEncoding.EncodeToString(make([]byte, measurementLength)),
|
|
expectedLength: measurementLength,
|
|
field: measurementField,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "Valid Host Data",
|
|
base64Data: base64.StdEncoding.EncodeToString(make([]byte, hostDataLength)),
|
|
expectedLength: hostDataLength,
|
|
field: hostDataField,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "Invalid Base64",
|
|
base64Data: "Invalid Base64",
|
|
expectedLength: measurementLength,
|
|
field: measurementField,
|
|
expectError: true,
|
|
errorType: errDecode,
|
|
},
|
|
{
|
|
name: "Invalid Data Length",
|
|
base64Data: base64.StdEncoding.EncodeToString(make([]byte, measurementLength-1)),
|
|
expectedLength: measurementLength,
|
|
field: measurementField,
|
|
expectError: true,
|
|
errorType: errDataLength,
|
|
},
|
|
{
|
|
name: "Invalid Field Type",
|
|
base64Data: base64.StdEncoding.EncodeToString(make([]byte, measurementLength)),
|
|
expectedLength: measurementLength,
|
|
field: fieldType(999),
|
|
expectError: true,
|
|
errorType: errAttestationPolicyField,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := changeAttestationConfiguration(tmpfile.Name(), tt.base64Data, tt.expectedLength, tt.field)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, tt.errorType)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
|
|
content, err := os.ReadFile(tmpfile.Name())
|
|
require.NoError(t, err)
|
|
|
|
ap := attestation.Config{Config: &check.Config{RootOfTrust: &check.RootOfTrust{}, Policy: &check.Policy{}}, PcrConfig: &attestation.PcrConfig{}}
|
|
err = vtpm.ReadPolicyFromByte(content, &ap)
|
|
require.NoError(t, err)
|
|
|
|
decodedData, _ := base64.StdEncoding.DecodeString(tt.base64Data)
|
|
if tt.field == measurementField {
|
|
assert.Equal(t, decodedData, ap.Config.Policy.Measurement)
|
|
} else if tt.field == hostDataField {
|
|
assert.Equal(t, decodedData, ap.Config.Policy.HostData)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewAttestationPolicyCmd(t *testing.T) {
|
|
cli := &CLI{}
|
|
cmd := cli.NewAttestationPolicyCmd()
|
|
|
|
assert.Equal(t, "policy [command]", cmd.Use)
|
|
assert.Equal(t, "Change attestation policy", cmd.Short)
|
|
assert.NotNil(t, cmd.Run)
|
|
}
|
|
|
|
func TestNewAddMeasurementCmd(t *testing.T) {
|
|
cli := &CLI{}
|
|
cmd := cli.NewAddMeasurementCmd()
|
|
|
|
assert.Equal(t, "measurement", cmd.Use)
|
|
assert.Equal(t, "Add measurement to the attestation policy file. The value should be in base64. The second parameter is attestation_policy.json file", cmd.Short)
|
|
assert.Equal(t, "measurement <measurement> <attestation_policy.json>", cmd.Example)
|
|
assert.NotNil(t, cmd.Run)
|
|
}
|
|
|
|
func TestNewAddHostDataCmd(t *testing.T) {
|
|
cli := &CLI{}
|
|
cmd := cli.NewAddHostDataCmd()
|
|
|
|
assert.Equal(t, "hostdata", cmd.Use)
|
|
assert.Equal(t, "Add host data to the attestation policy file. The value should be in base64. The second parameter is attestation_policy.json file", cmd.Short)
|
|
assert.Equal(t, "hostdata <host-data> <attestation_policy.json>", cmd.Example)
|
|
assert.NotNil(t, cmd.Run)
|
|
}
|
|
|
|
func TestChangeAttestationConfigurationFileErrors(t *testing.T) {
|
|
t.Run("File Not Found", func(t *testing.T) {
|
|
err := changeAttestationConfiguration("nonexistent.json", base64.StdEncoding.EncodeToString(make([]byte, measurementLength)), measurementLength, measurementField)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "error while reading the attestation policy file")
|
|
})
|
|
|
|
t.Run("Invalid JSON Content", func(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "invalid.json")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
err = os.WriteFile(tmpfile.Name(), []byte("invalid json"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
err = changeAttestationConfiguration(tmpfile.Name(), base64.StdEncoding.EncodeToString(make([]byte, measurementLength)), measurementLength, measurementField)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "failed to unmarshal json")
|
|
})
|
|
}
|
|
|
|
func TestNewGCPAttestationPolicy(t *testing.T) {
|
|
cli := &CLI{}
|
|
cmd := cli.NewGCPAttestationPolicy()
|
|
|
|
assert.Equal(t, "gcp", cmd.Use)
|
|
assert.Equal(t, "Get attestation policy for GCP CVM", cmd.Short)
|
|
assert.Equal(t, "gcp <bin_vtmp_attestation_report_file> <vcpu_count>", cmd.Example)
|
|
assert.NotNil(t, cmd.Run)
|
|
|
|
t.Run("File Not Found", func(t *testing.T) {
|
|
cmd.SetArgs([]string{"nonexistent.bin", "4"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err := cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error reading attestation report file")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
|
|
t.Run("Invalid vCPU Count", func(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "attestation.bin")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
err = os.WriteFile(tmpfile.Name(), []byte("dummy content"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
cmd.SetArgs([]string{tmpfile.Name(), "invalid"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err = cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error converting vCPU count to integer")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
|
|
t.Run("Invalid Attestation Data", func(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "attestation.bin")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
err = os.WriteFile(tmpfile.Name(), []byte("invalid protobuf data"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
cmd.SetArgs([]string{tmpfile.Name(), "4"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err = cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error unmarshaling attestation report")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
}
|
|
|
|
func TestNewDownloadGCPOvmfFile(t *testing.T) {
|
|
cli := &CLI{}
|
|
cmd := cli.NewDownloadGCPOvmfFile()
|
|
|
|
assert.Equal(t, "download", cmd.Use)
|
|
assert.Equal(t, "Download GCP OVMF file", cmd.Short)
|
|
assert.Equal(t, "download <bin_vtmp_attestation_report_file>", cmd.Example)
|
|
assert.NotNil(t, cmd.Run)
|
|
|
|
t.Run("File Not Found", func(t *testing.T) {
|
|
cmd.SetArgs([]string{"nonexistent.bin"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err := cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error reading attestation report file")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
|
|
t.Run("Invalid Attestation Data", func(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "attestation.bin")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
err = os.WriteFile(tmpfile.Name(), []byte("invalid protobuf data"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
cmd.SetArgs([]string{tmpfile.Name()})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err = cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error unmarshaling attestation report")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
}
|
|
|
|
func TestNewAzureAttestationPolicy(t *testing.T) {
|
|
cli := &CLI{}
|
|
cmd := cli.NewAzureAttestationPolicy()
|
|
|
|
assert.Equal(t, "azure", cmd.Use)
|
|
assert.Equal(t, "Get attestation policy for Azure CVM", cmd.Short)
|
|
assert.Equal(t, "azure <azure_maa_token_file> <product_name>", cmd.Example)
|
|
assert.NotNil(t, cmd.Run)
|
|
|
|
flag := cmd.Flags().Lookup("policy")
|
|
assert.NotNil(t, flag)
|
|
assert.Equal(t, "Policy of the guest CVM", flag.Usage)
|
|
|
|
t.Run("File Not Found", func(t *testing.T) {
|
|
cmd.SetArgs([]string{"nonexistent.token", "test-product"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err := cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error reading attestation report file")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
|
|
t.Run("Valid Token File", func(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "token.maa")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
err = os.WriteFile(tmpfile.Name(), []byte("dummy.token.content"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove("attestation_policy.json")
|
|
|
|
cmd.SetArgs([]string{tmpfile.Name(), "test-product"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err = cmd.Execute()
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Custom Policy Flag", func(t *testing.T) {
|
|
tmpfile, err := os.CreateTemp("", "token.maa")
|
|
require.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
err = os.WriteFile(tmpfile.Name(), []byte("dummy.token.content"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
cmd.SetArgs([]string{"--policy", "123456", tmpfile.Name(), "test-product"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err = cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
flag := cmd.Flags().Lookup("policy")
|
|
assert.NotNil(t, flag)
|
|
assert.Equal(t, "123456", flag.Value.String())
|
|
})
|
|
}
|
|
|
|
func TestCommandErrorHandling(t *testing.T) {
|
|
cli := &CLI{}
|
|
|
|
t.Run("Measurement Command Error", func(t *testing.T) {
|
|
cmd := cli.NewAddMeasurementCmd()
|
|
cmd.SetArgs([]string{"invalid-base64", "nonexistent.json"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err := cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error could not change measurement data")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
|
|
t.Run("Host Data Command Error", func(t *testing.T) {
|
|
cmd := cli.NewAddHostDataCmd()
|
|
cmd.SetArgs([]string{"invalid-base64", "nonexistent.json"})
|
|
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
cmd.SetErr(&buf)
|
|
|
|
err := cmd.Execute()
|
|
assert.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
assert.Contains(t, output, "Error could not change host data")
|
|
assert.Contains(t, output, "❌")
|
|
})
|
|
}
|