2016-09-17 20:34:50 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-09-17 20:34:50 +00:00
|
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2022-07-28 19:36:39 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2016-09-17 20:34:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExternalNoCommand(t *testing.T) {
|
|
|
|
|
file := "testdata/folder path/long filename.txt"
|
|
|
|
|
prepForRemoval(t, file)
|
|
|
|
|
defer os.RemoveAll("testdata")
|
|
|
|
|
|
|
|
|
|
// The file should exist before the versioner run.
|
|
|
|
|
|
|
|
|
|
if _, err := os.Lstat(file); err != nil {
|
|
|
|
|
t.Fatal("File should exist")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The versioner should fail due to missing command.
|
|
|
|
|
|
2019-11-26 08:39:31 +01:00
|
|
|
e := external{
|
2017-08-19 14:36:56 +00:00
|
|
|
filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
|
2017-02-25 08:12:13 +00:00
|
|
|
command: "nonexistent command",
|
2016-09-17 20:34:50 +00:00
|
|
|
}
|
|
|
|
|
if err := e.Archive(file); err == nil {
|
|
|
|
|
t.Error("Command should have failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The file should not have been removed.
|
|
|
|
|
|
|
|
|
|
if _, err := os.Lstat(file); err != nil {
|
|
|
|
|
t.Fatal("File should still exist")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExternal(t *testing.T) {
|
2017-08-19 14:36:56 +00:00
|
|
|
cmd := "./_external_test/external.sh %FOLDER_PATH% %FILE_PATH%"
|
2022-07-28 19:36:39 +02:00
|
|
|
if build.IsWindows {
|
2017-08-19 14:36:56 +00:00
|
|
|
cmd = `.\\_external_test\\external.bat %FOLDER_PATH% %FILE_PATH%`
|
2016-09-17 20:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
file := filepath.Join("testdata", "folder path", "dir (parens)", "/long filename (parens).txt")
|
2016-09-17 20:34:50 +00:00
|
|
|
prepForRemoval(t, file)
|
|
|
|
|
defer os.RemoveAll("testdata")
|
|
|
|
|
|
|
|
|
|
// The file should exist before the versioner run.
|
|
|
|
|
|
|
|
|
|
if _, err := os.Lstat(file); err != nil {
|
|
|
|
|
t.Fatal("File should exist")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The versioner should run successfully.
|
|
|
|
|
|
2019-11-26 08:39:31 +01:00
|
|
|
e := external{
|
2017-08-19 14:36:56 +00:00
|
|
|
filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
|
2016-09-17 20:34:50 +00:00
|
|
|
command: cmd,
|
|
|
|
|
}
|
|
|
|
|
if err := e.Archive(file); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The file should no longer exist.
|
|
|
|
|
|
|
|
|
|
if _, err := os.Lstat(file); !os.IsNotExist(err) {
|
|
|
|
|
t.Error("File should no longer exist")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 07:48:53 +02:00
|
|
|
func TestExternalCommandSplit(t *testing.T) {
|
|
|
|
|
e := external{
|
|
|
|
|
filesystem: fs.NewFilesystem(fs.FilesystemTypeFake, "TestExternalCommandSplit"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
|
cmd string
|
|
|
|
|
safe bool
|
|
|
|
|
}{
|
|
|
|
|
{`echo %FOLDER_PATH% %FILE_PATH%`, true},
|
|
|
|
|
{`echo "%FOLDER_PATH% %FILE_PATH%"`, false},
|
|
|
|
|
{`echo %FOLDER_PATH%/%FILE_PATH%`, true},
|
|
|
|
|
{`echo "%FOLDER_PATH%/%FILE_PATH%"`, true},
|
|
|
|
|
{`echo '%FOLDER_PATH%/%FILE_PATH%'`, true},
|
|
|
|
|
{`echo "'%FOLDER_PATH%/%FILE_PATH%'"`, false},
|
|
|
|
|
{`sh -c "echo '%FOLDER_PATH%/%FILE_PATH%'"`, false},
|
|
|
|
|
{`sh -c "echo %FOLDER_PATH%/%FILE_PATH%"`, false},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
|
e.command = tc.cmd
|
|
|
|
|
res, err := e.prepareCommand("evil file name")
|
|
|
|
|
if tc.safe && err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !tc.safe && err == nil {
|
|
|
|
|
t.Logf("%q", res.Path)
|
|
|
|
|
t.Logf("%q", res.Args)
|
|
|
|
|
t.Errorf("should be unsafe: %q", tc.cmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 20:34:50 +00:00
|
|
|
func prepForRemoval(t *testing.T, file string) {
|
|
|
|
|
if err := os.RemoveAll("testdata"); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 11:32:56 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil {
|
2016-09-17 20:34:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2024-11-19 11:32:56 +01:00
|
|
|
if err := os.WriteFile(file, []byte("hello\n"), 0o644); err != nil {
|
2016-09-17 20:34:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|