File: context_test.go

package info (click to toggle)
fscrypt 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: sh: 970; makefile: 159; ansic: 84
file content (103 lines) | stat: -rw-r--r-- 2,543 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * context_test.go - tests for creating new contexts
 *
 * Copyright 2017 Google Inc.
 * Author: Joe Richey (joerichey@google.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package actions

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/google/fscrypt/filesystem"
	"github.com/google/fscrypt/util"
	"github.com/pkg/errors"
)

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 setupContext() (ctx *Context, err error) {
	mountpoint, err := util.TestRoot()
	if err != nil {
		return nil, err
	}

	ConfigFileLocation = filepath.Join(mountpoint, "test.conf")

	// Should not be able to setup without a config file
	os.Remove(ConfigFileLocation)
	if badCtx, badCtxErr := NewContextFromMountpoint(mountpoint, nil); badCtxErr == nil {
		badCtx.Mount.RemoveAllMetadata()
		return nil, fmt.Errorf("created context at %q without config file", badCtx.Mount.Path)
	}

	if err = CreateConfigFile(testTime, 0); err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			os.RemoveAll(ConfigFileLocation)
		}
	}()

	ctx, err = NewContextFromMountpoint(mountpoint, nil)
	if err != nil {
		return nil, err
	}

	return ctx, ctx.Mount.Setup(filesystem.WorldWritable)
}

// 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
}

func TestMain(m *testing.M) {
	log.SetFlags(log.LstdFlags | log.Lmicroseconds)
	var err error
	testContext, err = setupContext()
	if err != nil {
		fmt.Println(err)
		if errors.Cause(err) != util.ErrSkipIntegration {
			os.Exit(1)
		}
		os.Exit(0)
	}

	returnCode := m.Run()
	err = cleaupContext(testContext)
	if err != nil {
		fmt.Printf("cleanupContext() = %v\n", err)
		os.Exit(1)
	}
	os.Exit(returnCode)
}