github.com/braveheart12/insolar-09-08-19@v0.8.7/functest/register_node_test.go (about)

     1  // +build functest
     2  
     3  /*
     4   *    Copyright 2019 Insolar Technologies
     5   *
     6   *    Licensed under the Apache License, Version 2.0 (the "License");
     7   *    you may not use this file except in compliance with the License.
     8   *    You may obtain a copy of the License at
     9   *
    10   *        http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *    Unless required by applicable law or agreed to in writing, software
    13   *    distributed under the License is distributed on an "AS IS" BASIS,
    14   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   *    See the License for the specific language governing permissions and
    16   *    limitations under the License.
    17   */
    18  
    19  package functest
    20  
    21  import (
    22  	"encoding/json"
    23  	"testing"
    24  
    25  	"github.com/insolar/insolar/certificate"
    26  	"github.com/insolar/insolar/core"
    27  	"github.com/insolar/insolar/platformpolicy"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  var scheme = platformpolicy.NewPlatformCryptographyScheme()
    32  var keyProcessor = platformpolicy.NewKeyProcessor()
    33  
    34  const TESTPUBLICKEY = "some_fancy_public_key"
    35  
    36  func registerNodeSignedCall(params ...interface{}) (string, error) {
    37  	res, err := signedRequest(&root, "RegisterNode", params...)
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	return res.(string), nil
    42  }
    43  
    44  func TestRegisterNodeVirtual(t *testing.T) {
    45  	const testRole = "virtual"
    46  	ref, err := registerNodeSignedCall(TESTPUBLICKEY, testRole)
    47  	require.NoError(t, err)
    48  
    49  	require.NotNil(t, ref)
    50  }
    51  
    52  func TestRegisterNodeHeavyMaterial(t *testing.T) {
    53  	const testRole = "heavy_material"
    54  	ref, err := registerNodeSignedCall(TESTPUBLICKEY, testRole)
    55  	require.NoError(t, err)
    56  
    57  	require.NotNil(t, ref)
    58  }
    59  
    60  func TestRegisterNodeLightMaterial(t *testing.T) {
    61  	const testRole = "light_material"
    62  	ref, err := registerNodeSignedCall(TESTPUBLICKEY, testRole)
    63  	require.NoError(t, err)
    64  
    65  	require.NotNil(t, ref)
    66  }
    67  
    68  func TestRegisterNodeNotExistRole(t *testing.T) {
    69  	_, err := registerNodeSignedCall(TESTPUBLICKEY, "some_not_fancy_role")
    70  	require.Contains(t, err.Error(),
    71  		"[ RegisterNode ] Can't save as child: [ SaveAsChild ] on calling main API: executer error: "+
    72  			"problem with API call: Can't call constructor NewNodeRecord: Role is not supported: some_not_fancy_role")
    73  }
    74  
    75  func TestRegisterNodeByNoRoot(t *testing.T) {
    76  	member := createMember(t, "Member1")
    77  	const testRole = "virtual"
    78  	_, err := signedRequest(member, "RegisterNode", TESTPUBLICKEY, testRole)
    79  	require.Contains(t, err.Error(), "[ RegisterNode ] Only Root member can register node")
    80  }
    81  
    82  func TestReceiveNodeCert(t *testing.T) {
    83  	const testRole = "virtual"
    84  	ref, err := registerNodeSignedCall(TESTPUBLICKEY, testRole)
    85  	require.NoError(t, err)
    86  
    87  	body := getRPSResponseBody(t, postParams{
    88  		"jsonrpc": "2.0",
    89  		"method":  "cert.Get",
    90  		"id":      "",
    91  		"params":  map[string]string{"ref": ref},
    92  	})
    93  
    94  	res := struct {
    95  		Result struct {
    96  			Cert certificate.Certificate
    97  		}
    98  	}{}
    99  
   100  	err = json.Unmarshal(body, &res)
   101  	require.NoError(t, err)
   102  
   103  	networkPart := res.Result.Cert.SerializeNetworkPart()
   104  	nodePart := res.Result.Cert.SerializeNodePart()
   105  
   106  	for _, discoveryNode := range res.Result.Cert.BootstrapNodes {
   107  		pKey, err := keyProcessor.ImportPublicKeyPEM([]byte(discoveryNode.PublicKey))
   108  		require.NoError(t, err)
   109  
   110  		t.Run("Verify network sign for "+discoveryNode.Host, func(t *testing.T) {
   111  			verified := scheme.Verifier(pKey).Verify(core.SignatureFromBytes(discoveryNode.NetworkSign), networkPart)
   112  			require.True(t, verified)
   113  		})
   114  		t.Run("Verify node sign for "+discoveryNode.Host, func(t *testing.T) {
   115  			verified := scheme.Verifier(pKey).Verify(core.SignatureFromBytes(discoveryNode.NodeSign), nodePart)
   116  			require.True(t, verified)
   117  		})
   118  	}
   119  }