2021-07-29 13:31:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_loadActivityPubPrivateKey(t *testing.T) {
|
|
|
|
|
|
|
|
app := &goBlog{
|
|
|
|
cfg: &config{
|
|
|
|
Db: &configDb{
|
|
|
|
File: filepath.Join(t.TempDir(), "test.db"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-02-12 11:37:13 +00:00
|
|
|
err := app.initDatabase(false)
|
|
|
|
require.NoError(t, err)
|
2022-01-26 19:16:26 +00:00
|
|
|
defer app.db.close()
|
2022-02-12 11:37:13 +00:00
|
|
|
require.NotNil(t, app.db)
|
2021-07-29 13:31:49 +00:00
|
|
|
|
|
|
|
// Generate
|
2022-02-12 11:37:13 +00:00
|
|
|
err = app.loadActivityPubPrivateKey()
|
2021-07-29 13:31:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, app.apPrivateKey)
|
|
|
|
|
|
|
|
oldEncodedKey := x509.MarshalPKCS1PrivateKey(app.apPrivateKey)
|
|
|
|
oldPemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: oldEncodedKey})
|
|
|
|
|
|
|
|
// Reset and reload
|
|
|
|
err = app.loadActivityPubPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, app.apPrivateKey)
|
|
|
|
|
|
|
|
newEncodedKey := x509.MarshalPKCS1PrivateKey(app.apPrivateKey)
|
|
|
|
newPemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: newEncodedKey})
|
|
|
|
|
|
|
|
assert.Equal(t, string(oldPemEncoded), string(newPemEncoded))
|
|
|
|
|
|
|
|
}
|