Skip to content

Commit

Permalink
Address sonarcloud duplicate code report
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat committed Mar 1, 2024
1 parent 1e81dd1 commit d05d528
Showing 1 changed file with 19 additions and 49 deletions.
68 changes: 19 additions & 49 deletions pkg/certificates/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,17 @@ func TestCreateCANegative(t *testing.T) {
}
}

func TestCreateCertReqValid(t *testing.T) {
type args struct {
opts *certificates.CertOptions
privateKey *rsa.PrivateKey
}

func setupGoodCertRequest() (certificates.CertOptions, *x509.CertificateRequest, error) {
goodCaTimeAfterString := "2032-01-07T00:03:51Z"
goodCaTimeAfter, err := time.Parse(time.RFC3339, goodCaTimeAfterString)
if err != nil {
t.Errorf("Error parsing time %s: %v", goodCaTimeAfterString, err)
return certificates.CertOptions{}, &x509.CertificateRequest{}, err
}

goodCaTimeBeforeString := "2022-01-07T00:03:51Z"
goodCaTimeBefore, err := time.Parse(time.RFC3339, goodCaTimeBeforeString)
if err != nil {
t.Errorf("Error parsing time %s: %v", goodCaTimeBeforeString, err)
return certificates.CertOptions{}, &x509.CertificateRequest{}, err
}

goodCertOptions := certificates.CertOptions{
Expand Down Expand Up @@ -337,6 +332,20 @@ func TestCreateCertReqValid(t *testing.T) {
Version: 0,
}

return goodCertOptions, goodCertificateRequest, nil
}

func TestCreateCertReqValid(t *testing.T) {
type args struct {
opts *certificates.CertOptions
privateKey *rsa.PrivateKey
}

goodCertOptions, goodCertificateRequest, err := setupGoodCertRequest()
if err != nil {
t.Fatal(err)
}

goodPrivateKeyBlock, _ := pem.Decode([]byte(`-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEAp17EU0yKFdjqoec7zSc0AWDmT6cZpys8C74HqKeOArJPvswE
b4OVyKZj20hFNj2N6TRry0x+pw+eP2XziEc0jdIqb33K6SbZKyezmKNYF+0TlzN9
Expand Down Expand Up @@ -509,48 +518,9 @@ func TestCreateCertReqWithKey(t *testing.T) {
opts *certificates.CertOptions
}

goodCaTimeAfterString := "2032-01-07T00:03:51Z"
goodCaTimeAfter, err := time.Parse(time.RFC3339, goodCaTimeAfterString)
goodCertOptions, goodCertificateRequest, err := setupGoodCertRequest()
if err != nil {
t.Errorf("Error parsing time %s: %v", goodCaTimeAfterString, err)
}
goodCaTimeBeforeString := "2022-01-07T00:03:51Z"
goodCaTimeBefore, err := time.Parse(time.RFC3339, goodCaTimeBeforeString)
if err != nil {
t.Errorf("Error parsing time %s: %v", goodCaTimeBeforeString, err)
}

goodCertOptions := certificates.CertOptions{
Bits: 4096,
CommonName: "Ansible Automation Controller Receptor",
NotAfter: goodCaTimeAfter,
NotBefore: goodCaTimeBefore,
}

goodCertificateRequest := &x509.CertificateRequest{
Attributes: nil,
DNSNames: nil,
EmailAddresses: nil,
Extensions: nil,
ExtraExtensions: nil,
IPAddresses: nil,
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA256WithRSA,
Subject: pkix.Name{
CommonName: goodCertOptions.CommonName,
Country: nil,
ExtraNames: []pkix.AttributeTypeAndValue{},
Locality: nil,
Names: []pkix.AttributeTypeAndValue{},
Organization: nil,
OrganizationalUnit: nil,
PostalCode: nil,
Province: nil,
SerialNumber: "",
StreetAddress: nil,
},
URIs: nil,
Version: 0,
t.Fatal(err)
}

tests := []struct {
Expand Down

0 comments on commit d05d528

Please sign in to comment.