sha256withrsa_pubkey_verifier_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package verifiers
  3. import (
  4. "context"
  5. "crypto/rsa"
  6. "testing"
  7. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  8. )
  9. const (
  10. testPubKeyID = "F5765756002FDD77"
  11. testPubKey = `-----BEGIN PUBLIC KEY-----
  12. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2VCTd91fnUn73Xy9DLvt
  13. /V62TVxRTEEstVdeRaZ3B3leO0pldE806mXO4RwdHXagHQ4vGeZN0yqm++rDsGK+
  14. U3AH7kejyD2pXshNP9Cq5YwbptiLGtjcquw4HNxJQUOmDeJf2vg6byms9RUipiq4
  15. SzbJKqJFlUpbuIPDpSpWz10PYmyCNeDGUUK65E5h2B834uxl1zNLYQCrkdBzb8oU
  16. xwYeP5a2DNxmjL5lsJML7DGr5znsevnoqGRwTm9fxCGfy8wus7hwKz6clt3Whmmd
  17. a7UAdb1c08hEQFVRbF14AR73xbnd8N0obCWJPCbzMCtkaSef4FdEEgEXJiw0VAJT
  18. 8wIDAQAB
  19. -----END PUBLIC KEY-----`
  20. // testExpectedSignature = "BKyAfU4iMCuvXMXS0Wzam3V/cnxZ+JaqigPM5OhljS2iOT95OO6Fsuml2JkFANJU9" +
  21. // "K6q9bLlDhPXuoVz+pp4hAm6pHU4ld815U4jsKu1RkyaII+1CYBUYC8TK0XtJ8FwUXXz8vZHh58rrAVN1XwNyv" +
  22. // "D1vfpxrMT4SL536GLwvpUHlCqIMzoZUguLli/K8V29QiOhuH6IEqLNJn8e9b3nwNcQ7be3CzYGpDAKBfDGPCq" +
  23. // "Cv8Rw5zndhlffk2FEA70G4hvMwe51qMN/RAJbknXG23bSlObuTCN7Ndj1aJGH6/L+hdwfLpUtJm4QYVazzW7D" +
  24. // "FD27EpSQEqA8bX9+8m1rLg=="
  25. )
  26. var (
  27. pubKey *rsa.PublicKey
  28. )
  29. func init() {
  30. var err error
  31. pubKey, err = utils.LoadPublicKey(testPubKey)
  32. if err != nil {
  33. panic(err)
  34. }
  35. }
  36. func TestWechatPayPubKeyVerifier(t *testing.T) {
  37. type args struct {
  38. ctx context.Context
  39. serialNumber string
  40. message string
  41. signature string
  42. }
  43. tests := []struct {
  44. name string
  45. fields *rsa.PublicKey
  46. args args
  47. wantErr bool
  48. }{
  49. {
  50. name: "verify success",
  51. fields: pubKey,
  52. args: args{
  53. ctx: context.Background(),
  54. serialNumber: testPubKeyID,
  55. signature: testExpectedSignature,
  56. message: "source",
  57. },
  58. wantErr: false,
  59. },
  60. {
  61. name: "verify failed",
  62. fields: pubKey,
  63. args: args{
  64. ctx: context.Background(),
  65. serialNumber: testPubKeyID,
  66. signature: testExpectedSignature,
  67. message: "wrong source",
  68. },
  69. wantErr: true,
  70. },
  71. {
  72. name: "verify failed with null context",
  73. fields: pubKey,
  74. args: args{
  75. ctx: nil,
  76. serialNumber: testWechatPayVerifierPlatformSerialNumber,
  77. signature: testExpectedSignature,
  78. message: "source",
  79. },
  80. wantErr: true,
  81. },
  82. {
  83. name: "verify failed with empty keyId",
  84. fields: pubKey,
  85. args: args{
  86. ctx: context.Background(),
  87. serialNumber: "",
  88. signature: testExpectedSignature,
  89. message: "source",
  90. },
  91. wantErr: true,
  92. },
  93. {
  94. name: "verify failed with empty message",
  95. fields: pubKey,
  96. args: args{
  97. ctx: context.Background(),
  98. serialNumber: testPubKeyID,
  99. signature: testExpectedSignature,
  100. message: "",
  101. },
  102. wantErr: true,
  103. },
  104. {
  105. name: "verify failed with empty signature",
  106. fields: pubKey,
  107. args: args{
  108. ctx: context.Background(),
  109. serialNumber: testPubKeyID,
  110. signature: "",
  111. message: "source",
  112. },
  113. wantErr: true,
  114. },
  115. {
  116. name: "verify failed with non-base64 signature",
  117. fields: pubKey,
  118. args: args{
  119. ctx: context.Background(),
  120. serialNumber: testPubKeyID,
  121. signature: "invalid base64 signature",
  122. message: "source",
  123. },
  124. wantErr: true,
  125. },
  126. {
  127. name: "verify failed with no corresponding pubkey",
  128. fields: pubKey,
  129. args: args{
  130. ctx: context.Background(),
  131. serialNumber: "invalid serial number",
  132. signature: testExpectedSignature,
  133. message: "source",
  134. },
  135. wantErr: true,
  136. },
  137. }
  138. for _, tt := range tests {
  139. t.Run(tt.name, func(t *testing.T) {
  140. var verifier = NewSHA256WithRSAPubkeyVerifier(testPubKeyID, *tt.fields)
  141. if err := verifier.Verify(tt.args.ctx, tt.args.serialNumber, tt.args.message,
  142. tt.args.signature); (err != nil) != tt.wantErr {
  143. t.Errorf("Verify() error = %v, wantErr %v", err, tt.wantErr)
  144. }
  145. })
  146. }
  147. }