github.com/braveheart12/just@v0.8.7/network/merkle/hashes_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  	"fmt"
    25  	"testing"
    26  
    27  	"github.com/insolar/insolar/component"
    28  	"github.com/insolar/insolar/core"
    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/stretchr/testify/require"
    35  	"github.com/stretchr/testify/suite"
    36  )
    37  
    38  func (t *calculatorHashesSuite) TestGetPulseHash() {
    39  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    40  	ph, _, err := t.calculator.GetPulseProof(pulseEntry)
    41  	t.Assert().NoError(err)
    42  
    43  	expectedHash, _ := hex.DecodeString(
    44  		"bd18c009950389026c5c6f85c838b899d188ec0d667f77948aa72a49747c3ed31835b1bdbb8bd1d1de62846b5f308ae3eac5127c7d36d7d5464985004122cc90",
    45  	)
    46  
    47  	t.Assert().Equal(OriginHash(expectedHash), ph)
    48  }
    49  
    50  func (t *calculatorHashesSuite) TestGetGlobuleHash() {
    51  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    52  	ph, pp, err := t.calculator.GetPulseProof(pulseEntry)
    53  	t.Assert().NoError(err)
    54  
    55  	prevCloudHash, _ := hex.DecodeString(
    56  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    57  	)
    58  
    59  	globuleEntry := &GlobuleEntry{
    60  		PulseEntry: pulseEntry,
    61  		PulseHash:  ph,
    62  		ProofSet: map[core.Node]*PulseProof{
    63  			t.nodeNetwork.GetOrigin(): pp,
    64  		},
    65  		PrevCloudHash: prevCloudHash,
    66  		GlobuleID:     0,
    67  	}
    68  	gh, _, err := t.calculator.GetGlobuleProof(globuleEntry)
    69  	t.Assert().NoError(err)
    70  
    71  	expectedHash, _ := hex.DecodeString(
    72  		"68cd36762548acd48795678c2e308978edd1ff74de2f5daf0511c1b52cf7a7bef44e09d5dd5806e99aa4ed4253aca88390e6b376e0c5f5a49ff48a8f9547e5c5",
    73  	)
    74  
    75  	t.Assert().Equal(OriginHash(expectedHash), gh)
    76  }
    77  
    78  func (t *calculatorHashesSuite) TestGetCloudHash() {
    79  	pulseEntry := &PulseEntry{Pulse: t.pulse}
    80  	ph, pp, err := t.calculator.GetPulseProof(pulseEntry)
    81  	t.Assert().NoError(err)
    82  
    83  	prevCloudHash, _ := hex.DecodeString(
    84  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    85  	)
    86  
    87  	globuleEntry := &GlobuleEntry{
    88  		PulseEntry: pulseEntry,
    89  		PulseHash:  ph,
    90  		ProofSet: map[core.Node]*PulseProof{
    91  			t.nodeNetwork.GetOrigin(): pp,
    92  		},
    93  		PrevCloudHash: prevCloudHash,
    94  		GlobuleID:     0,
    95  	}
    96  	_, gp, err := t.calculator.GetGlobuleProof(globuleEntry)
    97  
    98  	ch, _, err := t.calculator.GetCloudProof(&CloudEntry{
    99  		ProofSet:      []*GlobuleProof{gp},
   100  		PrevCloudHash: prevCloudHash,
   101  	})
   102  
   103  	t.Assert().NoError(err)
   104  
   105  	expectedHash, _ := hex.DecodeString(
   106  		"68cd36762548acd48795678c2e308978edd1ff74de2f5daf0511c1b52cf7a7bef44e09d5dd5806e99aa4ed4253aca88390e6b376e0c5f5a49ff48a8f9547e5c5",
   107  	)
   108  
   109  	fmt.Println(hex.EncodeToString(ch))
   110  
   111  	t.Assert().Equal(OriginHash(expectedHash), ch)
   112  }
   113  
   114  type calculatorHashesSuite struct {
   115  	suite.Suite
   116  
   117  	pulse       *core.Pulse
   118  	nodeNetwork core.NodeNetwork
   119  	service     core.CryptographyService
   120  
   121  	calculator Calculator
   122  }
   123  
   124  func TestCalculatorHashes(t *testing.T) {
   125  	calculator := &calculator{}
   126  
   127  	key, _ := platformpolicy.NewKeyProcessor().GeneratePrivateKey()
   128  	require.NotNil(t, key)
   129  
   130  	service := testutils.NewCryptographyServiceMock(t)
   131  	service.SignFunc = func(p []byte) (r *core.Signature, r1 error) {
   132  		signature := core.SignatureFromBytes([]byte("signature"))
   133  		return &signature, nil
   134  	}
   135  	service.GetPublicKeyFunc = func() (r crypto.PublicKey, r1 error) {
   136  		return "key", nil
   137  	}
   138  
   139  	am := testutils.NewArtifactManagerMock(t)
   140  	am.StateFunc = func() (r []byte, r1 error) {
   141  		return []byte("state"), nil
   142  	}
   143  
   144  	scheme := platformpolicy.NewPlatformCryptographyScheme()
   145  	nk := nodekeeper.GetTestNodekeeper(service)
   146  	th := terminationhandler.NewTestHandler()
   147  
   148  	cm := component.Manager{}
   149  	cm.Inject(th, nk, am, calculator, service, scheme)
   150  
   151  	require.NotNil(t, calculator.ArtifactManager)
   152  	require.NotNil(t, calculator.NodeNetwork)
   153  	require.NotNil(t, calculator.CryptographyService)
   154  	require.NotNil(t, calculator.PlatformCryptographyScheme)
   155  
   156  	err := cm.Init(context.Background())
   157  	require.NoError(t, err)
   158  
   159  	pulse := &core.Pulse{
   160  		PulseNumber:     core.PulseNumber(1337),
   161  		NextPulseNumber: core.PulseNumber(1347),
   162  		Entropy:         pulsartestutils.MockEntropyGenerator{}.GenerateEntropy(),
   163  	}
   164  
   165  	s := &calculatorHashesSuite{
   166  		Suite:       suite.Suite{},
   167  		calculator:  calculator,
   168  		pulse:       pulse,
   169  		nodeNetwork: nk,
   170  		service:     service,
   171  	}
   172  	suite.Run(t, s)
   173  }