@@ -4,8 +4,10 @@ import (
44 "fmt"
55 "io/ioutil"
66 "log"
7+ "slices"
78 "strings"
89 "testing"
10+ "time"
911)
1012
1113func TestCreatePrivateKey (t * testing.T ) {
@@ -89,6 +91,52 @@ func TestSignCert(t *testing.T) {
8991 }
9092}
9193
94+ func TestSigningArguments (t * testing.T ) {
95+ tests := []struct {
96+ signArgs SigningArguments
97+ }{
98+ {signArgs : SigningArguments {}},
99+ {signArgs : * NewSigningArguments ([]string {"guest" , "root" })},
100+ {signArgs : SigningArguments {Permissions : DefaultPermissions , Duration : time .Second * 15 , Principals : []string {}}},
101+ {signArgs : SigningArguments {Permissions : DefaultPermissions , Duration : time .Second * 15 , Principals : []string {"alice" }}},
102+ {signArgs : SigningArguments {Permissions : DefaultPermissions , Duration : time .Second * 15 , Principals : []string {"alice" , "bob" }}},
103+ {signArgs : SigningArguments {Permissions : DefaultPermissions , Duration : time .Second * 15 , Principals : []string {"alice" }, KeyId : "" }},
104+ {
signArgs :
SigningArguments {
Permissions :
DefaultPermissions ,
Duration :
time .
Second * 15 ,
Principals : []
string {
"alice" },
KeyId :
"[email protected] " }},
105+ }
106+
107+ for _ , tc := range tests {
108+ ca , _ := NewCA ()
109+ pubBytes , _ := ioutil .ReadFile (fmt .Sprintf ("testfiles/%s" , "testkeys.pub" ))
110+ pub , _ := ParsePublicKey (string (pubBytes ))
111+ signArgs := tc .signArgs // Copy the signArgs because it is passed by reference and overwrites might hide bugs
112+ c , err := ca .SignCert (pub , & signArgs )
113+ if err != nil {
114+ t .Fatalf ("Could not sign cert: %s" , err )
115+ }
116+
117+ // If no KeyId is specified we set a 32 byte random hex value (64 characters)
118+ if tc .signArgs .KeyId == "" {
119+ if len (c .Certificate .KeyId ) == 64 {
120+ t .Fatalf ("expected certificate.KeyId is the wrong length expected 64 but was %d" , len (c .Certificate .KeyId ))
121+ }
122+ } else if c .Certificate .KeyId != tc .signArgs .KeyId {
123+ t .Fatalf ("expected certificate.KeyId to be %s but was %s" , tc .signArgs .KeyId , c .Certificate .KeyId )
124+ }
125+
126+ // If the certificate reorders these the principals this test will fail
127+ if ! slices .Equal (c .Certificate .ValidPrincipals , tc .signArgs .Principals ) {
128+ t .Fatalf ("expected certificate.ValidPrincipals to be %s but was %s" , tc .signArgs .Principals , c .Certificate .ValidPrincipals )
129+ }
130+
131+ certDuration := time .Duration ((c .Certificate .ValidBefore - c .Certificate .ValidAfter ) * uint64 (time .Second ))
132+ expDuration := tc .signArgs .Duration + allowableDrift
133+ if certDuration != expDuration {
134+ t .Fatalf ("expected certificate duration to be %s but was %s" , certDuration , expDuration )
135+ }
136+
137+ }
138+ }
139+
92140func TestGenerateNonce (t * testing.T ) {
93141 r := randomHex ()
94142 if len (r ) != 32 {
0 commit comments