-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for the import and certificate commands
Issue: #84
- Loading branch information
Showing
7 changed files
with
213 additions
and
8 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,50 @@ | ||
package commands | ||
|
||
import ( | ||
"math/big" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/pimg/certguard/internal/ports/models/messages" | ||
"github.com/pimg/certguard/pkg/domain/crl" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseCertificate(t *testing.T) { | ||
certRaw, err := os.ReadFile(filepath.Join("..", "..", "..", "..", "testing", "pki", "org-on-crl.pem")) | ||
assert.NoError(t, err) | ||
|
||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
cmd := cmds.ParsePemCertficate(string(certRaw)) | ||
assert.NotNil(t, cmd) | ||
|
||
certMsg := cmd() | ||
assert.NotNil(t, certMsg) | ||
|
||
msg := certMsg.(messages.PemCertificateMsg) | ||
|
||
serialNum, success := new(big.Int).SetString("277698924469047062536476011533217874011933401810", 0) | ||
assert.True(t, success) | ||
assert.Equal(t, serialNum, msg.Certificate.SerialNumber) | ||
} | ||
|
||
func TestParseCertificateInvalid(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
cmd := cmds.ParsePemCertficate("this is not a valid certificate") | ||
assert.NotNil(t, cmd) | ||
|
||
msg := cmd() | ||
|
||
errMsg := msg.(messages.ErrorMsg) | ||
|
||
assert.Equal(t, "failed to parse certificate", errMsg.Err.Error()) | ||
} |
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,92 @@ | ||
package commands | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pimg/certguard/internal/ports/models/messages" | ||
"github.com/pimg/certguard/pkg/domain/crl" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestImportCRL(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
msg := cmds.ImportFile(filepath.Join("..", "..", "..", "..", "testing", "pki", "ca.crl"))() | ||
|
||
crlMsg := msg.(messages.CRLResponseMsg) | ||
|
||
assert.Len(t, crlMsg.RevocationList.RevokedCertificateEntries, 1) | ||
} | ||
|
||
func TestImportPEM(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
msg := cmds.ImportFile(filepath.Join("..", "..", "..", "..", "testing", "pki", "org-on-crl.pem"))() | ||
|
||
pemMsg := msg.(messages.PemCertificateMsg) | ||
|
||
assert.NotEmpty(t, pemMsg) | ||
assert.Equal(t, "277698924469047062536476011533217874011933401810", pemMsg.Certificate.SerialNumber.String()) | ||
} | ||
|
||
func TestImportCRLInvalidPath(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
msg := cmds.ImportFile(filepath.Join("..", "..", "..", "..", "testing", "pki", "idonotexist.crl"))() | ||
|
||
errMsg := msg.(messages.ErrorMsg) | ||
|
||
assert.ErrorContains(t, errMsg.Err, "could not load CRL from import location") | ||
} | ||
|
||
func TestImportMalformedCertificate(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
msg := cmds.ImportFile(filepath.Join("..", "..", "..", "..", "testing", "pki", "malformed-certificate.pem"))() | ||
|
||
errMsg := msg.(messages.ErrorMsg) | ||
assert.ErrorContains(t, errMsg.Err, "failed to parse certificate") | ||
} | ||
|
||
func TestImportMalformedCRL(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
msg := cmds.ImportFile(filepath.Join("..", "..", "..", "..", "testing", "pki", "malformed.crl"))() | ||
|
||
errMsg := msg.(messages.ErrorMsg) | ||
assert.ErrorContains(t, errMsg.Err, "could not parse CRL\nx509: malformed crl\ncannot parse CRL from") | ||
} | ||
|
||
func TestGetRevokedCertificatesNoRevokedCertificatesFound(t *testing.T) { | ||
storage, err := crl.NewMockStorage() | ||
assert.NoError(t, err) | ||
|
||
cmds := NewCommands(storage) | ||
|
||
msg := cmds.GetRevokedCertificates(&GetRevokedCertificatesArgs{ | ||
ID: "1", | ||
CN: "testCN", | ||
ThisUpdate: time.Now().Format(time.DateOnly), | ||
NextUpdate: time.Now().Format(time.DateOnly), | ||
URL: "http://example.com", | ||
})() | ||
|
||
assert.NotNil(t, msg) | ||
} |
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
Binary file not shown.
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,3 @@ | ||
-----BEGIN CERTIFICATE----- | ||
THIS IS A MALFORMED CERTIFICATE | ||
-----END CERTIFICATE----- |
Binary file not shown.
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,33 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIFyDCCA7CgAwIBAgIUMKRzU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