@@ -19,6 +19,7 @@ import (
1919 "testing"
2020
2121 "github.com/golang/mock/gomock"
22+ "github.com/google/go-cmp/cmp"
2223
2324 "github.com/ossf/scorecard/v4/checker"
2425 mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
@@ -49,3 +50,92 @@ func TestRepeatedSetup(t *testing.T) {
4950 }
5051 }
5152}
53+
54+ func asPointer (s string ) * string {
55+ return & s
56+ }
57+
58+ type stubDigester struct {}
59+
60+ func (s stubDigester ) Digest (name string ) (string , error ) {
61+ m := map [string ]string {
62+ "foo" : "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" ,
63+ "baz" : "fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9" ,
64+ "amazoncorretto:11" : "b1a711069b801a325a30885f08f5067b2b102232379750dda4d25a016afd9a88" ,
65+ }
66+ hash , ok := m [name ]
67+ if ! ok {
68+ //nolint:goerr113
69+ return "" , fmt .Errorf ("no hash for image: %q" , name )
70+ }
71+ return fmt .Sprintf ("sha256:%s" , hash ), nil
72+ }
73+
74+ func TestCreateDockerfilePinningRemediation (t * testing.T ) {
75+ t .Parallel ()
76+
77+ //nolint:govet,lll
78+ tests := []struct {
79+ name string
80+ dep checker.Dependency
81+ expected * checker.Remediation
82+ }{
83+ {
84+ name : "no depdendency" ,
85+ dep : checker.Dependency {},
86+ expected : nil ,
87+ },
88+ {
89+ name : "image name no tag" ,
90+ dep : checker.Dependency {
91+ Name : asPointer ("foo" ),
92+ Type : checker .DependencyUseTypeDockerfileContainerImage ,
93+ },
94+ expected : & checker.Remediation {
95+ HelpText : "pin your Docker image by updating foo to foo@sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" ,
96+ HelpMarkdown : "pin your Docker image by updating foo to foo@sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" ,
97+ },
98+ },
99+ {
100+ // github.com/ossf/scorecard/issues/2581
101+ name : "image name with tag" ,
102+ dep : checker.Dependency {
103+ Name : asPointer ("amazoncorretto" ),
104+ PinnedAt : asPointer ("11" ),
105+ Type : checker .DependencyUseTypeDockerfileContainerImage ,
106+ },
107+ expected : & checker.Remediation {
108+ HelpText : "pin your Docker image by updating amazoncorretto:11 to amazoncorretto:11@sha256:b1a711069b801a325a30885f08f5067b2b102232379750dda4d25a016afd9a88" ,
109+ HelpMarkdown : "pin your Docker image by updating amazoncorretto:11 to amazoncorretto:11@sha256:b1a711069b801a325a30885f08f5067b2b102232379750dda4d25a016afd9a88" ,
110+ },
111+ },
112+ {
113+ name : "unknown image" ,
114+ dep : checker.Dependency {
115+ Name : asPointer ("not-found" ),
116+ Type : checker .DependencyUseTypeDockerfileContainerImage ,
117+ },
118+ expected : nil ,
119+ },
120+ {
121+ name : "unknown tag" ,
122+ dep : checker.Dependency {
123+ Name : asPointer ("foo" ),
124+ PinnedAt : asPointer ("not-found" ),
125+ Type : checker .DependencyUseTypeDockerfileContainerImage ,
126+ },
127+ expected : nil ,
128+ },
129+ }
130+
131+ for _ , tt := range tests {
132+ tt := tt
133+ t .Run (tt .name , func (t * testing.T ) {
134+ t .Parallel ()
135+ got := CreateDockerfilePinningRemediation (& tt .dep , stubDigester {})
136+ if ! cmp .Equal (got , tt .expected ) {
137+ t .Errorf (cmp .Diff (got , tt .expected ))
138+ }
139+ })
140+ }
141+ }
0 commit comments