Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
c66c5c64
提交
c66c5c64
authored
6月 05, 2018
作者:
Jeromy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add tests for pubkey mismatch and bad pubkey
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
上级
af68a380
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
48 行增加
和
20 行删除
+48
-20
ipns_validate_test.go
namesys/ipns_validate_test.go
+44
-19
validator.go
namesys/validator.go
+4
-1
没有找到文件。
namesys/ipns_validate_test.go
浏览文件 @
c66c5c64
...
...
@@ -4,10 +4,12 @@ import (
"context"
"fmt"
"math/rand"
"strings"
"testing"
"time"
opts
"github.com/ipfs/go-ipfs/namesys/opts"
pb
"github.com/ipfs/go-ipfs/namesys/pb"
path
"github.com/ipfs/go-ipfs/path"
u
"gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
...
...
@@ -27,6 +29,25 @@ import (
func
testValidatorCase
(
t
*
testing
.
T
,
priv
ci
.
PrivKey
,
kbook
pstore
.
KeyBook
,
key
string
,
val
[]
byte
,
eol
time
.
Time
,
exp
error
)
{
t
.
Helper
()
match
:=
func
(
t
*
testing
.
T
,
err
error
)
{
t
.
Helper
()
if
err
!=
exp
{
params
:=
fmt
.
Sprintf
(
"key: %s
\n
eol: %s
\n
"
,
key
,
eol
)
if
exp
==
nil
{
t
.
Fatalf
(
"Unexpected error %s for params %s"
,
err
,
params
)
}
else
if
err
==
nil
{
t
.
Fatalf
(
"Expected error %s but there was no error for params %s"
,
exp
,
params
)
}
else
{
t
.
Fatalf
(
"Expected error %s but got %s for params %s"
,
exp
,
err
,
params
)
}
}
}
testValidatorCaseMatchFunc
(
t
,
priv
,
kbook
,
key
,
val
,
eol
,
match
)
}
func
testValidatorCaseMatchFunc
(
t
*
testing
.
T
,
priv
ci
.
PrivKey
,
kbook
pstore
.
KeyBook
,
key
string
,
val
[]
byte
,
eol
time
.
Time
,
matchf
func
(
*
testing
.
T
,
error
))
{
t
.
Helper
()
validator
:=
IpnsValidator
{
kbook
}
data
:=
val
...
...
@@ -43,17 +64,7 @@ func testValidatorCase(t *testing.T, priv ci.PrivKey, kbook pstore.KeyBook, key
}
}
err
:=
validator
.
Validate
(
key
,
data
)
if
err
!=
exp
{
params
:=
fmt
.
Sprintf
(
"key: %s
\n
eol: %s
\n
"
,
key
,
eol
)
if
exp
==
nil
{
t
.
Fatalf
(
"Unexpected error %s for params %s"
,
err
,
params
)
}
else
if
err
==
nil
{
t
.
Fatalf
(
"Expected error %s but there was no error for params %s"
,
exp
,
params
)
}
else
{
t
.
Fatalf
(
"Expected error %s but got %s for params %s"
,
exp
,
err
,
params
)
}
}
matchf
(
t
,
validator
.
Validate
(
key
,
data
))
}
func
TestValidator
(
t
*
testing
.
T
)
{
...
...
@@ -76,6 +87,15 @@ func TestValidator(t *testing.T) {
testValidatorCase
(
t
,
priv
,
kbook
,
"/wrong/"
+
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
),
ErrInvalidPath
)
}
func
mustMarshal
(
t
*
testing
.
T
,
entry
*
pb
.
IpnsEntry
)
[]
byte
{
t
.
Helper
()
data
,
err
:=
proto
.
Marshal
(
entry
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
data
}
func
TestEmbeddedPubKeyValidate
(
t
*
testing
.
T
)
{
goodeol
:=
time
.
Now
()
.
Add
(
time
.
Hour
)
kbook
:=
pstore
.
NewPeerstore
()
...
...
@@ -89,12 +109,7 @@ func TestEmbeddedPubKeyValidate(t *testing.T) {
t
.
Fatal
(
err
)
}
dataNoKey
,
err
:=
proto
.
Marshal
(
entry
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
testValidatorCase
(
t
,
priv
,
kbook
,
ipnsk
,
dataNoKey
,
goodeol
,
ErrPublicKeyNotFound
)
testValidatorCase
(
t
,
priv
,
kbook
,
ipnsk
,
mustMarshal
(
t
,
entry
),
goodeol
,
ErrPublicKeyNotFound
)
pubkb
,
err
:=
priv
.
GetPublic
()
.
Bytes
()
if
err
!=
nil
{
...
...
@@ -102,13 +117,23 @@ func TestEmbeddedPubKeyValidate(t *testing.T) {
}
entry
.
PubKey
=
pubkb
testValidatorCase
(
t
,
priv
,
kbook
,
ipnsk
,
mustMarshal
(
t
,
entry
),
goodeol
,
nil
)
entry
.
PubKey
=
[]
byte
(
"probably not a public key"
)
testValidatorCaseMatchFunc
(
t
,
priv
,
kbook
,
ipnsk
,
mustMarshal
(
t
,
entry
),
goodeol
,
func
(
t
*
testing
.
T
,
err
error
)
{
if
!
strings
.
Contains
(
err
.
Error
(),
"unmarshaling pubkey in record:"
)
{
t
.
Fatal
(
"expected pubkey unmarshaling to fail"
)
}
})
dataWithKey
,
err
:=
proto
.
Marshal
(
entry
)
opriv
,
_
,
_
,
_
:=
genKeys
(
t
)
wrongkeydata
,
err
:=
opriv
.
GetPublic
()
.
Bytes
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
testValidatorCase
(
t
,
priv
,
kbook
,
ipnsk
,
dataWithKey
,
goodeol
,
nil
)
entry
.
PubKey
=
wrongkeydata
testValidatorCase
(
t
,
priv
,
kbook
,
ipnsk
,
mustMarshal
(
t
,
entry
),
goodeol
,
ErrPublicKeyMismatch
)
}
func
TestPeerIDPubKeyValidate
(
t
*
testing
.
T
)
{
...
...
namesys/validator.go
浏览文件 @
c66c5c64
...
...
@@ -44,6 +44,8 @@ var ErrKeyFormat = errors.New("record key could not be parsed into peer ID")
// from the peer store
var
ErrPublicKeyNotFound
=
errors
.
New
(
"public key not found in peer store"
)
var
ErrPublicKeyMismatch
=
errors
.
New
(
"public key in record did not match expected pubkey"
)
type
IpnsValidator
struct
{
KeyBook
pstore
.
KeyBook
}
...
...
@@ -104,13 +106,14 @@ func (v IpnsValidator) getPublicKey(pid peer.ID, entry *pb.IpnsEntry) (ic.PubKey
log
.
Debugf
(
"public key in ipns record failed to parse: "
,
err
)
return
nil
,
fmt
.
Errorf
(
"unmarshaling pubkey in record: %s"
,
err
)
}
expPid
,
err
:=
peer
.
IDFromPublicKey
(
pk
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"could not regenerate peerID from pubkey: %s"
,
err
)
}
if
pid
!=
expPid
{
return
nil
,
fmt
.
Errorf
(
"pubkey in record did not match expected pubkey"
)
return
nil
,
ErrPublicKeyMismatch
}
return
pk
,
nil
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论