-
Notifications
You must be signed in to change notification settings - Fork 6
/
corpus_test.go
116 lines (110 loc) · 2.94 KB
/
corpus_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ike
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"testing"
"github.com/msgboxio/ike/protocol"
)
func newConfig() (config *Config) {
config = testConfig()
config.AddNetworkSelectors(
&net.IPNet{IP: net.IPv4(192, 168, 10, 2).To4(), Mask: net.CIDRMask(32, 32)},
&net.IPNet{IP: net.IPv4(10, 10, 10, 2).To4(), Mask: net.CIDRMask(32, 32)},
true)
return
}
var ids = PskIdentities{
Primary: "[email protected]",
Ids: map[string][]byte{"[email protected]": []byte("foo")},
}
func TestIkeMsgGen(t *testing.T) {
cfg := newConfig()
cfg.LocalID = pskTestID
cfg.PeerID = pskTestID
sess, _ := NewInitiator(cfg, zeroAddr, zeroAddr, nil, &SessionCallback{}, logger)
// init msg
init := InitFromSession(sess)
init.IkeHeader.MsgID = 42
// encode & write init msg
initIb, err := init.Encode(sess.tkm, true, logger)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile("protocol/fuzz/corpus/corpus/sa_init_gen", initIb, 0644); err != nil {
t.Fatal(err)
}
// auth
sess.initIb = initIb
no, _ := createNonce(sess.tkm.Ni.BitLen())
err = sess.CreateIkeSa(&initParams{
isInitiator: sess.isInitiator,
spiI: sess.IkeSpiI,
spiR: sess.IkeSpiR,
cookie: sess.responderCookie,
dhTransformID: sess.tkm.suite.DhGroup.TransformId(),
dhPublic: sess.tkm.DhPublic,
nonce: no,
rfc7427Signatures: sess.rfc7427Signatures,
})
if err != nil {
t.Fatal(err)
}
authI, err := authFromSession(sess)
if err != nil {
t.Fatal(err)
}
authI.IkeHeader.MsgID = 43
// encode & write authI msg
authIb, err := authI.Encode(sess.tkm, true, logger)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile("protocol/fuzz/corpus/corpus/authI_gen", authIb, 0644); err != nil {
t.Fatal(err)
}
}
func TestCorpusDecode(t *testing.T) {
files, err := ioutil.ReadDir("protocol/fuzz/corpus/corpus/")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
data, err := ioutil.ReadFile("protocol/fuzz/corpus/corpus/" + file.Name())
if err != nil {
t.Fatal(err)
}
t.Log("file", file.Name())
hdr, err := protocol.DecodeIkeHeader(data)
if err != nil {
t.Logf("hdr: %s:%s", file.Name(), err)
continue
}
plData := data[protocol.IKE_HEADER_LEN:]
payloads, err := protocol.DecodePayloads(plData, hdr.NextPayload)
if err != nil {
t.Logf("pld: %s:%s", file.Name(), err)
continue
}
// ensure encoding is same
if enc := hdr.Encode(); !bytes.Equal(enc, data[:protocol.IKE_HEADER_LEN]) {
t.Errorf("%s:%s", file.Name(), "unequal header")
}
if pld := protocol.EncodePayloads(payloads); !bytes.Equal(pld, plData[:len(pld)]) {
t.Logf("%s:%s", file.Name(), "unequal payload")
}
msg, err := decodeMessage(data, nil, false)
if err != nil {
t.Logf("%s:%s", file.Name(), err)
continue
}
js, err := json.MarshalIndent(msg, "", " ")
if err != nil {
t.Logf("%s:%s", file.Name(), err)
continue
}
_ = js
// t.Log("file", file.Name(), "data", string(js))
}
}