github.com/braveheart12/just@v0.8.7/network/merkle/errors_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  	"crypto"
    23  	"encoding/hex"
    24  	"testing"
    25  
    26  	"github.com/insolar/insolar/component"
    27  	"github.com/insolar/insolar/core"
    28  	"github.com/insolar/insolar/ledger/ledgertestutils"
    29  	"github.com/insolar/insolar/platformpolicy"
    30  	"github.com/insolar/insolar/pulsar/pulsartestutils"
    31  	"github.com/insolar/insolar/testutils"
    32  	"github.com/insolar/insolar/testutils/nodekeeper"
    33  	"github.com/insolar/insolar/testutils/terminationhandler"
    34  	"github.com/pkg/errors"
    35  	"github.com/stretchr/testify/require"
    36  	"github.com/stretchr/testify/suite"
    37  )
    38  
    39  type calculatorErrorSuite struct {
    40  	suite.Suite
    41  
    42  	pulse       *core.Pulse
    43  	nodeNetwork core.NodeNetwork
    44  	service     core.CryptographyService
    45  
    46  	calculator Calculator
    47  }
    48  
    49  func (t *calculatorErrorSuite) TestGetNodeProofError() {
    50  	ph, np, err := t.calculator.GetPulseProof(&PulseEntry{Pulse: t.pulse})
    51  
    52  	t.Assert().Error(err)
    53  	t.Assert().Contains(err.Error(), "[ GetPulseProof ] Failed to sign node info hash")
    54  	t.Assert().Nil(np)
    55  	t.Assert().Nil(ph)
    56  }
    57  
    58  func (t *calculatorErrorSuite) TestGetGlobuleProofCalculateError() {
    59  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    60  
    61  	prevCloudHash, _ := hex.DecodeString(
    62  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    63  	)
    64  
    65  	globuleEntry := &GlobuleEntry{
    66  		PulseEntry:    pulseEntry,
    67  		PulseHash:     nil,
    68  		ProofSet:      nil,
    69  		PrevCloudHash: prevCloudHash,
    70  		GlobuleID:     0,
    71  	}
    72  	gh, gp, err := t.calculator.GetGlobuleProof(globuleEntry)
    73  
    74  	t.Assert().Error(err)
    75  	t.Assert().Contains(err.Error(), "[ GetGlobuleProof ] Failed to calculate node root")
    76  	t.Assert().Nil(gh)
    77  	t.Assert().Nil(gp)
    78  }
    79  
    80  func (t *calculatorErrorSuite) TestGetGlobuleProofSignError() {
    81  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    82  
    83  	prevCloudHash, _ := hex.DecodeString(
    84  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    85  	)
    86  
    87  	globuleEntry := &GlobuleEntry{
    88  		PulseEntry: pulseEntry,
    89  		PulseHash:  nil,
    90  		ProofSet: map[core.Node]*PulseProof{
    91  			t.nodeNetwork.GetOrigin(): {},
    92  		},
    93  		PrevCloudHash: prevCloudHash,
    94  		GlobuleID:     0,
    95  	}
    96  	gh, gp, err := t.calculator.GetGlobuleProof(globuleEntry)
    97  
    98  	t.Assert().Error(err)
    99  	t.Assert().Contains(err.Error(), "[ GetGlobuleProof ] Failed to sign globule hash")
   100  	t.Assert().Nil(gh)
   101  	t.Assert().Nil(gp)
   102  }
   103  
   104  func (t *calculatorErrorSuite) TestGetCloudProofSignError() {
   105  	ch, cp, err := t.calculator.GetCloudProof(&CloudEntry{
   106  		ProofSet: []*GlobuleProof{
   107  			{},
   108  		},
   109  		PrevCloudHash: nil,
   110  	})
   111  
   112  	t.Assert().Error(err)
   113  	t.Assert().Contains(err.Error(), "[ GetCloudProof ] Failed to sign cloud hash")
   114  	t.Assert().Nil(ch)
   115  	t.Assert().Nil(cp)
   116  }
   117  
   118  func (t *calculatorErrorSuite) TestGetCloudProofCalculateError() {
   119  	ch, cp, err := t.calculator.GetCloudProof(&CloudEntry{
   120  		ProofSet:      nil,
   121  		PrevCloudHash: nil,
   122  	})
   123  
   124  	t.Assert().Error(err)
   125  	t.Assert().Contains(err.Error(), "[ GetCloudProof ] Failed to calculate cloud hash")
   126  	t.Assert().Nil(ch)
   127  	t.Assert().Nil(cp)
   128  }
   129  
   130  func TestCalculatorError(t *testing.T) {
   131  	// FIXME: TmpLedger is deprecated. Use mocks instead.
   132  	l, _, clean := ledgertestutils.TmpLedger(t, "", core.StaticRoleLightMaterial, core.Components{}, true)
   133  
   134  	calculator := &calculator{}
   135  
   136  	cm := component.Manager{}
   137  
   138  	key, _ := platformpolicy.NewKeyProcessor().GeneratePrivateKey()
   139  	require.NotNil(t, key)
   140  
   141  	service := testutils.NewCryptographyServiceMock(t)
   142  	service.SignFunc = func(p []byte) (r *core.Signature, r1 error) {
   143  		return nil, errors.New("Sign error")
   144  	}
   145  	service.GetPublicKeyFunc = func() (r crypto.PublicKey, r1 error) {
   146  		return "key", nil
   147  	}
   148  	scheme := platformpolicy.NewPlatformCryptographyScheme()
   149  
   150  	pulseManager := testutils.NewPulseStorageMock(t)
   151  
   152  	nk := nodekeeper.GetTestNodekeeper(service)
   153  	th := terminationhandler.NewTestHandler()
   154  
   155  	jc := testutils.NewJetCoordinatorMock(t)
   156  
   157  	cm.Inject(th, nk, jc, l.ArtifactManager, calculator, service, scheme, pulseManager)
   158  
   159  	require.NotNil(t, calculator.ArtifactManager)
   160  	require.NotNil(t, calculator.NodeNetwork)
   161  	require.NotNil(t, calculator.CryptographyService)
   162  	require.NotNil(t, calculator.PlatformCryptographyScheme)
   163  
   164  	err := cm.Init(context.Background())
   165  	require.NoError(t, err)
   166  
   167  	pulse := &core.Pulse{
   168  		PulseNumber:     core.PulseNumber(1337),
   169  		NextPulseNumber: core.PulseNumber(1347),
   170  		Entropy:         pulsartestutils.MockEntropyGenerator{}.GenerateEntropy(),
   171  	}
   172  
   173  	s := &calculatorErrorSuite{
   174  		Suite:       suite.Suite{},
   175  		calculator:  calculator,
   176  		pulse:       pulse,
   177  		nodeNetwork: nk,
   178  		service:     service,
   179  	}
   180  	suite.Run(t, s)
   181  
   182  	clean()
   183  }
   184  
   185  func TestCalculatorLedgerError(t *testing.T) {
   186  	calculator := &calculator{}
   187  
   188  	cm := component.Manager{}
   189  
   190  	key, _ := platformpolicy.NewKeyProcessor().GeneratePrivateKey()
   191  	require.NotNil(t, key)
   192  
   193  	service := testutils.NewCryptographyServiceMock(t)
   194  	service.SignFunc = func(p []byte) (r *core.Signature, r1 error) {
   195  		return nil, errors.New("Sign error")
   196  	}
   197  	service.GetPublicKeyFunc = func() (r crypto.PublicKey, r1 error) {
   198  		return "key", nil
   199  	}
   200  
   201  	am := testutils.NewArtifactManagerMock(t)
   202  	am.StateFunc = func() (r []byte, r1 error) {
   203  		return nil, errors.New("State error")
   204  	}
   205  
   206  	scheme := platformpolicy.NewPlatformCryptographyScheme()
   207  	nk := nodekeeper.GetTestNodekeeper(service)
   208  	th := terminationhandler.NewTestHandler()
   209  	cm.Inject(th, nk, am, calculator, service, scheme)
   210  
   211  	require.NotNil(t, calculator.ArtifactManager)
   212  	require.NotNil(t, calculator.NodeNetwork)
   213  	require.NotNil(t, calculator.CryptographyService)
   214  	require.NotNil(t, calculator.PlatformCryptographyScheme)
   215  
   216  	err := cm.Init(context.Background())
   217  	require.NoError(t, err)
   218  
   219  	pulse := &core.Pulse{
   220  		PulseNumber:     core.PulseNumber(1337),
   221  		NextPulseNumber: core.PulseNumber(1347),
   222  		Entropy:         pulsartestutils.MockEntropyGenerator{}.GenerateEntropy(),
   223  	}
   224  
   225  	ph, np, err := calculator.GetPulseProof(&PulseEntry{Pulse: pulse})
   226  
   227  	require.Error(t, err)
   228  	require.Contains(t, err.Error(), "[ GetPulseProof ] Failed to get node stateHash")
   229  	require.Nil(t, np)
   230  	require.Nil(t, ph)
   231  }