Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
5a088ffa
Commit
5a088ffa
authored
Jan 15, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
cf62ac88
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
27 deletions
+75
-27
t/neo/proto_test.go
t/neo/proto_test.go
+74
-26
t/neo/protogen.go
t/neo/protogen.go
+1
-1
No files found.
t/neo/proto_test.go
View file @
5a088ffa
...
...
@@ -15,11 +15,21 @@
package
neo
import
(
hexpkg
"encoding/hex"
"reflect"
"testing"
"unsafe"
)
// decode string as hex; panic on error
func
hex
(
s
string
)
string
{
b
,
err
:=
hexpkg
.
DecodeString
(
s
)
if
err
!=
nil
{
panic
(
err
)
}
return
string
(
b
)
}
func
TestPktHeader
(
t
*
testing
.
T
)
{
// make sure PktHeader is really packed
if
unsafe
.
Sizeof
(
PktHead
{})
!=
10
{
...
...
@@ -27,43 +37,81 @@ func TestPktHeader(t *testing.T) {
}
}
func
testPktMarshal
(
t
*
testing
.
T
,
pkt
NEODecoder
,
encoded
string
)
{
typ
:=
reflect
.
TypeOf
(
pkt
)
.
Elem
()
// type of *pkt
pkt2
:=
reflect
.
New
(
typ
)
.
Interface
()
.
(
NEODecoder
)
defer
func
()
{
if
e
:=
recover
();
e
!=
nil
{
t
.
Errorf
(
"%v: panic ↓↓↓:"
,
typ
)
panic
(
e
)
// to show traceback
}
}()
// TODO check encoding
// check decoding
data
:=
encoded
+
"noise"
n
,
err
:=
pkt2
.
NEODecode
([]
byte
(
data
))
// XXX
if
err
!=
nil
{
t
.
Errorf
(
"%v: decode error %v"
,
typ
,
err
)
}
if
n
!=
len
(
encoded
)
{
t
.
Errorf
(
"%v: nread = %v ; want %v"
,
typ
,
n
,
len
(
encoded
))
}
if
!
reflect
.
DeepEqual
(
pkt2
,
pkt
)
{
t
.
Errorf
(
"%v: decode result unexpected: %v ; want %v"
,
typ
,
pkt2
,
pkt
)
}
// decode must overflow on cut data TODO reenable
/*
for l := len(encoded)-1; l >= 0; l-- {
data = encoded[:l] // XXX also check on original byte [:l] ?
n, err = pkt2.NEODecode([]byte(data)) // XXX
if !(n==0 && err==ErrDecodeOverflow) {
t.Errorf("%v: decode overflow not detected on [:%v]", typ, l)
}
}
*/
}
// test encoding/decoding of packets
func
TestPktMarshal
(
t
*
testing
.
T
)
{
var
testv
=
[]
struct
{
pkt
NEODecoder
//interface {NEOEncoder; NEODecoder}
encoded
string
// []byte
}
{
// empty
{
&
Ping
{},
""
},
// uint32, string XXX string -> Notify?
{
&
Error
{
Code
:
0x01020304
,
Message
:
"hello"
},
"
\x01\x02\x03\x04\x00\x00\x00\x05
hello"
},
}
for
_
,
tt
:=
range
testv
{
// TODO check encoding
// check decoding
data
:=
tt
.
encoded
+
"noise"
typ
:=
reflect
.
TypeOf
(
tt
.
pkt
)
.
Elem
()
// type of *pkt
pkt2
:=
reflect
.
New
(
typ
)
.
Interface
()
.
(
NEODecoder
)
n
,
err
:=
pkt2
.
NEODecode
([]
byte
(
data
))
// XXX
if
err
!=
nil
{
t
.
Errorf
(
"%v: decode error %v"
,
typ
,
err
)
}
if
n
!=
len
(
tt
.
encoded
)
{
t
.
Errorf
(
"%v: nread = %v ; want %v"
,
typ
,
n
,
len
(
tt
.
encoded
))
}
// Oid, Tid, bool, Checksum, []byte
{
&
StoreObject
{
Oid
:
0x0102030405060708
,
Serial
:
0x0a0b0c0d0e0f0102
,
Compression
:
false
,
Checksum
:
Checksum
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
,
16
,
17
,
18
,
19
,
20
},
// XXX simpler?
Data
:
[]
byte
(
"hello world"
),
DataSerial
:
0x0a0b0c0d0e0f0103
,
Tid
:
0x0a0b0c0d0e0f0104
,
Unlock
:
true
,
},
if
!
reflect
.
DeepEqual
(
pkt2
,
tt
.
pkt
)
{
t
.
Errorf
(
"%v: decode result unexpected: %v ; want %v"
,
typ
,
pkt2
,
tt
.
pkt
)
}
hex
(
"01020304050607080a0b0c0d0e0f010200"
)
+
hex
(
"0102030405060708090a0b0c0d0e0f1011121314"
)
+
hex
(
"0000000b"
)
+
"hello world"
+
hex
(
"0a0b0c0d0e0f01030a0b0c0d0e0f010401"
)},
// decode must overflow on cut data
for
l
:=
len
(
tt
.
encoded
)
-
1
;
l
>=
0
;
l
--
{
data
=
tt
.
encoded
[
:
l
]
// XXX also check on original byte [:l] ?
n
,
err
=
pkt2
.
NEODecode
([]
byte
(
data
))
// XXX
if
!
(
n
==
0
&&
err
==
ErrDecodeOverflow
)
{
t
.
Errorf
(
"%v: decode overflow not detected on [:%v]"
,
typ
,
l
)
}
// TODO bool, [], map
// TODO Address, Checksum, Tid, PTid
}
// bool: StoreObject, AnswerGetObject
}
for
_
,
tt
:=
range
testv
{
testPktMarshal
(
t
,
tt
.
pkt
,
tt
.
encoded
)
}
}
t/neo/protogen.go
View file @
5a088ffa
...
...
@@ -11,7 +11,7 @@
// See COPYING file for full licensing terms.
// NEO. Protocol definition. Code generator
// TODO text what it does (generates code for proto.go)
// TODO text what it does (generates
marshal
code for proto.go)
// +build ignore
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment