fix validators and key prefix

This commit is contained in:
Jeromy 2014-11-10 14:22:56 -08:00 committed by Juan Batiz-Benet
parent 33985c530e
commit cb23d5b2ae
4 changed files with 26 additions and 16 deletions

View File

@ -33,6 +33,9 @@ func setupDHT(ctx context.Context, t *testing.T, p peer.Peer) *IpfsDHT {
d := NewDHT(ctx, p, peerstore, net, dhts, ds.NewMapDatastore())
dhts.SetHandler(d)
d.Validators["v"] = func(u.Key, []byte) error {
return nil
}
return d
}
@ -136,6 +139,12 @@ func TestValueGetSet(t *testing.T) {
dhtA := setupDHT(ctx, t, peerA)
dhtB := setupDHT(ctx, t, peerB)
vf := func(u.Key, []byte) error {
return nil
}
dhtA.Validators["v"] = vf
dhtB.Validators["v"] = vf
defer dhtA.Close()
defer dhtB.Close()
defer dhtA.dialer.(inet.Network).Close()
@ -147,10 +156,10 @@ func TestValueGetSet(t *testing.T) {
}
ctxT, _ := context.WithTimeout(ctx, time.Second)
dhtA.PutValue(ctxT, "hello", []byte("world"))
dhtA.PutValue(ctxT, "/v/hello", []byte("world"))
ctxT, _ = context.WithTimeout(ctx, time.Second*2)
val, err := dhtA.GetValue(ctxT, "hello")
val, err := dhtA.GetValue(ctxT, "/v/hello")
if err != nil {
t.Fatal(err)
}
@ -160,7 +169,7 @@ func TestValueGetSet(t *testing.T) {
}
ctxT, _ = context.WithTimeout(ctx, time.Second*2)
val, err = dhtB.GetValue(ctxT, "hello")
val, err = dhtB.GetValue(ctxT, "/v/hello")
if err != nil {
t.Fatal(err)
}
@ -326,12 +335,12 @@ func TestLayeredGet(t *testing.T) {
t.Fatal(err)
}
err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
err = dhts[3].putLocal(u.Key("/v/hello"), []byte("world"))
if err != nil {
t.Fatal(err)
}
err = dhts[3].Provide(ctx, u.Key("hello"))
err = dhts[3].Provide(ctx, u.Key("/v/hello"))
if err != nil {
t.Fatal(err)
}
@ -339,7 +348,7 @@ func TestLayeredGet(t *testing.T) {
time.Sleep(time.Millisecond * 60)
ctxT, _ := context.WithTimeout(ctx, time.Second)
val, err := dhts[0].GetValue(ctxT, u.Key("hello"))
val, err := dhts[0].GetValue(ctxT, u.Key("/v/hello"))
if err != nil {
t.Fatal(err)
}

View File

@ -4,6 +4,7 @@ import (
"testing"
crand "crypto/rand"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
@ -199,9 +200,6 @@ func TestGetFailures(t *testing.T) {
if pmes.GetRecord() != nil {
t.Fatal("shouldnt have value")
}
if len(pmes.GetCloserPeers()) > 0 {
t.Fatal("shouldnt have closer peers")
}
if pmes.GetProviderPeers() != nil {
t.Fatal("shouldnt have provider peers")
}

View File

@ -114,6 +114,7 @@ func (dht *IpfsDHT) handlePutValue(p peer.Peer, pmes *pb.Message) (*pb.Message,
err := dht.verifyRecord(pmes.GetRecord())
if err != nil {
fmt.Println(u.Key(pmes.GetRecord().GetAuthor()))
log.Error("Bad dht record in put request")
return nil, err
}

View File

@ -20,7 +20,7 @@ var ErrInvalidRecordType = errors.New("invalid record keytype")
func (dht *IpfsDHT) makePutRecord(key u.Key, value []byte) (*pb.Record, error) {
record := new(pb.Record)
record.Key = proto.String(key.String())
record.Key = proto.String(string(key))
record.Value = value
record.Author = proto.String(string(dht.self.ID()))
blob := bytes.Join([][]byte{[]byte(key), value, []byte(dht.self.ID())}, []byte{})
@ -38,13 +38,15 @@ func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
if err != nil {
return err
}
k := u.Key(r.GetKey())
blob := bytes.Join([][]byte{[]byte(r.GetKey()),
blob := bytes.Join([][]byte{[]byte(k),
r.GetValue(),
[]byte(r.GetKey())}, []byte{})
[]byte(r.GetAuthor())}, []byte{})
ok, err := p.PubKey().Verify(blob, r.GetSignature())
if err != nil {
log.Error("Signature verify failed.")
return err
}
@ -54,14 +56,14 @@ func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
// Now, check validity func
parts := strings.Split(r.GetKey(), "/")
if len(parts) < 2 {
log.Error("Record had bad key: %s", r.GetKey())
if len(parts) < 3 {
log.Errorf("Record had bad key: %s", u.Key(r.GetKey()))
return ErrBadRecord
}
fnc, ok := dht.Validators[parts[0]]
fnc, ok := dht.Validators[parts[1]]
if !ok {
log.Errorf("Unrecognized key prefix: %s", parts[0])
log.Errorf("Unrecognized key prefix: %s", parts[1])
return ErrInvalidRecordType
}