github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/clone/cmd/sumdbclone/internal/client/sumdb_test.go (about) 1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package client 16 17 import ( 18 "bytes" 19 "fmt" 20 "testing" 21 22 "golang.org/x/mod/sumdb/tlog" 23 ) 24 25 const ( 26 leafData = `golang.org/x/net v0.0.0-20180627171509-e514e69ffb8b h1:oXs/nlnyk1ue6g+mFGEHIuIaQIT28IgumdSIRMq2aJY= 27 golang.org/x/net v0.0.0-20180627171509-e514e69ffb8b/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 28 29 golang.org/x/exp/notary v0.0.0-20190409044807-56b785ea58b2 h1:f//7NxweD0pr6lfEPxg1OgwjMgrBOuLknHTUue6T60s= 30 golang.org/x/exp/notary v0.0.0-20190409044807-56b785ea58b2/go.mod h1:LX2MmXhzwauji5+hdOp9+fZiT7bHVjDavhHX8IDQ4T0= 31 32 golang.org/x/crypto v0.0.0-20181025213731-e84da0312774 h1:a4tQYYYuK9QdeO/+kEvNYyuR21S+7ve5EANok6hABhI= 33 golang.org/x/crypto v0.0.0-20181025213731-e84da0312774/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 34 35 golang.org/x/madethisup v0.0.0-1 h1:a4tQYYYuK9QdeO/+kEvNYyuR21S+7ve5EANok6hABhI= 36 golang.org/x/madethisup v0.0.0-1/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 37 ` 38 39 checkpointData = `go.sum database tree 40 1514086 41 kn9DgqDhXzoZMM8828SQsbuovr/WRn7QfFd5Qe1rpwA= 42 43 — sum.golang.org Az3grunuggF5mKymPJeK/l9Pq71lOg/rAVkQVCzGkWRJcnS3ZFunzveHr9PAH8LFsuhpcCWzGDNrn9FFDyXm/66tBg8= 44 ` 45 ) 46 47 func TestLeavesAtOffset(t *testing.T) { 48 sumdb := &SumDBClient{ 49 vkey: "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8", 50 height: 2, 51 fetcher: &FakeFetcher{ 52 values: map[string]string{"/tile/2/data/000": leafData}, 53 }, 54 } 55 leaves, err := sumdb.FullLeavesAtOffset(0) 56 if err != nil { 57 t.Fatalf("failed to get leaves: %v", err) 58 } 59 if got, want := len(leaves), 4; got != want { 60 t.Errorf("got: %d, want: %d", got, want) 61 } 62 for _, l := range leaves { 63 if l[len(l)-1] != '\n' { 64 t.Errorf("expected string terminating in newline, got: %x", l) 65 } 66 expStart := "golang.org/x/" 67 if got, want := string(l[:len(expStart)]), expStart; got != want { 68 t.Errorf("got prefix '%s', wanted '%s'", got, want) 69 } 70 } 71 } 72 73 func TestLatestCheckpoint(t *testing.T) { 74 sumdb := &SumDBClient{ 75 vkey: "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8", 76 height: 2, 77 fetcher: &FakeFetcher{ 78 values: map[string]string{"/latest": checkpointData}, 79 }, 80 } 81 checkpoint, err := sumdb.LatestCheckpoint() 82 if err != nil { 83 t.Fatalf("failed to get checkpoint: %v", err) 84 } 85 if !bytes.Equal(checkpoint.Raw, []byte(checkpointData)) { 86 t.Errorf("expected raw checkpoint %q but got %q", checkpointData, checkpoint.Raw) 87 } 88 expectedHash, err := tlog.ParseHash("kn9DgqDhXzoZMM8828SQsbuovr/WRn7QfFd5Qe1rpwA=") 89 if err != nil { 90 t.Fatalf("static hash failed to parse") 91 } 92 if got, want := checkpoint.Hash, expectedHash; got != want { 93 t.Errorf("unexpected hash. got, want = %s, %s", got, want) 94 } 95 if got, want := checkpoint.N, int64(1514086); got != want { 96 t.Errorf("unexpected tree size. got, want = %d, %d", got, want) 97 } 98 } 99 100 type FakeFetcher struct { 101 values map[string]string 102 } 103 104 func (f *FakeFetcher) GetData(path string) ([]byte, error) { 105 res, ok := f.values[path] 106 if !ok { 107 return nil, fmt.Errorf("could not find '%s'", path) 108 } 109 return []byte(res), nil 110 }