-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsyncer_test.go
More file actions
124 lines (109 loc) · 3.56 KB
/
Copy pathsyncer_test.go
File metadata and controls
124 lines (109 loc) · 3.56 KB
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
package zsync_test
/*
* SPDX-FileCopyrightText: 2026 Colin Phipps <[email protected]>
*
* SPDX-License-Identifier: Artistic-2.0
*/
// AI: copilot / GPT-5 mini, with significant refactoring.
import (
"bytes"
"crypto"
"encoding/binary"
"fmt"
"io"
"net/http"
"os"
"testing"
"github.com/cph6/zsync"
"github.com/cph6/zsync/internal/rcksum"
)
// fakeClient implements zsync.HTTPRequester for tests. It always returns the
// provided status code and body for any request.
type fakeClient struct {
status int
body []byte
}
func (f *fakeClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: f.status,
Body: io.NopCloser(bytes.NewReader(f.body)),
Header: make(http.Header),
}, nil
}
func MakeZsyncControl(data [][]byte) bytes.Buffer {
blocksize := len(data[0])
// Build control file: headers then binary checksums
var buf bytes.Buffer
buf.WriteString("zsync: 0.7.0\n")
fmt.Fprintf(&buf, "Length: %d\n", (len(data)-1)*blocksize+len(data[len(data)-1]))
fmt.Fprintf(&buf, "Blocksize: %d\n", blocksize)
buf.WriteString("Hash-Lengths: 1,4,16\n")
buf.WriteString("Filename: testfile\n")
buf.WriteString("URL: http://example.com/foo\n")
buf.WriteString("\n")
for _, d := range data {
// compute rsum and md4
rsum := rcksum.CalcRsumBlock(d)
md4sum := rcksum.CalcChecksum(d, crypto.MD4)
// write rsum as big-endian two uint16
var rsumBuf [4]byte
binary.Encode(rsumBuf[:4], binary.BigEndian, rsum)
buf.Write(rsumBuf[:4])
// write md4
buf.Write(md4sum[:])
}
return buf
}
// TestNewAndEnd verifies that zsync.New can parse a minimal control file
// and that End closes the temporary file.
func TestNewAndEnd(t *testing.T) {
// Create a one-block file control file.
data := []byte{1, 2, 3, 4, 5, 6, 7, 8}
control := MakeZsyncControl([][]byte{data})
dir := t.TempDir()
s, err := zsync.NewFromControlFile(bytes.NewReader(control.Bytes()), zsync.SyncerOptions{TargetDirectory: dir, OverrideTargetFilename: "outfile"})
if err != nil {
t.Fatalf("New failed: %v", err)
}
// End without renaming should close the temp file and return its name
fname, _ := s.Finalize()
// file should exist
if _, err := os.Stat(fname); err != nil {
t.Fatalf("expected temp file to exist: %v", err)
}
}
// TestFetchRemainingBlocks exercises the high-level FetchRemainingBlocks
// method using a fake HTTPRequester that returns the exact block data.
func TestFetchRemainingBlocks(t *testing.T) {
// Setup control file for a single block
data := []byte{10, 11, 12, 13, 14, 15, 16, 17}
control := MakeZsyncControl([][]byte{data})
dir := t.TempDir()
s, err := zsync.NewFromControlFile(bytes.NewReader(control.Bytes()), zsync.SyncerOptions{TargetDirectory: dir, OverrideTargetFilename: "outfile"})
if err != nil {
t.Fatalf("New failed: %v", err)
}
// replace URL list with a synthetic URL (doesn't matter for fake client)
// Use the fake client which returns 206 Partial Content and our block data
client := &fakeClient{status: http.StatusPartialContent, body: data}
// Fetch remaining blocks. We provide empty referer and noProgress=true
got, err := s.FetchRemainingBlocks(client, "", nil)
if err != nil {
t.Fatalf("FetchRemainingBlocks failed: %v", err)
}
if got == 0 {
t.Fatalf("expected some bytes downloaded")
}
if s.Status() != zsync.CompleteData {
t.Fatalf("expected Syncer to be complete, got status %d", s.Status())
}
// Verify file contents
fname, _ := s.Finalize()
b, err := os.ReadFile(fname)
if err != nil {
t.Fatalf("read file: %v", err)
}
if !bytes.Equal(b[:len(data)], data) {
t.Fatalf("file contents differ")
}
}