Files
tailscale/feature/taildrop/send_test.go
Mike Jensen 4ad3c29228 feature/taildrop: remove Close from FileOps interface (#22792)
In PR #22749 we attempteed to clear up the casting needed for the fileOps for the close action by using the io.Closer interface. This broke the dependency to our android repo.

This change both removes this interface change that breaks compatability, and also removes the closeOps field. After further review this field was no longer necessary after we removed the persistent root behavior.

Updates tailscale/corp#51118

Change-Id: I1870030f130e92950843afb8c306b32f2008fca4

Signed-off-by: Mike Jensen <mikej@tailscale.com>
2026-12-01 10:46:25 -07:00

105 lines
2.6 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package taildrop
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"tailscale.com/tstime"
"tailscale.com/util/must"
)
func TestPutFileSymlink(t *testing.T) {
t.Parallel()
for _, offset := range []int64{0, 2} {
t.Run(fmt.Sprintf("offset_%d", offset), func(t *testing.T) {
dir := t.TempDir()
outside := filepath.Join(t.TempDir(), "outside")
const original = "original"
if err := os.WriteFile(outside, []byte(original), 0o600); err != nil {
t.Fatal(err)
}
id := clientID("peer")
if err := os.Symlink(outside, filepath.Join(dir, "file.txt"+id.partialSuffix())); err != nil {
t.Skipf("cannot create symlinks: %v", err)
}
fops := must.Get(newFileOps(dir))
mgr := managerOptions{Logf: t.Logf, fileOps: fops, DirectFileMode: true}.New()
t.Cleanup(mgr.Shutdown)
if _, err := mgr.PutFile(id, "file.txt", strings.NewReader("payload"), offset, 7); err == nil {
t.Fatal("PutFile succeeded through a symlink outside the Taildrop root")
}
got, err := os.ReadFile(outside)
if err != nil {
t.Fatal(err)
}
if string(got) != original {
t.Errorf("outside file contents = %q; want %q", got, original)
}
})
}
}
func TestPutFile(t *testing.T) {
const content = "hello, world"
tests := []struct {
name string
directFileMode bool
}{
{"DirectFileMode", true},
{"NonDirectFileMode", false},
}
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()
t.Cleanup(mgr.Shutdown)
id := clientID("0")
n, err := mgr.PutFile(id, "file.txt", strings.NewReader(content), 0, int64(len(content)))
if err != nil {
t.Fatalf("PutFile error: %v", err)
}
if n != int64(len(content)) {
t.Errorf("wrote %d bytes; want %d", n, len(content))
}
path := filepath.Join(dir, "file.txt")
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())
}
}
})
}
}