Debian Codegraph @

filesystem/path_test.go

raw
1 /*
2 * path_test.go - Tests for path utilities.
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 filesystem
20
21 import (
22 "fmt"
23 "os"
24 "testing"
25
26 "github.com/google/fscrypt/util"
27 )
28
29 func TestDeviceNumber(t *testing.T) {
30 num, err := getDeviceNumber("/NONEXISTENT")
31 if num != 0 || err == nil {
32 t.Error("Should have failed to get device number of nonexistent file")
33 }
34 // /dev/null is always device 1:3 on Linux.
35 num, err = getDeviceNumber("/dev/null")
36 if err != nil {
37 t.Fatal(err)
38 }
39 if str := num.String(); str != "1:3" {
40 t.Errorf("Wrong device number string: %q", str)
41 }
42 if str := fmt.Sprintf("%v", num); str != "1:3" {
43 t.Errorf("Wrong device number string: %q", str)
44 }
45 var num2 DeviceNumber
46 num2, err = newDeviceNumberFromString("1:3")
47 if err != nil {
48 t.Error("Failed to parse device number")
49 }
50 if num2 != num {
51 t.Errorf("Wrong device number: %d", num2)
52 }
53 num2, err = newDeviceNumberFromString("foo")
54 if num2 != 0 || err == nil {
55 t.Error("Should have failed to parse invalid device number")
56 }
57 }
58
59 func TestHaveReadAccessTo(t *testing.T) {
60 if util.IsUserRoot() {
61 t.Skip("This test cannot be run as root")
62 }
63 file, err := os.CreateTemp("", "fscrypt_test")
64 if err != nil {
65 t.Fatal(err)
66 }
67 file.Close()
68 defer os.Remove(file.Name())
69
70 testCases := map[os.FileMode]bool{
71 0444: true,
72 0400: true,
73 0000: false,
74 0040: false, // user bits take priority in Linux
75 0004: false, // user bits take priority in Linux
76 }
77 for mode, readable := range testCases {
78 if err := os.Chmod(file.Name(), mode); err != nil {
79 t.Error(err)
80 }
81 if HaveReadAccessTo(file.Name()) != readable {
82 t.Errorf("Expected readable=%v on mode=0%03o", readable, mode)
83 }
84 }
85 }