mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-09 13:04:24 +00:00
Refactor: Use persistent cert storage with reuse logic
Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -328,41 +329,70 @@ func TestShutdownCallbacks(t *testing.T) {
|
||||
assert.True(t, executed, "Shutdown callback should have been executed")
|
||||
}
|
||||
|
||||
func TestSelfSignedSSLCleanup(t *testing.T) {
|
||||
func TestSelfSignedSSLCertificateReuse(t *testing.T) {
|
||||
logger.Init(true)
|
||||
sm := NewManager()
|
||||
|
||||
testPort := getFreePort(t)
|
||||
instance, err := sm.Add(Config{
|
||||
Name: "SSLTestServer",
|
||||
Host: "localhost",
|
||||
Port: testPort,
|
||||
|
||||
// Get cert directory to verify file creation
|
||||
certDir, err := getCertDirectory()
|
||||
require.NoError(t, err)
|
||||
|
||||
host := "localhost"
|
||||
certFile := filepath.Join(certDir, fmt.Sprintf("%s-cert.pem", host))
|
||||
keyFile := filepath.Join(certDir, fmt.Sprintf("%s-key.pem", host))
|
||||
|
||||
// Clean up any existing cert files from previous tests
|
||||
os.Remove(certFile)
|
||||
os.Remove(keyFile)
|
||||
|
||||
// First server creation - should generate new certificates
|
||||
sm1 := NewManager()
|
||||
testPort1 := getFreePort(t)
|
||||
_, err = sm1.Add(Config{
|
||||
Name: "SSLTestServer1",
|
||||
Host: host,
|
||||
Port: testPort1,
|
||||
Handler: http.NewServeMux(),
|
||||
SelfSignedSSL: true,
|
||||
ShutdownTimeout: 5 * time.Second,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get the serverInstance to access the tempCertDir
|
||||
si, ok := instance.(*serverInstance)
|
||||
require.True(t, ok, "instance should be of type *serverInstance")
|
||||
require.NotEmpty(t, si.tempCertDir, "temporary certificate directory should be set")
|
||||
|
||||
// Verify the temp directory exists
|
||||
_, err = os.Stat(si.tempCertDir)
|
||||
require.NoError(t, err, "temporary certificate directory should exist")
|
||||
|
||||
// Start the server
|
||||
err = sm.StartAll()
|
||||
|
||||
// Verify certificates were created
|
||||
_, err = os.Stat(certFile)
|
||||
require.NoError(t, err, "certificate file should exist after first creation")
|
||||
_, err = os.Stat(keyFile)
|
||||
require.NoError(t, err, "key file should exist after first creation")
|
||||
|
||||
// Get modification time of cert file
|
||||
info1, err := os.Stat(certFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
modTime1 := info1.ModTime()
|
||||
|
||||
// Wait a bit to ensure different modification times
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Stop the server
|
||||
err = sm.StopAll()
|
||||
|
||||
// Second server creation - should reuse existing certificates
|
||||
sm2 := NewManager()
|
||||
testPort2 := getFreePort(t)
|
||||
_, err = sm2.Add(Config{
|
||||
Name: "SSLTestServer2",
|
||||
Host: host,
|
||||
Port: testPort2,
|
||||
Handler: http.NewServeMux(),
|
||||
SelfSignedSSL: true,
|
||||
ShutdownTimeout: 5 * time.Second,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the temp directory has been cleaned up
|
||||
_, err = os.Stat(si.tempCertDir)
|
||||
assert.True(t, os.IsNotExist(err), "temporary certificate directory should be cleaned up after shutdown")
|
||||
|
||||
// Get modification time of cert file after second creation
|
||||
info2, err := os.Stat(certFile)
|
||||
require.NoError(t, err)
|
||||
modTime2 := info2.ModTime()
|
||||
|
||||
// Verify the certificate was reused (same modification time)
|
||||
assert.Equal(t, modTime1, modTime2, "certificate should be reused, not regenerated")
|
||||
|
||||
// Clean up
|
||||
sm1.StopAll()
|
||||
sm2.StopAll()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user