File: identity_test.go

package info (click to toggle)
golang-github-smallstep-cli 0.15.16%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,404 kB
  • sloc: sh: 512; makefile: 99
file content (68 lines) | stat: -rw-r--r-- 1,516 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package x509util

import (
	"testing"

	"github.com/pkg/errors"
	"github.com/smallstep/assert"
	"github.com/smallstep/cli/crypto/pemutil"
)

func TestLoadIdentityFromDisk(t *testing.T) {
	var (
		testBadCert          = "./test_files/badca.crt"
		testCert             = "./test_files/ca.crt"
		testNoPasscodeBadKey = "./test_files/noPasscodeBadCa.key"
		noPasscodeKey        = "./test_files/noPasscodeCa.key"
	)

	tests := map[string]struct {
		crtPath string
		keyPath string
		pass    string
		err     error
	}{
		"error parsing x509 certificate": {
			crtPath: testBadCert,
			keyPath: "",
			pass:    "",
			err: errors.Errorf("error parsing %s: asn1: syntax error: trailing data",
				testBadCert),
		},
		"error parsing rsa key": {
			crtPath: testCert,
			keyPath: testNoPasscodeBadKey,
			pass:    "",
			err:     errors.Errorf("error parsing %s: asn1:", testNoPasscodeBadKey),
		},
		"success": {
			crtPath: testCert,
			keyPath: noPasscodeKey,
			pass:    "",
		},
	}

	for name, test := range tests {
		t.Logf("Running test case: %s", name)

		var (
			err error
			i   *Identity
		)
		if test.pass == "" {
			i, err = LoadIdentityFromDisk(test.crtPath, test.keyPath)
		} else {
			i, err = LoadIdentityFromDisk(test.crtPath, test.keyPath,
				pemutil.WithPassword([]byte(test.pass)))
		}
		if err != nil {
			if assert.NotNil(t, test.err) {
				assert.HasPrefix(t, err.Error(), test.err.Error())
			}
		} else {
			assert.FatalError(t, err)
			assert.NotNil(t, i.Crt)
			assert.NotNil(t, i.Key)
		}
	}
}