2025-05-20 15:30:19 -07:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package taildrop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2025-08-01 15:10:00 -07:00
|
|
|
"strings"
|
2025-05-20 15:30:19 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"tailscale.com/tstime"
|
2025-08-01 15:10:00 -07:00
|
|
|
"tailscale.com/util/must"
|
2025-05-20 15:30:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPutFile(t *testing.T) {
|
|
|
|
const content = "hello, world"
|
|
|
|
|
|
|
|
tests := []struct {
|
2025-08-01 15:10:00 -07:00
|
|
|
name string
|
|
|
|
directFileMode bool
|
2025-05-20 15:30:19 -07:00
|
|
|
}{
|
2025-08-01 15:10:00 -07:00
|
|
|
{"DirectFileMode", true},
|
|
|
|
{"NonDirectFileMode", false},
|
2025-05-20 15:30:19 -07:00
|
|
|
}
|
|
|
|
|
2025-08-01 15:10:00 -07:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
mgr := managerOptions{
|
|
|
|
Logf: t.Logf,
|
|
|
|
Clock: tstime.DefaultClock{},
|
|
|
|
State: nil,
|
|
|
|
fileOps: must.Get(newFileOps(dir)),
|
|
|
|
DirectFileMode: tt.directFileMode,
|
|
|
|
SendFileNotify: func() {},
|
|
|
|
}.New()
|
2025-05-20 15:30:19 -07:00
|
|
|
|
2025-08-01 15:10:00 -07:00
|
|
|
id := clientID("0")
|
|
|
|
n, err := mgr.PutFile(id, "file.txt", strings.NewReader(content), 0, int64(len(content)))
|
2025-05-20 15:30:19 -07:00
|
|
|
if err != nil {
|
2025-08-01 15:10:00 -07:00
|
|
|
t.Fatalf("PutFile error: %v", err)
|
2025-05-20 15:30:19 -07:00
|
|
|
}
|
|
|
|
if n != int64(len(content)) {
|
|
|
|
t.Errorf("wrote %d bytes; want %d", n, len(content))
|
|
|
|
}
|
|
|
|
|
2025-08-01 15:10:00 -07:00
|
|
|
path := filepath.Join(dir, "file.txt")
|
2025-05-20 15:30:19 -07:00
|
|
|
|
2025-08-01 15:10:00 -07:00
|
|
|
got, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadFile %q: %v", path, err)
|
|
|
|
}
|
|
|
|
if string(got) != content {
|
|
|
|
t.Errorf("file contents = %q; want %q", string(got), content)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
if strings.Contains(entry.Name(), ".partial") {
|
|
|
|
t.Errorf("unexpected partial file left behind: %s", entry.Name())
|
2025-05-20 15:30:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|