github.com/eris-ltd/erisdb@v0.25.0/acm/account_test.go (about)

     1  // Copyright 2017 Monax Industries Limited
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package acm
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/hyperledger/burrow/event/query"
    23  	"github.com/hyperledger/burrow/execution/solidity"
    24  
    25  	"github.com/hyperledger/burrow/crypto"
    26  	"github.com/hyperledger/burrow/permission"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestAddress(t *testing.T) {
    32  	bs := []byte{
    33  		1, 2, 3, 4, 5,
    34  		1, 2, 3, 4, 5,
    35  		1, 2, 3, 4, 5,
    36  		1, 2, 3, 4, 5,
    37  	}
    38  	addr, err := crypto.AddressFromBytes(bs)
    39  	assert.NoError(t, err)
    40  	word256 := addr.Word256()
    41  	leadingZeroes := []byte{
    42  		0, 0, 0, 0,
    43  		0, 0, 0, 0,
    44  		0, 0, 0, 0,
    45  	}
    46  	assert.Equal(t, leadingZeroes, word256[:12])
    47  	addrFromWord256 := crypto.AddressFromWord256(word256)
    48  	assert.Equal(t, bs, addrFromWord256[:])
    49  	assert.Equal(t, addr, addrFromWord256)
    50  }
    51  
    52  func TestDecodeConcrete(t *testing.T) {
    53  	concreteAcc := NewAccountFromSecret("Super Semi Secret")
    54  	concreteAcc.Permissions = permission.AccountPermissions{
    55  		Base: permission.BasePermissions{
    56  			Perms:  permission.SetGlobal,
    57  			SetBit: permission.SetGlobal,
    58  		},
    59  		Roles: []string{"bums"},
    60  	}
    61  	acc := concreteAcc
    62  	encodedAcc, err := acc.Encode()
    63  	require.NoError(t, err)
    64  
    65  	concreteAccOut, err := Decode(encodedAcc)
    66  	require.NoError(t, err)
    67  
    68  	assert.Equal(t, concreteAcc, concreteAccOut)
    69  	concreteAccOut, err = Decode([]byte("flungepliffery munknut tolopops"))
    70  	assert.Error(t, err)
    71  }
    72  
    73  func TestDecode(t *testing.T) {
    74  	acc := NewAccountFromSecret("Super Semi Secret")
    75  	encodedAcc, err := acc.Encode()
    76  	require.NoError(t, err)
    77  	accOut, err := Decode(encodedAcc)
    78  	require.NoError(t, err)
    79  	assert.Equal(t, NewAccountFromSecret("Super Semi Secret"), accOut)
    80  
    81  	accOut, err = Decode([]byte("flungepliffery munknut tolopops"))
    82  	require.Error(t, err)
    83  	assert.Nil(t, accOut)
    84  }
    85  
    86  func TestMarshalJSON(t *testing.T) {
    87  	acc := NewAccountFromSecret("Super Semi Secret")
    88  	acc.Code = []byte{60, 23, 45}
    89  	acc.Permissions = permission.AccountPermissions{
    90  		Base: permission.BasePermissions{
    91  			Perms: permission.AllPermFlags,
    92  		},
    93  	}
    94  	acc.Sequence = 4
    95  	acc.Balance = 10
    96  	bs, err := json.Marshal(acc)
    97  
    98  	expected := fmt.Sprintf(`{"Address":"%s","PublicKey":{"CurveType":"ed25519","PublicKey":"%s"},`+
    99  		`"Sequence":4,"Balance":10,"Code":"3C172D",`+
   100  		`"Permissions":{"Base":{"Perms":"root | send | call | createContract | createAccount | bond | name | proposal | input | batch | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole","SetBit":""}}}`,
   101  		acc.Address, acc.PublicKey)
   102  	assert.Equal(t, expected, string(bs))
   103  	assert.NoError(t, err)
   104  }
   105  
   106  func TestAccountTags(t *testing.T) {
   107  	perms := permission.DefaultAccountPermissions
   108  	perms.Roles = []string{"frogs", "dogs"}
   109  	acc := &Account{
   110  		Permissions: perms,
   111  		Code:        solidity.Bytecode_StrangeLoop,
   112  	}
   113  	tagged := acc.Tagged()
   114  	assert.Equal(t, []string{"Address", "Balance", "Sequence", "Code", "Permissions", "Roles"}, tagged.Keys())
   115  	str, _ := tagged.Get("Permissions")
   116  	assert.Equal(t, "send | call | createContract | createAccount | bond | name | proposal | input | batch | hasBase | hasRole", str)
   117  	str, _ = tagged.Get("Roles")
   118  	assert.Equal(t, "frogs;dogs", str)
   119  	str, _ = tagged.Get("Code")
   120  	qry, err := query.New("Code CONTAINS '0116002556001600360006101000A815'")
   121  	require.NoError(t, err)
   122  	assert.True(t, qry.Matches(tagged))
   123  }