| 1 | //go:build (linux || darwin || freebsd) && !nofuse |
| 2 | |
| 3 | package mount |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/hanwen/go-fuse/v2/fuse" |
| 9 | ) |
| 10 | |
| 11 | // TestDefaultBlksizeAnchor pins DefaultBlksize to 1 MiB so a silent |
| 12 | // refactor cannot drift the value FUSE mounts advertise to tools. |
| 13 | // See stat.go for the rationale (CID-deterministic profile alignment). |
| 14 | func TestDefaultBlksizeAnchor(t *testing.T) { |
| 15 | if DefaultBlksize != 1024*1024 { |
| 16 | t.Fatalf("DefaultBlksize = %d, want 1 MiB (%d)", DefaultBlksize, 1024*1024) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestBlksizeFromChunker(t *testing.T) { |
| 21 | tests := []struct { |
| 22 | name string |
| 23 | chunker string |
| 24 | want uint32 |
| 25 | }{ |
| 26 | // Kubo defaults and common user choices. |
| 27 | {"default chunker", "size-262144", 262144}, |
| 28 | {"CID-deterministic profile", "size-1048576", 1024 * 1024}, |
| 29 | {"small custom", "size-65536", 65536}, |
| 30 | |
| 31 | // Non-size chunkers: fall back to DefaultBlksize because no |
| 32 | // single preferred I/O size describes their variable output. |
| 33 | {"rabin", "rabin", DefaultBlksize}, |
| 34 | {"rabin with params", "rabin-512-1024-2048", DefaultBlksize}, |
| 35 | {"buzhash", "buzhash", DefaultBlksize}, |
| 36 | |
| 37 | // Defensive: malformed or empty input must not panic or return |
| 38 | // a surprising value. |
| 39 | {"empty", "", DefaultBlksize}, |
| 40 | {"size prefix only", "size-", DefaultBlksize}, |
| 41 | {"non-numeric size", "size-abc", DefaultBlksize}, |
| 42 | {"zero size", "size-0", DefaultBlksize}, |
| 43 | |
| 44 | // Clamp: values above fuse.MAX_KERNEL_WRITE (the largest single FUSE |
| 45 | // request the kernel delivers) are capped so tools can't be |
| 46 | // tricked into allocating buffers the kernel will just split. |
| 47 | {"above cap clamped", "size-2097152", fuse.MAX_KERNEL_WRITE}, |
| 48 | {"16 MiB clamped", "size-16777216", fuse.MAX_KERNEL_WRITE}, |
| 49 | {"uint32 max clamped", "size-4294967295", fuse.MAX_KERNEL_WRITE}, |
| 50 | {"beyond uint32 clamped", "size-99999999999", fuse.MAX_KERNEL_WRITE}, |
| 51 | } |
| 52 | |
| 53 | for _, tc := range tests { |
| 54 | t.Run(tc.name, func(t *testing.T) { |
| 55 | if got := BlksizeFromChunker(tc.chunker); got != tc.want { |
| 56 | t.Fatalf("BlksizeFromChunker(%q) = %d, want %d", tc.chunker, got, tc.want) |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | } |