Debian Codegraph @

debian/tests/library-test.go

raw
1 // Minimal test for the desync library.
2 //
3 // Creates a chunk from plain data, takes its content-addressed ID, round-trips
4 // it through the compressed storage format and verifys the ID validates.
5 package main
6
7 import (
8 "bytes"
9 "fmt"
10 "os"
11
12 "github.com/folbricht/desync"
13 )
14
15 func main() {
16 plain := []byte("Hello from the Debian desync library autopkgtest.")
17
18 // Build a chunk and compute its SHA512/256 content ID.
19 chunk := desync.NewChunk(plain)
20 id := chunk.ID()
21 fmt.Printf("chunk id: %s\n", id)
22
23 // Convert to the compressed storage format and back again.
24 converters := desync.Converters{desync.Compressor{}}
25 storage, err := chunk.Storage(converters)
26 if err != nil {
27 fmt.Fprintln(os.Stderr, "Storage failed:", err)
28 os.Exit(1)
29 }
30
31 restored, err := desync.NewChunkFromStorage(id, storage, converters, false)
32 if err != nil {
33 fmt.Fprintln(os.Stderr, "NewChunkFromStorage failed:", err)
34 os.Exit(1)
35 }
36
37 data, err := restored.Data()
38 if err != nil {
39 fmt.Fprintln(os.Stderr, "Data failed:", err)
40 os.Exit(1)
41 }
42
43 if !bytes.Equal(data, plain) {
44 fmt.Fprintln(os.Stderr, "round-tripped data does not match original")
45 os.Exit(1)
46 }
47
48 if restored.ID() != id {
49 fmt.Fprintln(os.Stderr, "round-tripped chunk id does not match original")
50 os.Exit(1)
51 }
52
53 fmt.Println("desync library test: chunk round trip OK")
54 }