aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/klauspost/compress/zstd/decodeheader_test.go
blob: 741369c8f3378aa2337a0bad617f26d9cc054434 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package zstd

import (
	"bytes"
	"encoding/json"
	"io"
	"os"
	"strings"
	"testing"
)

func TestHeader_Decode(t *testing.T) {
	zr := testCreateZipReader("testdata/headers.zip", t)

	// Regenerate golden data...
	const regen = false
	golden := make(map[string]Header)
	if !regen {
		b, err := os.ReadFile("testdata/headers-want.json.zst")
		if err != nil {
			t.Fatal(err)
		}
		dec, err := NewReader(nil)
		if err != nil {
			t.Fatal(err)
		}
		defer dec.Close()
		b, err = dec.DecodeAll(b, nil)
		if err != nil {
			t.Fatal(err)
		}
		err = json.Unmarshal(b, &golden)
		if err != nil {
			t.Fatal(err)
		}
	}

	for i, tt := range zr.File {
		if !strings.HasSuffix(t.Name(), "") {
			continue
		}
		if testing.Short() && i > 100 {
			break
		}

		t.Run(tt.Name, func(t *testing.T) {
			r, err := tt.Open()
			if err != nil {
				t.Error(err)
				return
			}
			defer r.Close()
			b, err := io.ReadAll(r)
			if err != nil {
				t.Error(err)
				return
			}
			want, ok := golden[tt.Name]
			var got Header
			err = got.Decode(b)
			if err != nil {
				if ok {
					t.Errorf("got unexpected error %v", err)
				}
				return
			}
			if regen {
				// errored entries are not set
				golden[tt.Name] = got
				return
			}
			if !ok {
				t.Errorf("want error, got result: %v", got)
			}
			if want != got {
				t.Errorf("header mismatch:\nwant %#v\ngot  %#v", want, got)
			}
		})
	}
	if regen {
		w, err := os.Create("testdata/headers-want.json.zst")
		if err != nil {
			t.Fatal(err)
		}
		defer w.Close()
		enc, err := NewWriter(w, WithEncoderLevel(SpeedBestCompression))
		if err != nil {
			t.Fatal(err)
		}
		b, err := json.Marshal(golden)
		if err != nil {
			t.Fatal(err)
		}
		enc.ReadFrom(bytes.NewBuffer(b))
		enc.Close()
		t.SkipNow()
	}
}