mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-25 15:08:45 +08:00
test to ensure embedding the key in the record works
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -3,6 +3,7 @@ package namesys
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -28,20 +29,21 @@ func testValidatorCase(t *testing.T, priv ci.PrivKey, kbook pstore.KeyBook, key
|
||||
|
||||
validator := IpnsValidator{kbook}
|
||||
|
||||
p := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
|
||||
entry, err := CreateRoutingEntryData(priv, p, 1, eol)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data := val
|
||||
if data == nil {
|
||||
p := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
|
||||
entry, err := CreateRoutingEntryData(priv, p, 1, eol)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, err = proto.Marshal(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
err = validator.Validate(key, data)
|
||||
|
||||
err := validator.Validate(key, data)
|
||||
if err != exp {
|
||||
params := fmt.Sprintf("key: %s\neol: %s\n", key, eol)
|
||||
if exp == nil {
|
||||
@ -74,6 +76,72 @@ func TestValidator(t *testing.T) {
|
||||
testValidatorCase(t, priv, kbook, "/wrong/"+string(id), nil, ts.Add(time.Hour), ErrInvalidPath)
|
||||
}
|
||||
|
||||
func TestEmbeddedPubKeyValidate(t *testing.T) {
|
||||
goodeol := time.Now().Add(time.Hour)
|
||||
kbook := pstore.NewPeerstore()
|
||||
|
||||
pth := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
|
||||
|
||||
priv, _, _, ipnsk := genKeys(t)
|
||||
|
||||
entry, err := CreateRoutingEntryData(priv, pth, 1, goodeol)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dataNoKey, err := proto.Marshal(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testValidatorCase(t, priv, kbook, ipnsk, dataNoKey, goodeol, ErrPublicKeyNotFound)
|
||||
|
||||
pubkb, err := priv.GetPublic().Bytes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entry.PubKey = pubkb
|
||||
|
||||
dataWithKey, err := proto.Marshal(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testValidatorCase(t, priv, kbook, ipnsk, dataWithKey, goodeol, nil)
|
||||
}
|
||||
|
||||
func TestPeerIDPubKeyValidate(t *testing.T) {
|
||||
goodeol := time.Now().Add(time.Hour)
|
||||
kbook := pstore.NewPeerstore()
|
||||
|
||||
pth := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
|
||||
|
||||
sk, pk, err := ci.GenerateEd25519Key(rand.New(rand.NewSource(42)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pid, err := peer.IDFromPublicKey(pk)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ipnsk := "/ipns/" + string(pid)
|
||||
|
||||
entry, err := CreateRoutingEntryData(sk, pth, 1, goodeol)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dataNoKey, err := proto.Marshal(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testValidatorCase(t, sk, kbook, ipnsk, dataNoKey, goodeol, nil)
|
||||
}
|
||||
|
||||
func TestResolverValidation(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
rid := testutil.RandIdentityOrFatal(t)
|
||||
|
@ -70,7 +70,7 @@ func (v IpnsValidator) Validate(key string, value []byte) error {
|
||||
|
||||
pubk, err := v.getPublicKey(pid, entry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting public key failed: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Check the ipns record signature with the public key
|
||||
@ -102,7 +102,7 @@ func (v IpnsValidator) getPublicKey(pid peer.ID, entry *pb.IpnsEntry) (ic.PubKey
|
||||
if err != nil {
|
||||
// TODO: i think this counts as a 'malformed record' and should be discarded
|
||||
log.Debugf("public key in ipns record failed to parse: ", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshaling pubkey in record: %s", err)
|
||||
}
|
||||
expPid, err := peer.IDFromPublicKey(pk)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user