-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrypto_test.go
75 lines (61 loc) · 2.27 KB
/
crypto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package crypter
import (
"encoding/xml"
"testing"
)
func TestDecryptMsg(t *testing.T) {
token := "RMNlACHlV5ThzfRlVS4D4"
corpID := "wx5823bf96d3bd56c7"
encodingAESKey := "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"
msgEncrypt := "RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q=="
messageCrypter, _ := NewMessageCrypter(token, encodingAESKey, corpID)
_, corpIDDecrypted, err := messageCrypter.Decrypt(msgEncrypt)
if err != nil {
t.Fatal("Decrypt Message error:", err)
}
if corpIDDecrypted != corpID {
t.Errorf("CorpID: want[%s], but actually[%s]", corpID, corpIDDecrypted)
}
}
func TestEcryptMsg(t *testing.T) {
token := "RMNlACHlV5ThzfRlVS4D4"
corpID := "wx5823bf96d3bd56c7"
encodingAESKey := "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"
msgText := `<xml>
<ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName>
<FromUserName><![CDATA[heroic]]></FromUserName>
<CreateTime>1426498001</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[hello world]]></Content>
<MsgId>000001</MsgId>
<AgentID>3</AgentID>
</xml>`
messageCrypter, _ := NewMessageCrypter(token, encodingAESKey, corpID)
msgEncrypt, err := messageCrypter.Encrypt(msgText)
if err != nil {
t.Fatal("Ecrypt Message error:", err)
}
message, corpIDDecrypted, err := messageCrypter.Decrypt(msgEncrypt)
if err != nil {
t.Fatal("Decrypt Message error:", err)
}
var recvMsg = struct {
ToUserName string
FromUserName string
CreateTime int
MsgType string
Content string
MsgID uint64
AgentID int
}{}
err = xml.Unmarshal(message, &recvMsg)
if err != nil {
t.Fatal("Xml decoding error:", err)
}
if corpIDDecrypted != corpID {
t.Errorf("CorpID: want[%s], but actually[%s]", corpID, corpIDDecrypted)
}
if recvMsg.Content != "hello world" {
t.Errorf("Message: want[%s], but actually[%s]", "hello world", recvMsg.Content)
}
}