Debian Codegraph @

format_test.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "os"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestFormatDecoder(t *testing.T) {
12 f, err := os.Open("testdata/flat.catar")
13 require.NoError(t, err)
14 defer f.Close()
15
16 d := NewFormatDecoder(f)
17
18 // Define an array of what is expected in the test file
19 expected := []any{
20 FormatEntry{},
21 FormatUser{},
22 FormatGroup{},
23 FormatSELinux{},
24 FormatFilename{}, // "device"
25 FormatEntry{},
26 FormatSELinux{},
27 FormatDevice{},
28 FormatFilename{}, // "file1.txt"
29 FormatEntry{},
30 FormatUser{},
31 FormatGroup{},
32 FormatSELinux{},
33 FormatPayload{},
34 FormatFilename{}, // "file2.txt"
35 FormatEntry{},
36 FormatGroup{},
37 FormatSELinux{},
38 FormatPayload{},
39 FormatFilename{}, // "symlink"
40 FormatEntry{},
41 FormatUser{},
42 FormatGroup{},
43 FormatSELinux{},
44 FormatSymlink{},
45 FormatGoodbye{},
46 nil,
47 }
48
49 for _, exp := range expected {
50 v, err := d.Next()
51 require.NoError(t, err)
52 require.IsType(t, exp, v)
53 }
54 }
55
56 func TestIndexDecoder(t *testing.T) {
57 f, err := os.Open("testdata/index.caibx")
58 require.NoError(t, err)
59 defer f.Close()
60
61 d := NewFormatDecoder(f)
62
63 // The file should start with the index
64 e, err := d.Next()
65 require.NoError(t, err)
66 index, ok := e.(FormatIndex)
67 require.True(t, ok, "file doesn't start with an index")
68 require.Equal(t, uint64(CaFormatSHA512256|CaFormatExcludeNoDump), index.FeatureFlags)
69
70 // Now get the table with the chunks
71 e, err = d.Next()
72 require.NoError(t, err)
73 table, ok := e.(FormatTable)
74 require.True(t, ok, "index table not found")
75
76 // Define the chunk IDs and the order they should be in the file
77 expected := []string{
78 "437884da2d1e61cf50b43b263ff15f25a870b0eae84bc22e4b5c307a0428764d",
79 "985462e6b3293bbe61e43882686b481751ecf4b285bae4dffc2dfa8829f971ac",
80 "fadff4b303624f2be3d0e04c2f105306118a9f608ef1e4f83c1babbd23a2315f",
81 }
82 // Check the expected length of the table
83 require.Len(t, table.Items, len(expected))
84 // And then make sure the IDs and order match
85 for i := range expected {
86 id, _ := ChunkIDFromString(expected[i])
87 require.Equal(t, id, table.Items[i].Chunk)
88 }
89 }
90
91 // Decode and then encode index/archive files to test the encode produces the
92 // exact same output.
93 func TestEncoder(t *testing.T) {
94 files := []string{
95 "testdata/index.caibx",
96 "testdata/nested.catar",
97 }
98 for _, name := range files {
99 in, err := os.ReadFile(name)
100 require.NoError(t, err)
101
102 // Decoder
103 d := NewFormatDecoder(bytes.NewReader(in))
104
105 // Encoder
106 out := new(bytes.Buffer)
107 e := NewFormatEncoder(out)
108
109 // Decode each element, then encode it again
110 var total int64
111 for {
112 v, err := d.Next()
113 require.NoError(t, err)
114 if v == nil {
115 break
116 }
117 n, err := e.Encode(v)
118 require.NoError(t, err)
119 total += n
120 }
121
122 // in/out should match
123 require.Equal(t, in, out.Bytes(), "decoded/encoded don't match for file '%s'", name)
124 require.Equal(t, int64(out.Len()), total, "unexpected length for encoding of '%s'", name)
125 }
126 }
127
128 // Goodbye items in a catar are a complete BST in array form. Test the sorting algorithm
129 // for those. The key in the BST is the hash.
130 func TestGoodbyeBST(t *testing.T) {
131 in := []FormatGoodbyeItem{
132 {Offset: 0x0, Hash: 0xb4bedf9e7796b4d},
133 {Offset: 0x1, Hash: 0x218f89516a601c9c},
134 {Offset: 0x2, Hash: 0x28b19de616c15f21},
135 {Offset: 0x3, Hash: 0x490c091d8b45918f},
136 {Offset: 0x4, Hash: 0x51ba5a19e058c7ad},
137 {Offset: 0x5, Hash: 0x61cffdbff93ec8e0},
138 {Offset: 0x6, Hash: 0x6b38ee3f1236bc32},
139 {Offset: 0x7, Hash: 0x6ec111ca376a466e},
140 {Offset: 0x8, Hash: 0x7d411df513f323cf},
141 {Offset: 0x9, Hash: 0x9007695395e7df8f},
142 {Offset: 0xa, Hash: 0x99a552eadd2d1199},
143 {Offset: 0xb, Hash: 0x9e09fb7343978b70},
144 {Offset: 0xc, Hash: 0xa1a7aeca9969d80a},
145 {Offset: 0xd, Hash: 0xbcbe4464f8e3043b},
146 {Offset: 0xe, Hash: 0xc01a4819ff41b89c},
147 {Offset: 0xf, Hash: 0xc7bb588a3af1fb89},
148 }
149
150 expected := []FormatGoodbyeItem{
151 {Offset: 0x8, Hash: 0x7d411df513f323cf},
152 {Offset: 0x4, Hash: 0x51ba5a19e058c7ad},
153 {Offset: 0xc, Hash: 0xa1a7aeca9969d80a},
154 {Offset: 0x2, Hash: 0x28b19de616c15f21},
155 {Offset: 0x6, Hash: 0x6b38ee3f1236bc32},
156 {Offset: 0xa, Hash: 0x99a552eadd2d1199},
157 {Offset: 0xe, Hash: 0xc01a4819ff41b89c},
158 {Offset: 0x1, Hash: 0x218f89516a601c9c},
159 {Offset: 0x3, Hash: 0x490c091d8b45918f},
160 {Offset: 0x5, Hash: 0x61cffdbff93ec8e0},
161 {Offset: 0x7, Hash: 0x6ec111ca376a466e},
162 {Offset: 0x9, Hash: 0x9007695395e7df8f},
163 {Offset: 0xb, Hash: 0x9e09fb7343978b70},
164 {Offset: 0xd, Hash: 0xbcbe4464f8e3043b},
165 {Offset: 0xf, Hash: 0xc7bb588a3af1fb89},
166 {Offset: 0x0, Hash: 0xb4bedf9e7796b4d},
167 }
168
169 out := makeGoodbyeBST(in)
170
171 require.Equal(t, expected, out)
172 }