Skip to content

Commit ce86709

Browse files
committed
Add unit test for noconn package
1 parent 0c60c01 commit ce86709

11 files changed

Lines changed: 306 additions & 62 deletions

File tree

‎crypto/etls/conn.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ import (
3030
)
3131

3232
const (
33+
// MagicSize is the ETLS magic header size.
3334
MagicSize = 2
3435
)
3536

3637
var (
37-
// MagicBytes is the ETLS connection magic header
38+
// MagicBytes is the ETLS connection magic header.
3839
MagicBytes = [MagicSize]byte{0xC0, 0x4E}
3940
)
4041

@@ -44,6 +45,7 @@ type CryptoConn struct {
4445
*Cipher
4546
}
4647

48+
// NewConn returns a new CryptoConn.
4749
func NewConn(c net.Conn, cipher *Cipher) *CryptoConn {
4850
return &CryptoConn{
4951
Conn: c,

‎crypto/etls/conn_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func client(pass string) (ret int, err error) {
114114
log.Errorf("client: dial: %s", err)
115115
return 0, err
116116
}
117-
defer conn.Close()
117+
defer func() { _ = conn.Close() }()
118118
//conn.SetDeadline(time.Time{})
119119
//conn.SetReadDeadline(time.Time{})
120120
//conn.SetWriteDeadline(time.Time{})
@@ -164,7 +164,7 @@ func clientComplex(pass string, args *QueryComplex) (ret *ResultComplex, err err
164164
log.Errorf("client: dial: %s", err)
165165
return nil, err
166166
}
167-
defer conn.Close()
167+
defer func() { _ = conn.Close() }()
168168

169169
log.Debugln("client: connected to: ", conn.RemoteAddr())
170170
log.Debugln("client: LocalAddr: ", conn.LocalAddr())
@@ -179,7 +179,7 @@ func clientComplex(pass string, args *QueryComplex) (ret *ResultComplex, err err
179179
}
180180

181181
func handleClient(conn net.Conn) {
182-
defer conn.Close()
182+
defer func() { _ = conn.Close() }()
183183
var err error
184184

185185
if c, ok := conn.(*CryptoConn); ok {

‎csconn/resolver.go‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

csconn/conn.go renamed to noconn/conn.go

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
package csconn
1+
/*
2+
* Copyright 2019 The CovenantSQL Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package noconn
218

319
import (
420
"bytes"
@@ -14,45 +30,49 @@ import (
1430
)
1531

1632
const (
17-
// HeaderSize is the header size with ETLSHeader + NodeID + Nonce
33+
// HeaderSize is the header size with ETLSHeader + NodeID + Nonce.
1834
HeaderSize = etls.MagicSize + hash.HashBSize + cpuminer.Uint256Size
1935
)
2036

21-
type CSConn struct {
37+
// NOConn defines node-oriented connection based on ETLS crypto connection.
38+
type NOConn struct {
2239
*etls.CryptoConn
2340
isClient bool
2441

2542
// The following fields may be rewritten during handshake.
26-
isAnonymous bool
27-
remoteNodeID proto.RawNodeID
43+
isAnonymous bool
44+
remote proto.RawNodeID
2845
}
2946

30-
func NewServerConn(conn net.Conn) *CSConn {
31-
return &CSConn{
47+
// NewServerConn takes a raw connection and returns a new server side NOConn.
48+
func NewServerConn(conn net.Conn) *NOConn {
49+
return &NOConn{
3250
CryptoConn: etls.NewConn(conn, nil), // at server side, cipher will be set during handshake
3351
}
3452
}
3553

36-
//func NewClientConn(conn net.Conn) *CSConn {
37-
// return &CSConn{
54+
//func NewClientConn(conn net.Conn) *NOConn {
55+
// return &NOConn{
3856
// CryptoConn: etls.NewConn(conn, nil),
3957
// isClient: true,
4058
// isAnonymous: true,
4159
// }
4260
//}
4361

44-
func (c *CSConn) RemoteNodeID() proto.RawNodeID {
45-
return c.remoteNodeID
62+
// Remote returns the remote node ID of the NOConn.
63+
func (c *NOConn) Remote() proto.RawNodeID {
64+
return c.remote
4665
}
4766

48-
func (c *CSConn) Handshake() (err error) {
67+
// Handshake does the initial handshaking according to the connection role.
68+
func (c *NOConn) Handshake() (err error) {
4969
if c.isClient {
5070
return c.clientHandshake()
5171
}
5272
return c.serverHandshake()
5373
}
5474

55-
func (c *CSConn) serverHandshake() (err error) {
75+
func (c *NOConn) serverHandshake() (err error) {
5676
headerBuf := make([]byte, HeaderSize)
5777
rCount, err := c.CryptoConn.Conn.Read(headerBuf)
5878
if err != nil {
@@ -74,7 +94,7 @@ func (c *CSConn) serverHandshake() (err error) {
7494
idHash, _ := hash.NewHash(headerBuf[etls.MagicSize : etls.MagicSize+hash.HashBSize])
7595
rawNodeID := &proto.RawNodeID{Hash: *idHash}
7696
// TODO(auxten): compute the nonce and check difficulty
77-
cpuminer.Uint256FromBytes(headerBuf[etls.MagicSize+hash.HashBSize:])
97+
_, _ = cpuminer.Uint256FromBytes(headerBuf[etls.MagicSize+hash.HashBSize:])
7898

7999
isAnonymous := rawNodeID.IsEqual(&kms.AnonymousRawNodeID.Hash)
80100
symmetricKey, err := GetSharedSecretWith(defaultResolver, rawNodeID, isAnonymous)
@@ -83,14 +103,14 @@ func (c *CSConn) serverHandshake() (err error) {
83103
return
84104
}
85105
cipher := etls.NewCipher(symmetricKey)
86-
c.CryptoConn.Cipher = cipher
87-
c.remoteNodeID = *rawNodeID
106+
c.CryptoConn.Cipher = cipher // reset cipher
107+
c.remote = *rawNodeID
88108
c.isAnonymous = isAnonymous
89109

90110
return
91111
}
92112

93-
func (c *CSConn) clientHandshake() (err error) {
113+
func (c *NOConn) clientHandshake() (err error) {
94114
writeBuf := make([]byte, HeaderSize)
95115
copy(writeBuf, etls.MagicBytes[:])
96116
if c.isAnonymous {
@@ -127,12 +147,12 @@ func (c *CSConn) clientHandshake() (err error) {
127147
}
128148

129149
// Accept takes the ownership of conn and accepts it as a CSConn.
130-
func Accept(conn net.Conn) (*CSConn, error) {
131-
csconn := NewServerConn(conn)
132-
if err := csconn.Handshake(); err != nil {
150+
func Accept(conn net.Conn) (*NOConn, error) {
151+
noconn := NewServerConn(conn)
152+
if err := noconn.Handshake(); err != nil {
133153
return nil, err
134154
}
135-
return csconn, nil
155+
return noconn, nil
136156
}
137157

138158
// Dial connects to the node with remote node id.
@@ -176,17 +196,17 @@ func DialEx(remote proto.NodeID, isAnonymous bool) (conn net.Conn, err error) {
176196
return
177197
}
178198

179-
csconn := &CSConn{
180-
CryptoConn: etls.NewConn(iconn, cipher),
181-
isAnonymous: isAnonymous,
182-
isClient: true,
183-
remoteNodeID: *rawNodeID,
199+
noconn := &NOConn{
200+
CryptoConn: etls.NewConn(iconn, cipher),
201+
isAnonymous: isAnonymous,
202+
isClient: true,
203+
remote: *rawNodeID,
184204
}
185205

186-
if err = csconn.Handshake(); err != nil {
206+
if err = noconn.Handshake(); err != nil {
187207
err = errors.Wrapf(err, "connect %s %s failed", rawNodeID.String(), nodeAddr)
188208
return
189209
}
190210

191-
return csconn, nil
211+
return noconn, nil
192212
}

0 commit comments

Comments
 (0)