This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / spindle / storage / storage_test.go
2.1 kB 101 lines
1package storage 2 3import ( 4 "context" 5 "errors" 6 "io" 7 "strings" 8 "testing" 9) 10 11func TestValidateKey(t *testing.T) { 12 valid := []string{ 13 "did:plc:xyz123/go-mod-v1", 14 "did:web:spindle.example.com/cache.tar", 15 "abc", 16 "a/b/c/d", 17 } 18 for _, k := range valid { 19 if err := ValidateKey(k); err != nil { 20 t.Errorf("ValidateKey(%q) = %v, want nil", k, err) 21 } 22 } 23 24 invalid := []string{ 25 "", 26 "../escape", 27 "a/../../b", 28 "/leading", 29 "trailing/", 30 "double//slash", 31 "with space", 32 "with\\backslash", 33 "-leading-dash", 34 } 35 for _, k := range invalid { 36 if err := ValidateKey(k); err == nil { 37 t.Errorf("ValidateKey(%q) = nil, want error", k) 38 } 39 } 40} 41 42func TestDiskRoundTrip(t *testing.T) { 43 ctx := context.Background() 44 d, err := NewDisk(t.TempDir()) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 key := "did:plc:xyz/go-mod-v1" 50 if err := d.Put(ctx, key, strings.NewReader("archive-bytes")); err != nil { 51 t.Fatal(err) 52 } 53 54 rc, err := d.Get(ctx, key) 55 if err != nil { 56 t.Fatal(err) 57 } 58 got, err := io.ReadAll(rc) 59 rc.Close() 60 if err != nil { 61 t.Fatal(err) 62 } 63 if string(got) != "archive-bytes" { 64 t.Fatalf("got %q, want %q", got, "archive-bytes") 65 } 66 67 // overwrite 68 if err := d.Put(ctx, key, strings.NewReader("new-bytes")); err != nil { 69 t.Fatal(err) 70 } 71 rc, _ = d.Get(ctx, key) 72 got, _ = io.ReadAll(rc) 73 rc.Close() 74 if string(got) != "new-bytes" { 75 t.Fatalf("overwrite: got %q, want %q", got, "new-bytes") 76 } 77 78 if err := d.Delete(ctx, key); err != nil { 79 t.Fatalf("delete: %v", err) 80 } 81 if _, err := d.Get(ctx, key); !errors.Is(err, ErrNotExist) { 82 t.Fatalf("get deleted: got %v, want ErrNotExist", err) 83 } 84 if err := d.Delete(ctx, key); err != nil { 85 t.Fatalf("delete missing: %v", err) 86 } 87} 88 89func TestDiskRejectsTraversal(t *testing.T) { 90 ctx := context.Background() 91 d, err := NewDisk(t.TempDir()) 92 if err != nil { 93 t.Fatal(err) 94 } 95 if err := d.Put(ctx, "../evil", strings.NewReader("x")); err == nil { 96 t.Fatal("put with traversal key succeeded") 97 } 98 if _, err := d.Get(ctx, "../evil"); err == nil { 99 t.Fatal("get with traversal key succeeded") 100 } 101}