Skip to content

Commit

Permalink
Mock findP11KitProxy on New test
Browse files Browse the repository at this point in the history
  • Loading branch information
maraino committed Jun 8, 2023
1 parent 2723a8f commit 237a370
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion kms/pkcs11/pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func findCertificate(ctx P11, rawuri string) (*x509.Certificate, error) {
}

// findP11KitProxy uses pkg-config to locate p11-kit-proxy.so
func findP11KitProxy(ctx context.Context) string {
var findP11KitProxy = func(ctx context.Context) string {
var out strings.Builder

// It should be more than enough even in constraint VMs
Expand Down
59 changes: 48 additions & 11 deletions kms/pkcs11/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,35 @@ import (
)

func TestNew(t *testing.T) {
tmp := p11Configure
tmp0 := p11Configure
tmp1 := findP11KitProxy
t.Cleanup(func() {
p11Configure = tmp
p11Configure = tmp0
findP11KitProxy = tmp1
})

k := mustPKCS11(t)
t.Cleanup(func() {
k.Close()
})

p11Configure = func(config *crypto11.Config) (P11, error) {
if strings.Contains(config.Path, "fail") {
return nil, errors.New("an error")
}
return k.p11, nil
}

var (
wantMissingModule *PKCS11
wantErrMissingModule = true
)
if findP11KitProxy(context.Background()) != "" {
wantMissingModule = k
wantErrMissingModule = false
findP11KitProxy = func(ctx context.Context) string {
select {
case <-ctx.Done():
return ""
default:
if fail, _ := ctx.Value("fail").(bool); fail {
return ""
}
return "/usr/local/lib/p11-kit-proxy.so"
}
}

canceledContext, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -80,11 +86,16 @@ func TestNew(t *testing.T) {
URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test",
Pin: "passowrd",
}}, k, false},
{"perhaps with missing module", args{context.Background(), apiv1.Options{
{"ok with missing module", args{context.Background(), apiv1.Options{
Type: "pkcs11",
URI: "pkcs11:token=pkcs11-test",
Pin: "passowrd",
}}, k, false},
{"fail with missing module", args{context.WithValue(context.Background(), "fail", true), apiv1.Options{
Type: "pkcs11",
URI: "pkcs11:token=pkcs11-test",
Pin: "passowrd",
}}, wantMissingModule, wantErrMissingModule},
}}, nil, true},
{"fail findP11KitProxy", args{canceledContext, apiv1.Options{
Type: "pkcs11",
URI: "pkcs11:token=pkcs11-test?pin-value=password",
Expand Down Expand Up @@ -850,3 +861,29 @@ func TestPKCS11_Close(t *testing.T) {
})
}
}

func Test_findP11KitProxy(t *testing.T) {
expected := findP11KitProxy(context.Background())

canceledContext, cancel := context.WithCancel(context.Background())
cancel()

type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want string
}{
{"expected", args{context.Background()}, expected},
{"fail", args{canceledContext}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findP11KitProxy(tt.args.ctx); got != tt.want {
t.Errorf("findP11KitProxy() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 237a370

Please sign in to comment.