Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
a3a48ce6
提交
a3a48ce6
authored
12月 14, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
conn: tests pass :)
上级
0061f0c1
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
82 行增加
和
69 行删除
+82
-69
conn_test.go
net/conn/conn_test.go
+3
-8
dial.go
net/conn/dial.go
+4
-0
dial_test.go
net/conn/dial_test.go
+13
-3
interface.go
net/conn/interface.go
+9
-0
listen.go
net/conn/listen.go
+21
-20
secure_conn_test.go
net/conn/secure_conn_test.go
+32
-38
没有找到文件。
net/conn/conn_test.go
浏览文件 @
a3a48ce6
...
...
@@ -42,22 +42,17 @@ func TestClose(t *testing.T) {
// t.Skip("Skipping in favor of another test")
ctx
:=
context
.
Background
()
c1
,
c2
:=
setupConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/5534"
,
"/ip4/127.0.0.1/tcp/5545"
)
c1
,
c2
:=
setup
Single
Conn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/5534"
,
"/ip4/127.0.0.1/tcp/5545"
)
testOneSendRecv
(
t
,
c1
,
c2
)
testOneSendRecv
(
t
,
c2
,
c1
)
c1
.
Close
()
time
.
After
(
200
*
time
.
Millisecond
)
testNotOneSendRecv
(
t
,
c1
,
c2
)
testNotOneSendRecv
(
t
,
c2
,
c1
)
c2
.
Close
()
time
.
After
(
20000
*
time
.
Millisecond
)
testNotOneSendRecv
(
t
,
c1
,
c2
)
testNotOneSendRecv
(
t
,
c2
,
c1
)
testNotOneSendRecv
(
t
,
c1
,
c2
)
}
func
TestCloseLeak
(
t
*
testing
.
T
)
{
...
...
@@ -75,7 +70,7 @@ func TestCloseLeak(t *testing.T) {
a1
:=
strconv
.
Itoa
(
p1
)
a2
:=
strconv
.
Itoa
(
p2
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
c1
,
c2
:=
setupConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/"
+
a1
,
"/ip4/127.0.0.1/tcp/"
+
a2
)
c1
,
c2
:=
setup
Single
Conn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/"
+
a1
,
"/ip4/127.0.0.1/tcp/"
+
a2
)
for
i
:=
0
;
i
<
num
;
i
++
{
b1
:=
[]
byte
(
fmt
.
Sprintf
(
"beep%d"
,
i
))
...
...
net/conn/dial.go
浏览文件 @
a3a48ce6
...
...
@@ -78,6 +78,10 @@ func (d *Dialer) DialAddr(ctx context.Context, raddr ma.Multiaddr, remote peer.P
return
nil
,
err
}
if
d
.
WithoutSecureTransport
{
return
c
,
nil
}
select
{
case
<-
ctx
.
Done
()
:
c
.
Close
()
...
...
net/conn/dial_test.go
浏览文件 @
a3a48ce6
...
...
@@ -49,7 +49,15 @@ func echo(c Conn) {
io
.
Copy
(
c
,
c
)
}
func
setupConn
(
t
*
testing
.
T
,
ctx
context
.
Context
,
a1
,
a2
string
)
(
a
,
b
Conn
)
{
func
setupSecureConn
(
t
*
testing
.
T
,
ctx
context
.
Context
,
a1
,
a2
string
)
(
a
,
b
Conn
)
{
return
setupConn
(
t
,
ctx
,
a1
,
a2
,
true
)
}
func
setupSingleConn
(
t
*
testing
.
T
,
ctx
context
.
Context
,
a1
,
a2
string
)
(
a
,
b
Conn
)
{
return
setupConn
(
t
,
ctx
,
a1
,
a2
,
false
)
}
func
setupConn
(
t
*
testing
.
T
,
ctx
context
.
Context
,
a1
,
a2
string
,
secure
bool
)
(
a
,
b
Conn
)
{
p1
,
err
:=
setupPeer
(
a1
)
if
err
!=
nil
{
...
...
@@ -72,13 +80,15 @@ func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
ps2
.
Add
(
p2
)
l1
,
err
:=
Listen
(
ctx
,
laddr
,
p1
,
ps1
)
l1
.
SetWithoutSecureTransport
(
!
secure
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
d2
:=
&
Dialer
{
Peerstore
:
ps2
,
LocalPeer
:
p2
,
Peerstore
:
ps2
,
LocalPeer
:
p2
,
WithoutSecureTransport
:
!
secure
,
}
var
c2
Conn
...
...
net/conn/interface.go
浏览文件 @
a3a48ce6
...
...
@@ -57,6 +57,10 @@ type Dialer struct {
// because when an incoming connection is identified, we should reuse the
// same peer objects (otherwise things get inconsistent).
Peerstore
peer
.
Peerstore
// WithoutSecureTransport determines whether to initialize an insecure connection.
// Phrased negatively so default is Secure, and verbosely to be very clear.
WithoutSecureTransport
bool
}
// Listener is an object that can accept connections. It matches net.Listener
...
...
@@ -65,6 +69,11 @@ type Listener interface {
// Accept waits for and returns the next connection to the listener.
Accept
()
(
net
.
Conn
,
error
)
// {Set}WithoutSecureTransport decides whether to start insecure connections.
// Phrased negatively so default is Secure, and verbosely to be very clear.
WithoutSecureTransport
()
bool
SetWithoutSecureTransport
(
bool
)
// Addr is the local address
Addr
()
net
.
Addr
...
...
net/conn/listen.go
浏览文件 @
a3a48ce6
...
...
@@ -13,8 +13,7 @@ import (
// listener is an object that can accept connections. It implements Listener
type
listener
struct
{
notSecure
bool
notSecureIMeanIt
bool
withoutSecureTransport
bool
manet
.
Listener
...
...
@@ -52,19 +51,22 @@ func (l *listener) Accept() (net.Conn, error) {
return
nil
,
fmt
.
Errorf
(
"Error accepting connection: %v"
,
err
)
}
if
l
.
Secure
()
{
sc
,
err
:=
newSecureConn
(
ctx
,
c
,
l
.
peers
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Error securing connection: %v"
,
err
)
}
return
sc
,
nil
if
l
.
withoutSecureTransport
{
return
c
,
nil
}
sc
,
err
:=
newSecureConn
(
ctx
,
c
,
l
.
peers
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Error securing connection: %v"
,
err
)
}
return
sc
,
nil
}
return
c
,
nil
func
(
l
*
listener
)
WithoutSecureTransport
()
bool
{
return
l
.
withoutSecureTransport
}
func
(
l
*
listener
)
Se
cure
()
bool
{
return
!
(
l
.
notSecure
&&
l
.
notSecureIMeanIt
)
func
(
l
*
listener
)
Se
tWithoutSecureTransport
(
b
bool
)
{
l
.
withoutSecureTransport
=
b
}
func
(
l
*
listener
)
Addr
()
net
.
Addr
{
...
...
@@ -91,9 +93,9 @@ func (l *listener) Peerstore() peer.Peerstore {
func
(
l
*
listener
)
Loggable
()
map
[
string
]
interface
{}
{
return
map
[
string
]
interface
{}{
"listener"
:
map
[
string
]
interface
{}{
"peer"
:
l
.
LocalPeer
(),
"address"
:
l
.
Multiaddr
(),
"
secure"
:
l
.
Secure
()
,
"peer"
:
l
.
LocalPeer
(),
"address"
:
l
.
Multiaddr
(),
"
withoutSecureTransport"
:
l
.
withoutSecureTransport
,
},
}
}
...
...
@@ -107,12 +109,11 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local peer.Peer, peers peer.
}
l
:=
&
listener
{
Listener
:
ml
,
maddr
:
addr
,
peers
:
peers
,
local
:
local
,
notSecure
:
false
,
notSecureIMeanIt
:
false
,
Listener
:
ml
,
maddr
:
addr
,
peers
:
peers
,
local
:
local
,
withoutSecureTransport
:
false
,
}
log
.
Infof
(
"swarm listening on %s
\n
"
,
l
.
Multiaddr
())
...
...
net/conn/secure_conn_test.go
浏览文件 @
a3a48ce6
...
...
@@ -15,9 +15,8 @@ import (
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
func
setupSecureConn
(
t
*
testing
.
T
,
ctx
context
.
Context
,
c
Conn
)
(
Conn
,
error
)
{
c
,
ok
:=
c
.
(
*
secureConn
)
if
ok
{
func
upgradeToSecureConn
(
t
*
testing
.
T
,
ctx
context
.
Context
,
c
Conn
)
(
Conn
,
error
)
{
if
c
,
ok
:=
c
.
(
*
secureConn
);
ok
{
return
c
,
nil
}
...
...
@@ -29,26 +28,33 @@ func setupSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) {
return
s
,
nil
}
func
secureHandshake
(
t
*
testing
.
T
,
ctx
context
.
Context
,
c
Conn
,
done
chan
error
)
{
_
,
err
:=
upgradeToSecureConn
(
t
,
ctx
,
c
)
done
<-
err
}
func
TestSecureClose
(
t
*
testing
.
T
)
{
// t.Skip("Skipping in favor of another test")
ctx
:=
context
.
Background
()
c1
,
c2
:=
setupConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/6634"
,
"/ip4/127.0.0.1/tcp/6645"
)
c1
,
c2
:=
setup
Single
Conn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/6634"
,
"/ip4/127.0.0.1/tcp/6645"
)
c1
,
err1
:=
setupSecureConn
(
t
,
ctx
,
c1
)
c2
,
err2
:=
setupSecureConn
(
t
,
ctx
,
c2
)
if
err1
!=
nil
{
t
.
Fatal
(
err1
)
}
if
err2
!=
nil
{
t
.
Fatal
(
err2
)
done
:=
make
(
chan
error
)
go
secureHandshake
(
t
,
ctx
,
c1
,
done
)
go
secureHandshake
(
t
,
ctx
,
c2
,
done
)
for
i
:=
0
;
i
<
2
;
i
++
{
if
err
:=
<-
done
;
err
!=
nil
{
t
.
Error
(
err
)
}
}
testOneSendRecv
(
t
,
c1
,
c2
)
testOneSendRecv
(
t
,
c2
,
c1
)
c1
.
Close
()
testNotOneSendRecv
(
t
,
c1
,
c2
)
c2
.
Close
()
testNotOneSendRecv
(
t
,
c1
,
c2
)
testNotOneSendRecv
(
t
,
c2
,
c1
)
...
...
@@ -57,23 +63,20 @@ func TestSecureClose(t *testing.T) {
func
TestSecureCancelHandshake
(
t
*
testing
.
T
)
{
// t.Skip("Skipping in favor of another test")
ctx
:=
context
.
Background
()
c1
,
c2
:=
setupConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/6634"
,
"/ip4/127.0.0.1/tcp/6645"
)
done
:=
make
(
chan
struct
{})
go
func
()
{
_
,
err1
:=
setupSecureConn
(
t
,
ctx
,
c1
)
_
,
err2
:=
setupSecureConn
(
t
,
ctx
,
c2
)
if
err1
==
nil
{
t
.
Fatal
(
err1
)
}
if
err2
==
nil
{
t
.
Fatal
(
err2
)
}
done
<-
struct
{}{}
}()
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
c1
,
c2
:=
setupSingleConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/6634"
,
"/ip4/127.0.0.1/tcp/6645"
)
done
:=
make
(
chan
error
)
go
secureHandshake
(
t
,
ctx
,
c1
,
done
)
<-
time
.
After
(
50
*
time
.
Millisecond
)
cancel
()
// cancel ctx
go
secureHandshake
(
t
,
ctx
,
c2
,
done
)
<-
done
for
i
:=
0
;
i
<
2
;
i
++
{
if
err
:=
<-
done
;
err
==
nil
{
t
.
Error
(
"cancel should've errored out"
)
}
}
}
func
TestSecureCloseLeak
(
t
*
testing
.
T
)
{
...
...
@@ -92,16 +95,7 @@ func TestSecureCloseLeak(t *testing.T) {
a1
:=
strconv
.
Itoa
(
p1
)
a2
:=
strconv
.
Itoa
(
p2
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
c1
,
c2
:=
setupConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/"
+
a1
,
"/ip4/127.0.0.1/tcp/"
+
a2
)
c1
,
err1
:=
setupSecureConn
(
t
,
ctx
,
c1
)
c2
,
err2
:=
setupSecureConn
(
t
,
ctx
,
c2
)
if
err1
!=
nil
{
t
.
Fatal
(
err1
)
}
if
err2
!=
nil
{
t
.
Fatal
(
err2
)
}
c1
,
c2
:=
setupSecureConn
(
t
,
ctx
,
"/ip4/127.0.0.1/tcp/"
+
a1
,
"/ip4/127.0.0.1/tcp/"
+
a2
)
for
i
:=
0
;
i
<
num
;
i
++
{
b1
:=
[]
byte
(
"beep"
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论