| 1 |
/* |
| 2 |
* policy_test.go - Tests the getting/setting of encryption policies |
| 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 metadata |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"golang.org/x/sys/unix" |
| 29 |
"google.golang.org/protobuf/proto" |
| 30 |
|
| 31 |
"github.com/google/fscrypt/util" |
| 32 |
) |
| 33 |
|
| 34 |
const goodV1Descriptor = "0123456789abcdef" |
| 35 |
|
| 36 |
var goodV1Policy = &PolicyData{ |
| 37 |
KeyDescriptor: goodV1Descriptor, |
| 38 |
Options: DefaultOptions, |
| 39 |
} |
| 40 |
|
| 41 |
var goodV2EncryptionOptions = &EncryptionOptions{ |
| 42 |
Padding: 32, |
| 43 |
Contents: EncryptionOptions_AES_256_XTS, |
| 44 |
Filenames: EncryptionOptions_AES_256_CTS, |
| 45 |
PolicyVersion: 2, |
| 46 |
} |
| 47 |
|
| 48 |
var goodV2Policy = &PolicyData{ |
| 49 |
KeyDescriptor: "0123456789abcdef0123456789abcdef", |
| 50 |
Options: goodV2EncryptionOptions, |
| 51 |
} |
| 52 |
|
| 53 |
// Creates a temporary directory for testing. |
| 54 |
func createTestDirectory(t *testing.T) (directory string, err error) { |
| 55 |
baseDirectory, err := util.TestRoot() |
| 56 |
if err != nil { |
| 57 |
t.Skip(err) |
| 58 |
} |
| 59 |
if s, err := os.Stat(baseDirectory); err != nil || !s.IsDir() { |
| 60 |
return "", fmt.Errorf("test directory %q is not valid", baseDirectory) |
| 61 |
} |
| 62 |
|
| 63 |
directoryPath := filepath.Join(baseDirectory, "test") |
| 64 |
return directoryPath, os.MkdirAll(directoryPath, os.ModePerm) |
| 65 |
} |
| 66 |
|
| 67 |
// Makes a test directory, makes a file in the directory, and fills the file |
| 68 |
// with data. Returns the directory name, file name, and error (if one). |
| 69 |
func createTestFile(t *testing.T) (directory, file string, err error) { |
| 70 |
if directory, err = createTestDirectory(t); err != nil { |
| 71 |
return |
| 72 |
} |
| 73 |
// Cleanup if the file creation fails |
| 74 |
defer func() { |
| 75 |
if err != nil { |
| 76 |
os.RemoveAll(directory) |
| 77 |
} |
| 78 |
}() |
| 79 |
|
| 80 |
filePath := filepath.Join(directory, "test.txt") |
| 81 |
fileHandle, err := os.Create(filePath) |
| 82 |
if err != nil { |
| 83 |
return directory, filePath, err |
| 84 |
} |
| 85 |
defer fileHandle.Close() |
| 86 |
|
| 87 |
_, err = fileHandle.Write([]byte("Here is some test data!\n")) |
| 88 |
return directory, filePath, err |
| 89 |
} |
| 90 |
|
| 91 |
// Tests that we can set a policy on an empty directory |
| 92 |
func TestSetPolicyEmptyDirectory(t *testing.T) { |
| 93 |
directory, err := createTestDirectory(t) |
| 94 |
if err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
defer os.RemoveAll(directory) |
| 98 |
|
| 99 |
if err = SetPolicy(directory, goodV1Policy); err != nil { |
| 100 |
t.Error(err) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Tests that we cannot set a policy on a nonempty directory |
| 105 |
func TestSetPolicyNonemptyDirectory(t *testing.T) { |
| 106 |
directory, _, err := createTestFile(t) |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
defer os.RemoveAll(directory) |
| 111 |
|
| 112 |
if err = SetPolicy(directory, goodV1Policy); err == nil { |
| 113 |
t.Error("should have failed to set policy on a nonempty directory") |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Tests that we cannot set a policy on a file |
| 118 |
func TestSetPolicyFile(t *testing.T) { |
| 119 |
directory, file, err := createTestFile(t) |
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
defer os.RemoveAll(directory) |
| 124 |
|
| 125 |
if err = SetPolicy(file, goodV1Policy); err == nil { |
| 126 |
t.Error("should have failed to set policy on a file") |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Tests that we fail when using bad policies |
| 131 |
func TestSetPolicyBadDescriptors(t *testing.T) { |
| 132 |
// Policies that are too short, have invalid chars, or are too long |
| 133 |
badDescriptors := []string{"123456789abcde", "xxxxxxxxxxxxxxxx", "0123456789abcdef00"} |
| 134 |
for _, badDescriptor := range badDescriptors { |
| 135 |
badPolicy := &PolicyData{KeyDescriptor: badDescriptor, Options: DefaultOptions} |
| 136 |
directory, err := createTestDirectory(t) |
| 137 |
if err != nil { |
| 138 |
t.Fatal(err) |
| 139 |
} |
| 140 |
|
| 141 |
if err = SetPolicy(directory, badPolicy); err == nil { |
| 142 |
t.Errorf("descriptor %q should have made SetPolicy fail", badDescriptor) |
| 143 |
} |
| 144 |
os.RemoveAll(directory) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Tests that we get back the same policy that we set on a directory |
| 149 |
func TestGetPolicyEmptyDirectory(t *testing.T) { |
| 150 |
directory, err := createTestDirectory(t) |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
defer os.RemoveAll(directory) |
| 155 |
|
| 156 |
var actualPolicy *PolicyData |
| 157 |
if err = SetPolicy(directory, goodV1Policy); err != nil { |
| 158 |
t.Fatal(err) |
| 159 |
} |
| 160 |
if actualPolicy, err = GetPolicy(directory); err != nil { |
| 161 |
t.Fatal(err) |
| 162 |
} |
| 163 |
|
| 164 |
if !proto.Equal(actualPolicy, goodV1Policy) { |
| 165 |
t.Errorf("policy %+v does not equal expected policy %+v", actualPolicy, goodV1Policy) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Tests that we cannot get a policy on an unencrypted directory |
| 170 |
func TestGetPolicyUnencrypted(t *testing.T) { |
| 171 |
directory, err := createTestDirectory(t) |
| 172 |
if err != nil { |
| 173 |
t.Fatal(err) |
| 174 |
} |
| 175 |
defer os.RemoveAll(directory) |
| 176 |
|
| 177 |
if _, err = GetPolicy(directory); err == nil { |
| 178 |
t.Error("should have failed to set policy on a file") |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func requireV2PolicySupport(t *testing.T, directory string) { |
| 183 |
file, err := os.Open(directory) |
| 184 |
if err != nil { |
| 185 |
t.Fatal(err) |
| 186 |
} |
| 187 |
defer file.Close() |
| 188 |
|
| 189 |
err = getPolicyIoctl(file, unix.FS_IOC_GET_ENCRYPTION_POLICY_EX, nil) |
| 190 |
if err == ErrEncryptionNotSupported { |
| 191 |
t.Skip("No support for v2 encryption policies, skipping test") |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Tests that a non-root user cannot set a v2 encryption policy unless the key |
| 196 |
// has been added. |
| 197 |
func TestSetV2PolicyNoKey(t *testing.T) { |
| 198 |
if util.IsUserRoot() { |
| 199 |
t.Skip("This test cannot be run as root") |
| 200 |
} |
| 201 |
directory, err := createTestDirectory(t) |
| 202 |
if err != nil { |
| 203 |
t.Fatal(err) |
| 204 |
} |
| 205 |
defer os.RemoveAll(directory) |
| 206 |
requireV2PolicySupport(t, directory) |
| 207 |
|
| 208 |
err = SetPolicy(directory, goodV2Policy) |
| 209 |
if err == nil { |
| 210 |
t.Error("shouldn't have been able to set v2 policy without key added") |
| 211 |
} |
| 212 |
} |