generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from deploymenttheory/feature-scaffolding
Feature scaffolding - added client assertion (oidc) auth method to provider for testing
- Loading branch information
Showing
7 changed files
with
548 additions
and
20 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
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,195 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
pkcs12 "software.sslmate.com/src/go-pkcs12" | ||
) | ||
|
||
func TestParseCertificateData(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
t.Run("Valid PFX with password", func(t *testing.T) { | ||
pfxData, password, err := generatePFXWithPassword() | ||
require.NoError(t, err) | ||
|
||
certs, privKey, err := ParseCertificateData(ctx, pfxData, []byte(password)) | ||
assert.NoError(t, err) | ||
assert.Len(t, certs, 1) | ||
assert.NotNil(t, privKey) | ||
_, ok := privKey.(*rsa.PrivateKey) | ||
assert.True(t, ok) | ||
}) | ||
|
||
t.Run("Valid PFX without password", func(t *testing.T) { | ||
pfxData, err := generatePFXWithoutPassword() | ||
require.NoError(t, err) | ||
|
||
certs, privKey, err := ParseCertificateData(ctx, pfxData, []byte("")) | ||
assert.NoError(t, err) | ||
assert.Len(t, certs, 1) | ||
assert.NotNil(t, privKey) | ||
_, ok := privKey.(*rsa.PrivateKey) | ||
assert.True(t, ok) | ||
}) | ||
|
||
t.Run("PFX with non-RSA key", func(t *testing.T) { | ||
pfxData, password, err := generatePFXWithNonRSAKey() | ||
require.NoError(t, err) | ||
|
||
_, _, err = ParseCertificateData(ctx, pfxData, []byte(password)) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "private key is not of RSA type") | ||
}) | ||
|
||
t.Run("Invalid PFX data", func(t *testing.T) { | ||
_, _, err := ParseCertificateData(ctx, []byte("invalid data"), []byte("password")) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "failed to parse PKCS#12 data") | ||
}) | ||
|
||
t.Run("Incorrect password", func(t *testing.T) { | ||
pfxData, _, err := generatePFXWithPassword() | ||
require.NoError(t, err) | ||
|
||
_, _, err = ParseCertificateData(ctx, pfxData, []byte("wrongpassword")) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "failed to parse PKCS#12 data") | ||
}) | ||
} | ||
|
||
// generatePFXWithPassword creates a PKCS#12 (PFX) certificate with an RSA private key and password | ||
func generatePFXWithPassword() (pfxData []byte, password string, err error) { | ||
// Generate RSA private key | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
// Create certificate template | ||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
Subject: pkix.Name{ | ||
CommonName: "Test Cert", | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(24 * time.Hour), | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
// Create certificate | ||
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
cert, err := x509.ParseCertificate(certDER) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
// Encode to PKCS#12 | ||
password = "testpassword" | ||
pfxData, err = pkcs12.Modern.Encode(privateKey, cert, nil, password) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return pfxData, password, nil | ||
} | ||
|
||
// generatePFXWithoutPassword creates a PKCS#12 (PFX) certificate with an RSA private key and no password | ||
func generatePFXWithoutPassword() (pfxData []byte, err error) { | ||
// Generate RSA private key | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create certificate template | ||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
Subject: pkix.Name{ | ||
CommonName: "Test Cert No Password", | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(24 * time.Hour), | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
// Create certificate | ||
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cert, err := x509.ParseCertificate(certDER) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Encode to PKCS#12 without password | ||
pfxData, err = pkcs12.Modern.Encode(privateKey, cert, nil, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pfxData, nil | ||
} | ||
|
||
// generatePFXWithNonRSAKey creates a PKCS#12 (PFX) certificate with an ECDSA private key (non-RSA) | ||
func generatePFXWithNonRSAKey() (pfxData []byte, password string, err error) { | ||
// Generate ECDSA private key | ||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
// Create certificate template | ||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
Subject: pkix.Name{ | ||
CommonName: "Test Cert ECDSA", | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(24 * time.Hour), | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
// Create certificate | ||
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
cert, err := x509.ParseCertificate(certDER) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
// Encode to PKCS#12 | ||
password = "testpassword" | ||
pfxData, err = pkcs12.Modern.Encode(privateKey, cert, nil, password) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return pfxData, password, nil | ||
} |
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 @@ | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStringPtrToString(t *testing.T) { | ||
t.Run("Non-nil string pointer", func(t *testing.T) { | ||
input := "test string" | ||
result := StringPtrToString(&input) | ||
assert.Equal(t, input, result, "Should return the dereferenced string value") | ||
}) | ||
|
||
t.Run("Nil string pointer", func(t *testing.T) { | ||
var input *string | ||
result := StringPtrToString(input) | ||
assert.Equal(t, "", result, "Should return an empty string for nil input") | ||
}) | ||
|
||
t.Run("Empty string pointer", func(t *testing.T) { | ||
input := "" | ||
result := StringPtrToString(&input) | ||
assert.Equal(t, "", result, "Should return an empty string for empty string input") | ||
}) | ||
|
||
t.Run("String pointer with whitespace", func(t *testing.T) { | ||
input := " " | ||
result := StringPtrToString(&input) | ||
assert.Equal(t, " ", result, "Should preserve whitespace") | ||
}) | ||
} |
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,76 @@ | ||
package helpers | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCalculateMd5(t *testing.T) { | ||
tempDir := t.TempDir() | ||
|
||
t.Run("Calculate MD5 for non-empty file", func(t *testing.T) { | ||
content := []byte("Hello, World!") | ||
filePath := filepath.Join(tempDir, "test1.txt") | ||
err := os.WriteFile(filePath, content, 0644) | ||
require.NoError(t, err) | ||
|
||
expectedMD5 := md5.Sum(content) | ||
expectedMD5String := hex.EncodeToString(expectedMD5[:]) | ||
|
||
result, err := CalculateMd5(filePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedMD5String, result) | ||
}) | ||
|
||
t.Run("Calculate MD5 for empty file", func(t *testing.T) { | ||
filePath := filepath.Join(tempDir, "empty.txt") | ||
err := os.WriteFile(filePath, []byte{}, 0644) | ||
require.NoError(t, err) | ||
|
||
expectedMD5 := md5.Sum([]byte{}) | ||
expectedMD5String := hex.EncodeToString(expectedMD5[:]) | ||
|
||
result, err := CalculateMd5(filePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedMD5String, result) | ||
}) | ||
|
||
t.Run("Calculate MD5 for non-existent file", func(t *testing.T) { | ||
filePath := filepath.Join(tempDir, "non_existent.txt") | ||
|
||
result, err := CalculateMd5(filePath) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "failed to open file") | ||
assert.Empty(t, result) | ||
}) | ||
|
||
t.Run("Calculate MD5 for large file", func(t *testing.T) { | ||
filePath := filepath.Join(tempDir, "large.bin") | ||
f, err := os.Create(filePath) | ||
require.NoError(t, err) | ||
defer f.Close() | ||
|
||
// Write 10MB of random data | ||
data := make([]byte, 1024*1024*10) | ||
_, err = io.ReadFull(rand.Reader, data) | ||
require.NoError(t, err) | ||
|
||
_, err = f.Write(data) | ||
require.NoError(t, err) | ||
|
||
expectedMD5 := md5.Sum(data) | ||
expectedMD5String := hex.EncodeToString(expectedMD5[:]) | ||
|
||
result, err := CalculateMd5(filePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedMD5String, result) | ||
}) | ||
} |
Oops, something went wrong.