From ac339f6e9145c0865a93d27726051756821e765d Mon Sep 17 00:00:00 2001 From: hpya93 <94533459+hpya93@users.noreply.github.com> Date: Tue, 28 Nov 2023 07:14:05 +0530 Subject: [PATCH] RotateContext handle command tests (#252) 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 --- verification/rotateContextHandle.go | 67 +++++++++++++++++++++++++++++ verification/verification.go | 8 ++++ 2 files changed, 75 insertions(+) create mode 100644 verification/rotateContextHandle.go diff --git a/verification/rotateContextHandle.go b/verification/rotateContextHandle.go new file mode 100644 index 00000000..951fcf89 --- /dev/null +++ b/verification/rotateContextHandle.go @@ -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) + } +} diff --git a/verification/verification.go b/verification/verification.go index 5be5da0f..7133d8f1 100644 --- a/verification/verification.go +++ b/verification/verification.go @@ -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"}, } @@ -72,6 +78,8 @@ var AllTestCases = []TestCase{ GetCertificateChainTestCase, ExtendTCITestCase, ExtendDerivedTciTestCase, + RotateContextTestCase, + RotateContextSimulationTestCase, SignAsymmetricTestCase, SignSymmetricTestCase, SignSimulationTestCase,