From 93415b198a3ef427c02893b8fdf036aa75ffe50f Mon Sep 17 00:00:00 2001 From: "Joe Richey joerichey@google.com" Date: Wed, 21 Jun 2017 10:03:44 -0700 Subject: actions: error handling and API changed This commit changes the error handling for the actions package to use the error handling library github.com/pkg/errors. This means replacing "errors" with "github.com/pkg/errors", reworking some of the error values, and wrapping some errors with additional context. This commit also changes the Protector/Policy API, moving most of the package functionality into Protector or Policy methods. These types are now "locked" when they are queried from the filesystem, and Unlock() must be used to get their corresponding keys. Note that only certain operations will require unlocking the keys. Certain unnecessary functions and methods are also removed. This CL also fixes two bugs reported by Tyler Hicks in CreateConfigFile. CPU time is used instead of wall time, and kiB is used instead of kB. Change-Id: I88f45659e9fe4938d148843e3289e7b6d5b698d8 --- actions/context_test.go | 82 ++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 31 deletions(-) (limited to 'actions/context_test.go') diff --git a/actions/context_test.go b/actions/context_test.go index 74629a3..79adedf 100644 --- a/actions/context_test.go +++ b/actions/context_test.go @@ -20,57 +20,77 @@ package actions import ( + "fmt" + "fscrypt/util" + "log" "os" + "path/filepath" "testing" - - "fscrypt/filesystem" + "time" ) -var mountpoint = os.Getenv("TEST_FILESYSTEM_ROOT") +const testTime = 10 * time.Millisecond + +// holds the context we will use throughout the actions tests +var testContext *Context // Makes a context using the testing locations for the filesystem and // configuration file. -func makeContext() (*Context, error) { - if err := CreateConfigFile(testTime, true); err != nil { +func setupContext() (ctx *Context, err error) { + mountpoint, err := util.TestPath() + if err != nil { return nil, err } - mnt := filesystem.Mount{Path: mountpoint} - if err := mnt.Setup(); err != nil { - return nil, err - } + ConfigFileLocation = filepath.Join(mountpoint, "test.conf") - return NewContextFromMountpoint(mountpoint) -} + // Should not be able to setup without a config file + if badCtx, badCtxErr := NewContextFromMountpoint(mountpoint); badCtxErr == nil { + badCtx.Mount.RemoveAllMetadata() + return nil, fmt.Errorf("created context at %q without config file", badCtx.Mount.Path) + } -// Cleans up the testing config file and testing filesystem data. -func cleaupContext() { - os.RemoveAll(ConfigFileLocation) - mnt := filesystem.Mount{Path: mountpoint} - mnt.RemoveAllMetadata() -} + if err = CreateConfigFile(testTime, true); err != nil { + return nil, err + } + defer func() { + if err != nil { + os.RemoveAll(ConfigFileLocation) + } + }() -// Tests that we can create a context -func TestSetupContext(t *testing.T) { - _, err := makeContext() - defer cleaupContext() + ctx, err = NewContextFromMountpoint(mountpoint) if err != nil { - t.Fatal(err) + return nil, err } + return ctx, ctx.Mount.Setup() } -// Tests that we cannot create a context without a config file. -func TestNoConfigFile(t *testing.T) { - mnt := filesystem.Mount{Path: mountpoint} - if err := mnt.Setup(); err != nil { - t.Fatal(err) +// Cleans up the testing config file and testing filesystem data. +func cleaupContext(ctx *Context) error { + err1 := os.RemoveAll(ConfigFileLocation) + err2 := ctx.Mount.RemoveAllMetadata() + if err1 != nil { + return err1 } + return err2 +} - _, err := NewContextFromMountpoint(mountpoint) - defer cleaupContext() +func TestMain(m *testing.M) { + log.SetFlags(log.LstdFlags | log.Lmicroseconds) + var err error + testContext, err = setupContext() + if err != nil { + fmt.Printf("setupContext() = %v\n", err) + os.Exit(1) + } - if err == nil { - t.Error("should not be able to create context without config file") + returnCode := m.Run() + err = cleaupContext(testContext) + if err != nil { + fmt.Printf("cleanupContext() = %v\n", err) + os.Exit(1) } + os.Exit(returnCode) } -- cgit v1.2.3