Skip to content

Commit f7ca472

Browse files
authored
Merge pull request #610 from ArtisanCloud/develop
feat(test): add signer test for SignSHA256WithHMac
2 parents 61ad4c9 + 2b22fbf commit f7ca472

File tree

4 files changed

+107
-39
lines changed

4 files changed

+107
-39
lines changed

go.mod

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,19 @@ require (
1313
github.com/pkg/errors v0.9.1
1414
github.com/redis/go-redis/v9 v9.6.1
1515
github.com/stretchr/testify v1.9.0
16-
go.opentelemetry.io/otel v1.31.0
17-
go.opentelemetry.io/otel/sdk v1.31.0
1816
)
1917

2018
require (
2119
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2220
github.com/clbanning/mxj/v2 v2.7.0 // indirect
2321
github.com/davecgh/go-spew v1.1.1 // indirect
2422
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
25-
github.com/go-logr/logr v1.4.2 // indirect
26-
github.com/go-logr/stdr v1.2.2 // indirect
27-
github.com/google/uuid v1.6.0 // indirect
2823
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
2924
github.com/pmezard/go-difflib v1.0.0 // indirect
30-
go.opentelemetry.io/otel/metric v1.31.0 // indirect
31-
go.opentelemetry.io/otel/trace v1.31.0 // indirect
25+
go.opentelemetry.io/otel v1.4.0 // indirect
26+
go.opentelemetry.io/otel/trace v1.4.0 // indirect
3227
go.uber.org/multierr v1.11.0 // indirect
3328
go.uber.org/zap v1.27.0 // indirect
3429
golang.org/x/crypto v0.28.0 // indirect
35-
golang.org/x/sys v0.26.0 // indirect
3630
gopkg.in/yaml.v3 v3.0.1 // indirect
3731
)

go.sum

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
1010
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
1111
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
1212
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
13+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1314
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1415
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1516
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
1617
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
18+
github.com/go-logr/logr v1.2.2 h1:ahHml/yUpnlb96Rp8HCvtYVPY8ZYpxq3g7UYchIYwbs=
1719
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
18-
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
19-
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
2020
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
2121
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
2222
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
2323
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
24-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
25-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
26-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
27-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
24+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
25+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
2826
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
2927
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
3028
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -37,16 +35,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3735
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3836
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
3937
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
38+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
39+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4040
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4141
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
42-
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
43-
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
44-
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
45-
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
46-
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
47-
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
48-
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
49-
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
42+
go.opentelemetry.io/otel v1.4.0 h1:7ESuKPq6zpjRaY5nvVDGiuwK7VAJ8MwkKnmNJ9whNZ4=
43+
go.opentelemetry.io/otel v1.4.0/go.mod h1:jeAqMFKy2uLIxCtKxoFj0FAL5zAPKQagc3+GtBWakzk=
44+
go.opentelemetry.io/otel/sdk v1.4.0 h1:LJE4SW3jd4lQTESnlpQZcBhQ3oci0U2MLR5uhicfTHQ=
45+
go.opentelemetry.io/otel/sdk v1.4.0/go.mod h1:71GJPNJh4Qju6zJuYl1CrYtXbrgfau/M9UAggqiy1UE=
46+
go.opentelemetry.io/otel/trace v1.4.0 h1:4OOUrPZdVFQkbzl/JSdvGCWIdw5ONXXxzHlaLlWppmo=
47+
go.opentelemetry.io/otel/trace v1.4.0/go.mod h1:uc3eRsqDfWs9R7b92xbQbU42/eTNz4N+gLP8qJCi4aE=
5048
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5149
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
5250
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
@@ -57,8 +55,10 @@ golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
5755
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
5856
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
5957
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
58+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6059
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6160
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
6261
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
62+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6363
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6464
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork"
1818
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment"
1919
"github.com/ArtisanCloud/PowerWeChat/v3/src/work"
20-
"go.opentelemetry.io/otel"
21-
"go.opentelemetry.io/otel/sdk/trace"
2220
)
2321

2422
func GetOfficialConfig() *officialAccount.UserConfig {
@@ -213,30 +211,16 @@ func GetOpenWorkConfig() *openWork.UserConfig {
213211
}
214212
}
215213

