From d58efa3409008666d9d2619038eac84b21e08429 Mon Sep 17 00:00:00 2001
From: dlorenc <dlorenc@google.com>
Date: Mon, 20 Dec 2021 07:33:54 -0600
Subject: [PATCH] Deduplicate some test logic. (#554)

This was duplicated across the unit tests for all of our types, moved it
up to the top-level package.

Signed-off-by: Dan Lorenc <lorenc.d@gmail.com>
---
 pkg/types/alpine/alpine_test.go             | 44 +---------------
 pkg/types/hashedrekord/hashedrekord_test.go | 40 +--------------
 pkg/types/helm/helm_test.go                 | 44 +---------------
 pkg/types/intoto/intoto_test.go             | 44 +---------------
 pkg/types/jar/jar_test.go                   | 44 +---------------
 pkg/types/rekord/rekord_test.go             | 44 +---------------
 pkg/types/rfc3161/rfc3161_test.go           | 47 +----------------
 pkg/types/rpm/rpm_test.go                   | 44 +---------------
 pkg/types/test_util.go                      | 57 +++++++++++++++++++++
 pkg/types/tuf/tuf_test.go                   | 40 +--------------
 10 files changed, 75 insertions(+), 373 deletions(-)
 create mode 100644 pkg/types/test_util.go

diff --git a/pkg/types/alpine/alpine_test.go b/pkg/types/alpine/alpine_test.go
index a1e5741..1128db3 100644
--- a/pkg/types/alpine/alpine_test.go
+++ b/pkg/types/alpine/alpine_test.go
@@ -16,7 +16,6 @@
 package alpine
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,50 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Alpine
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/hashedrekord/hashedrekord_test.go b/pkg/types/hashedrekord/hashedrekord_test.go
index 0abde63..5dda7f5 100644
--- a/pkg/types/hashedrekord/hashedrekord_test.go
+++ b/pkg/types/hashedrekord/hashedrekord_test.go
@@ -16,7 +16,6 @@
 package hashedrekord
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,46 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Hashedrekord
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/helm/helm_test.go b/pkg/types/helm/helm_test.go
index e628a57..72cba15 100644
--- a/pkg/types/helm/helm_test.go
+++ b/pkg/types/helm/helm_test.go
@@ -16,7 +16,6 @@
 package helm
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,50 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Helm
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/intoto/intoto_test.go b/pkg/types/intoto/intoto_test.go
index 7ff7cfd..69a9bbb 100644
--- a/pkg/types/intoto/intoto_test.go
+++ b/pkg/types/intoto/intoto_test.go
@@ -16,7 +16,6 @@
 package intoto
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,50 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Intoto
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/jar/jar_test.go b/pkg/types/jar/jar_test.go
index 92ee515..b602141 100644
--- a/pkg/types/jar/jar_test.go
+++ b/pkg/types/jar/jar_test.go
@@ -16,7 +16,6 @@
 package jar
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -27,50 +26,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Jar
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/rekord/rekord_test.go b/pkg/types/rekord/rekord_test.go
index 1f781eb..7b77db6 100644
--- a/pkg/types/rekord/rekord_test.go
+++ b/pkg/types/rekord/rekord_test.go
@@ -16,7 +16,6 @@
 package rekord
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,50 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Rekord
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/rfc3161/rfc3161_test.go b/pkg/types/rfc3161/rfc3161_test.go
index dd8c6e6..fda5902 100644
--- a/pkg/types/rfc3161/rfc3161_test.go
+++ b/pkg/types/rfc3161/rfc3161_test.go
@@ -16,7 +16,6 @@
 package rfc3161
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,53 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Rfc3161
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalFailsTester) Attestation() (string, []byte) {
-	return "", nil
-}
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/rpm/rpm_test.go b/pkg/types/rpm/rpm_test.go
index df276d5..9fa25ed 100644
--- a/pkg/types/rpm/rpm_test.go
+++ b/pkg/types/rpm/rpm_test.go
@@ -16,7 +16,6 @@
 package rpm
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,50 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.Rpm
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Validate() error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
diff --git a/pkg/types/test_util.go b/pkg/types/test_util.go
new file mode 100644
index 0000000..bd663e0
--- /dev/null
+++ b/pkg/types/test_util.go
@@ -0,0 +1,57 @@
+/*
+Copyright © 2021 The Sigstore Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package types
+
+import (
+	"context"
+
+	"github.com/sigstore/rekor/pkg/generated/models"
+)
+
+type BaseUnmarshalTester struct{}
+
+func (u BaseUnmarshalTester) NewEntry() EntryImpl {
+	return &BaseUnmarshalTester{}
+}
+
+func (u BaseUnmarshalTester) APIVersion() string {
+	return "2.0.1"
+}
+
+func (u BaseUnmarshalTester) IndexKeys() ([]string, error) {
+	return []string{}, nil
+}
+
+func (u BaseUnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
+	return nil, nil
+}
+
+func (u BaseUnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
+	return nil
+}
+
+func (u BaseUnmarshalTester) Validate() error {
+	return nil
+}
+
+func (u BaseUnmarshalTester) Attestation() (string, []byte) {
+	return "", nil
+}
+
+func (u BaseUnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ ArtifactProperties) (models.ProposedEntry, error) {
+	return nil, nil
+}
diff --git a/pkg/types/tuf/tuf_test.go b/pkg/types/tuf/tuf_test.go
index 172d0cb..572947f 100644
--- a/pkg/types/tuf/tuf_test.go
+++ b/pkg/types/tuf/tuf_test.go
@@ -17,7 +17,6 @@ limitations under the License.
 package tuf
 
 import (
-	"context"
 	"errors"
 	"testing"
 
@@ -28,46 +27,11 @@ import (
 
 type UnmarshalTester struct {
 	models.TUF
-}
-
-func (u UnmarshalTester) NewEntry() types.EntryImpl {
-	return &UnmarshalTester{}
-}
-
-func (u UnmarshalTester) APIVersion() string {
-	return "2.0.1"
-}
-
-func (u UnmarshalTester) IndexKeys() ([]string, error) {
-	return []string{}, nil
-}
-
-func (u UnmarshalTester) Canonicalize(ctx context.Context) ([]byte, error) {
-	return nil, nil
-}
-
-func (u UnmarshalTester) HasExternalEntities() bool {
-	return false
-}
-
-func (u *UnmarshalTester) FetchExternalEntities(ctx context.Context) error {
-	return nil
-}
-
-func (u UnmarshalTester) Unmarshal(pe models.ProposedEntry) error {
-	return nil
-}
-
-func (u UnmarshalTester) Attestation() (string, []byte) {
-	return "", nil
-}
-
-func (u UnmarshalTester) CreateFromArtifactProperties(_ context.Context, _ types.ArtifactProperties) (models.ProposedEntry, error) {
-	return nil, nil
+	types.BaseUnmarshalTester
 }
 
 type UnmarshalFailsTester struct {
-	UnmarshalTester
+	types.BaseUnmarshalTester
 }
 
 func (u UnmarshalFailsTester) NewEntry() types.EntryImpl {
-- 
GitLab