| 1 |
/* |
| 2 |
* context_test.go - tests for creating new contexts |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package actions |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"log" |
| 25 |
"os" |
| 26 |
"path/filepath" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
"github.com/google/fscrypt/filesystem" |
| 31 |
"github.com/google/fscrypt/util" |
| 32 |
"github.com/pkg/errors" |
| 33 |
) |
| 34 |
|
| 35 |
const testTime = 10 * time.Millisecond |
| 36 |
|
| 37 |
// holds the context we will use throughout the actions tests |
| 38 |
var testContext *Context |
| 39 |
|
| 40 |
// Makes a context using the testing locations for the filesystem and |
| 41 |
// configuration file. |
| 42 |
func setupContext() (ctx *Context, err error) { |
| 43 |
mountpoint, err := util.TestRoot() |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
|
| 48 |
ConfigFileLocation = filepath.Join(mountpoint, "test.conf") |
| 49 |
|
| 50 |
// Should not be able to setup without a config file |
| 51 |
os.Remove(ConfigFileLocation) |
| 52 |
if badCtx, badCtxErr := NewContextFromMountpoint(mountpoint, nil); badCtxErr == nil { |
| 53 |
badCtx.Mount.RemoveAllMetadata() |
| 54 |
return nil, fmt.Errorf("created context at %q without config file", badCtx.Mount.Path) |
| 55 |
} |
| 56 |
|
| 57 |
if err = CreateConfigFile(testTime, 0); err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
defer func() { |
| 61 |
if err != nil { |
| 62 |
os.RemoveAll(ConfigFileLocation) |
| 63 |
} |
| 64 |
}() |
| 65 |
|
| 66 |
ctx, err = NewContextFromMountpoint(mountpoint, nil) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
return ctx, ctx.Mount.Setup(filesystem.WorldWritable) |
| 72 |
} |
| 73 |
|
| 74 |
// Cleans up the testing config file and testing filesystem data. |
| 75 |
func cleaupContext(ctx *Context) error { |
| 76 |
err1 := os.RemoveAll(ConfigFileLocation) |
| 77 |
err2 := ctx.Mount.RemoveAllMetadata() |
| 78 |
if err1 != nil { |
| 79 |
return err1 |
| 80 |
} |
| 81 |
return err2 |
| 82 |
} |
| 83 |
|
| 84 |
func TestMain(m *testing.M) { |
| 85 |
log.SetFlags(log.LstdFlags | log.Lmicroseconds) |
| 86 |
var err error |
| 87 |
testContext, err = setupContext() |
| 88 |
if err != nil { |
| 89 |
fmt.Println(err) |
| 90 |
if errors.Cause(err) != util.ErrSkipIntegration { |
| 91 |
os.Exit(1) |
| 92 |
} |
| 93 |
os.Exit(0) |
| 94 |
} |
| 95 |
|
| 96 |
returnCode := m.Run() |
| 97 |
err = cleaupContext(testContext) |
| 98 |
if err != nil { |
| 99 |
fmt.Printf("cleanupContext() = %v\n", err) |
| 100 |
os.Exit(1) |
| 101 |
} |
| 102 |
os.Exit(returnCode) |
| 103 |
} |