-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathinterop_test.go
202 lines (165 loc) · 5.46 KB
/
interop_test.go
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
package client
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"github.com/theupdateframework/go-tuf/util"
. "gopkg.in/check.v1"
goTufGenerator "github.com/theupdateframework/go-tuf/client/testdata/go-tuf/generator"
)
type InteropSuite struct{}
var _ = Suite(&InteropSuite{})
func (InteropSuite) TestGoClientIdentityConsistentSnapshotFalse(c *C) {
checkGoIdentity(c, false)
}
func (InteropSuite) TestGoClientIdentityConsistentSnapshotTrue(c *C) {
checkGoIdentity(c, true)
}
func checkGoIdentity(c *C, consistentSnapshot bool) {
cwd, err := os.Getwd()
c.Assert(err, IsNil)
testDataDir := filepath.Join(cwd, "testdata")
tempDir, err := ioutil.TempDir("", "")
c.Assert(err, IsNil)
defer os.RemoveAll(tempDir)
// Generate the metadata and compute hashes for all the files.
goTufGenerator.Generate(tempDir, filepath.Join(testDataDir, "keys.json"), consistentSnapshot)
hashes := computeHashes(c, tempDir)
snapshotDir := filepath.Join(testDataDir, "go-tuf", fmt.Sprintf("consistent-snapshot-%t", consistentSnapshot))
snapshotHashes := computeHashes(c, snapshotDir)
c.Assert(hashes, DeepEquals, snapshotHashes, Commentf("metadata out of date, regenerate by running client/testdata/go-tuf/regenerate-metadata.sh"))
}
func computeHashes(c *C, dir string) map[string]string {
hashes := make(map[string]string)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
path, err = filepath.Rel(dir, path)
if err != nil {
return err
}
hashes[path] = string(bytes)
return nil
})
c.Assert(err, IsNil)
return hashes
}
func (InteropSuite) TestGoClientCompatibility(c *C) {
names := []string{
"go-tuf",
"go-tuf-transition-M3",
"go-tuf-transition-M4",
}
options := &HTTPRemoteOptions{MetadataPath: "", TargetsPath: "targets"}
for _, name := range names {
for _, consistentSnapshot := range []bool{false, true} {
t := newTestCase(c, name, consistentSnapshot, options)
t.run(c)
}
}
}
type testCase struct {
name string
consistentSnapshot bool
options *HTTPRemoteOptions
local LocalStore
targets map[string][]byte
testDir string
testSteps []string
}
func newTestCase(c *C, name string, consistentSnapshot bool, options *HTTPRemoteOptions) testCase {
cwd, err := os.Getwd()
c.Assert(err, IsNil)
testDir := filepath.Join(cwd, "testdata", name, fmt.Sprintf("consistent-snapshot-%t", consistentSnapshot))
dirEntries, err := ioutil.ReadDir(testDir)
c.Assert(err, IsNil)
c.Assert(dirEntries, Not(HasLen), 0)
testSteps := []string{}
for _, dirEntry := range dirEntries {
if dirEntry.IsDir() {
testSteps = append(testSteps, dirEntry.Name())
}
}
return testCase{
name: name,
consistentSnapshot: consistentSnapshot,
options: options,
local: MemoryLocalStore(),
targets: make(map[string][]byte),
testDir: testDir,
testSteps: testSteps,
}
}
func (t *testCase) run(c *C) {
c.Logf("test case: %s consistent-snapshot: %t", t.name, t.consistentSnapshot)
for _, stepName := range t.testSteps {
t.runStep(c, stepName)
}
}
func (t *testCase) runStep(c *C, stepName string) {
c.Logf("step: %s", stepName)
addr, cleanup := startFileServer(c, t.testDir)
defer cleanup()
remote, err := HTTPRemoteStore(fmt.Sprintf("http://%s/%s/repository", addr, stepName), t.options, nil)
c.Assert(err, IsNil)
client := NewClient(t.local, remote)
// initiate a client with the root metadata
ioReader, _, err := remote.GetMeta("root.json")
c.Assert(err, IsNil)
rootJsonBytes, err := io.ReadAll(ioReader)
c.Assert(err, IsNil)
c.Assert(client.InitLocal(rootJsonBytes), IsNil)
// check update returns the correct updated targets
files, err := client.Update()
c.Assert(err, IsNil)
if stepName != "2" {
// The rest of the test cases add one target file at a time for each cycle, so this is why we expect that
// the number of updated targets returned by Update() should equals to 1
c.Assert(files, HasLen, 1)
} else {
// The following test case (#2) verifies that when a targets key has been rotated in the latest 3.root.json,
// the local targets.json meta is indeed ignored since it's signed with a key that has been now changed.
// The reason we check for 3 here is that the updated targets corresponds to all target files listed in the
// targets.json for test case #2
c.Assert(files, HasLen, 3)
}
targetName := stepName
t.targets[targetName] = []byte(targetName)
file, ok := files[targetName]
if !ok {
c.Fatalf("expected updated targets to contain %s", targetName)
}
data := t.targets[targetName]
meta, err := util.GenerateTargetFileMeta(bytes.NewReader(data), file.HashAlgorithms()...)
c.Assert(err, IsNil)
c.Assert(util.TargetFileMetaEqual(file, meta), IsNil)
// download the files and check they have the correct content
for name, data := range t.targets {
for _, prefix := range []string{"", "/"} {
var dest testDestination
c.Assert(client.Download(prefix+name, &dest), IsNil)
c.Assert(dest.deleted, Equals, false)
c.Assert(dest.String(), Equals, string(data))
}
}
}
func startFileServer(c *C, dir string) (string, func() error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, IsNil)
addr := l.Addr().String()
go http.Serve(l, http.FileServer(http.Dir(dir)))
return addr, l.Close
}