OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [crypto/] [tls/] [handshake_messages_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package tls
6
 
7
import (
8
        "math/rand"
9
        "reflect"
10
        "testing"
11
        "testing/quick"
12
)
13
 
14
var tests = []interface{}{
15
        &clientHelloMsg{},
16
        &serverHelloMsg{},
17
        &finishedMsg{},
18
 
19
        &certificateMsg{},
20
        &certificateRequestMsg{},
21
        &certificateVerifyMsg{},
22
        &certificateStatusMsg{},
23
        &clientKeyExchangeMsg{},
24
        &nextProtoMsg{},
25
}
26
 
27
type testMessage interface {
28
        marshal() []byte
29
        unmarshal([]byte) bool
30
        equal(interface{}) bool
31
}
32
 
33
func TestMarshalUnmarshal(t *testing.T) {
34
        rand := rand.New(rand.NewSource(0))
35
 
36
        for i, iface := range tests {
37
                ty := reflect.ValueOf(iface).Type()
38
 
39
                n := 100
40
                if testing.Short() {
41
                        n = 5
42
                }
43
                for j := 0; j < n; j++ {
44
                        v, ok := quick.Value(ty, rand)
45
                        if !ok {
46
                                t.Errorf("#%d: failed to create value", i)
47
                                break
48
                        }
49
 
50
                        m1 := v.Interface().(testMessage)
51
                        marshaled := m1.marshal()
52
                        m2 := iface.(testMessage)
53
                        if !m2.unmarshal(marshaled) {
54
                                t.Errorf("#%d failed to unmarshal %#v %x", i, m1, marshaled)
55
                                break
56
                        }
57
                        m2.marshal() // to fill any marshal cache in the message
58
 
59
                        if !m1.equal(m2) {
60
                                t.Errorf("#%d got:%#v want:%#v %x", i, m2, m1, marshaled)
61
                                break
62
                        }
63
 
64
                        if i >= 3 {
65
                                // The first three message types (ClientHello,
66
                                // ServerHello and Finished) are allowed to
67
                                // have parsable prefixes because the extension
68
                                // data is optional and the length of the
69
                                // Finished varies across versions.
70
                                for j := 0; j < len(marshaled); j++ {
71
                                        if m2.unmarshal(marshaled[0:j]) {
72
                                                t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1)
73
                                                break
74
                                        }
75
                                }
76
                        }
77
                }
78
        }
79
}
80
 
81
func TestFuzz(t *testing.T) {
82
        rand := rand.New(rand.NewSource(0))
83
        for _, iface := range tests {
84
                m := iface.(testMessage)
85
 
86
                for j := 0; j < 1000; j++ {
87
                        len := rand.Intn(100)
88
                        bytes := randomBytes(len, rand)
89
                        // This just looks for crashes due to bounds errors etc.
90
                        m.unmarshal(bytes)
91
                }
92
        }
93
}
94
 
95
func randomBytes(n int, rand *rand.Rand) []byte {
96
        r := make([]byte, n)
97
        for i := 0; i < n; i++ {
98
                r[i] = byte(rand.Int31())
99
        }
100
        return r
101
}
102
 
103
func randomString(n int, rand *rand.Rand) string {
104
        b := randomBytes(n, rand)
105
        return string(b)
106
}
107
 
108
func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
109
        m := &clientHelloMsg{}
110
        m.vers = uint16(rand.Intn(65536))
111
        m.random = randomBytes(32, rand)
112
        m.sessionId = randomBytes(rand.Intn(32), rand)
113
        m.cipherSuites = make([]uint16, rand.Intn(63)+1)
114
        for i := 0; i < len(m.cipherSuites); i++ {
115
                m.cipherSuites[i] = uint16(rand.Int31())
116
        }
117
        m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
118
        if rand.Intn(10) > 5 {
119
                m.nextProtoNeg = true
120
        }
121
        if rand.Intn(10) > 5 {
122
                m.serverName = randomString(rand.Intn(255), rand)
123
        }
124
        m.ocspStapling = rand.Intn(10) > 5
125
        m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)
126
        m.supportedCurves = make([]uint16, rand.Intn(5)+1)
127
        for i := range m.supportedCurves {
128
                m.supportedCurves[i] = uint16(rand.Intn(30000))
129
        }
130
 
131
        return reflect.ValueOf(m)
132
}
133
 
134
func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
135
        m := &serverHelloMsg{}
136
        m.vers = uint16(rand.Intn(65536))
137
        m.random = randomBytes(32, rand)
138
        m.sessionId = randomBytes(rand.Intn(32), rand)
139
        m.cipherSuite = uint16(rand.Int31())
140
        m.compressionMethod = uint8(rand.Intn(256))
141
 
142
        if rand.Intn(10) > 5 {
143
                m.nextProtoNeg = true
144
 
145
                n := rand.Intn(10)
146
                m.nextProtos = make([]string, n)
147
                for i := 0; i < n; i++ {
148
                        m.nextProtos[i] = randomString(20, rand)
149
                }
150
        }
151
 
152
        return reflect.ValueOf(m)
153
}
154
 
155
func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
156
        m := &certificateMsg{}
157
        numCerts := rand.Intn(20)
158
        m.certificates = make([][]byte, numCerts)
159
        for i := 0; i < numCerts; i++ {
160
                m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
161
        }
162
        return reflect.ValueOf(m)
163
}
164
 
165
func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
166
        m := &certificateRequestMsg{}
167
        m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
168
        numCAs := rand.Intn(100)
169
        m.certificateAuthorities = make([][]byte, numCAs)
170
        for i := 0; i < numCAs; i++ {
171
                m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
172
        }
173
        return reflect.ValueOf(m)
174
}
175
 
176
func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
177
        m := &certificateVerifyMsg{}
178
        m.signature = randomBytes(rand.Intn(15)+1, rand)
179
        return reflect.ValueOf(m)
180
}
181
 
182
func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
183
        m := &certificateStatusMsg{}
184
        if rand.Intn(10) > 5 {
185
                m.statusType = statusTypeOCSP
186
                m.response = randomBytes(rand.Intn(10)+1, rand)
187
        } else {
188
                m.statusType = 42
189
        }
190
        return reflect.ValueOf(m)
191
}
192
 
193
func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
194
        m := &clientKeyExchangeMsg{}
195
        m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
196
        return reflect.ValueOf(m)
197
}
198
 
199
func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {
200
        m := &finishedMsg{}
201
        m.verifyData = randomBytes(12, rand)
202
        return reflect.ValueOf(m)
203
}
204
 
205
func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
206
        m := &nextProtoMsg{}
207
        m.proto = randomString(rand.Intn(255), rand)
208
        return reflect.ValueOf(m)
209
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.