2018-06-27 19:42:54 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-10-12 22:10:50 +01:00
|
|
|
"context"
|
2018-06-27 19:42:54 +01:00
|
|
|
"testing"
|
|
|
|
|
2018-07-06 09:51:13 +01:00
|
|
|
"github.com/mr-tron/base58/base58"
|
2018-06-27 19:42:54 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-16 12:43:44 +01:00
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-10-12 22:10:50 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-06-27 19:42:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPieceID(t *testing.T) {
|
|
|
|
t.Run("should return an id string", func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
id := NewPieceID()
|
|
|
|
assert.Equal(id.IsValid(), true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should return a different string on each call", func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.NotEqual(NewPieceID(), NewPieceID())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-06 09:51:13 +01:00
|
|
|
func TestDerivePieceID(t *testing.T) {
|
|
|
|
pid := NewPieceID()
|
2018-10-12 22:10:50 +01:00
|
|
|
fid, err := newTestIdentity()
|
2018-07-06 09:51:13 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-12 22:10:50 +01:00
|
|
|
nid := dht.NodeID(fid.ID)
|
2018-07-06 09:51:13 +01:00
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
did, err := pid.Derive(nid.Bytes())
|
|
|
|
assert.NoError(t, err)
|
2018-07-06 09:51:13 +01:00
|
|
|
assert.NotEqual(t, pid, did)
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
did2, err := pid.Derive(nid.Bytes())
|
|
|
|
assert.NoError(t, err)
|
2018-07-06 09:51:13 +01:00
|
|
|
assert.Equal(t, did, did2)
|
|
|
|
|
|
|
|
_, err = base58.Decode(did.String())
|
|
|
|
assert.NoError(t, err)
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
2018-10-12 22:10:50 +01:00
|
|
|
|
|
|
|
// helper function to generate new node identities with
|
|
|
|
// correct difficulty and concurrency
|
|
|
|
func newTestIdentity() (*provider.FullIdentity, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fid, err := node.NewFullIdentity(ctx, 12, 4)
|
|
|
|
return fid, err
|
|
|
|
}
|