-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cb304c
commit 3909233
Showing
5 changed files
with
242 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package grpc_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/plgd-dev/hub/v2/certificate-authority/service/grpc" | ||
"github.com/plgd-dev/hub/v2/pkg/config/property/urischeme" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCRLConfigValidate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input grpc.CRLConfig | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Disabled CRLConfig", | ||
input: grpc.CRLConfig{ | ||
Enabled: false, | ||
}, | ||
}, | ||
{ | ||
name: "Enabled CRLConfig with valid ExternalAddress and ExpiresIn", | ||
input: grpc.CRLConfig{ | ||
Enabled: true, | ||
ExternalAddress: "http://example.com/crl", | ||
ExpiresIn: time.Hour, | ||
}, | ||
}, | ||
{ | ||
name: "Enabled CRLConfig with empty ExternalAddress", | ||
input: grpc.CRLConfig{ | ||
Enabled: true, | ||
ExternalAddress: "", | ||
ExpiresIn: time.Hour, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Enabled CRLConfig with ExpiresIn less than 1 minute", | ||
input: grpc.CRLConfig{ | ||
Enabled: true, | ||
ExternalAddress: "http://example.com/crl", | ||
ExpiresIn: 30 * time.Second, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.input.Validate() | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestSignerConfigValidate(t *testing.T) { | ||
crl := grpc.CRLConfig{ | ||
Enabled: true, | ||
ExternalAddress: "http://example.com/crl", | ||
ExpiresIn: time.Hour, | ||
} | ||
tests := []struct { | ||
name string | ||
input grpc.SignerConfig | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid SignerConfig", | ||
input: grpc.SignerConfig{ | ||
CAPool: []string{"ca1.pem", "ca2.pem"}, | ||
KeyFile: urischeme.URIScheme("key.pem"), | ||
CertFile: urischeme.URIScheme("cert.pem"), | ||
ValidFrom: time.Now().Format(time.RFC3339), | ||
ExpiresIn: time.Hour * 24, | ||
CRL: crl, | ||
}, | ||
}, | ||
{ | ||
name: "Invalid CA Pool", | ||
input: grpc.SignerConfig{ | ||
CAPool: 42, | ||
KeyFile: urischeme.URIScheme("key.pem"), | ||
CertFile: urischeme.URIScheme("cert.pem"), | ||
ValidFrom: time.Now().Format(time.RFC3339), | ||
ExpiresIn: time.Hour * 24, | ||
CRL: crl, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Empty CertFile", | ||
input: grpc.SignerConfig{ | ||
CAPool: []string{"ca1.pem"}, | ||
KeyFile: urischeme.URIScheme("key.pem"), | ||
CertFile: "", | ||
ValidFrom: time.Now().Format(time.RFC3339), | ||
ExpiresIn: time.Hour * 24, | ||
CRL: crl, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Empty KeyFile", | ||
input: grpc.SignerConfig{ | ||
CAPool: []string{"ca1.pem"}, | ||
KeyFile: "", | ||
CertFile: urischeme.URIScheme("cert.pem"), | ||
ValidFrom: time.Now().Format(time.RFC3339), | ||
ExpiresIn: time.Hour * 24, | ||
CRL: crl, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid ExpiresIn", | ||
input: grpc.SignerConfig{ | ||
CAPool: []string{"ca1.pem", "ca2.pem"}, | ||
KeyFile: urischeme.URIScheme("key.pem"), | ||
CertFile: urischeme.URIScheme("cert.pem"), | ||
ValidFrom: time.Now().Format(time.RFC3339), | ||
ExpiresIn: -1, | ||
CRL: crl, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid ValidFrom format", | ||
input: grpc.SignerConfig{ | ||
CAPool: []string{"ca1.pem"}, | ||
KeyFile: urischeme.URIScheme("key.pem"), | ||
CertFile: urischeme.URIScheme("cert.pem"), | ||
ValidFrom: "invalid-date", | ||
ExpiresIn: time.Hour * 24, | ||
CRL: crl, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid CRL", | ||
input: grpc.SignerConfig{ | ||
CAPool: []string{"ca1.pem", "ca2.pem"}, | ||
KeyFile: urischeme.URIScheme("key.pem"), | ||
CertFile: urischeme.URIScheme("cert.pem"), | ||
ValidFrom: time.Now().Format(time.RFC3339), | ||
ExpiresIn: time.Hour * 24, | ||
CRL: grpc.CRLConfig{ | ||
Enabled: true, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.input.Validate() | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package store | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRevocationListsQueryValidate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input RevocationListsQuery | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid query with one valid UUID", | ||
input: RevocationListsQuery{ | ||
IssuerIdFilter: []string{uuid.New().String()}, | ||
IncludeExpired: true, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Valid query with multiple valid UUIDs", | ||
input: RevocationListsQuery{ | ||
IssuerIdFilter: []string{uuid.New().String(), uuid.New().String()}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Invalid query with an invalid UUID", | ||
input: RevocationListsQuery{ | ||
IssuerIdFilter: []string{uuid.New().String(), "invalid-uuid"}, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Valid query with empty IssuerIdFilter", | ||
input: RevocationListsQuery{ | ||
IssuerIdFilter: []string{}, | ||
IncludeExpired: true, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.input.Validate() | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters