Debian Codegraph @

actions/config_test.go

raw
1 /*
2 * config_test.go - tests for creating the config file
3 *
4 * Copyright 2019 Google LLC
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 * use this file except in compliance with the License. You may obtain a copy of
8 * the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations under
16 * the License.
17 */
18
19 package actions
20
21 import (
22 "os"
23 "path/filepath"
24 "testing"
25 "time"
26
27 "golang.org/x/sys/unix"
28
29 "github.com/google/fscrypt/metadata"
30 )
31
32 // Test that the global config file is created with mode 0644, regardless of the
33 // current umask.
34 func TestConfigFileIsCreatedWithCorrectMode(t *testing.T) {
35 oldMask := unix.Umask(0)
36 defer unix.Umask(oldMask)
37 unix.Umask(0077)
38
39 tempDir, err := os.MkdirTemp("", "fscrypt")
40 if err != nil {
41 t.Fatal(err)
42 }
43 defer os.RemoveAll(tempDir)
44 ConfigFileLocation = filepath.Join(tempDir, "test.conf")
45
46 if err = CreateConfigFile(time.Millisecond, 0); err != nil {
47 t.Fatal(err)
48 }
49 fileInfo, err := os.Stat(ConfigFileLocation)
50 if err != nil {
51 t.Fatal(err)
52 }
53 if fileInfo.Mode().Perm() != 0644 {
54 t.Error("Expected newly created config file to have mode 0644")
55 }
56 }
57
58 func TestCreateConfigFileV2Policy(t *testing.T) {
59 tempDir, err := os.MkdirTemp("", "fscrypt")
60 if err != nil {
61 t.Fatal(err)
62 }
63 defer os.RemoveAll(tempDir)
64 ConfigFileLocation = filepath.Join(tempDir, "test.conf")
65
66 if err = CreateConfigFile(time.Millisecond, 2); err != nil {
67 t.Fatal(err)
68 }
69
70 var config *metadata.Config
71 config, err = getConfig()
72 if err != nil {
73 t.Fatal(err)
74 }
75 if config.Options.PolicyVersion != 2 {
76 t.Error("Expected PolicyVersion 2")
77 }
78 }