...

Source file src/github.com/sigstore/rekor/pkg/pki/tuf/tuf_e2e_test.go

Documentation: github.com/sigstore/rekor/pkg/pki/tuf

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  //go:build e2e
    17  // +build e2e
    18  
    19  package tuf
    20  
    21  import (
    22  	"io/ioutil"
    23  	"testing"
    24  
    25  	"github.com/theupdateframework/go-tuf"
    26  )
    27  
    28  func generateTestRepo(t *testing.T, files map[string][]byte) tuf.LocalStore {
    29  	store := tuf.MemoryStore(nil, files)
    30  	repo, err := tuf.NewRepo(store)
    31  	if err := repo.Init(false); err != nil {
    32  		t.Fatalf("unexpected error")
    33  	}
    34  	if err != nil {
    35  		t.Fatalf("unexpected error")
    36  	}
    37  	for _, role := range []string{"root", "snapshot", "targets", "timestamp"} {
    38  		_, err := repo.GenKey(role)
    39  		if err != nil {
    40  			t.Fatalf("unexpected error")
    41  		}
    42  	}
    43  	for file := range files {
    44  		repo.AddTarget(file, nil)
    45  	}
    46  	repo.Snapshot()
    47  	repo.Timestamp()
    48  	repo.Commit()
    49  
    50  	return store
    51  }
    52  
    53  // createTufSignedArtifact gets the test dir setup correctly with some random artifacts and keys.
    54  func createTufSignedArtifact(t *testing.T, artifactPath, rootPath string) {
    55  	t.Helper()
    56  
    57  	store := generateTestRepo(t, map[string][]byte{
    58  		"foo.txt": []byte("foo")})
    59  	meta, err := store.GetMeta()
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	rootJSON, ok := meta["root.json"]
    64  	if !ok {
    65  		t.Fatal(err)
    66  	}
    67  	timestampJSON, ok := meta["timestamp.json"]
    68  	if !ok {
    69  		t.Fatal(err)
    70  	}
    71  	if err := ioutil.WriteFile(artifactPath, timestampJSON, 0644); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	if err := ioutil.WriteFile(rootPath, rootJSON, 0644); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  }
    78  

View as plain text