yubihsm2_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //go:build cgo && yubihsm2
  2. // +build cgo,yubihsm2
  3. package pkcs11
  4. import (
  5. "runtime"
  6. "sync"
  7. "github.com/ThalesIgnite/crypto11"
  8. )
  9. var yubiHSM2Once sync.Once
  10. // mustPKCS11 configures a *PKCS11 KMS to be used with YubiHSM2. To initialize
  11. // these tests, we should run:
  12. // yubihsm-connector -d
  13. func mustPKCS11(t TBTesting) *PKCS11 {
  14. t.Helper()
  15. testModule = "YubiHSM2"
  16. if runtime.GOARCH != "amd64" {
  17. t.Skipf("yubiHSM2 test skipped on %s:%s", runtime.GOOS, runtime.GOARCH)
  18. }
  19. var path string
  20. switch runtime.GOOS {
  21. case "darwin":
  22. path = "/usr/local/lib/pkcs11/yubihsm_pkcs11.dylib"
  23. case "linux":
  24. path = "/usr/lib/x86_64-linux-gnu/pkcs11/yubihsm_pkcs11.so"
  25. default:
  26. t.Skipf("yubiHSM2 test skipped on %s", runtime.GOOS)
  27. return nil
  28. }
  29. p11, err := crypto11.Configure(&crypto11.Config{
  30. Path: path,
  31. TokenLabel: "YubiHSM",
  32. Pin: "0001password",
  33. })
  34. if err != nil {
  35. t.Fatalf("failed to configure YubiHSM2 on %s: %v", runtime.GOOS, err)
  36. }
  37. k := &PKCS11{
  38. p11: p11,
  39. }
  40. // Setup
  41. yubiHSM2Once.Do(func() {
  42. teardown(t, k)
  43. setup(t, k)
  44. })
  45. return k
  46. }