Skip to content

Commit

Permalink
test: Add more helper functions for identifiers
Browse files Browse the repository at this point in the history
It is often needed to get another ID, as well as a list of random ones.
With new functions, `cidtest.IDWithChecksum` becomes redundant, so it
marked as deprecated.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Jul 2, 2024
1 parent 66ae541 commit 1674080
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 37 deletions.
22 changes: 10 additions & 12 deletions container/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const emptyID = "11111111111111111111111111111111"

func TestID_ToV2(t *testing.T) {
t.Run("non-zero", func(t *testing.T) {
checksum := randSHA256Checksum()

id := cidtest.IDWithChecksum(checksum)
id := cidtest.ID()

var idV2 refs.ContainerID
id.WriteToV2(&idV2)
Expand All @@ -33,7 +31,7 @@ func TestID_ToV2(t *testing.T) {
require.NoError(t, newID.ReadFromV2(idV2))

require.Equal(t, id, newID)
require.Equal(t, checksum[:], idV2.GetValue())
require.Equal(t, id[:], idV2.GetValue())
})

t.Run("zero", func(t *testing.T) {
Expand All @@ -48,16 +46,16 @@ func TestID_ToV2(t *testing.T) {
}

func TestID_Equal(t *testing.T) {
cs := randSHA256Checksum()

id1 := cidtest.IDWithChecksum(cs)
id2 := cidtest.IDWithChecksum(cs)

id1 := cidtest.ID()
require.True(t, id1.Equals(id1))
id2 := id1
require.True(t, id1.Equals(id2))

id3 := cidtest.ID()

require.True(t, id2.Equals(id1))
id3 := cidtest.OtherID(id1)
require.False(t, id1.Equals(id3))
require.False(t, id3.Equals(id1))
require.False(t, id2.Equals(id3))
require.False(t, id3.Equals(id2))
}

func TestID_String(t *testing.T) {
Expand Down
29 changes: 26 additions & 3 deletions container/id/test/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,38 @@ import (

// ID returns random cid.ID.
func ID() cid.ID {
checksum := [sha256.Size]byte{}
var res cid.ID
//nolint:staticcheck
rand.Read(checksum[:])
rand.Read(res[:])
return res
}

// IDs returns n random cid.ID instances.
func IDs(n int) []cid.ID {
res := make([]cid.ID, n)
for i := range res {
res[i] = ID()
}
return res
}

return IDWithChecksum(checksum)
// OtherID returns random cid.ID other than any given one.
func OtherID(vs ...cid.ID) cid.ID {
loop:
for {
v := ID()
for i := range vs {
if v == vs[i] {
continue loop
}
}
return v
}
}

// IDWithChecksum returns cid.ID initialized
// with specified checksum.
// Deprecated: use [ID], [OtherID] or manual creation instead.
func IDWithChecksum(cs [sha256.Size]byte) cid.ID {
var id cid.ID
id.SetSHA256(cs)
Expand Down
31 changes: 31 additions & 0 deletions container/id/test/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cidtest_test

import (
"math/rand"
"testing"

"github.com/nspcc-dev/neofs-api-go/v2/refs"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/stretchr/testify/require"
)

func TestID(t *testing.T) {
id := cidtest.ID()
require.NotEqual(t, id, cidtest.ID())

var m refs.ContainerID
id.WriteToV2(&m)
var id2 cid.ID
require.NoError(t, id2.ReadFromV2(m))
}

func TestNIDs(t *testing.T) {
n := rand.Int() % 10
require.Len(t, cidtest.IDs(n), n)
}

func TestOtherID(t *testing.T) {
ids := cidtest.IDs(100)
require.NotContains(t, ids, cidtest.OtherID(ids...))
}
7 changes: 2 additions & 5 deletions eacl/table_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eacl

import (
"bytes"
"crypto/sha256"
"testing"

cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
Expand All @@ -11,8 +10,7 @@ import (
)

func TestTable_CopyTo(t *testing.T) {
sha := sha256.Sum256([]byte("container id"))
id := cidtest.IDWithChecksum(sha)
id := cidtest.ID()

var table Table
table.SetVersion(version.Current())
Expand Down Expand Up @@ -71,8 +69,7 @@ func TestTable_CopyTo(t *testing.T) {

require.True(t, cid1.Equals(cid2))

sha = sha256.Sum256([]byte("container id 2"))
dst.SetCID(cidtest.IDWithChecksum(sha))
dst.SetCID(cidtest.OtherID(id))

cid1, isSet1 = table.CID()
require.True(t, isSet1)
Expand Down
8 changes: 2 additions & 6 deletions eacl/table_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eacl_test

import (
"crypto/sha256"
"testing"

"github.com/nspcc-dev/neofs-api-go/v2/refs"
Expand All @@ -13,11 +12,8 @@ import (
)

func TestTable(t *testing.T) {
id := cidtest.ID()
var v version.Version

sha := sha256.Sum256([]byte("container id"))
id := cidtest.IDWithChecksum(sha)

v.SetMajor(3)
v.SetMinor(2)

Expand All @@ -30,7 +26,7 @@ func TestTable(t *testing.T) {
require.NotNil(t, v2)
require.Equal(t, uint32(3), v2.GetVersion().GetMajor())
require.Equal(t, uint32(2), v2.GetVersion().GetMinor())
require.Equal(t, sha[:], v2.GetContainerID().GetValue())
require.Equal(t, id[:], v2.GetContainerID().GetValue())
require.Len(t, v2.GetRecords(), 1)

newTable := eacl.NewTableFromV2(v2)
Expand Down
57 changes: 46 additions & 11 deletions object/id/test/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oidtest

import (
"crypto/sha256"
"math/rand"

cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
Expand All @@ -10,20 +9,33 @@ import (

// ID returns random oid.ID.
func ID() oid.ID {
checksum := [sha256.Size]byte{}
var res oid.ID
//nolint:staticcheck
rand.Read(checksum[:])

return idWithChecksum(checksum)
rand.Read(res[:])
return res
}

// idWithChecksum returns oid.ID initialized
// with specified checksum.
func idWithChecksum(cs [sha256.Size]byte) oid.ID {
var id oid.ID
id.SetSHA256(cs)
// OtherID returns random oid.ID other than any given one.
func OtherID(vs ...oid.ID) oid.ID {
loop:
for {
v := ID()
for i := range vs {
if v == vs[i] {
continue loop
}
}
return v
}
}

return id
// IDs returns n random oid.ID instances.
func IDs(n int) []oid.ID {
res := make([]oid.ID, n)
for i := range res {
res[i] = ID()
}
return res
}

// Address returns random oid.Address.
Expand All @@ -35,3 +47,26 @@ func Address() oid.Address {

return x
}

// OtherAddress returns random oid.Address other than any given one.
func OtherAddress(vs ...oid.Address) oid.Address {
loop:
for {
v := Address()
for i := range vs {
if v == vs[i] {
continue loop
}
}
return v
}
}

// Addresses returns n random oid.Address instances.
func Addresses(n int) []oid.Address {
res := make([]oid.Address, n)
for i := range res {
res[i] = Address()
}
return res
}
51 changes: 51 additions & 0 deletions object/id/test/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package oidtest_test

import (
"math/rand"
"testing"

"github.com/nspcc-dev/neofs-api-go/v2/refs"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
)

func TestID(t *testing.T) {
id := oidtest.ID()
require.NotEqual(t, id, oidtest.ID())

var m refs.ObjectID
id.WriteToV2(&m)
var id2 oid.ID
require.NoError(t, id2.ReadFromV2(m))
}

func TestNIDs(t *testing.T) {
n := rand.Int() % 10
require.Len(t, oidtest.IDs(n), n)
}

func TestOtherID(t *testing.T) {
ids := oidtest.IDs(100)
require.NotContains(t, ids, oidtest.OtherID(ids...))
}

func TestAddress(t *testing.T) {
a := oidtest.Address()
require.NotEqual(t, a, oidtest.Address())

var m refs.Address
a.WriteToV2(&m)
var id2 oid.Address
require.NoError(t, id2.ReadFromV2(m))
}

func TestChangeAddress(t *testing.T) {
addrs := oidtest.Addresses(100)
require.NotContains(t, addrs, oidtest.OtherAddress(addrs...))
}

func TestNAddresses(t *testing.T) {
n := rand.Int() % 10
require.Len(t, oidtest.Addresses(n), n)
}
23 changes: 23 additions & 0 deletions user/test/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@ func ID() user.ID {
res.SetScriptHash(h)
return res
}

// OtherID returns random user.ID other than any given one.
func OtherID(vs ...user.ID) user.ID {
loop:
for {
v := ID()
for i := range vs {
if v.Equals(vs[i]) {
continue loop
}
}
return v
}
}

// IDs returns n random user.ID instances.
func IDs(n int) []user.ID {
res := make([]user.ID, n)
for i := range res {
res[i] = ID()
}
return res
}
32 changes: 32 additions & 0 deletions user/test/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package usertest_test

import (
"math/rand"
"testing"

"github.com/nspcc-dev/neofs-api-go/v2/refs"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/nspcc-dev/neofs-sdk-go/user"
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
"github.com/stretchr/testify/require"
)

func TestID(t *testing.T) {
id := usertest.ID()
require.NotEqual(t, id, usertest.ID())

var m refs.OwnerID
id.WriteToV2(&m)
var id2 user.ID
require.NoError(t, id2.ReadFromV2(m))
}

func TestNIDs(t *testing.T) {
n := rand.Int() % 10
require.Len(t, cidtest.IDs(n), n)
}

func TestOtherID(t *testing.T) {
ids := usertest.IDs(100)
require.NotContains(t, ids, usertest.OtherID(ids...))
}

0 comments on commit 1674080

Please sign in to comment.