softhsm2_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // +build cgo,softhsm2
  2. package pkcs11
  3. import (
  4. "runtime"
  5. "sync"
  6. "github.com/ThalesIgnite/crypto11"
  7. )
  8. var softHSM2Once sync.Once
  9. // mustPKCS11 configures a *PKCS11 KMS to be used with SoftHSM2. To initialize
  10. // these tests, we should run:
  11. // softhsm2-util --init-token --free \
  12. // --token pkcs11-test --label pkcs11-test \
  13. // --so-pin password --pin password
  14. //
  15. // To delete we should run:
  16. // softhsm2-util --delete-token --token pkcs11-test
  17. func mustPKCS11(t TBTesting) *PKCS11 {
  18. t.Helper()
  19. testModule = "SoftHSM2"
  20. if runtime.GOARCH != "amd64" {
  21. t.Fatalf("softHSM2 test skipped on %s:%s", runtime.GOOS, runtime.GOARCH)
  22. }
  23. var path string
  24. switch runtime.GOOS {
  25. case "darwin":
  26. path = "/usr/local/lib/softhsm/libsofthsm2.so"
  27. case "linux":
  28. path = "/usr/lib/softhsm/libsofthsm2.so"
  29. default:
  30. t.Skipf("softHSM2 test skipped on %s", runtime.GOOS)
  31. return nil
  32. }
  33. p11, err := crypto11.Configure(&crypto11.Config{
  34. Path: path,
  35. TokenLabel: "pkcs11-test",
  36. Pin: "password",
  37. })
  38. if err != nil {
  39. t.Fatalf("failed to configure softHSM2 on %s: %v", runtime.GOOS, err)
  40. }
  41. k := &PKCS11{
  42. p11: p11,
  43. }
  44. // Setup
  45. softHSM2Once.Do(func() {
  46. teardown(t, k)
  47. setup(t, k)
  48. })
  49. return k
  50. }