Artifact 26bfc26d2a0972b080b8e2a78480cf134122aca0b3183a3979bbd7bcb6641268:
- File
internal/hash/hash_test.go
— part of check-in
[f5744359d3]
at
2026-04-19 16:30:54
on branch trunk
— Initial open source release
libfossil is a pure-Go implementation of the Fossil SCM, reading and writing the same .fossil SQLite repository format as upstream Fossil.
This is the first public release of code that previously lived in a private repository. The module path is github.com/danmestas/libfossil and the binary is named libfossil to avoid $PATH collisions with upstream fossil.
Included: - Repo lifecycle (create, clone), checkout/checkin, timeline, merge, diff, annotate, shun, tag, branch, bisect, stash, undo/redo - Sync-over-HTTP protocol (client and handler) - Two SQLite drivers: modernc (pure Go, default) and ncruces (wasm-capable) - Observer interfaces (SyncObserver, CheckoutObserver) with an optional OpenTelemetry implementation in observer/otel (separate submodule) - Deterministic simulation testing harness with BUGGIFY fault injection - Kong-based embeddable CLI under cli/ and a standalone binary under cmd/libfossil/ (user: [email protected], size: 2109) [annotate] [blame] [check-ins using]
package hash import ( "os" "os/exec" "path/filepath" "strings" "testing" ) func TestSHA1(t *testing.T) { tests := []struct { input string want string }{ {"", "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, {"hello", "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"}, {"Fossil SCM", "a4304aff1fcb8a78d973db242692175b2e579612"}, } for _, tt := range tests { got := SHA1([]byte(tt.input)) if got != tt.want { t.Errorf("SHA1(%q) = %q, want %q", tt.input, got, tt.want) } } } func TestSHA3(t *testing.T) { tests := []struct { input string want string }{ {"", "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"}, {"hello", "3338be694f50c5f338814986cdf0686453a888b84f424d792af4b9202398f392"}, } for _, tt := range tests { got := SHA3([]byte(tt.input)) if got != tt.want { t.Errorf("SHA3(%q) = %q, want %q", tt.input, got, tt.want) } } } func TestSHA1_FossilValidation(t *testing.T) { if testing.Short() { t.Skip("skipping fossil CLI validation in short mode") } input := []byte("test content for hashing") goHash := SHA1(input) tmpFile := filepath.Join(t.TempDir(), "testfile") os.WriteFile(tmpFile, input, 0644) cmd := exec.Command("fossil", "sha1sum", tmpFile) out, err := cmd.Output() if err != nil { t.Skipf("fossil sha1sum not available: %v", err) } fossilHash := strings.Fields(string(out))[0] if goHash != fossilHash { t.Fatalf("SHA1 mismatch: go=%q fossil=%q", goHash, fossilHash) } } func TestSHA3Format(t *testing.T) { got := SHA3([]byte("test")) if len(got) != 64 { t.Fatalf("SHA3 length = %d, want 64", len(got)) } for _, c := range got { if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { t.Fatalf("SHA3 contains non-lowercase-hex char: %c", c) } } } func BenchmarkSHA1(b *testing.B) { data := make([]byte, 10000) for i := range data { data[i] = byte(i) } b.ResetTimer() for i := 0; i < b.N; i++ { SHA1(data) } } func BenchmarkSHA3(b *testing.B) { data := make([]byte, 10000) for i := range data { data[i] = byte(i) } b.ResetTimer() for i := 0; i < b.N; i++ { SHA3(data) } }