@@ -17,10 +17,12 @@ import (
17
17
"context"
18
18
"crypto/x509"
19
19
"crypto/x509/pkix"
20
+ "encoding/json"
20
21
"encoding/pem"
21
22
"errors"
22
23
"fmt"
23
24
"net/http"
25
+ "os"
24
26
"path/filepath"
25
27
"reflect"
26
28
"strconv"
@@ -728,9 +730,13 @@ func TestNewVerifierWithOptions(t *testing.T) {
728
730
if err != nil {
729
731
t .Fatalf ("unexpected error while creating revocation object: %v" , err )
730
732
}
731
- opts := VerifierOptions {RevocationClient : r }
732
733
733
- v , err := NewVerifierWithOptions (& ociPolicy , & blobPolicy , store , pm , opts )
734
+ v , err := NewVerifierWithOptions (store , VerifierOptions {
735
+ RevocationClient : r ,
736
+ OCITrustPolicy : & ociPolicy ,
737
+ BlobTrustPolicy : & blobPolicy ,
738
+ PluginManager : pm ,
739
+ })
734
740
if err != nil {
735
741
t .Fatalf ("expected NewVerifierWithOptions constructor to succeed, but got %v" , err )
736
742
}
@@ -750,18 +756,28 @@ func TestNewVerifierWithOptions(t *testing.T) {
750
756
t .Fatal ("expected nil revocationCodeSigningValidator" )
751
757
}
752
758
753
- _ , err = NewVerifierWithOptions (nil , & blobPolicy , store , pm , opts )
759
+ _ , err = NewVerifierWithOptions (store , VerifierOptions {
760
+ RevocationClient : r ,
761
+ BlobTrustPolicy : & blobPolicy ,
762
+ PluginManager : pm ,
763
+ })
754
764
if err != nil {
755
765
t .Fatalf ("expected NewVerifierWithOptions constructor to succeed, but got %v" , err )
756
766
}
757
767
758
- _ , err = NewVerifierWithOptions (& ociPolicy , nil , store , pm , opts )
768
+ _ , err = NewVerifierWithOptions (store , VerifierOptions {
769
+ RevocationClient : r ,
770
+ OCITrustPolicy : & ociPolicy ,
771
+ PluginManager : pm ,
772
+ })
759
773
if err != nil {
760
774
t .Fatalf ("expected NewVerifierWithOptions constructor to succeed, but got %v" , err )
761
775
}
762
776
763
- opts .RevocationClient = nil
764
- _ , err = NewVerifierWithOptions (& ociPolicy , nil , store , pm , opts )
777
+ _ , err = NewVerifierWithOptions (store , VerifierOptions {
778
+ OCITrustPolicy : & ociPolicy ,
779
+ PluginManager : pm ,
780
+ })
765
781
if err != nil {
766
782
t .Fatalf ("expected NewVerifierWithOptions constructor to succeed, but got %v" , err )
767
783
}
@@ -770,19 +786,11 @@ func TestNewVerifierWithOptions(t *testing.T) {
770
786
if err != nil {
771
787
t .Fatal (err )
772
788
}
773
- opts = VerifierOptions {
789
+ v , err = NewVerifierWithOptions ( store , VerifierOptions {
774
790
RevocationCodeSigningValidator : csValidator ,
775
- }
776
- v , err = NewVerifierWithOptions (& ociPolicy , nil , store , pm , opts )
777
- if err != nil {
778
- t .Fatalf ("expected NewVerifierWithOptions constructor to succeed, but got %v" , err )
779
- }
780
- if v .revocationCodeSigningValidator == nil {
781
- t .Fatal ("expected v.revocationCodeSigningValidator to be non-nil" )
782
- }
783
-
784
- opts = VerifierOptions {}
785
- v , err = NewVerifierWithOptions (& ociPolicy , nil , store , pm , opts )
791
+ OCITrustPolicy : & ociPolicy ,
792
+ PluginManager : pm ,
793
+ })
786
794
if err != nil {
787
795
t .Fatalf ("expected NewVerifierWithOptions constructor to succeed, but got %v" , err )
788
796
}
@@ -803,22 +811,68 @@ func TestNewVerifierWithOptionsError(t *testing.T) {
803
811
if err != nil {
804
812
t .Fatalf ("unexpected error while creating revocation timestamp object: %v" , err )
805
813
}
806
- opts := VerifierOptions {
814
+
815
+ _ , err = NewVerifierWithOptions (store , VerifierOptions {
807
816
RevocationClient : r ,
808
817
RevocationTimestampingValidator : rt ,
809
- }
810
-
811
- _ , err = NewVerifierWithOptions (nil , nil , store , pm , opts )
818
+ PluginManager : pm ,
819
+ })
812
820
if err == nil || err .Error () != "ociTrustPolicy and blobTrustPolicy both cannot be nil" {
813
821
t .Errorf ("expected err but not found." )
814
822
}
815
823
816
- _ , err = NewVerifierWithOptions (& ociPolicy , & blobPolicy , nil , pm , opts )
824
+ _ , err = NewVerifierWithOptions (nil , VerifierOptions {
825
+ RevocationClient : r ,
826
+ RevocationTimestampingValidator : rt ,
827
+ OCITrustPolicy : & ociPolicy ,
828
+ BlobTrustPolicy : & blobPolicy ,
829
+ PluginManager : pm ,
830
+ })
817
831
if err == nil || err .Error () != "trustStore cannot be nil" {
818
832
t .Errorf ("expected err but not found." )
819
833
}
820
834
}
821
835
836
+ func TestNewOCIVerifierFromConfig (t * testing.T ) {
837
+ defer func (oldUserConfigDir string ) {
838
+ dir .UserConfigDir = oldUserConfigDir
839
+ }(dir .UserConfigDir )
840
+
841
+ tempRoot := t .TempDir ()
842
+ dir .UserConfigDir = tempRoot
843
+ path := filepath .Join (tempRoot , "trustpolicy.oci.json" )
844
+ policyJson , _ := json .Marshal (dummyOCIPolicyDocument ())
845
+ if err := os .WriteFile (path , policyJson , 0600 ); err != nil {
846
+ t .Fatalf ("TestLoadOCIDocument write policy file failed. Error: %v" , err )
847
+ }
848
+ t .Cleanup (func () { os .RemoveAll (tempRoot ) })
849
+
850
+ _ , err := NewOCIVerifierFromConfig ()
851
+ if err != nil {
852
+ t .Fatalf ("expected NewOCIVerifierFromConfig constructor to succeed, but got %v" , err )
853
+ }
854
+ }
855
+
856
+ func TestNewBlobVerifierFromConfig (t * testing.T ) {
857
+ defer func (oldUserConfigDir string ) {
858
+ dir .UserConfigDir = oldUserConfigDir
859
+ }(dir .UserConfigDir )
860
+
861
+ tempRoot := t .TempDir ()
862
+ dir .UserConfigDir = tempRoot
863
+ path := filepath .Join (tempRoot , "trustpolicy.blob.json" )
864
+ policyJson , _ := json .Marshal (dummyBlobPolicyDocument ())
865
+ if err := os .WriteFile (path , policyJson , 0600 ); err != nil {
866
+ t .Fatalf ("TestLoadBlobDocument write policy file failed. Error: %v" , err )
867
+ }
868
+ t .Cleanup (func () { os .RemoveAll (tempRoot ) })
869
+
870
+ _ , err := NewBlobVerifierFromConfig ()
871
+ if err != nil {
872
+ t .Fatalf ("expected NewBlobVerifierFromConfig constructor to succeed, but got %v" , err )
873
+ }
874
+ }
875
+
822
876
func TestVerifyBlob (t * testing.T ) {
823
877
policy := & trustpolicy.BlobDocument {
824
878
Version : "1.0" ,
@@ -831,7 +885,10 @@ func TestVerifyBlob(t *testing.T) {
831
885
},
832
886
},
833
887
}
834
- v , err := NewVerifier (nil , policy , & testTrustStore {}, pm )
888
+ v , err := NewVerifierWithOptions (& testTrustStore {}, VerifierOptions {
889
+ BlobTrustPolicy : policy ,
890
+ PluginManager : pm ,
891
+ })
835
892
if err != nil {
836
893
t .Fatalf ("unexpected error while creating verifier: %v" , err )
837
894
}
@@ -877,7 +934,10 @@ func TestVerifyBlob_Error(t *testing.T) {
877
934
},
878
935
},
879
936
}
880
- v , err := NewVerifier (nil , policy , & testTrustStore {}, pm )
937
+ v , err := NewVerifierWithOptions (& testTrustStore {}, VerifierOptions {
938
+ BlobTrustPolicy : policy ,
939
+ PluginManager : pm ,
940
+ })
881
941
if err != nil {
882
942
t .Fatalf ("unexpected error while creating verifier: %v" , err )
883
943
}
0 commit comments