Skip to content

Commit

Permalink
Fixing linting errors.
Browse files Browse the repository at this point in the history
Combined certificate chain initialization into a concise slice literal. Removed unused mock structures in ca_certs tests. Enhanced error messages for failError function and added additional failure checks. Updated identifier representation in ura_issuer_test.go for consistency.
  • Loading branch information
rolandgroen committed Oct 16, 2024
1 parent 24ab559 commit 97c4c25
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
11 changes: 0 additions & 11 deletions ca_certs/uzi_ca_certs_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package ca_certs

import (
"io"
"testing"
)

// Mock structure of UZICaPool for the purpose of test
type mockUziCaPool struct{}

// Mock structure of http response for the purpose of test
type mockResponse struct {
Status string
StatusCode int
Body io.ReadCloser
}

func TestGetCertPools(t *testing.T) {
// Define the test cases
tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion pem/pem_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func convertToString(certBlock []byte) string {

func failError(t *testing.T, err error) {
if err != nil {
t.Errorf(err.Error())
t.Errorf("an error occured: %v", err.Error())
t.Fatal(err)
}
}
20 changes: 14 additions & 6 deletions uzi_vc_issuer/ura_issuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package uzi_vc_issuer

import (
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/json"
"fmt"
clo "github.com/huandu/go-clone"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/did"
Expand All @@ -28,8 +31,7 @@ func TestBuildUraVerifiableCredential(t *testing.T) {
{
name: "empty chain",
in: func(certs []*x509.Certificate) ([]*x509.Certificate, *rsa.PrivateKey, string) {
certs = make([]*x509.Certificate, 0)
return certs, privateKey, "did:example:123"
return []*x509.Certificate{}, privateKey, "did:example:123"
},
errorText: "empty certificate chain",
},
Expand Down Expand Up @@ -227,12 +229,16 @@ func TestIssue(t *testing.T) {

brokenChain, _, _, _, _, err := x509_cert.BuildSelfSignedCertChain("KAAS")
failError(t, err)
chain, _, _, privKey, _, err := x509_cert.BuildSelfSignedCertChain("2.16.528.1.1007.99.2110-1-900030787-S-90000380-00.000-11223344")
identifier := "2.16.528.1.1007.99.2110-1-900030787-S-90000380-00.000-11223344"
chain, _, rootCert, privKey, _, err := x509_cert.BuildSelfSignedCertChain(identifier)
bytesRootHash := sha512.Sum512(rootCert.Raw)
rootHash := base64.RawURLEncoding.EncodeToString(bytesRootHash[:])
failError(t, err)

chainPems, err := x509_cert.EncodeCertificates(chain...)
failError(t, err)
siglePem, err := x509_cert.EncodeCertificates(chain[0])
failError(t, err)
brokenPem, err := x509_cert.EncodeCertificates(brokenChain...)
failError(t, err)
signingKeyPem, err := x509_cert.EncodeRSAPrivateKey(privKey)
Expand All @@ -254,12 +260,14 @@ func TestIssue(t *testing.T) {
failError(t, err)

keyFile, err := os.CreateTemp(t.TempDir(), "signing_key.pem")
failError(t, err)
err = os.WriteFile(keyFile.Name(), signingKeyPem, 0644)
failError(t, err)

emptyFile, err := os.CreateTemp(t.TempDir(), "empty.pem")
failError(t, err)
err = os.WriteFile(emptyFile.Name(), []byte{}, 0644)
failError(t, err)

tests := []struct {
name string
Expand All @@ -278,7 +286,7 @@ func TestIssue(t *testing.T) {
allowTest: true,
out: &vc.VerifiableCredential{
Context: []ssi.URI{ssi.MustParseURI("https://www.w3.org/2018/credentials/v1")},
Issuer: did.MustParseDID("did:example:123").URI(),
Issuer: did.MustParseDID(fmt.Sprintf("did:x509:0:sha512:%s::san:otherName:%s", rootHash, identifier)).URI(),
Type: []ssi.URI{ssi.MustParseURI("VerifiableCredential"), ssi.MustParseURI("UziServerCertificateCredential")},
},
errorText: "",
Expand Down Expand Up @@ -322,7 +330,7 @@ func TestIssue(t *testing.T) {
}
} else if err == nil && tt.errorText != "" {
t.Errorf("Issue() unexpected success, want error")
} else if err != nil {
} else if err == nil {
found := vc.VerifiableCredential{}
err = json.Unmarshal([]byte("\""+result+"\""), &found)
failError(t, err)
Expand All @@ -336,7 +344,7 @@ func TestIssue(t *testing.T) {

func failError(t *testing.T, err error) {
if err != nil {
t.Errorf(err.Error())
t.Errorf("an error occured: %v", err.Error())
t.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion x509_cert/x509_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestParsePrivateKey(t *testing.T) {
}
func failError(t *testing.T, err error) {
if err != nil {
t.Errorf(err.Error())
t.Errorf("an error occured: %v", err.Error())
t.Fatal(err)
}
}
5 changes: 1 addition & 4 deletions x509_cert/x509_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ func BuildSelfSignedCertChain(identifier string) (chain []*x509.Certificate, cha
return nil, nil, nil, nil, nil, err
}

chain = make([]*x509.Certificate, 4)
for i, c := range []*x509.Certificate{signingCert, intermediateL2Cert, intermediateL1Cert, rootCert} {
chain[i] = c
}
chain = []*x509.Certificate{signingCert, intermediateL2Cert, intermediateL1Cert, rootCert}

chainPems = &cert.Chain{}
for _, p := range [][]byte{signingPEM, intermediateL2Pem, intermediateL1Pem, rootPem} {
Expand Down

0 comments on commit 97c4c25

Please sign in to comment.