github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/osutil/osutil_test.go (about) 1 // Copyright 2017 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package osutil 5 6 import ( 7 "fmt" 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestIsExist(t *testing.T) { 18 if f := os.Args[0]; !IsExist(f) { 19 t.Fatalf("executable %v does not exist", f) 20 } 21 if f := os.Args[0] + "-foo-bar-buz"; IsExist(f) { 22 t.Fatalf("file %v exists", f) 23 } 24 } 25 26 func TestCopyFiles(t *testing.T) { 27 type Test struct { 28 files []string 29 patterns map[string]bool 30 err string 31 } 32 tests := []Test{ 33 { 34 files: []string{ 35 "foo", 36 "bar", 37 "baz/foo", 38 "baz/bar", 39 }, 40 patterns: map[string]bool{ 41 "foo": true, 42 "bar": false, 43 "qux": false, 44 "baz/foo": true, 45 "baz/bar": false, 46 }, 47 }, 48 { 49 files: []string{ 50 "foo", 51 }, 52 patterns: map[string]bool{ 53 "bar": true, 54 }, 55 err: "file bar does not exist", 56 }, 57 { 58 files: []string{ 59 "baz/foo", 60 "baz/bar", 61 }, 62 patterns: map[string]bool{ 63 "baz/*": true, 64 }, 65 }, 66 { 67 files: []string{ 68 "qux/foo/foo", 69 "qux/foo/bar", 70 "qux/bar/foo", 71 "qux/bar/bar", 72 }, 73 patterns: map[string]bool{ 74 "qux/*/*": false, 75 }, 76 }, 77 } 78 for _, link := range []bool{false, true} { 79 fn, fnName := CopyFiles, "CopyFiles" 80 if link { 81 fn, fnName = LinkFiles, "LinkFiles" 82 } 83 t.Run(fnName, func(t *testing.T) { 84 for i, test := range tests { 85 t.Run(fmt.Sprint(i), func(t *testing.T) { 86 dir := t.TempDir() 87 src := filepath.Join(dir, "src") 88 dst := filepath.Join(dir, "dst") 89 for _, file := range test.files { 90 file = filepath.Join(src, filepath.FromSlash(file)) 91 if err := MkdirAll(filepath.Dir(file)); err != nil { 92 t.Fatal(err) 93 } 94 if err := WriteFile(file, []byte{'a'}); err != nil { 95 t.Fatal(err) 96 } 97 } 98 if err := fn(src, dst, test.patterns); err != nil { 99 if test.err != "" { 100 if strings.Contains(err.Error(), test.err) { 101 return 102 } 103 t.Fatalf("got err %q, want %q", err, test.err) 104 } 105 t.Fatal(err) 106 } else if test.err != "" { 107 t.Fatalf("got no err, want %q", test.err) 108 } 109 if err := os.RemoveAll(src); err != nil { 110 t.Fatal(err) 111 } 112 for _, file := range test.files { 113 if !IsExist(filepath.Join(dst, filepath.FromSlash(file))) { 114 t.Fatalf("%v does not exist in dst", file) 115 } 116 } 117 if !FilesExist(dst, test.patterns) { 118 t.Fatalf("dst files don't exist after copy") 119 } 120 }) 121 } 122 }) 123 } 124 } 125 126 func TestMonotonicNano(t *testing.T) { 127 start := MonotonicNano() 128 time.Sleep(100 * time.Millisecond) 129 diff := MonotonicNano() - start 130 if diff <= 0 || diff > 10*time.Second { 131 t.Fatalf("diff %v", diff) 132 } 133 } 134 135 func TestReadWriteJSON(t *testing.T) { 136 type Test struct { 137 X int 138 Y string 139 } 140 test := Test{10, "foo"} 141 file := filepath.Join(t.TempDir(), "file") 142 if err := WriteJSON(file, test); err != nil { 143 t.Fatal(err) 144 } 145 test2, err := ReadJSON[Test](file) 146 if err != nil { 147 t.Fatal(err) 148 } 149 if diff := cmp.Diff(test, test2); diff != "" { 150 t.Fatal(diff) 151 } 152 }