github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/filesystem/mode_test.go (about) 1 package filesystem 2 3 import ( 4 "testing" 5 ) 6 7 // TestModePermissionMaskIsExpected is a sanity check that ModePermissionsMask 8 // is equivalent to 0777 on all platforms (which it should be on POSIX platforms 9 // under the POSIX standard and on Windows platforms based on the os package's 10 // (immutable) FileMode definition). 11 func TestModePermissionMaskIsExpected(t *testing.T) { 12 if ModePermissionsMask != Mode(0777) { 13 t.Error("ModePermissionsMask value not equal to expected:", ModePermissionsMask, "!=", Mode(0777)) 14 } 15 } 16 17 // TestModePermissionMaskIsUnionOfPermissions is a sanity check that 18 // ModePermissionMask is equal to the union of individual permissions. 19 func TestModePermissionMaskIsUnionOfPermissions(t *testing.T) { 20 permissionUnion := ModePermissionUserRead | ModePermissionUserWrite | ModePermissionUserExecute | 21 ModePermissionGroupRead | ModePermissionGroupWrite | ModePermissionGroupExecute | 22 ModePermissionOthersRead | ModePermissionOthersWrite | ModePermissionOthersExecute 23 if ModePermissionsMask != permissionUnion { 24 t.Error("ModePermissionsMask value not equal to union of permissions:", ModePermissionsMask, "!=", permissionUnion) 25 } 26 } 27 28 // parseModeTestCase represents a test case for ParseMode. 29 type parseModeTestCase struct { 30 // value is the value to parse. 31 value string 32 // mask is the mask to use in parsing. 33 mask Mode 34 // expectFailure indicates whether or not parsing failure is expected. 35 expectFailure bool 36 // expected indicates the expected result in the absence of failure. 37 expected Mode 38 } 39 40 // run executes the test in the provided test context. 41 func (c *parseModeTestCase) run(t *testing.T) { 42 // Mark ourselves as a helper function. 43 t.Helper() 44 45 // Perform parsing and verify that the expected behavior is observed. 46 if result, err := parseMode(c.value, c.mask); err == nil && c.expectFailure { 47 t.Fatal("parsing succeeded when failure was expected") 48 } else if err != nil && !c.expectFailure { 49 t.Fatal("parsing failed unexpectedly:", err) 50 } else if result != c.expected { 51 t.Error("parsing result does not match expected:", result, "!=", c.expected) 52 } 53 } 54 55 // TestParseModeEmpty verifies that parseMode fails on an empty string. 56 func TestParseModeEmpty(t *testing.T) { 57 // Create the test case. 58 testCase := &parseModeTestCase{ 59 mask: ModePermissionsMask, 60 expectFailure: true, 61 } 62 63 // Run the test case. 64 testCase.run(t) 65 } 66 67 // TestParseModeInvalid verifies that parseMode fails on an invalid string. 68 func TestParseModeInvalid(t *testing.T) { 69 // Create the test case. 70 testCase := &parseModeTestCase{ 71 value: "laksjfd", 72 mask: ModePermissionsMask, 73 expectFailure: true, 74 } 75 76 // Run the test case. 77 testCase.run(t) 78 } 79 80 // TestParseModeOverflow verifies that parseMode fails on a mode specification 81 // that overflows an unsigned 32-bit integer. 82 func TestParseModeOverflow(t *testing.T) { 83 // Create the test case. 84 testCase := &parseModeTestCase{ 85 value: "45201371000", 86 mask: ModePermissionsMask, 87 expectFailure: true, 88 } 89 90 // Run the test case. 91 testCase.run(t) 92 } 93 94 // TestParseModeInvalidBits verifies that parseMode fails on a mode that doesn't 95 // fit within to the specified bit mask. 96 func TestParseModeInvalidBits(t *testing.T) { 97 // Create the test case. 98 testCase := &parseModeTestCase{ 99 value: "1000", 100 mask: ModePermissionsMask, 101 expectFailure: true, 102 } 103 104 // Run the test case. 105 testCase.run(t) 106 } 107 108 // TestParseModeValid verifies that parseMode succeeds on a valid string. 109 func TestParseModeValid(t *testing.T) { 110 // Create the test case. 111 testCase := &parseModeTestCase{ 112 value: "777", 113 mask: ModePermissionsMask, 114 expected: 0777, 115 } 116 117 // Run the test case. 118 testCase.run(t) 119 } 120 121 // TestParseModeValidWithZeroPrefix verifies that parseMode succeeds on a valid 122 // string with a zero prefix 123 func TestParseModeValidWithZeroPrefix(t *testing.T) { 124 // Create the test case. 125 testCase := &parseModeTestCase{ 126 value: "0755", 127 mask: ModePermissionsMask, 128 expected: 0755, 129 } 130 131 // Run the test case. 132 testCase.run(t) 133 } 134 135 // TestParseModeValidWithMultiZeroPrefix verifies that parseMode succeeds on a 136 // valid string with a multi-zero prefix 137 func TestParseModeValidWithMultiZeroPrefix(t *testing.T) { 138 // Create the test case. 139 testCase := &parseModeTestCase{ 140 value: "00644", 141 mask: ModePermissionsMask, 142 expected: 0644, 143 } 144 145 // Run the test case. 146 testCase.run(t) 147 } 148 149 // TestModeUnmarshalTextUnmodifiedOnFailure verifies that Mode.UnmarshalText 150 // leaves the underlying mode unmodified in the case of failure. 151 func TestModeUnmarshalTextUnmodifiedOnFailure(t *testing.T) { 152 // Create a zero-valued mode. 153 var mode Mode 154 155 // Unmarshal an invalid value. 156 if mode.UnmarshalText([]byte("0888")) == nil { 157 t.Fatal("mode unmarshalling succeeded unexpectedly") 158 } else if mode != 0 { 159 t.Error("mode modified during unsuccessful unmarshalling operation") 160 } 161 }