yubihsm2_test.go 1.0 KB

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