Skip to content

Commit

Permalink
RotateContext handle command tests (#252)
Browse files Browse the repository at this point in the history
Added tests for RotateContextHandle command

* Added a test to check and report invalid handle with a test handle (moved to negativeCases.go, taken care in PR 255, as a common negative case test)

The following are part of this PR:

* RotateHandle in default context
* RotateHandle in non-default context
* RotateHandle in with TARGET_DEFAULT flag
  • Loading branch information
hpya93 authored Nov 28, 2023
1 parent d0c06e8 commit ac339f6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
67 changes: 67 additions & 0 deletions verification/rotateContextHandle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed under the Apache-2.0 license

package verification

import (
"errors"
"testing"
)

func TestRotateContextHandle(d TestDPEInstance, c DPEClient, t *testing.T) {
simulation := false
handle := getInitialContextHandle(d, c, t, simulation)

// Check whether the rotated context handle is a random context handle
handle, err := c.RotateContextHandle(handle, RotateContextHandleFlags(0))
if err != nil {
t.Fatalf("[FATAL]: Could not rotate context handle: %v", err)
}
if *handle == DefaultContextHandle {
t.Errorf("[ERROR]: Expected random context handle but have got default context %v", handle)
}

// Rotate back the handle to default handle for subsequent tests
// This works only when there is no default handle available
handle, err = c.RotateContextHandle(handle, TargetIsDefault)
if err != nil {
t.Fatalf("[FATAL]: Could not rotate context handle: %v", err)
}
if *handle != DefaultContextHandle {
t.Errorf("[ERROR]: TARGET_IS_DEFAULT is set, have got %v but want %v", handle, DefaultContextHandle)
}

// Check for error when a default context handle exists already and handle is rotated to default handle
// Since, there cannot be more than one default context handle
_, err = c.RotateContextHandle(handle, TargetIsDefault)
if err == nil {
t.Fatalf("[FATAL]: Should return %q for default context, but returned no error", StatusInvalidArgument)
} else if !errors.Is(err, StatusInvalidArgument) {
t.Fatalf("[FATAL]: Incorrect error type. Should return %q, but returned %q", StatusInvalidArgument, err)
}
}

func TestRotateContextHandleSimulation(d TestDPEInstance, c DPEClient, t *testing.T) {
simulation := true
handle := getInitialContextHandle(d, c, t, simulation)
defer func() {
c.DestroyContext(handle, DestroyDescendants)
}()

// Check whether the rotated context handle is a random context handle
handle, err := c.RotateContextHandle(handle, RotateContextHandleFlags(0))
if err != nil {
t.Fatalf("[FATAL]: Could not rotate context handle: %v", err)
}
if *handle == DefaultContextHandle {
t.Errorf("[ERROR]: Expected random context handle but have got default context %v", handle)
}

// In simulated context, the handle cannot be rotated to default handle
// Since, it is not allowed to have a both of default and non-default context handles together
_, err = c.RotateContextHandle(handle, TargetIsDefault)
if err == nil {
t.Fatalf("[FATAL]: Should return %q for simulation context, but returned no error", StatusInvalidArgument)
} else if !errors.Is(err, StatusInvalidArgument) {
t.Fatalf("[FATAL]: Incorrect error type. Should return %q, but returned %q", StatusInvalidArgument, err)
}
}
8 changes: 8 additions & 0 deletions verification/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ var UnsupportedCommand = TestCase{
var UnsupportedCommandFlag = TestCase{
"CheckSupportForCommmandFlag", TestUnsupportedCommandFlag, []string{"AutoInit", "RotateContext", "ExtendTci"},
}
var RotateContextTestCase = TestCase{
"RotateContextHandle", TestRotateContextHandle, []string{"AutoInit", "RotateContext"},
}
var RotateContextSimulationTestCase = TestCase{
"RotateContextHandleSimulation", TestRotateContextHandleSimulation, []string{"Simulation", "RotateContext"},
}
var SignAsymmetricTestCase = TestCase{
"Sign", TestAsymmetricSigning, []string{"AutoInit", "X509"},
}
Expand All @@ -72,6 +78,8 @@ var AllTestCases = []TestCase{
GetCertificateChainTestCase,
ExtendTCITestCase,
ExtendDerivedTciTestCase,
RotateContextTestCase,
RotateContextSimulationTestCase,
SignAsymmetricTestCase,
SignSymmetricTestCase,
SignSimulationTestCase,
Expand Down

0 comments on commit ac339f6

Please sign in to comment.