38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package payments
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/base64"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseAlipayAmount(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
s string
|
|
want int64
|
|
ok bool
|
|
}{{"1.50", 150, true}, {"0.01", 1, true}, {"1.5", 150, true}, {"-1.00", 0, false}, {"1.000", 0, false}, {"abc", 0, false}} {
|
|
got, e := parseAlipayAmount(tc.s)
|
|
if (e == nil) != tc.ok || e == nil && got != tc.want {
|
|
t.Fatalf("%q got %d err %v", tc.s, got, e)
|
|
}
|
|
}
|
|
}
|
|
func TestDecryptGCMRejectsInvalidNonce(t *testing.T) {
|
|
if _, e := decryptGCM("01234567890123456789012345678901", "short", "", "AA=="); e == nil {
|
|
t.Fatal("expected nonce length error")
|
|
}
|
|
}
|
|
func TestDecryptGCM(t *testing.T) {
|
|
key := []byte("01234567890123456789012345678901")
|
|
b, _ := aes.NewCipher(key)
|
|
g, _ := cipher.NewGCM(b)
|
|
nonce := []byte("nonce1234567")
|
|
ct := g.Seal(nil, nonce, []byte(`{"out_trade_no":"m-1"}`), []byte("ad"))
|
|
got, e := decryptGCM(string(key), string(nonce), "ad", base64.StdEncoding.EncodeToString(ct))
|
|
if e != nil || got == "" {
|
|
t.Fatalf("decrypt failed: %q %v", got, e)
|
|
}
|
|
}
|