2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test vectors are taken from the main NEO repo
|
|
|
|
// https://github.com/neo-project/neo/blob/master/tests/neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs#L10
|
|
|
|
func TestManifest_MarshalJSON(t *testing.T) {
|
|
|
|
t.Run("default", func(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
s := `{"groups":[],"supportedstandards":[],"name":"Test","abi":{"methods":[],"events":[]},"permissions":[{"contract":"*","methods":"*"}],"trusts":[],"extra":null}`
|
2020-03-18 15:21:12 +00:00
|
|
|
m := testUnmarshalMarshalManifest(t, s)
|
2020-11-18 09:43:51 +00:00
|
|
|
require.Equal(t, DefaultManifest("Test"), m)
|
2020-03-18 15:21:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("permissions", func(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
s := `{"groups":[],"supportedstandards":[],"name":"Test","abi":{"methods":[],"events":[]},"permissions":[{"contract":"0x0000000000000000000000000000000000000000","methods":["method1","method2"]}],"trusts":[],"extra":null}`
|
2020-03-18 15:21:12 +00:00
|
|
|
testUnmarshalMarshalManifest(t, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("safe methods", func(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
s := `{"groups":[],"supportedstandards":[],"name":"Test","abi":{"methods":[{"name":"safeMet","offset":123,"parameters":[],"returntype":"Integer","safe":true}],"events":[]},"permissions":[{"contract":"*","methods":"*"}],"trusts":[],"extra":null}`
|
2020-03-18 15:21:12 +00:00
|
|
|
testUnmarshalMarshalManifest(t, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("trust", func(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
s := `{"groups":[],"supportedstandards":[],"name":"Test","abi":{"methods":[],"events":[]},"permissions":[{"contract":"*","methods":"*"}],"trusts":["0x0000000000000000000000000000000000000001"],"extra":null}`
|
2020-03-18 15:21:12 +00:00
|
|
|
testUnmarshalMarshalManifest(t, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("groups", func(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
s := `{"groups":[{"pubkey":"03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c","signature":"QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ=="}],"supportedstandards":[],"name":"Test","abi":{"methods":[],"events":[]},"permissions":[{"contract":"*","methods":"*"}],"trusts":[],"extra":null}`
|
2020-03-18 15:21:12 +00:00
|
|
|
testUnmarshalMarshalManifest(t, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("extra", func(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
s := `{"groups":[],"supportedstandards":[],"name":"Test","abi":{"methods":[],"events":[]},"permissions":[{"contract":"*","methods":"*"}],"trusts":[],"extra":{"key":"value"}}`
|
2020-03-18 15:21:12 +00:00
|
|
|
testUnmarshalMarshalManifest(t, s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUnmarshalMarshalManifest(t *testing.T, s string) *Manifest {
|
|
|
|
js := []byte(s)
|
2020-11-18 09:43:51 +00:00
|
|
|
c := NewManifest("Test")
|
2020-03-18 15:21:12 +00:00
|
|
|
require.NoError(t, json.Unmarshal(js, c))
|
|
|
|
|
|
|
|
data, err := json.Marshal(c)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, s, string(data))
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifest_CanCall(t *testing.T) {
|
2020-12-08 10:27:41 +00:00
|
|
|
man1 := DefaultManifest("Test1")
|
|
|
|
man2 := DefaultManifest("Test2")
|
|
|
|
require.True(t, man1.CanCall(util.Uint160{}, man2, "method1"))
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermission_IsAllowed(t *testing.T) {
|
2020-11-18 09:43:51 +00:00
|
|
|
manifest := DefaultManifest("Test")
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
t.Run("wildcard", func(t *testing.T) {
|
|
|
|
perm := NewPermission(PermissionWildcard)
|
2020-11-18 09:43:51 +00:00
|
|
|
require.True(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("hash", func(t *testing.T) {
|
|
|
|
perm := NewPermission(PermissionHash, util.Uint160{})
|
2020-11-18 09:43:51 +00:00
|
|
|
require.True(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
t.Run("restrict methods", func(t *testing.T) {
|
|
|
|
perm.Methods.Restrict()
|
2020-11-18 09:43:51 +00:00
|
|
|
require.False(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
perm.Methods.Add("AAA")
|
2020-11-18 09:43:51 +00:00
|
|
|
require.True(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid hash", func(t *testing.T) {
|
|
|
|
perm := NewPermission(PermissionHash, util.Uint160{1})
|
2020-11-18 09:43:51 +00:00
|
|
|
require.False(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
manifest.Groups = []Group{{PublicKey: priv.PublicKey()}}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
perm := NewPermission(PermissionGroup, priv.PublicKey())
|
2020-11-18 09:43:51 +00:00
|
|
|
require.True(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid group", func(t *testing.T) {
|
|
|
|
priv2, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
perm := NewPermission(PermissionGroup, priv2.PublicKey())
|
2020-11-18 09:43:51 +00:00
|
|
|
require.False(t, perm.IsAllowed(util.Uint160{}, manifest, "AAA"))
|
2020-03-18 15:21:12 +00:00
|
|
|
})
|
|
|
|
}
|
2020-07-15 11:39:20 +00:00
|
|
|
|
|
|
|
func TestIsValid(t *testing.T) {
|
|
|
|
contractHash := util.Uint160{1, 2, 3}
|
2020-11-18 09:43:51 +00:00
|
|
|
m := NewManifest("Test")
|
2020-07-15 11:39:20 +00:00
|
|
|
|
|
|
|
t.Run("valid, no groups", func(t *testing.T) {
|
|
|
|
require.True(t, m.IsValid(contractHash))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with groups", func(t *testing.T) {
|
|
|
|
m.Groups = make([]Group, 3)
|
|
|
|
pks := make([]*keys.PrivateKey, 3)
|
|
|
|
for i := range pks {
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pks[i] = pk
|
|
|
|
m.Groups[i] = Group{
|
|
|
|
PublicKey: pk.PublicKey(),
|
|
|
|
Signature: pk.Sign(contractHash.BytesBE()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
require.True(t, m.IsValid(contractHash))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid, wrong contract hash", func(t *testing.T) {
|
|
|
|
require.False(t, m.IsValid(util.Uint160{4, 5, 6}))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid, wrong group signature", func(t *testing.T) {
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
m.Groups = append(m.Groups, Group{
|
|
|
|
PublicKey: pk.PublicKey(),
|
|
|
|
// actually, there shouldn't be such situation, as Signature is always the signature
|
|
|
|
// of the contract hash.
|
|
|
|
Signature: pk.Sign([]byte{1, 2, 3}),
|
|
|
|
})
|
|
|
|
require.False(t, m.IsValid(contractHash))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|