| 1 |
// Copyright 2019 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package sumdb |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"maps" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"golang.org/x/mod/sumdb/note" |
| 16 |
"golang.org/x/mod/sumdb/tlog" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
testName = "localhost.localdev/sumdb" |
| 21 |
testVerifierKey = "localhost.localdev/sumdb+00000c67+AcTrnkbUA+TU4heY3hkjiSES/DSQniBqIeQ/YppAUtK6" |
| 22 |
testSignerKey = "PRIVATE+KEY+localhost.localdev/sumdb+00000c67+AXu6+oaVaOYuQOFrf1V59JK1owcFlJcHwwXHDfDGxSPk" |
| 23 |
) |
| 24 |
|
| 25 |
func TestClientLookup(t *testing.T) { |
| 26 |
tc := newTestClient(t) |
| 27 |
tc.mustHaveLatest(1) |
| 28 |
|
| 29 |
// Basic lookup. |
| 30 |
tc.mustLookup("rsc.io/sampler", "v1.3.0", "rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=") |
| 31 |
tc.mustHaveLatest(3) |
| 32 |
|
| 33 |
// Everything should now be cached, both for the original package and its /go.mod. |
| 34 |
tc.getOK = false |
| 35 |
tc.mustLookup("rsc.io/sampler", "v1.3.0", "rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=") |
| 36 |
tc.mustLookup("rsc.io/sampler", "v1.3.0/go.mod", "rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=") |
| 37 |
tc.mustHaveLatest(3) |
| 38 |
tc.getOK = true |
| 39 |
tc.getTileOK = false // the cache has what we need |
| 40 |
|
| 41 |
// Lookup with multiple returned lines. |
| 42 |
tc.mustLookup("rsc.io/quote", "v1.5.2", "rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=\nrsc.io/quote v1.5.2 h2:xyzzy") |
| 43 |
tc.mustHaveLatest(3) |
| 44 |
|
| 45 |
// Lookup with need for !-encoding. |
| 46 |
// rsc.io/Quote is the only record written after rsc.io/samper, |
| 47 |
// so it is the only one that should need more tiles. |
| 48 |
tc.getTileOK = true |
| 49 |
tc.mustLookup("rsc.io/Quote", "v1.5.2", "rsc.io/Quote v1.5.2 h1:uppercase!=") |
| 50 |
tc.mustHaveLatest(4) |
| 51 |
} |
| 52 |
|
| 53 |
func TestClientBadTiles(t *testing.T) { |
| 54 |
tc := newTestClient(t) |
| 55 |
|
| 56 |
flipBits := func() { |
| 57 |
for url, data := range tc.remote { |
| 58 |
if strings.Contains(url, "/tile/") { |
| 59 |
for i := range data { |
| 60 |
data[i] ^= 0x80 |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Bad tiles in initial download. |
| 67 |
tc.mustHaveLatest(1) |
| 68 |
flipBits() |
| 69 |
_, err := tc.client.Lookup("rsc.io/sampler", "v1.3.0") |
| 70 |
tc.mustError(err, "rsc.io/[email protected]: initializing sumdb.Client: checking tree#1: downloaded inconsistent tile") |
| 71 |
flipBits() |
| 72 |
tc.newClient() |
| 73 |
tc.mustLookup("rsc.io/sampler", "v1.3.0", "rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=") |
| 74 |
|
| 75 |
// Bad tiles after initial download. |
| 76 |
flipBits() |
| 77 |
_, err = tc.client.Lookup("rsc.io/Quote", "v1.5.2") |
| 78 |
tc.mustError(err, "rsc.io/[email protected]: checking tree#3 against tree#4: downloaded inconsistent tile") |
| 79 |
flipBits() |
| 80 |
tc.newClient() |
| 81 |
tc.mustLookup("rsc.io/Quote", "v1.5.2", "rsc.io/Quote v1.5.2 h1:uppercase!=") |
| 82 |
|
| 83 |
// Bad starting tree hash looks like bad tiles. |
| 84 |
tc.newClient() |
| 85 |
text := tlog.FormatTree(tlog.Tree{N: 1, Hash: tlog.Hash{}}) |
| 86 |
data, err := note.Sign(¬e.Note{Text: string(text)}, tc.signer) |
| 87 |
if err != nil { |
| 88 |
tc.t.Fatal(err) |
| 89 |
} |
| 90 |
tc.config[testName+"/latest"] = data |
| 91 |
_, err = tc.client.Lookup("rsc.io/sampler", "v1.3.0") |
| 92 |
tc.mustError(err, "rsc.io/[email protected]: initializing sumdb.Client: checking tree#1: downloaded inconsistent tile") |
| 93 |
} |
| 94 |
|
| 95 |
func TestClientFork(t *testing.T) { |
| 96 |
tc := newTestClient(t) |
| 97 |
tc2 := tc.fork() |
| 98 |
|
| 99 |
tc.addRecord("rsc.io/[email protected]", `rsc.io/pkg1 v1.5.2 h1:hash!= |
| 100 |
`) |
| 101 |
tc.addRecord("rsc.io/[email protected]", `rsc.io/pkg1 v1.5.4 h1:hash!= |
| 102 |
`) |
| 103 |
tc.mustLookup("rsc.io/pkg1", "v1.5.2", "rsc.io/pkg1 v1.5.2 h1:hash!=") |
| 104 |
|
| 105 |
tc2.addRecord("rsc.io/[email protected]", `rsc.io/pkg1 v1.5.3 h1:hash!= |
| 106 |
`) |
| 107 |
tc2.addRecord("rsc.io/[email protected]", `rsc.io/pkg1 v1.5.4 h1:hash!= |
| 108 |
`) |
| 109 |
tc2.mustLookup("rsc.io/pkg1", "v1.5.4", "rsc.io/pkg1 v1.5.4 h1:hash!=") |
| 110 |
|
| 111 |
key := "/lookup/rsc.io/[email protected]" |
| 112 |
tc2.remote[key] = tc.remote[key] |
| 113 |
_, err := tc2.client.Lookup("rsc.io/pkg1", "v1.5.2") |
| 114 |
tc2.mustError(err, ErrSecurity.Error()) |
| 115 |
|
| 116 |
/* |
| 117 |
SECURITY ERROR |
| 118 |
go.sum database server misbehavior detected! |
| 119 |
|
| 120 |
old database: |
| 121 |
go.sum database tree! |
| 122 |
5 |
| 123 |
nWzN20+pwMt62p7jbv1/NlN95ePTlHijabv5zO/s36w= |
| 124 |
|
| 125 |
— localhost.localdev/sumdb AAAMZ5/2FVAdMH58kmnz/0h299pwyskEbzDzoa2/YaPdhvLya4YWDFQQxu2TQb5GpwAH4NdWnTwuhILafisyf3CNbgg= |
| 126 |
|
| 127 |
new database: |
| 128 |
go.sum database tree |
| 129 |
6 |
| 130 |
wc4SkQt52o5W2nQ8To2ARs+mWuUJjss+sdleoiqxMmM= |
| 131 |
|
| 132 |
— localhost.localdev/sumdb AAAMZ6oRNswlEZ6ZZhxrCvgl1MBy+nusq4JU+TG6Fe2NihWLqOzb+y2c2kzRLoCr4tvw9o36ucQEnhc20e4nA4Qc/wc= |
| 133 |
|
| 134 |
proof of misbehavior: |
| 135 |
T7i+H/8ER4nXOiw4Bj0koZOkGjkxoNvlI34GpvhHhQg= |
| 136 |
Nsuejv72de9hYNM5bqFv8rv3gm3zJQwv/DT/WNbLDLA= |
| 137 |
mOmqqZ1aI/lzS94oq/JSbj7pD8Rv9S+xDyi12BtVSHo= |
| 138 |
/7Aw5jVSMM9sFjQhaMg+iiDYPMk6decH7QLOGrL9Lx0= |
| 139 |
*/ |
| 140 |
|
| 141 |
wants := []string{ |
| 142 |
"SECURITY ERROR", |
| 143 |
"go.sum database server misbehavior detected!", |
| 144 |
"old database:\n\tgo.sum database tree\n\t5\n", |
| 145 |
"— localhost.localdev/sumdb AAAMZ5/2FVAd", |
| 146 |
"new database:\n\tgo.sum database tree\n\t6\n", |
| 147 |
"— localhost.localdev/sumdb AAAMZ6oRNswl", |
| 148 |
"proof of misbehavior:\n\tT7i+H/8ER4nXOiw4Bj0k", |
| 149 |
} |
| 150 |
text := tc2.security.String() |
| 151 |
for _, want := range wants { |
| 152 |
if !strings.Contains(text, want) { |
| 153 |
t.Fatalf("cannot find %q in security text:\n%s", want, text) |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestClientGONOSUMDB(t *testing.T) { |
| 159 |
tc := newTestClient(t) |
| 160 |
tc.client.SetGONOSUMDB("p,*/q") |
| 161 |
tc.client.Lookup("rsc.io/sampler", "v1.3.0") // initialize before we turn off network |
| 162 |
tc.getOK = false |
| 163 |
|
| 164 |
ok := []string{ |
| 165 |
"abc", |
| 166 |
"a/p", |
| 167 |
"pq", |
| 168 |
"q", |
| 169 |
"n/o/p/q", |
| 170 |
} |
| 171 |
skip := []string{ |
| 172 |
"p", |
| 173 |
"p/x", |
| 174 |
"x/q", |
| 175 |
"x/q/z", |
| 176 |
} |
| 177 |
|
| 178 |
for _, path := range ok { |
| 179 |
_, err := tc.client.Lookup(path, "v1.0.0") |
| 180 |
if err == ErrGONOSUMDB { |
| 181 |
t.Errorf("Lookup(%q): ErrGONOSUMDB, wanted failed actual lookup", path) |
| 182 |
} |
| 183 |
} |
| 184 |
for _, path := range skip { |
| 185 |
_, err := tc.client.Lookup(path, "v1.0.0") |
| 186 |
if err != ErrGONOSUMDB { |
| 187 |
t.Errorf("Lookup(%q): %v, wanted ErrGONOSUMDB", path, err) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestRejectUnauthenticatedLines(t *testing.T) { |
| 193 |
tc := newTestClient(t) |
| 194 |
|
| 195 |
data := "golang.org/x/good v1.0.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=\n" |
| 196 |
id := tc.treeSize |
| 197 |
tc.treeSize++ |
| 198 |
rec, err := tlog.FormatRecord(id, []byte(data)) |
| 199 |
if err != nil { |
| 200 |
t.Fatal(err) |
| 201 |
} |
| 202 |
hashes, err := tlog.StoredHashesForRecordHash(id, tlog.RecordHash([]byte(data)), tc) |
| 203 |
if err != nil { |
| 204 |
t.Fatal(err) |
| 205 |
} |
| 206 |
tc.hashes = append(tc.hashes, hashes...) |
| 207 |
|
| 208 |
// Create lookup result. |
| 209 |
h, err := tlog.TreeHash(tc.treeSize, tc) |
| 210 |
if err != nil { |
| 211 |
t.Fatal(err) |
| 212 |
} |
| 213 |
text := append(tlog.FormatTree(tlog.Tree{N: tc.treeSize, Hash: h}), []byte("golang.org/x/bad v1.0.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=\n")...) |
| 214 |
signed, err := note.Sign(¬e.Note{Text: string(text)}, tc.signer) |
| 215 |
if err != nil { |
| 216 |
t.Fatal(err) |
| 217 |
} |
| 218 |
|
| 219 |
lookupRes := append(rec, signed...) |
| 220 |
tc.remote["/lookup/golang.org/x/[email protected]"] = lookupRes |
| 221 |
|
| 222 |
// Create new tiles. |
| 223 |
tiles := tlog.NewTiles(tc.tileHeight, id, tc.treeSize) |
| 224 |
for _, tile := range tiles { |
| 225 |
data, err := tlog.ReadTileData(tile, tc) |
| 226 |
if err != nil { |
| 227 |
t.Fatal(err) |
| 228 |
} |
| 229 |
tc.remote["/"+tile.Path()] = data |
| 230 |
} |
| 231 |
|
| 232 |
lines, err := tc.client.Lookup("golang.org/x/bad", "v1.0.0") |
| 233 |
if err != nil { |
| 234 |
t.Fatal(err) |
| 235 |
} |
| 236 |
if len(lines) != 0 { |
| 237 |
t.Errorf("Lookup(%q): expected no hashes, got %q", "golang.org/x/bad", strings.Join(lines, "\n")) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
// A testClient is a self-contained client-side testing environment. |
| 242 |
type testClient struct { |
| 243 |
t *testing.T // active test |
| 244 |
client *Client // client being tested |
| 245 |
tileHeight int // tile height to use (default 2) |
| 246 |
getOK bool // should tc.GetURL succeed? |
| 247 |
getTileOK bool // should tc.GetURL of tiles succeed? |
| 248 |
treeSize int64 |
| 249 |
hashes []tlog.Hash |
| 250 |
remote map[string][]byte |
| 251 |
signer note.Signer |
| 252 |
|
| 253 |
// mu protects config, cache, log, security |
| 254 |
// during concurrent use of the exported methods |
| 255 |
// by the client itself (testClient is the Client's ClientOps, |
| 256 |
// and the Client methods can both read and write these fields). |
| 257 |
// Unexported methods invoked directly by the test |
| 258 |
// (for example, addRecord) need not hold the mutex: |
| 259 |
// for proper test execution those methods should only |
| 260 |
// be called when the Client is idle and not using its ClientOps. |
| 261 |
// Not holding the mutex in those methods ensures |
| 262 |
// that if a mistake is made, go test -race will report it. |
| 263 |
// (Holding the mutex would eliminate the race report but |
| 264 |
// not the underlying problem.) |
| 265 |
// Similarly, the get map is not protected by the mutex, |
| 266 |
// because the Client methods only read it. |
| 267 |
mu sync.Mutex // prot |
| 268 |
config map[string][]byte |
| 269 |
cache map[string][]byte |
| 270 |
security bytes.Buffer |
| 271 |
} |
| 272 |
|
| 273 |
// newTestClient returns a new testClient that will call t.Fatal on error |
| 274 |
// and has a few records already available on the remote server. |
| 275 |
func newTestClient(t *testing.T) *testClient { |
| 276 |
tc := &testClient{ |
| 277 |
t: t, |
| 278 |
tileHeight: 2, |
| 279 |
getOK: true, |
| 280 |
getTileOK: true, |
| 281 |
config: make(map[string][]byte), |
| 282 |
cache: make(map[string][]byte), |
| 283 |
remote: make(map[string][]byte), |
| 284 |
} |
| 285 |
|
| 286 |
tc.config["key"] = []byte(testVerifierKey + "\n") |
| 287 |
var err error |
| 288 |
tc.signer, err = note.NewSigner(testSignerKey) |
| 289 |
if err != nil { |
| 290 |
t.Fatal(err) |
| 291 |
} |
| 292 |
|
| 293 |
tc.newClient() |
| 294 |
|
| 295 |
tc.addRecord("rsc.io/[email protected]", `rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y= |
| 296 |
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0= |
| 297 |
rsc.io/quote v1.5.2 h2:xyzzy |
| 298 |
`) |
| 299 |
|
| 300 |
tc.addRecord("golang.org/x/[email protected]", `golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8= |
| 301 |
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
| 302 |
`) |
| 303 |
tc.addRecord("rsc.io/[email protected]", `rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4= |
| 304 |
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= |
| 305 |
`) |
| 306 |
tc.config[testName+"/latest"] = tc.signTree(1) |
| 307 |
|
| 308 |
tc.addRecord("rsc.io/[email protected]", `rsc.io/Quote v1.5.2 h1:uppercase!= |
| 309 |
`) |
| 310 |
return tc |
| 311 |
} |
| 312 |
|
| 313 |
// newClient resets the Client associated with tc. |
| 314 |
// This clears any in-memory cache from the Client |
| 315 |
// but not tc's on-disk cache. |
| 316 |
func (tc *testClient) newClient() { |
| 317 |
tc.client = NewClient(tc) |
| 318 |
tc.client.SetTileHeight(tc.tileHeight) |
| 319 |
} |
| 320 |
|
| 321 |
// mustLookup does a lookup for path@vers and checks that the lines that come back match want. |
| 322 |
func (tc *testClient) mustLookup(path, vers, want string) { |
| 323 |
tc.t.Helper() |
| 324 |
lines, err := tc.client.Lookup(path, vers) |
| 325 |
if err != nil { |
| 326 |
tc.t.Fatal(err) |
| 327 |
} |
| 328 |
if strings.Join(lines, "\n") != want { |
| 329 |
tc.t.Fatalf("Lookup(%q, %q):\n\t%s\nwant:\n\t%s", path, vers, strings.Join(lines, "\n\t"), strings.Replace(want, "\n", "\n\t", -1)) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// mustHaveLatest checks that the on-disk configuration |
| 334 |
// for latest is a tree of size n. |
| 335 |
func (tc *testClient) mustHaveLatest(n int64) { |
| 336 |
tc.t.Helper() |
| 337 |
|
| 338 |
latest := tc.config[testName+"/latest"] |
| 339 |
lines := strings.Split(string(latest), "\n") |
| 340 |
if len(lines) < 2 || lines[1] != fmt.Sprint(n) { |
| 341 |
tc.t.Fatalf("/latest should have tree %d, but has:\n%s", n, latest) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// mustError checks that err's error string contains the text. |
| 346 |
func (tc *testClient) mustError(err error, text string) { |
| 347 |
tc.t.Helper() |
| 348 |
if err == nil || !strings.Contains(err.Error(), text) { |
| 349 |
tc.t.Fatalf("err = %v, want %q", err, text) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
// fork returns a copy of tc. |
| 354 |
// Changes made to the new copy or to tc are not reflected in the other. |
| 355 |
func (tc *testClient) fork() *testClient { |
| 356 |
tc2 := &testClient{ |
| 357 |
t: tc.t, |
| 358 |
getOK: tc.getOK, |
| 359 |
getTileOK: tc.getTileOK, |
| 360 |
tileHeight: tc.tileHeight, |
| 361 |
treeSize: tc.treeSize, |
| 362 |
hashes: append([]tlog.Hash{}, tc.hashes...), |
| 363 |
signer: tc.signer, |
| 364 |
config: maps.Clone(tc.config), |
| 365 |
cache: maps.Clone(tc.cache), |
| 366 |
remote: maps.Clone(tc.remote), |
| 367 |
} |
| 368 |
tc2.newClient() |
| 369 |
return tc2 |
| 370 |
} |
| 371 |
|
| 372 |
// ReadHashes is tc's implementation of tlog.HashReader, for use with |
| 373 |
// tlog.TreeHash and so on. |
| 374 |
func (tc *testClient) ReadHashes(indexes []int64) ([]tlog.Hash, error) { |
| 375 |
var list []tlog.Hash |
| 376 |
for _, id := range indexes { |
| 377 |
list = append(list, tc.hashes[id]) |
| 378 |
} |
| 379 |
return list, nil |
| 380 |
} |
| 381 |
|
| 382 |
// addRecord adds a log record using the given (!-encoded) key and data. |
| 383 |
func (tc *testClient) addRecord(key, data string) { |
| 384 |
tc.t.Helper() |
| 385 |
|
| 386 |
// Create record, add hashes to log tree. |
| 387 |
id := tc.treeSize |
| 388 |
tc.treeSize++ |
| 389 |
rec, err := tlog.FormatRecord(id, []byte(data)) |
| 390 |
if err != nil { |
| 391 |
tc.t.Fatal(err) |
| 392 |
} |
| 393 |
hashes, err := tlog.StoredHashesForRecordHash(id, tlog.RecordHash([]byte(data)), tc) |
| 394 |
if err != nil { |
| 395 |
tc.t.Fatal(err) |
| 396 |
} |
| 397 |
tc.hashes = append(tc.hashes, hashes...) |
| 398 |
|
| 399 |
// Create lookup result. |
| 400 |
tc.remote["/lookup/"+key] = append(rec, tc.signTree(tc.treeSize)...) |
| 401 |
|
| 402 |
// Create new tiles. |
| 403 |
tiles := tlog.NewTiles(tc.tileHeight, id, tc.treeSize) |
| 404 |
for _, tile := range tiles { |
| 405 |
data, err := tlog.ReadTileData(tile, tc) |
| 406 |
if err != nil { |
| 407 |
tc.t.Fatal(err) |
| 408 |
} |
| 409 |
tc.remote["/"+tile.Path()] = data |
| 410 |
// TODO delete old partial tiles |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
// signTree returns the signed head for the tree of the given size. |
| 415 |
func (tc *testClient) signTree(size int64) []byte { |
| 416 |
h, err := tlog.TreeHash(size, tc) |
| 417 |
if err != nil { |
| 418 |
tc.t.Fatal(err) |
| 419 |
} |
| 420 |
text := tlog.FormatTree(tlog.Tree{N: size, Hash: h}) |
| 421 |
data, err := note.Sign(¬e.Note{Text: string(text)}, tc.signer) |
| 422 |
if err != nil { |
| 423 |
tc.t.Fatal(err) |
| 424 |
} |
| 425 |
return data |
| 426 |
} |
| 427 |
|
| 428 |
// ReadRemote is for tc's implementation of Client. |
| 429 |
func (tc *testClient) ReadRemote(path string) ([]byte, error) { |
| 430 |
// No mutex here because only the Client should be running |
| 431 |
// and the Client cannot change tc.get. |
| 432 |
if !tc.getOK { |
| 433 |
return nil, fmt.Errorf("disallowed remote read %s", path) |
| 434 |
} |
| 435 |
if strings.Contains(path, "/tile/") && !tc.getTileOK { |
| 436 |
return nil, fmt.Errorf("disallowed remote tile read %s", path) |
| 437 |
} |
| 438 |
|
| 439 |
data, ok := tc.remote[path] |
| 440 |
if !ok { |
| 441 |
return nil, fmt.Errorf("no remote path %s", path) |
| 442 |
} |
| 443 |
return data, nil |
| 444 |
} |
| 445 |
|
| 446 |
// ReadConfig is for tc's implementation of Client. |
| 447 |
func (tc *testClient) ReadConfig(file string) ([]byte, error) { |
| 448 |
tc.mu.Lock() |
| 449 |
defer tc.mu.Unlock() |
| 450 |
|
| 451 |
data, ok := tc.config[file] |
| 452 |
if !ok { |
| 453 |
return nil, fmt.Errorf("no config %s", file) |
| 454 |
} |
| 455 |
return data, nil |
| 456 |
} |
| 457 |
|
| 458 |
// WriteConfig is for tc's implementation of Client. |
| 459 |
func (tc *testClient) WriteConfig(file string, old, new []byte) error { |
| 460 |
tc.mu.Lock() |
| 461 |
defer tc.mu.Unlock() |
| 462 |
|
| 463 |
data := tc.config[file] |
| 464 |
if !bytes.Equal(old, data) { |
| 465 |
return ErrWriteConflict |
| 466 |
} |
| 467 |
tc.config[file] = new |
| 468 |
return nil |
| 469 |
} |
| 470 |
|
| 471 |
// ReadCache is for tc's implementation of Client. |
| 472 |
func (tc *testClient) ReadCache(file string) ([]byte, error) { |
| 473 |
tc.mu.Lock() |
| 474 |
defer tc.mu.Unlock() |
| 475 |
|
| 476 |
data, ok := tc.cache[file] |
| 477 |
if !ok { |
| 478 |
return nil, fmt.Errorf("no cache %s", file) |
| 479 |
} |
| 480 |
return data, nil |
| 481 |
} |
| 482 |
|
| 483 |
// WriteCache is for tc's implementation of Client. |
| 484 |
func (tc *testClient) WriteCache(file string, data []byte) { |
| 485 |
tc.mu.Lock() |
| 486 |
defer tc.mu.Unlock() |
| 487 |
|
| 488 |
tc.cache[file] = data |
| 489 |
} |
| 490 |
|
| 491 |
// Log is for tc's implementation of Client. |
| 492 |
func (tc *testClient) Log(msg string) { |
| 493 |
tc.t.Log(msg) |
| 494 |
} |
| 495 |
|
| 496 |
// SecurityError is for tc's implementation of Client. |
| 497 |
func (tc *testClient) SecurityError(msg string) { |
| 498 |
tc.mu.Lock() |
| 499 |
defer tc.mu.Unlock() |
| 500 |
|
| 501 |
fmt.Fprintf(&tc.security, "%s\n", strings.TrimRight(msg, "\n")) |
| 502 |
} |