gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/lock/lock_range_test.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package lock 16 17 import ( 18 "testing" 19 20 "golang.org/x/sys/unix" 21 ) 22 23 func TestComputeRange(t *testing.T) { 24 tests := []struct { 25 // Description of test. 26 name string 27 28 // Requested start of the lock range. 29 start int64 30 31 // Requested length of the lock range, 32 // can be negative :( 33 length int64 34 35 // Pre-computed file offset based on whence. 36 // Will be added to start. 37 offset int64 38 39 // Expected error. 40 err error 41 42 // If error is nil, the expected LockRange. 43 LockRange 44 }{ 45 { 46 name: "offset, start, and length all zero", 47 LockRange: LockRange{Start: 0, End: LockEOF}, 48 }, 49 { 50 name: "zero offset, zero start, positive length", 51 start: 0, 52 length: 4096, 53 offset: 0, 54 LockRange: LockRange{Start: 0, End: 4096}, 55 }, 56 { 57 name: "zero offset, negative start", 58 start: -4096, 59 offset: 0, 60 err: unix.EINVAL, 61 }, 62 { 63 name: "large offset, negative start, positive length", 64 start: -2048, 65 length: 2048, 66 offset: 4096, 67 LockRange: LockRange{Start: 2048, End: 4096}, 68 }, 69 { 70 name: "large offset, negative start, zero length", 71 start: -2048, 72 length: 0, 73 offset: 4096, 74 LockRange: LockRange{Start: 2048, End: LockEOF}, 75 }, 76 { 77 name: "zero offset, zero start, negative length", 78 start: 0, 79 length: -4096, 80 offset: 0, 81 err: unix.EINVAL, 82 }, 83 { 84 name: "large offset, zero start, negative length", 85 start: 0, 86 length: -4096, 87 offset: 4096, 88 LockRange: LockRange{Start: 0, End: 4096}, 89 }, 90 { 91 name: "offset, start, and length equal, length is negative", 92 start: 1024, 93 length: -1024, 94 offset: 1024, 95 LockRange: LockRange{Start: 1024, End: 2048}, 96 }, 97 { 98 name: "offset, start, and length equal, start is negative", 99 start: -1024, 100 length: 1024, 101 offset: 1024, 102 LockRange: LockRange{Start: 0, End: 1024}, 103 }, 104 { 105 name: "offset, start, and length equal, offset is negative", 106 start: 1024, 107 length: 1024, 108 offset: -1024, 109 LockRange: LockRange{Start: 0, End: 1024}, 110 }, 111 { 112 name: "offset, start, and length equal, all negative", 113 start: -1024, 114 length: -1024, 115 offset: -1024, 116 err: unix.EINVAL, 117 }, 118 { 119 name: "offset, start, and length equal, all positive", 120 start: 1024, 121 length: 1024, 122 offset: 1024, 123 LockRange: LockRange{Start: 2048, End: 3072}, 124 }, 125 } 126 127 for _, test := range tests { 128 rng, err := ComputeRange(test.start, test.length, test.offset) 129 if err != test.err { 130 t.Errorf("%s: lockRange(%d, %d, %d) got error %v, want %v", test.name, test.start, test.length, test.offset, err, test.err) 131 continue 132 } 133 if err == nil && rng != test.LockRange { 134 t.Errorf("%s: lockRange(%d, %d, %d) got LockRange %v, want %v", test.name, test.start, test.length, test.offset, rng, test.LockRange) 135 } 136 } 137 }