Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
86372b20
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
86372b20
编写于
4月 14, 2015
作者:
P
Péter Szilágyi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
whisper: add basic tests for the whiper peers
上级
f6efdd8a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
221 addition
and
0 deletion
+221
-0
whisper/common_test.go
whisper/common_test.go
+38
-0
whisper/peer_test.go
whisper/peer_test.go
+183
-0
未找到文件。
whisper/common_test.go
0 → 100644
浏览文件 @
86372b20
// Contains some common utility functions for testing.
package
whisper
import
(
"fmt"
"math/rand"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/p2p/discover"
)
// randomNodeID generates and returns a random P2P discovery node id for the
// whisper tests.
func
randomNodeID
()
(
id
discover
.
NodeID
)
{
for
i
:=
range
id
{
id
[
i
]
=
byte
(
rand
.
Intn
(
255
))
}
return
id
}
// randomNodeName generates and returns a random P2P node name for the whisper
// tests.
func
randomNodeName
()
string
{
return
common
.
MakeName
(
fmt
.
Sprintf
(
"whisper-go-test-%3d"
,
rand
.
Intn
(
999
)),
"1.0"
)
}
// whisperCaps returns the node capabilities for running the whisper sub-protocol.
func
whisperCaps
()
[]
p2p
.
Cap
{
return
[]
p2p
.
Cap
{
p2p
.
Cap
{
Name
:
protocolName
,
Version
:
uint
(
protocolVersion
),
},
}
}
whisper/peer_test.go
0 → 100644
浏览文件 @
86372b20
package
whisper
import
(
"testing"
"time"
"github.com/ethereum/go-ethereum/p2p"
)
type
testPeer
struct
{
client
*
Whisper
stream
*
p2p
.
MsgPipeRW
termed
chan
struct
{}
}
func
startTestPeer
()
*
testPeer
{
// Create a simulated P2P remote peer and data streams to it
remote
:=
p2p
.
NewPeer
(
randomNodeID
(),
randomNodeName
(),
whisperCaps
())
tester
,
tested
:=
p2p
.
MsgPipe
()
// Create a whisper client and connect with it to the tester peer
client
:=
New
()
client
.
Start
()
termed
:=
make
(
chan
struct
{})
go
func
()
{
defer
client
.
Stop
()
defer
close
(
termed
)
defer
tested
.
Close
()
client
.
handlePeer
(
remote
,
tested
)
}()
// Assemble and return the test peer
return
&
testPeer
{
client
:
client
,
stream
:
tester
,
termed
:
termed
,
}
}
func
TestPeerStatusMessage
(
t
*
testing
.
T
)
{
tester
:=
startTestPeer
()
// Wait for the handshake status message and check it
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
statusCode
,
[]
uint64
{
protocolVersion
});
err
!=
nil
{
t
.
Fatalf
(
"status message mismatch: %v"
,
err
)
}
// Terminate the node
tester
.
stream
.
Close
()
select
{
case
<-
tester
.
termed
:
case
<-
time
.
After
(
time
.
Second
)
:
t
.
Fatalf
(
"local close timed out"
)
}
}
func
TestPeerHandshakeFail
(
t
*
testing
.
T
)
{
tester
:=
startTestPeer
()
// Wait for and check the handshake
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
statusCode
,
[]
uint64
{
protocolVersion
});
err
!=
nil
{
t
.
Fatalf
(
"status message mismatch: %v"
,
err
)
}
// Send an invalid handshake status and verify disconnect
if
err
:=
p2p
.
SendItems
(
tester
.
stream
,
messagesCode
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send malformed status: %v"
,
err
)
}
select
{
case
<-
tester
.
termed
:
case
<-
time
.
After
(
time
.
Second
)
:
t
.
Fatalf
(
"remote close timed out"
)
}
}
func
TestPeerHandshakeSuccess
(
t
*
testing
.
T
)
{
tester
:=
startTestPeer
()
// Wait for and check the handshake
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
statusCode
,
[]
uint64
{
protocolVersion
});
err
!=
nil
{
t
.
Fatalf
(
"status message mismatch: %v"
,
err
)
}
// Send a valid handshake status and make sure connection stays live
if
err
:=
p2p
.
SendItems
(
tester
.
stream
,
statusCode
,
protocolVersion
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send status: %v"
,
err
)
}
select
{
case
<-
tester
.
termed
:
t
.
Fatalf
(
"valid handshake disconnected"
)
case
<-
time
.
After
(
100
*
time
.
Millisecond
)
:
}
// Clean up the test
tester
.
stream
.
Close
()
select
{
case
<-
tester
.
termed
:
case
<-
time
.
After
(
time
.
Second
)
:
t
.
Fatalf
(
"local close timed out"
)
}
}
func
TestPeerSend
(
t
*
testing
.
T
)
{
// Start a tester and execute the handshake
tester
:=
startTestPeer
()
defer
tester
.
stream
.
Close
()
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
statusCode
,
[]
uint64
{
protocolVersion
});
err
!=
nil
{
t
.
Fatalf
(
"status message mismatch: %v"
,
err
)
}
if
err
:=
p2p
.
SendItems
(
tester
.
stream
,
statusCode
,
protocolVersion
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send status: %v"
,
err
)
}
// Construct a message and inject into the tester
message
:=
NewMessage
([]
byte
(
"peer broadcast test message"
))
envelope
,
err
:=
message
.
Wrap
(
DefaultPoW
,
Options
{
TTL
:
DefaultTTL
,
})
if
err
!=
nil
{
t
.
Fatalf
(
"failed to wrap message: %v"
,
err
)
}
if
err
:=
tester
.
client
.
Send
(
envelope
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send message: %v"
,
err
)
}
// Check that the message is eventually forwarded
payload
:=
[]
interface
{}{
envelope
}
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
messagesCode
,
payload
);
err
!=
nil
{
t
.
Fatalf
(
"message mismatch: %v"
,
err
)
}
// Make sure that even with a re-insert, an empty batch is received
if
err
:=
tester
.
client
.
Send
(
envelope
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send message: %v"
,
err
)
}
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
messagesCode
,
[]
interface
{}{});
err
!=
nil
{
t
.
Fatalf
(
"message mismatch: %v"
,
err
)
}
}
func
TestPeerDeliver
(
t
*
testing
.
T
)
{
// Start a tester and execute the handshake
tester
:=
startTestPeer
()
defer
tester
.
stream
.
Close
()
if
err
:=
p2p
.
ExpectMsg
(
tester
.
stream
,
statusCode
,
[]
uint64
{
protocolVersion
});
err
!=
nil
{
t
.
Fatalf
(
"status message mismatch: %v"
,
err
)
}
if
err
:=
p2p
.
SendItems
(
tester
.
stream
,
statusCode
,
protocolVersion
);
err
!=
nil
{
t
.
Fatalf
(
"failed to send status: %v"
,
err
)
}
// Watch for all inbound messages
arrived
:=
make
(
chan
struct
{},
1
)
tester
.
client
.
Watch
(
Filter
{
Fn
:
func
(
message
*
Message
)
{
arrived
<-
struct
{}{}
},
})
// Construct a message and deliver it to the tester peer
message
:=
NewMessage
([]
byte
(
"peer broadcast test message"
))
envelope
,
err
:=
message
.
Wrap
(
DefaultPoW
,
Options
{
TTL
:
DefaultTTL
,
})
if
err
!=
nil
{
t
.
Fatalf
(
"failed to wrap message: %v"
,
err
)
}
if
err
:=
p2p
.
Send
(
tester
.
stream
,
messagesCode
,
[]
*
Envelope
{
envelope
});
err
!=
nil
{
t
.
Fatalf
(
"failed to transfer message: %v"
,
err
)
}
// Check that the message is delivered upstream
select
{
case
<-
arrived
:
case
<-
time
.
After
(
time
.
Second
)
:
t
.
Fatalf
(
"message delivery timeout"
)
}
// Check that a resend is not delivered
if
err
:=
p2p
.
Send
(
tester
.
stream
,
messagesCode
,
[]
*
Envelope
{
envelope
});
err
!=
nil
{
t
.
Fatalf
(
"failed to transfer message: %v"
,
err
)
}
select
{
case
<-
time
.
After
(
2
*
transmissionTicks
)
:
case
<-
arrived
:
t
.
Fatalf
(
"repeating message arrived"
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录