File: tlogclient_test.go

package info (click to toggle)
torchwood 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 632 kB
  • sloc: python: 237; sql: 135; sh: 17; makefile: 14
file content (203 lines) | stat: -rw-r--r-- 5,170 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//go:build go1.24

package torchwood_test

import (
	"bytes"
	"fmt"
	"log/slog"
	"path/filepath"
	"testing"

	"filippo.io/torchwood"
	"golang.org/x/mod/sumdb/tlog"
)

func TestReadEndpoint(t *testing.T) {
	handler, _ := testLogHandler(t)
	fetcher, err := torchwood.NewTileFetcher("https://sum.golang.org/",
		torchwood.WithTileFetcherLogger(slog.New(handler)))
	if err != nil {
		t.Fatal(err)
	}
	data, err := fetcher.ReadEndpoint(t.Context(), "latest")
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.HasPrefix(data, []byte("go.sum database tree\n")) {
		t.Fatalf("got %q, want prefix %q", data, "go.sum database tree\n")
	}
}

func TestSumDB(t *testing.T) {
	latest := []byte(`go.sum database tree
31048497
InZSsRXdXKTMF3W5wEcd9T6ro5zyOiRMGQsEPSTco6U=
`)
	tree, err := tlog.ParseTree(latest)
	if err != nil {
		t.Fatal(err)
	}

	handler, _ := testLogHandler(t)

	for _, idx := range []int64{0, 1, 255, 256, 257,
		31048497 - 31048497%256 - 1, 31048497 - 31048497%256,
		31048497 - 31048497%256 + 1, 31048497 - 1} {
		t.Run(fmt.Sprintf("Entry%d", idx), func(t *testing.T) {
			fetcher, err := torchwood.NewTileFetcher("https://sum.golang.org/",
				torchwood.WithTilePath(tlog.Tile.Path),
				torchwood.WithTileFetcherLogger(slog.New(handler)))
			if err != nil {
				t.Fatal(err)
			}
			client, err := torchwood.NewClient(fetcher, torchwood.WithSumDBEntries())
			if err != nil {
				t.Fatal(err)
			}
			entry, proof, err := client.Entry(t.Context(), tree, idx)
			if err != nil {
				t.Fatal(err)
			}
			rh := tlog.RecordHash(entry)
			if err := tlog.CheckRecord(proof, tree.N, tree.Hash, idx, rh); err != nil {
				t.Fatalf("CheckRecord failed: %v", err)
			}
		})
	}

	tests := []struct {
		start     int64
		expect    int
		expectAll int
	}{
		{0, 1000, 1000},
		{100000, 1000, 1000},
		{31048497 - 1000, 1000 - 31048497%256, 1000},              // Stop before the partial (without AllEntries).
		{31048497 - 31048497%256, 31048497 % 256, 31048497 % 256}, // Consume the partial.
		{31048497, 0, 0},
	}

	for _, tt := range tests {
		t.Run(fmt.Sprintf("Start%d", tt.start), func(t *testing.T) {
			t.Run("NoCache", func(t *testing.T) {
				fetcher, err := torchwood.NewTileFetcher("https://sum.golang.org/",
					torchwood.WithTilePath(tlog.Tile.Path),
					torchwood.WithTileFetcherLogger(slog.New(handler)))
				if err != nil {
					t.Fatal(err)
				}
				client, err := torchwood.NewClient(fetcher, torchwood.WithSumDBEntries())
				if err != nil {
					t.Fatal(err)
				}

				count := 0
				for range client.Entries(t.Context(), tree, tt.start) {
					count++
					if count >= 1000 {
						break
					}
				}
				if err := client.Err(); err != nil {
					t.Fatal(err)
				}
				if count != tt.expect {
					t.Errorf("got %d entries, want %d", count, tt.expect)
				}

				count = 0
				for range client.AllEntries(t.Context(), tree, tt.start) {
					count++
					if count >= 1000 {
						break
					}
				}
				if err := client.Err(); err != nil {
					t.Fatal(err)
				}
				if count != tt.expectAll {
					t.Errorf("got %d entries with AllEntries, want %d", count, tt.expectAll)
				}
			})

			t.Run("DirCache", func(t *testing.T) {
				fetcher, err := torchwood.NewTileFetcher("https://sum.golang.org/",
					torchwood.WithTilePath(tlog.Tile.Path),
					torchwood.WithTileFetcherLogger(slog.New(handler)))
				if err != nil {
					t.Fatal(err)
				}
				dirCache, err := torchwood.NewPermanentCache(fetcher, t.TempDir(),
					torchwood.WithPermanentCacheLogger(slog.New(handler)),
					torchwood.WithPermanentCacheTilePath(tlog.Tile.Path))
				if err != nil {
					t.Fatal(err)
				}
				client, err := torchwood.NewClient(dirCache, torchwood.WithSumDBEntries())
				if err != nil {
					t.Fatal(err)
				}

				count := 0
				for range client.Entries(t.Context(), tree, tt.start) {
					count++
					if count >= 1000 {
						break
					}
				}
				if err := client.Err(); err != nil {
					t.Fatal(err)
				}
				if count != tt.expect {
					t.Errorf("got %d entries, want %d", count, tt.expect)
				}

				// Again, from cache.
				client, err = torchwood.NewClient(dirCache, torchwood.WithSumDBEntries())
				if err != nil {
					t.Fatal(err)
				}
				count = 0
				for range client.Entries(t.Context(), tree, tt.start) {
					count++
					if count >= 1000 {
						break
					}
				}
				if err := client.Err(); err != nil {
					t.Fatal(err)
				}
				if count != tt.expect {
					t.Errorf("got %d entries, want %d", count, tt.expect)
				}
			})
		})
	}
}

func testLogHandler(t testing.TB) (slog.Handler, *slog.LevelVar) {
	level := &slog.LevelVar{}
	level.Set(slog.LevelDebug)
	h := slog.NewTextHandler(writerFunc(func(p []byte) (n int, err error) {
		t.Logf("%s", p)
		return len(p), nil
	}), &slog.HandlerOptions{
		AddSource: true,
		Level:     level,
		ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
			if a.Key == slog.SourceKey {
				src := a.Value.Any().(*slog.Source)
				a.Value = slog.StringValue(fmt.Sprintf("%s:%d", filepath.Base(src.File), src.Line))
			}
			return a
		},
	})
	return h, level
}

type writerFunc func(p []byte) (n int, err error)

func (f writerFunc) Write(p []byte) (n int, err error) {
	return f(p)
}