github.com/braveheart12/insolar-09-08-19@v0.8.7/network/merkle/calculator_test.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package merkle
    19  
    20  import (
    21  	"context"
    22  	"encoding/hex"
    23  	"testing"
    24  
    25  	"github.com/insolar/insolar/component"
    26  	"github.com/insolar/insolar/core"
    27  	"github.com/insolar/insolar/cryptography"
    28  	"github.com/insolar/insolar/platformpolicy"
    29  	"github.com/insolar/insolar/pulsar/pulsartestutils"
    30  	"github.com/insolar/insolar/testutils"
    31  	"github.com/insolar/insolar/testutils/nodekeeper"
    32  	"github.com/insolar/insolar/testutils/terminationhandler"
    33  	"github.com/stretchr/testify/require"
    34  	"github.com/stretchr/testify/suite"
    35  )
    36  
    37  type calculatorSuite struct {
    38  	suite.Suite
    39  
    40  	pulse       *core.Pulse
    41  	nodeNetwork core.NodeNetwork
    42  	service     core.CryptographyService
    43  
    44  	calculator Calculator
    45  }
    46  
    47  func (t *calculatorSuite) TestGetNodeProof() {
    48  	ph, np, err := t.calculator.GetPulseProof(&PulseEntry{Pulse: t.pulse})
    49  
    50  	t.Assert().NoError(err)
    51  	t.Assert().NotNil(np)
    52  
    53  	key, err := t.service.GetPublicKey()
    54  	t.Assert().NoError(err)
    55  
    56  	t.Assert().True(t.calculator.IsValid(np, ph, key))
    57  }
    58  
    59  func (t *calculatorSuite) TestGetGlobuleProof() {
    60  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    61  	ph, pp, err := t.calculator.GetPulseProof(pulseEntry)
    62  	t.Assert().NoError(err)
    63  
    64  	prevCloudHash, _ := hex.DecodeString(
    65  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    66  	)
    67  
    68  	globuleEntry := &GlobuleEntry{
    69  		PulseEntry: pulseEntry,
    70  		PulseHash:  ph,
    71  		ProofSet: map[core.Node]*PulseProof{
    72  			t.nodeNetwork.GetOrigin(): pp,
    73  		},
    74  		PrevCloudHash: prevCloudHash,
    75  		GlobuleID:     0,
    76  	}
    77  	gh, gp, err := t.calculator.GetGlobuleProof(globuleEntry)
    78  
    79  	t.Assert().NoError(err)
    80  	t.Assert().NotNil(gp)
    81  
    82  	key, err := t.service.GetPublicKey()
    83  	t.Assert().NoError(err)
    84  
    85  	valid := t.calculator.IsValid(gp, gh, key)
    86  	t.Assert().True(valid)
    87  }
    88  
    89  func (t *calculatorSuite) TestGetCloudProof() {
    90  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    91  	ph, pp, err := t.calculator.GetPulseProof(pulseEntry)
    92  	t.Assert().NoError(err)
    93  
    94  	prevCloudHash, _ := hex.DecodeString(
    95  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    96  	)
    97  
    98  	globuleEntry := &GlobuleEntry{
    99  		PulseEntry: pulseEntry,
   100  		PulseHash:  ph,
   101  		ProofSet: map[core.Node]*PulseProof{
   102  			t.nodeNetwork.GetOrigin(): pp,
   103  		},
   104  		PrevCloudHash: prevCloudHash,
   105  		GlobuleID:     0,
   106  	}
   107  	_, gp, err := t.calculator.GetGlobuleProof(globuleEntry)
   108  
   109  	ch, cp, err := t.calculator.GetCloudProof(&CloudEntry{
   110  		ProofSet:      []*GlobuleProof{gp},
   111  		PrevCloudHash: prevCloudHash,
   112  	})
   113  
   114  	t.Assert().NoError(err)
   115  	t.Assert().NotNil(gp)
   116  
   117  	key, err := t.service.GetPublicKey()
   118  	t.Assert().NoError(err)
   119  
   120  	valid := t.calculator.IsValid(cp, ch, key)
   121  	t.Assert().True(valid)
   122  }
   123  
   124  func TestNewCalculator(t *testing.T) {
   125  	c := NewCalculator()
   126  	require.NotNil(t, c)
   127  }
   128  
   129  func TestCalculator(t *testing.T) {
   130  	calculator := &calculator{}
   131  
   132  	key, _ := platformpolicy.NewKeyProcessor().GeneratePrivateKey()
   133  	require.NotNil(t, key)
   134  
   135  	service := cryptography.NewKeyBoundCryptographyService(key)
   136  	scheme := platformpolicy.NewPlatformCryptographyScheme()
   137  	nk := nodekeeper.GetTestNodekeeper(service)
   138  	th := terminationhandler.NewTestHandler()
   139  
   140  	am := testutils.NewArtifactManagerMock(t)
   141  	am.StateFunc = func() (r []byte, r1 error) {
   142  		return []byte("state"), nil
   143  	}
   144  
   145  	cm := component.Manager{}
   146  	cm.Inject(th, nk, am, calculator, service, scheme)
   147  
   148  	require.NotNil(t, calculator.ArtifactManager)
   149  	require.NotNil(t, calculator.NodeNetwork)
   150  	require.NotNil(t, calculator.CryptographyService)
   151  	require.NotNil(t, calculator.PlatformCryptographyScheme)
   152  
   153  	err := cm.Init(context.Background())
   154  	require.NoError(t, err)
   155  
   156  	pulse := &core.Pulse{
   157  		PulseNumber:     core.PulseNumber(1337),
   158  		NextPulseNumber: core.PulseNumber(1347),
   159  		Entropy:         pulsartestutils.MockEntropyGenerator{}.GenerateEntropy(),
   160  	}
   161  
   162  	s := &calculatorSuite{
   163  		Suite:       suite.Suite{},
   164  		calculator:  calculator,
   165  		pulse:       pulse,
   166  		nodeNetwork: nk,
   167  		service:     service,
   168  	}
   169  	suite.Run(t, s)
   170  }