216-
func initTracer() {
217-
tp := trace.NewTracerProvider()
218-
// Set Global Tracer Provider
219-
otel.SetTracerProvider(tp)
220-
}
221-
222-
func init() {
223-
initTracer()
224-
}
225-
226214
func main() {
227215
fmt.Printf("hello Wechat! \n")
228216

229-
tracer := otel.Tracer("example-tracer")
230-
ctx, span := tracer.Start(context.Background(), "test")
231-
defer span.End()
232-
233217
// init officialAccount app
234218
configOfficialAccount := GetOfficialConfig()
235219
officialAccountApp, err := officialAccount.NewOfficialAccount(configOfficialAccount)
236220
if err != nil {
237221
fmt.Println(err.Error())
238222
}
239-
223+
ctx := context.Background()
240224
officialAccountApp.TemplateMessage.Send(ctx, &request.RequestTemlateMessage{
241225
ToUser: "",
242226
TemplateID: "",

src/kernel/support/signer_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package support
22

33
import (
4+
"crypto/hmac"
45
"crypto/rsa"
6+
"crypto/sha256"
57
"crypto/x509"
68
"encoding/pem"
9+
"errors"
710
"fmt"
811
"github.com/go-playground/assert/v2"
912
"strings"
@@ -105,3 +108,90 @@ func TestSha256WithRsa(t *testing.T) {
105108
})
106109
}
107110
}
111+
112+
// 辅助函数:比较两个字节切片是否相等
113+
func compareByteSlices(a, b []byte) bool {
114+
if len(a) != len(b) {
115+
return false
116+
}
117+
for i, v := range a {
118+
if v != b[i] {
119+
return false
120+
}
121+
}
122+
return true
123+
}
124+
125+
// 测试函数
126+
func TestSignSHA256WithHMac(t *testing.T) {
127+
testCases := []struct {
128+
name string
129+
sessionKey []byte
130+
input string
131+
expectedErr error
132+
}{
133+
{
134+
name: "Empty Session Key",
135+
sessionKey: []byte{},
136+
input: "test input",
137+
expectedErr: errors.New("session key is empty"),
138+
},
139+
{
140+
name: "Valid Input",
141+
sessionKey: []byte("secret key"),
142+
input: "test input",
143+
expectedErr: nil,
144+
},
145+
{
146+
name: "Empty Input",
147+
sessionKey: []byte("secret key"),
148+
input: "",
149+
expectedErr: nil,
150+
},
151+
{
152+
name: "Another Valid Input",
153+
sessionKey: []byte("another secret"),
154+
input: "another input string",
155+
expectedErr: nil,
156+
},
157+
{
158+
name: "Input with Special Characters", // 测试特殊字符
159+
sessionKey: []byte("secret key"),
160+
input: "test input!@#$%^&*()_+=-`~[]\\{}|;':\",./<>?",
161+
expectedErr: nil,
162+
},
163+
{
164+
name: "Long Input String", // 测试长字符串
165+
sessionKey: []byte("secret key"),
166+
input: "This is a long input string to test the HMAC-SHA256 function with a larger amount of data. It should still produce the correct signature.",
167+
expectedErr: nil,
168+
},
169+
}
170+
171+
for _, tc := range testCases {
172+
t.Run(tc.name, func(t *testing.T) {
173+
// 1. 计算预期结果
174+
expectedHmac := hmac.New(sha256.New, tc.sessionKey)
175+
expectedHmac.Write([]byte(tc.input))
176+
expectedSig := expectedHmac.Sum(nil)
177+
178+
// 2. 调用 SignSHA256WithHMac 函数
179+
sig, err := SignSHA256WithHMac(tc.sessionKey, tc.input)
180+
181+
// 3. 进行断言
182+
if tc.expectedErr != nil {
183+
if err == nil {
184+
t.Errorf("Expected error: %v, but got nil", tc.expectedErr)
185+
} else if err.Error() != tc.expectedErr.Error() {
186+
t.Errorf("Expected error message: %q, but got %q", tc.expectedErr.Error(), err.Error())
187+
}
188+
} else {
189+
if err != nil {
190+
t.Errorf("Unexpected error: %v", err)
191+
} else if !compareByteSlices(sig, expectedSig) {
192+
t.Errorf("Signature mismatch. Expected: %x, Got: %x", expectedSig, sig)
193+
}
194+
}
195+
})
196+
}
197+
}

0 commit comments

Comments
 (0)