github.com/tych0/umoci@v0.4.2/pkg/unpriv/unpriv_utimes_test.go (about) 1 /* 2 * umoci: Umoci Modifies Open Containers' Images 3 * Copyright (C) 2016, 2017, 2018 SUSE LLC. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package unpriv 19 20 import ( 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 "time" 26 27 "golang.org/x/sys/unix" 28 ) 29 30 func TestLutimesFile(t *testing.T) { 31 var fiOld, fiNew unix.Stat_t 32 33 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesFile") 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 path := filepath.Join(dir, "some file") 39 40 if err := ioutil.WriteFile(path, []byte("some contents"), 0755); err != nil { 41 t.Fatal(err) 42 } 43 44 atime := time.Unix(125812851, 128518257) 45 mtime := time.Unix(257172893, 995216512) 46 47 if err := unix.Lstat(path, &fiOld); err != nil { 48 t.Fatal(err) 49 } 50 51 if err := Lutimes(path, atime, mtime); err != nil { 52 t.Errorf("unexpected error with system.lutimes: %s", err) 53 } 54 55 if err := unix.Lstat(path, &fiNew); err != nil { 56 t.Fatal(err) 57 } 58 59 atimeOld := time.Unix(fiOld.Atim.Unix()) 60 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 61 atimeNew := time.Unix(fiNew.Atim.Unix()) 62 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 63 64 if atimeOld.Equal(atimeNew) { 65 t.Errorf("atime was not changed at all!") 66 } 67 if !atimeNew.Equal(atime) { 68 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 69 } 70 if mtimeOld.Equal(mtimeNew) { 71 t.Errorf("mtime was not changed at all!") 72 } 73 if !mtimeNew.Equal(mtime) { 74 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 75 } 76 } 77 78 func TestLutimesDirectory(t *testing.T) { 79 var fiOld, fiNew unix.Stat_t 80 81 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesDirectory") 82 if err != nil { 83 t.Fatal(err) 84 } 85 86 path := filepath.Join(dir, " a directory ") 87 88 if err := os.Mkdir(path, 0755); err != nil { 89 t.Fatal(err) 90 } 91 92 atime := time.Unix(128551231, 273285257) 93 mtime := time.Unix(185726393, 752135712) 94 95 if err := unix.Lstat(path, &fiOld); err != nil { 96 t.Fatal(err) 97 } 98 99 if err := Lutimes(path, atime, mtime); err != nil { 100 t.Errorf("unexpected error with system.lutimes: %s", err) 101 } 102 103 if err := unix.Lstat(path, &fiNew); err != nil { 104 t.Fatal(err) 105 } 106 107 atimeOld := time.Unix(fiOld.Atim.Unix()) 108 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 109 atimeNew := time.Unix(fiNew.Atim.Unix()) 110 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 111 112 if atimeOld.Equal(atimeNew) { 113 t.Errorf("atime was not changed at all!") 114 } 115 if !atimeNew.Equal(atime) { 116 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 117 } 118 if mtimeOld.Equal(mtimeNew) { 119 t.Errorf("mtime was not changed at all!") 120 } 121 if !mtimeNew.Equal(mtime) { 122 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 123 } 124 } 125 126 func TestLutimesSymlink(t *testing.T) { 127 var fiOld, fiParentOld, fiNew, fiParentNew unix.Stat_t 128 129 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesSymlink") 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 path := filepath.Join(dir, " !! symlink here") 135 136 if err := os.Symlink(".", path); err != nil { 137 t.Fatal(err) 138 } 139 140 atime := time.Unix(128551231, 273285257) 141 mtime := time.Unix(185726393, 752135712) 142 143 if err := unix.Lstat(path, &fiOld); err != nil { 144 t.Fatal(err) 145 } 146 if err := unix.Lstat(dir, &fiParentOld); err != nil { 147 t.Fatal(err) 148 } 149 150 if err := Lutimes(path, atime, mtime); err != nil { 151 t.Errorf("unexpected error with system.lutimes: %s", err) 152 } 153 154 if err := unix.Lstat(path, &fiNew); err != nil { 155 t.Fatal(err) 156 } 157 if err := unix.Lstat(dir, &fiParentNew); err != nil { 158 t.Fatal(err) 159 } 160 161 atimeOld := time.Unix(fiOld.Atim.Unix()) 162 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 163 atimeNew := time.Unix(fiNew.Atim.Unix()) 164 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 165 166 if atimeOld.Equal(atimeNew) { 167 t.Errorf("atime was not changed at all!") 168 } 169 if !atimeNew.Equal(atime) { 170 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 171 } 172 if mtimeOld.Equal(mtimeNew) { 173 t.Errorf("mtime was not changed at all!") 174 } 175 if !mtimeNew.Equal(mtime) { 176 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 177 } 178 179 // Make sure that the parent directory was unchanged. 180 atimeParentOld := time.Unix(fiParentOld.Atim.Unix()) 181 mtimeParentOld := time.Unix(fiParentOld.Mtim.Unix()) 182 atimeParentNew := time.Unix(fiParentNew.Atim.Unix()) 183 mtimeParentNew := time.Unix(fiParentNew.Mtim.Unix()) 184 185 if !atimeParentOld.Equal(atimeParentNew) { 186 t.Errorf("parent directory atime was changed! old='%s' new='%s'", atimeParentOld, atimeParentNew) 187 } 188 if !mtimeParentOld.Equal(mtimeParentNew) { 189 t.Errorf("parent directory mtime was changed! old='%s' new='%s'", mtimeParentOld, mtimeParentNew) 190 } 191 } 192 193 func TestLutimesRelative(t *testing.T) { 194 var fiOld, fiParentOld, fiNew, fiParentNew unix.Stat_t 195 196 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesRelative") 197 if err != nil { 198 t.Fatal(err) 199 } 200 defer os.RemoveAll(dir) 201 202 oldwd, err := os.Getwd() 203 if err != nil { 204 t.Fatal(err) 205 } 206 os.Chdir(dir) 207 defer os.Chdir(oldwd) 208 209 path := filepath.Join("some parent", " !! symlink here") 210 211 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { 212 t.Fatal(err) 213 } 214 if err := os.Symlink(".", path); err != nil { 215 t.Fatal(err) 216 } 217 218 atime := time.Unix(134858232, 258921237) 219 mtime := time.Unix(171257291, 425815288) 220 221 if err := unix.Lstat(path, &fiOld); err != nil { 222 t.Fatal(err) 223 } 224 if err := unix.Lstat(".", &fiParentOld); err != nil { 225 t.Fatal(err) 226 } 227 228 if err := Lutimes(path, atime, mtime); err != nil { 229 t.Errorf("unexpected error with system.lutimes: %s", err) 230 } 231 232 if err := unix.Lstat(path, &fiNew); err != nil { 233 t.Fatal(err) 234 } 235 if err := unix.Lstat(".", &fiParentNew); err != nil { 236 t.Fatal(err) 237 } 238 239 atimeOld := time.Unix(fiOld.Atim.Unix()) 240 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 241 atimeNew := time.Unix(fiNew.Atim.Unix()) 242 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 243 244 if atimeOld.Equal(atimeNew) { 245 t.Errorf("atime was not changed at all!") 246 } 247 if !atimeNew.Equal(atime) { 248 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 249 } 250 if mtimeOld.Equal(mtimeNew) { 251 t.Errorf("mtime was not changed at all!") 252 } 253 if !mtimeNew.Equal(mtime) { 254 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 255 } 256 257 // Make sure that the parent directory was unchanged. 258 atimeParentOld := time.Unix(fiParentOld.Atim.Unix()) 259 mtimeParentOld := time.Unix(fiParentOld.Mtim.Unix()) 260 atimeParentNew := time.Unix(fiParentNew.Atim.Unix()) 261 mtimeParentNew := time.Unix(fiParentNew.Mtim.Unix()) 262 263 if !atimeParentOld.Equal(atimeParentNew) { 264 t.Errorf("parent directory atime was changed! old='%s' new='%s'", atimeParentOld, atimeParentNew) 265 } 266 if !mtimeParentOld.Equal(mtimeParentNew) { 267 t.Errorf("parent directory mtime was changed! old='%s' new='%s'", mtimeParentOld, mtimeParentNew) 268 } 269 }