Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
4811f460
Commit
4811f460
authored
Mar 19, 2015
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p: export ExpectMsg (for eth protocol testing)
parent
e13c6739
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
32 deletions
+32
-32
message.go
p2p/message.go
+32
-0
peer_test.go
p2p/peer_test.go
+0
-32
No files found.
p2p/message.go
View file @
4811f460
...
...
@@ -208,3 +208,35 @@ func (p *MsgPipeRW) Close() error {
close
(
p
.
closing
)
return
nil
}
// ExpectMsg reads a message from r and verifies that its
// code and encoded RLP content match the provided values.
// If content is nil, the payload is discarded and not verified.
func
ExpectMsg
(
r
MsgReader
,
code
uint64
,
content
interface
{})
error
{
msg
,
err
:=
r
.
ReadMsg
()
if
err
!=
nil
{
return
err
}
if
msg
.
Code
!=
code
{
return
fmt
.
Errorf
(
"message code mismatch: got %d, expected %d"
,
msg
.
Code
,
code
)
}
if
content
==
nil
{
return
msg
.
Discard
()
}
else
{
contentEnc
,
err
:=
rlp
.
EncodeToBytes
(
content
)
if
err
!=
nil
{
panic
(
"content encode error: "
+
err
.
Error
())
}
if
int
(
msg
.
Size
)
!=
len
(
contentEnc
)
{
return
fmt
.
Errorf
(
"message size mismatch: got %d, want %d"
,
msg
.
Size
,
len
(
contentEnc
))
}
actualContent
,
err
:=
ioutil
.
ReadAll
(
msg
.
Payload
)
if
err
!=
nil
{
return
err
}
if
!
bytes
.
Equal
(
actualContent
,
contentEnc
)
{
return
fmt
.
Errorf
(
"message payload mismatch:
\n
got: %x
\n
want: %x"
,
actualContent
,
contentEnc
)
}
}
return
nil
}
p2p/peer_test.go
View file @
4811f460
...
...
@@ -192,35 +192,3 @@ func TestNewPeer(t *testing.T) {
p
.
Disconnect
(
DiscAlreadyConnected
)
// Should not hang
}
// expectMsg reads a message from r and verifies that its
// code and encoded RLP content match the provided values.
// If content is nil, the payload is discarded and not verified.
func
expectMsg
(
r
MsgReader
,
code
uint64
,
content
interface
{})
error
{
msg
,
err
:=
r
.
ReadMsg
()
if
err
!=
nil
{
return
err
}
if
msg
.
Code
!=
code
{
return
fmt
.
Errorf
(
"message code mismatch: got %d, expected %d"
,
msg
.
Code
,
code
)
}
if
content
==
nil
{
return
msg
.
Discard
()
}
else
{
contentEnc
,
err
:=
rlp
.
EncodeToBytes
(
content
)
if
err
!=
nil
{
panic
(
"content encode error: "
+
err
.
Error
())
}
if
int
(
msg
.
Size
)
!=
len
(
contentEnc
)
{
return
fmt
.
Errorf
(
"message size mismatch: got %d, want %d"
,
msg
.
Size
,
len
(
contentEnc
))
}
actualContent
,
err
:=
ioutil
.
ReadAll
(
msg
.
Payload
)
if
err
!=
nil
{
return
err
}
if
!
bytes
.
Equal
(
actualContent
,
contentEnc
)
{
return
fmt
.
Errorf
(
"message payload mismatch:
\n
got: %x
\n
want: %x"
,
actualContent
,
contentEnc
)
}
}
return
nil
}
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