forked from msgboxio/ike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_test.go
63 lines (57 loc) · 1.44 KB
/
message_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
package ike
import (
"bytes"
"encoding/json"
"errors"
"testing"
"github.com/google/gopacket/bytediff"
"github.com/msgboxio/ike/protocol"
)
func decodeMessage(dec []byte, tkm *Tkm, forInitiator bool) (*Message, error) {
msg := &Message{}
err := msg.DecodeHeader(dec)
if err != nil {
return nil, err
}
if len(dec) < int(msg.IkeHeader.MsgLength) {
err = protocol.ERR_INVALID_SYNTAX
return nil, err
}
if err = msg.DecodePayloads(dec[protocol.IKE_HEADER_LEN:msg.IkeHeader.MsgLength], msg.IkeHeader.NextPayload, logger); err != nil {
return nil, err
}
if msg.IkeHeader.NextPayload == protocol.PayloadTypeSK {
if tkm == nil {
err = errors.New("cant decrypt, no tkm found")
return nil, err
}
b, err := tkm.VerifyDecrypt(dec, forInitiator)
if err != nil {
return nil, err
}
sk := msg.Payloads.Get(protocol.PayloadTypeSK)
if err = msg.DecodePayloads(b, sk.NextPayloadType(), logger); err != nil {
return nil, err
}
}
return msg, nil
}
func testDecode(dec []byte, tkm *Tkm, forInitiator bool, t *testing.T) *Message {
msg, err := decodeMessage(dec, tkm, forInitiator)
if err != nil {
t.Fatal(err)
}
js, err := json.MarshalIndent(msg, "", " ")
if err != nil {
t.Fatal(err)
}
t.Logf("\n%s", string(js))
enc, err := msg.Encode(tkm, forInitiator, logger)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(enc, dec) {
t.Errorf("comapre failed\n%s", bytediff.BashOutput.String(bytediff.Diff(dec, enc)))
}
return msg
}