Spaces:
Running
Running
File size: 5,151 Bytes
7c5da52 | 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | package main
import (
"os"
"path/filepath"
"testing"
)
func TestCleanRemotePath(t *testing.T) {
cases := map[string]string{
"file.txt": "file.txt",
"folder\\nested\\file.txt": "folder/nested/file.txt",
"folder/./file.txt": "folder/file.txt",
}
for input, want := range cases {
got, err := cleanRemotePath(input)
if err != nil || got != want {
t.Fatalf("cleanRemotePath(%q) = %q, %v; want %q", input, got, err, want)
}
}
}
func TestCleanRemotePathRejectsTraversal(t *testing.T) {
for _, input := range []string{"", "../secret", "../../x", "/etc/passwd", "..\\secret"} {
if _, err := cleanRemotePath(input); err == nil {
t.Errorf("cleanRemotePath(%q) should fail", input)
}
}
}
func TestLoadDotEnv(t *testing.T) {
tmp := filepath.Join(t.TempDir(), ".env")
content := "# comment\nTEST_DOTENV_KEY=hello_world\nTEST_QUOTED=\"quoted_val\"\n"
if err := os.WriteFile(tmp, []byte(content), 0600); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
os.Unsetenv("TEST_DOTENV_KEY")
os.Unsetenv("TEST_QUOTED")
})
loadDotEnv(tmp)
if got := os.Getenv("TEST_DOTENV_KEY"); got != "hello_world" {
t.Fatalf("TEST_DOTENV_KEY = %q; want %q", got, "hello_world")
}
if got := os.Getenv("TEST_QUOTED"); got != "quoted_val" {
t.Fatalf("TEST_QUOTED = %q; want %q", got, "quoted_val")
}
}
func TestParseBucketLsLine(t *testing.T) {
line := " 48999197 2026-09-15 06:37:25 folder/111.mp4"
item, ok := parseBucketLsLine(line)
if !ok {
t.Fatal("expected ok=true")
}
if item.Path != "folder/111.mp4" || item.Size != 48999197 || item.UpdatedAt != "2026-09-15 06:37:25" {
t.Fatalf("unexpected parsed item: %+v", item)
}
}
func TestUserStoreAndRBAC(t *testing.T) {
storePath := filepath.Join(t.TempDir(), "users.json")
store, err := NewUserStore(storePath, "test-secret-key")
if err != nil {
t.Fatalf("NewUserStore failed: %v", err)
}
// Default admin/admin should exist
adminUser, ok := store.Authenticate("admin", "admin")
if !ok || adminUser.Role != "admin" {
t.Fatalf("expected default admin/admin to authenticate, got ok=%v, role=%s", ok, adminUser.Role)
}
// Issue & verify token
token, err := store.IssueToken("admin", 10*60*1e9)
if err != nil {
t.Fatalf("IssueToken failed: %v", err)
}
verified, ok := store.VerifyToken(token)
if !ok || verified.Username != "admin" {
t.Fatalf("VerifyToken failed: ok=%v, user=%+v", ok, verified)
}
}
func TestFileOwnerIsolation(t *testing.T) {
ownerFile := filepath.Join(t.TempDir(), "file_owners.json")
owners, err := NewFileOwnerStore(ownerFile)
if err != nil {
t.Fatalf("NewFileOwnerStore failed: %v", err)
}
adminAcc := UserAccount{Username: "admin", Role: "admin"}
userAcc := UserAccount{Username: "user", Role: "user"}
otherAcc := UserAccount{Username: "alice", Role: "user"}
// Legacy unrecorded file defaults to "admin"
if !owners.CanAccess(adminAcc, "111.mp4") {
t.Fatal("admin should access legacy file 111.mp4")
}
if owners.CanAccess(userAcc, "111.mp4") {
t.Fatal("regular user should NOT access legacy admin file 111.mp4")
}
// User uploads a file
owners.SetOwner("demo/user_video.mp4", "user")
if !owners.CanAccess(userAcc, "demo/user_video.mp4") {
t.Fatal("user should access their own uploaded file")
}
if !owners.CanAccess(adminAcc, "demo/user_video.mp4") {
t.Fatal("admin should access all files including user's uploaded file")
}
if owners.CanAccess(otherAcc, "demo/user_video.mp4") {
t.Fatal("other user (alice) should NOT access user's uploaded file")
}
// Rename preserves ownership
owners.Rename("demo/user_video.mp4", "demo/renamed_video.mp4")
if !owners.CanAccess(userAcc, "demo/renamed_video.mp4") {
t.Fatal("user should still own renamed file")
}
// Two users uploading the same filename/content never lose their file
p1 := scopeUserRemotePath("user", "same_file.mp4")
p2 := scopeUserRemotePath("alice", "same_file.mp4")
if p1 == p2 {
t.Fatalf("expected distinct scoped paths for different users, got %s == %s", p1, p2)
}
owners.SetOwner(p1, "user")
owners.SetOwner(p2, "alice")
if !owners.CanAccess(userAcc, p1) || !owners.CanAccess(otherAcc, p2) {
t.Fatal("both users must retain access to their respective uploaded file")
}
// Even on a legacy shared path, SetOwner appends rather than overwrites
owners.SetOwner("shared.pptx", "user")
owners.SetOwner("shared.pptx", "alice")
if !owners.CanAccess(userAcc, "shared.pptx") || !owners.CanAccess(otherAcc, "shared.pptx") {
t.Fatal("both users should retain access when uploading to a shared legacy path")
}
// Same user uploading same filename with different content gets auto-numbered path " (1)", " (2)"
existing := map[string]bool{"user/report.pptx": true}
second := nextAvailableRemotePath("user/report.pptx", func(c string) bool { return existing[c] })
if second != "user/report (1).pptx" {
t.Fatalf("expected user/report (1).pptx, got %s", second)
}
existing[second] = true
third := nextAvailableRemotePath("user/report.pptx", func(c string) bool { return existing[c] })
if third != "user/report (2).pptx" {
t.Fatalf("expected user/report (2).pptx, got %s", third)
}
}
|