-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapiutil_test.go
More file actions
46 lines (35 loc) · 1.09 KB
/
apiutil_test.go
File metadata and controls
46 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dnapi
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestInsertConfigPrivateKey(t *testing.T) {
cfg, err := InsertConfigPrivateKey([]byte(`
pki: {}
`), []byte("foobar"))
require.NoError(t, err)
var y map[string]any
err = yaml.Unmarshal(cfg, &y)
require.NoError(t, err)
require.Equal(t, "foobar", y["pki"].(map[any]any)["key"])
_, err = InsertConfigPrivateKey([]byte(``), []byte("foobar"))
require.Error(t, err)
}
func TestFetchConfigPrivateKey(t *testing.T) {
keyValue := []byte("foobar")
certValue := []byte("lolwat")
configValue := fmt.Sprintf(`pki: { cert: %s }`, certValue)
cfg, err := InsertConfigPrivateKey([]byte(configValue), keyValue)
require.NoError(t, err)
var y map[string]any
err = yaml.Unmarshal(cfg, &y)
require.NoError(t, err)
require.Equal(t, keyValue, []byte(y["pki"].(map[any]any)["key"].(string)))
fetchedVal, fetchedCert, err := FetchConfigPrivateKeyAndCert(cfg)
require.NoError(t, err)
assert.Equal(t, certValue, fetchedCert)
assert.Equal(t, keyValue, fetchedVal)
}