forked from linuxdeepin/startdde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyfile_test.go
101 lines (94 loc) · 2.22 KB
/
copyfile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"fmt"
"os"
"testing"
)
func testCopyFileInit() {
err := os.Remove("/tmp/test")
if err != nil {
logger.Warning(err)
}
err = os.Remove("/tmp/test2")
if err != nil {
logger.Warning(err)
}
err = os.Remove("/tmp/ls")
if err != nil {
logger.Warning(err)
}
err = os.Symlink("/bin/ls", "/tmp/test")
if err != nil {
logger.Warning(err)
}
}
func TestCopyFileNotKeepSymlink(t *testing.T) {
testCopyFileInit()
if err := copyFile("/bin/ls", "/tmp/ls", CopyFileNotKeepSymlink); err != nil {
t.Fatal(err)
} else {
s, err := os.Stat("/tmp/ls")
if (s.Mode() & os.ModeSymlink) != os.ModeSymlink {
t.Log("pass copy a file with CopyFileNotKeepSymlink")
} else {
t.Error(err)
}
}
if err := copyFile("/tmp/test", "/tmp/test2", CopyFileNotKeepSymlink); err != nil {
t.Fatal(err)
} else {
s, err := os.Stat("/tmp/test2")
if err != nil && s.Mode().IsRegular() {
t.Fatal(err)
} else {
t.Log("pass copy a symlink with CopyFileNotKeepSymlink")
}
}
}
func TestCopyFileCopyFileNone(t *testing.T) {
testCopyFileInit()
err := os.Symlink("/bin/ls", "/tmp/ls")
if err != nil {
logger.Warning(err)
}
if err := copyFile("/tmp/test", "/tmp/ls", CopyFileNone); err != nil {
t.Log("pass CopyFileKeepNone")
} else {
t.Error("overwrite existed file, copy file none failed")
}
}
func TestCopyFileOverWrite(t *testing.T) {
testCopyFileInit()
if err := copyFile("/tmp/test", "/tmp/ls", CopyFileOverWrite); err != nil {
t.Error("failed,", err)
} else {
s, err := os.Lstat("/tmp/ls")
if err != nil {
t.Error("failed,", err)
} else {
if (s.Mode() & os.ModeSymlink) == os.ModeSymlink {
t.Log("pass CopyFileOverWrite")
} else {
t.Error("failed, dst is not symlink")
}
}
}
if err := copyFile("/tmp/test", "/tmp/ls",
CopyFileNotKeepSymlink|CopyFileOverWrite); err != nil {
fmt.Println(err)
} else {
s, err := os.Stat("/tmp/ls")
if err != nil {
t.Error("failed,", err)
} else {
if s.Mode().IsRegular() {
t.Log("pass CopyFileNotKeepSymlink|CopyFileOverWrite")
} else {
t.Error("failed, dst is not a regular file")
}
}
}
}