|
| 1 | +// Copyright 2026 PingCAP, Inc. |
| 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 util |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "runtime" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestCreateFileAllowsRelativeDestinations(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + fileName string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "normal file name", |
| 31 | + fileName: "a.sql.gz", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "subdirectory", |
| 35 | + fileName: filepath.Join("folder", "a.sql.gz"), |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "nested subdirectory", |
| 39 | + fileName: filepath.Join("a", "b", "c.sql.gz"), |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + base := t.TempDir() |
| 46 | + parent := filepath.Dir(filepath.Join(base, tt.fileName)) |
| 47 | + if err := os.MkdirAll(parent, 0755); err != nil { |
| 48 | + t.Fatalf("MkdirAll() error = %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + file, err := CreateFile(base, tt.fileName) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("CreateFile() error = %v", err) |
| 54 | + } |
| 55 | + file.Close() |
| 56 | + |
| 57 | + if _, err := os.Stat(filepath.Join(base, tt.fileName)); err != nil { |
| 58 | + t.Fatalf("expected file inside base: %v", err) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestCreateFileRejectsUnsupportedDestinations(t *testing.T) { |
| 65 | + base := t.TempDir() |
| 66 | + |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + fileName string |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "path outside output path", |
| 73 | + fileName: filepath.Join("..", "..", "tmp", "pwned"), |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "absolute path", |
| 77 | + fileName: filepath.Join(string(os.PathSeparator), "tmp", "pwned"), |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "empty file name", |
| 81 | + fileName: "", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "current directory", |
| 85 | + fileName: ".", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "parent directory", |
| 89 | + fileName: "..", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "cleans to current directory", |
| 93 | + fileName: filepath.Join("a", ".."), |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tt := range tests { |
| 98 | + t.Run(tt.name, func(t *testing.T) { |
| 99 | + file, err := CreateFile(base, tt.fileName) |
| 100 | + if err == nil { |
| 101 | + file.Close() |
| 102 | + t.Fatalf("CreateFile() succeeded for %q", tt.fileName) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestCreateFileDoesNotOverwriteExistingDestination(t *testing.T) { |
| 109 | + base := t.TempDir() |
| 110 | + path := filepath.Join(base, "a.sql.gz") |
| 111 | + if err := os.WriteFile(path, []byte("existing"), 0644); err != nil { |
| 112 | + t.Fatalf("WriteFile() error = %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + file, err := CreateFile(base, "a.sql.gz") |
| 116 | + if err == nil { |
| 117 | + file.Close() |
| 118 | + t.Fatalf("CreateFile() succeeded for existing destination") |
| 119 | + } |
| 120 | + |
| 121 | + content, err := os.ReadFile(path) |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("ReadFile() error = %v", err) |
| 124 | + } |
| 125 | + if string(content) != "existing" { |
| 126 | + t.Fatalf("existing destination was overwritten: %q", string(content)) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestCreateFileUsesCurrentDirectoryWhenBaseIsEmpty(t *testing.T) { |
| 131 | + wd, err := os.Getwd() |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("Getwd() error = %v", err) |
| 134 | + } |
| 135 | + base := t.TempDir() |
| 136 | + if err := os.Chdir(base); err != nil { |
| 137 | + t.Fatalf("Chdir() error = %v", err) |
| 138 | + } |
| 139 | + t.Cleanup(func() { |
| 140 | + if err := os.Chdir(wd); err != nil { |
| 141 | + t.Fatalf("restore working directory: %v", err) |
| 142 | + } |
| 143 | + }) |
| 144 | + |
| 145 | + file, err := CreateFile("", "a.sql.gz") |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("CreateFile() error = %v", err) |
| 148 | + } |
| 149 | + file.Close() |
| 150 | + |
| 151 | + if _, err := os.Stat(filepath.Join(base, "a.sql.gz")); err != nil { |
| 152 | + t.Fatalf("expected file in current directory: %v", err) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestCreateFileRejectsSymlinkedParentOutsideBase(t *testing.T) { |
| 157 | + if runtime.GOOS == "windows" { |
| 158 | + t.Skip("symlink behavior requires additional privileges on Windows") |
| 159 | + } |
| 160 | + |
| 161 | + base := t.TempDir() |
| 162 | + outside := t.TempDir() |
| 163 | + if err := os.Symlink(outside, filepath.Join(base, "link")); err != nil { |
| 164 | + t.Fatalf("Symlink() error = %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + file, err := CreateFile(base, filepath.Join("link", "export.sql.gz")) |
| 168 | + if err == nil { |
| 169 | + file.Close() |
| 170 | + t.Fatalf("CreateFile() succeeded through symlinked parent") |
| 171 | + } |
| 172 | + if _, statErr := os.Stat(filepath.Join(outside, "export.sql.gz")); !os.IsNotExist(statErr) { |
| 173 | + t.Fatalf("expected no file outside base, stat error = %v", statErr) |
| 174 | + } |
| 175 | +} |
0 commit comments