package storage import ( "errors" "os" "path/filepath" "strings" "testing" ) func newSpaces(t *testing.T) *Spaces { t.Helper() return NewSpaces(newStore(t)) } func space(t *testing.T, sp *Spaces, id string, admin bool) *Space { t.Helper() u, err := sp.For(id, admin) if err != nil { t.Fatal(err) } return u } func uput(t *testing.T, u *Space, p, content string) Entry { t.Helper() e, err := u.Put(p, strings.NewReader(content), int64(len(content)), PutOptions{}) if err != nil { t.Fatalf("Put(%s) : %v", p, err) } return e } func TestPersonalSpaceIsPrivate(t *testing.T) { sp := newSpaces(t) alice, bob := space(t, sp, "alice", false), space(t, sp, "bob", false) uput(t, alice, ".perso/secret.txt", "S") uput(t, alice, "commun.txt", "C") if es, _ := bob.List(".perso"); len(es) != 0 { t.Errorf("bob voit l'espace d'alice : %v", es) } if es, _ := alice.List(".perso"); len(es) != 1 || es[0].Path != ".perso/secret.txt" { t.Errorf("alice : %v", es) } root, _ := bob.List("") var specials []string for _, e := range root { if e.Special != "" { specials = append(specials, e.Special) } } if len(specials) != 3 { t.Errorf("dossiers virtuels = %v", specials) } if hits, _ := bob.Search("secret", Filter{}, 10); len(hits) != 0 { t.Errorf("recherche : %v", hits) } // Chemins qui tentent d'atteindre les espaces d'autrui. for _, p := range []string{".users/alice/home/secret.txt", "../.users/alice/home/secret.txt"} { if _, _, err := bob.Open(p); err == nil { t.Errorf("accès à %s", p) } } } func TestMoveBetweenSpacesAndFavorites(t *testing.T) { sp := newSpaces(t) alice := space(t, sp, "alice", false) uput(t, alice, "doc.pdf", "PDF") if err := alice.EditTags([]string{"doc.pdf"}, []string{"important"}, nil); err != nil { t.Fatal(err) } if err := alice.SetFavorite([]string{"doc.pdf"}, true); err != nil { t.Fatal(err) } favs, _ := alice.List(".favoris") if len(favs) != 1 || favs[0].Path != "doc.pdf" || favs[0].Broken { t.Fatalf("favoris = %+v", favs) } // Déplacement commun -> personnel : le favori suit, les tags aussi. if _, err := alice.Move("doc.pdf", ".perso/doc.pdf"); err != nil { t.Fatal(err) } favs, _ = alice.List(".favoris") if len(favs) != 1 || favs[0].Path != ".perso/doc.pdf" || favs[0].Broken { t.Errorf("favori après déplacement = %+v", favs) } es, _ := alice.List(".perso") if len(es) != 1 || len(es[0].Tags) != 1 { t.Errorf("tags après déplacement : %+v", es) } // Un dossier peut être favori. if _, err := alice.Mkdir("projets"); err != nil { t.Fatal(err) } if err := alice.SetFavorite([]string{"projets"}, true); err != nil { t.Fatal(err) } if favs, _ = alice.List(".favoris"); len(favs) != 2 { t.Errorf("favoris dossier : %+v", favs) } // Suppression de l'original : le favori disparaît. if err := alice.Delete(".perso/doc.pdf", false); err != nil { t.Fatal(err) } if favs, _ = alice.List(".favoris"); len(favs) != 1 { t.Errorf("favori d'un fichier supprimé : %+v", favs) } } func TestCommonReadonly(t *testing.T) { sp := newSpaces(t) admin, user := space(t, sp, "root", true), space(t, sp, "bob", false) uput(t, admin, "regles.txt", "R") if _, err := sp.Main.WriteCommonReadonly(true); err != nil { t.Fatal(err) } if _, err := user.Put("x.txt", strings.NewReader("x"), 1, PutOptions{}); !errors.Is(err, ErrForbidden) { t.Errorf("écriture : %v", err) } if _, err := user.Mkdir("d"); !errors.Is(err, ErrForbidden) { t.Errorf("Mkdir : %v", err) } if err := user.Delete("regles.txt", false); !errors.Is(err, ErrForbidden) { t.Errorf("Delete : %v", err) } if _, err := user.Move("regles.txt", "r2.txt"); !errors.Is(err, ErrForbidden) { t.Errorf("Move : %v", err) } if err := user.EditTags([]string{"regles.txt"}, []string{"t"}, nil); !errors.Is(err, ErrForbidden) { t.Errorf("EditTags : %v", err) } if _, err := user.Copy("regles.txt", "copie.txt"); !errors.Is(err, ErrForbidden) { t.Errorf("Copy : %v", err) } // Lecture et espace personnel restent libres, copie vers le personnel aussi. if _, _, err := user.Open("regles.txt"); err != nil { t.Errorf("lecture : %v", err) } if _, err := user.Copy("regles.txt", ".perso/regles.txt"); err != nil { t.Errorf("copie vers le personnel : %v", err) } uput(t, user, ".perso/mien.txt", "M") // L'administrateur écrit toujours. uput(t, admin, "admin.txt", "A") } func TestQuota(t *testing.T) { sp := newSpaces(t) if _, err := sp.Main.WriteQuota(1); err != nil { // 1 Mo t.Fatal(err) } u := space(t, sp, "alice", false) big := strings.Repeat("x", 700<<10) uput(t, u, ".perso/a.bin", big) if _, err := u.Put(".perso/b.bin", strings.NewReader(big), int64(len(big)), PutOptions{}); !errors.Is(err, ErrQuota) { t.Errorf("dépassement : %v", err) } // Sans taille annoncée : coupé en cours de route. if _, err := u.Put(".perso/c.bin", strings.NewReader(big), -1, PutOptions{}); !errors.Is(err, ErrQuota) { t.Errorf("dépassement en flux : %v", err) } if used, quota := u.Usage(); quota != 1<<20 || used < 700<<10 { t.Errorf("Usage = %d / %d", used, quota) } // Le quota ne concerne pas l'arborescence commune. uput(t, u, "commun.bin", big+big) // Déplacer vers le personnel doit tenir dans le quota. if _, err := u.Move("commun.bin", ".perso/commun.bin"); !errors.Is(err, ErrQuota) { t.Errorf("déplacement : %v", err) } } func TestMaxUpload(t *testing.T) { sp := newSpaces(t) sp.MaxUpload = 10 u := space(t, sp, "alice", false) if _, err := u.Put("a.bin", strings.NewReader("12345678901"), 11, PutOptions{}); !errors.Is(err, ErrTooLarge) { t.Errorf("taille annoncée : %v", err) } if _, err := u.Put("b.bin", strings.NewReader("12345678901"), -1, PutOptions{}); !errors.Is(err, ErrTooLarge) { t.Errorf("flux : %v", err) } uput(t, u, "ok.bin", "1234567890") } func TestShareInPersonalSpaceResolvedGlobally(t *testing.T) { sp := newSpaces(t) alice := space(t, sp, "alice", false) uput(t, alice, ".perso/p.txt", "P") sh, err := alice.CreateShare(".perso/p.txt", ShareOptions{}) if err != nil { t.Fatal(err) } if sh.Path != ".perso/p.txt" { t.Errorf("chemin virtuel = %q", sh.Path) } got, st, err := sp.ResolveShare(sh.Token) if err != nil || got.Path != "p.txt" || st == sp.Main { t.Fatalf("ResolveShare = %+v, %v", got, err) } bob := space(t, sp, "bob", false) if l, _ := bob.Shares(""); len(l) != 0 { t.Errorf("bob voit les liens d'alice : %v", l) } if err := bob.RevokeShare(sh.ID); err == nil { t.Error("bob a révoqué le lien d'alice") } if err := alice.RevokeAllShares(); !errors.Is(err, ErrForbidden) { t.Errorf("révocation générale par un non-admin : %v", err) } if err := alice.RevokeShare(sh.ID); err != nil { t.Fatal(err) } } func TestTrashSeenAcrossSpaces(t *testing.T) { sp := newSpaces(t) alice := space(t, sp, "alice", false) uput(t, alice, "c.txt", "C") uput(t, alice, ".perso/p.txt", "P") _ = alice.Delete("c.txt", false) _ = alice.Delete(".perso/p.txt", false) tr, _ := alice.Trash() if len(tr) != 2 { t.Fatalf("poubelle = %+v", tr) } res := alice.RestoreTrash([]string{tr[0].Path, tr[1].Path}) for _, r := range res { if r.Error != "" { t.Errorf("restauration : %+v", r) } } if es, _ := alice.List(".perso"); len(es) != 1 { t.Errorf("perso : %v", es) } } func TestZipItemsAcrossSpaces(t *testing.T) { sp := newSpaces(t) alice := space(t, sp, "alice", false) uput(t, alice, "notes.txt", "1") uput(t, alice, ".perso/notes.txt", "2") items, name, err := alice.ZipItems([]string{"notes.txt", ".perso/notes.txt"}) if err != nil || len(items) != 2 || items[0].Name == items[1].Name || name != "fichiers" { t.Errorf("ZipItems = %+v, %q, %v", items, name, err) } if _, _, err := alice.ZipItems([]string{"../../etc/passwd"}); err == nil { t.Error("chemin hors racine accepté") } } func TestRetireUser(t *testing.T) { sp := newSpaces(t) alice := space(t, sp, "alice", false) uput(t, alice, ".perso/a.txt", "A") if err := sp.RetireUser("alice"); err != nil { t.Fatal(err) } // Un nouvel « alice » ne retrouve rien. again := space(t, sp, "alice", false) if es, _ := again.List(".perso"); len(es) != 0 { t.Errorf("héritage de fichiers : %v", es) } des, _ := os.ReadDir(filepath.Join(sp.Main.root, ".users-removed")) if len(des) != 1 { t.Errorf("espace non mis de côté : %v", des) } } func TestSettingsDefaultsAndOverride(t *testing.T) { s := newStore(t) s.Defaults = SettingsDefaults{Registration: RegApproval, QuotaMB: 50, CommonReadonly: true, TrashDays: 30} st, _ := s.ReadSettings() if st.Registration != RegApproval || st.QuotaMB != 50 || !st.CommonReadonly || st.TrashDays != 30 { t.Errorf("défauts = %+v", st) } if st, _ = s.WriteRegistration(RegOpen); st.Registration != RegOpen { t.Errorf("registration = %+v", st) } if st, _ = s.WriteQuota(0); st.QuotaMB != 0 { // 0 explicite : illimité, malgré le défaut t.Errorf("quota = %+v", st) } if _, err := s.WriteRegistration("n'importe quoi"); err == nil { t.Error("mode invalide accepté") } if _, err := s.WriteQuota(-1); err == nil { t.Error("quota négatif accepté") } }