github.com/chentex/umoci@v0.4.3-0.20180919062622-ef73f30ff150/pkg/system/utime_linux_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 system 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 defer os.RemoveAll(dir) 38 39 path := filepath.Join(dir, "some file") 40 41 if err := ioutil.WriteFile(path, []byte("some contents"), 0755); err != nil { 42 t.Fatal(err) 43 } 44 45 atime := time.Unix(125812851, 128518257) 46 mtime := time.Unix(257172893, 995216512) 47 48 if err := unix.Lstat(path, &fiOld); err != nil { 49 t.Fatal(err) 50 } 51 52 if err := Lutimes(path, atime, mtime); err != nil { 53 t.Errorf("unexpected error with system.lutimes: %s", err) 54 } 55 56 if err := unix.Lstat(path, &fiNew); err != nil { 57 t.Fatal(err) 58 } 59 60 atimeOld := time.Unix(fiOld.Atim.Unix()) 61 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 62 atimeNew := time.Unix(fiNew.Atim.Unix()) 63 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 64 65 if atimeOld.Equal(atimeNew) { 66 t.Errorf("atime was not changed at all!") 67 } 68 if !atimeNew.Equal(atime) { 69 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 70 } 71 if mtimeOld.Equal(mtimeNew) { 72 t.Errorf("mtime was not changed at all!") 73 } 74 if !mtimeNew.Equal(mtime) { 75 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 76 } 77 } 78 79 func TestLutimesDirectory(t *testing.T) { 80 var fiOld, fiNew unix.Stat_t 81 82 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesDirectory") 83 if err != nil { 84 t.Fatal(err) 85 } 86 defer os.RemoveAll(dir) 87 88 path := filepath.Join(dir, " a directory ") 89 90 if err := os.Mkdir(path, 0755); err != nil { 91 t.Fatal(err) 92 } 93 94 atime := time.Unix(128551231, 273285257) 95 mtime := time.Unix(185726393, 752135712) 96 97 if err := unix.Lstat(path, &fiOld); err != nil { 98 t.Fatal(err) 99 } 100 101 if err := Lutimes(path, atime, mtime); err != nil { 102 t.Errorf("unexpected error with system.lutimes: %s", err) 103 } 104 105 if err := unix.Lstat(path, &fiNew); err != nil { 106 t.Fatal(err) 107 } 108 109 atimeOld := time.Unix(fiOld.Atim.Unix()) 110 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 111 atimeNew := time.Unix(fiNew.Atim.Unix()) 112 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 113 114 if atimeOld.Equal(atimeNew) { 115 t.Errorf("atime was not changed at all!") 116 } 117 if !atimeNew.Equal(atime) { 118 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 119 } 120 if mtimeOld.Equal(mtimeNew) { 121 t.Errorf("mtime was not changed at all!") 122 } 123 if !mtimeNew.Equal(mtime) { 124 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 125 } 126 } 127 128 func TestLutimesSymlink(t *testing.T) { 129 var fiOld, fiParentOld, fiNew, fiParentNew unix.Stat_t 130 131 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesSymlink") 132 if err != nil { 133 t.Fatal(err) 134 } 135 defer os.RemoveAll(dir) 136 137 path := filepath.Join(dir, " a symlink ") 138 139 if err := os.Symlink(".", path); err != nil { 140 t.Fatal(err) 141 } 142 143 atime := time.Unix(128551231, 273285257) 144 mtime := time.Unix(185726393, 752135712) 145 146 if err := unix.Lstat(path, &fiOld); err != nil { 147 t.Fatal(err) 148 } 149 if err := unix.Lstat(dir, &fiParentOld); err != nil { 150 t.Fatal(err) 151 } 152 153 if err := Lutimes(path, atime, mtime); err != nil { 154 t.Errorf("unexpected error with system.lutimes: %s", err) 155 } 156 157 if err := unix.Lstat(path, &fiNew); err != nil { 158 t.Fatal(err) 159 } 160 if err := unix.Lstat(dir, &fiParentNew); err != nil { 161 t.Fatal(err) 162 } 163 164 atimeOld := time.Unix(fiOld.Atim.Unix()) 165 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 166 atimeNew := time.Unix(fiNew.Atim.Unix()) 167 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 168 169 if atimeOld.Equal(atimeNew) { 170 t.Errorf("atime was not changed at all!") 171 } 172 if !atimeNew.Equal(atime) { 173 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 174 } 175 if mtimeOld.Equal(mtimeNew) { 176 t.Errorf("mtime was not changed at all!") 177 } 178 if !mtimeNew.Equal(mtime) { 179 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 180 } 181 182 // Make sure that the parent directory was unchanged. 183 atimeParentOld := time.Unix(fiParentOld.Atim.Unix()) 184 mtimeParentOld := time.Unix(fiParentOld.Mtim.Unix()) 185 atimeParentNew := time.Unix(fiParentNew.Atim.Unix()) 186 mtimeParentNew := time.Unix(fiParentNew.Mtim.Unix()) 187 188 if !atimeParentOld.Equal(atimeParentNew) { 189 t.Errorf("parent directory atime was changed! old='%s' new='%s'", atimeParentOld, atimeParentNew) 190 } 191 if !mtimeParentOld.Equal(mtimeParentNew) { 192 t.Errorf("parent directory mtime was changed! old='%s' new='%s'", mtimeParentOld, mtimeParentNew) 193 } 194 } 195 196 func TestLutimesRelative(t *testing.T) { 197 var fiOld, fiParentOld, fiNew, fiParentNew unix.Stat_t 198 199 dir, err := ioutil.TempDir("", "umoci-system.TestLutimesRelative") 200 if err != nil { 201 t.Fatal(err) 202 } 203 defer os.RemoveAll(dir) 204 205 oldwd, err := os.Getwd() 206 if err != nil { 207 t.Fatal(err) 208 } 209 os.Chdir(dir) 210 defer os.Chdir(oldwd) 211 212 path := filepath.Join("some parent", " !! symlink here") 213 214 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { 215 t.Fatal(err) 216 } 217 if err := os.Symlink(".", path); err != nil { 218 t.Fatal(err) 219 } 220 221 atime := time.Unix(134858232, 258921237) 222 mtime := time.Unix(171257291, 425815288) 223 224 if err := unix.Lstat(path, &fiOld); err != nil { 225 t.Fatal(err) 226 } 227 if err := unix.Lstat(".", &fiParentOld); err != nil { 228 t.Fatal(err) 229 } 230 231 if err := Lutimes(path, atime, mtime); err != nil { 232 t.Errorf("unexpected error with system.lutimes: %s", err) 233 } 234 235 if err := unix.Lstat(path, &fiNew); err != nil { 236 t.Fatal(err) 237 } 238 if err := unix.Lstat(".", &fiParentNew); err != nil { 239 t.Fatal(err) 240 } 241 242 atimeOld := time.Unix(fiOld.Atim.Unix()) 243 mtimeOld := time.Unix(fiOld.Mtim.Unix()) 244 atimeNew := time.Unix(fiNew.Atim.Unix()) 245 mtimeNew := time.Unix(fiNew.Mtim.Unix()) 246 247 if atimeOld.Equal(atimeNew) { 248 t.Errorf("atime was not changed at all!") 249 } 250 if !atimeNew.Equal(atime) { 251 t.Errorf("atime was not changed to expected value: expected='%s' got='%s' old='%s'", atime, atimeNew, atimeOld) 252 } 253 if mtimeOld.Equal(mtimeNew) { 254 t.Errorf("mtime was not changed at all!") 255 } 256 if !mtimeNew.Equal(mtime) { 257 t.Errorf("mtime was not changed: expected='%s' got='%s' old='%s'", mtime, mtimeNew, mtimeOld) 258 } 259 260 // Make sure that the parent directory was unchanged. 261 atimeParentOld := time.Unix(fiParentOld.Atim.Unix()) 262 mtimeParentOld := time.Unix(fiParentOld.Mtim.Unix()) 263 atimeParentNew := time.Unix(fiParentNew.Atim.Unix()) 264 mtimeParentNew := time.Unix(fiParentNew.Mtim.Unix()) 265 266 if !atimeParentOld.Equal(atimeParentNew) { 267 t.Errorf("parent directory atime was changed! old='%s' new='%s'", atimeParentOld, atimeParentNew) 268 } 269 if !mtimeParentOld.Equal(mtimeParentNew) { 270 t.Errorf("parent directory mtime was changed! old='%s' new='%s'", mtimeParentOld, mtimeParentNew) 271 } 272 }