github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/multi_hasher_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package packages
     7  
     8  import (
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  const (
    16  	expectedMD5    = "e3bef03c5f3b7f6b3ab3e3053ed71e9c"
    17  	expectedSHA1   = "060b3b99f88e96085b4a68e095bc9e3d1d91e1bc"
    18  	expectedSHA256 = "6ccce4863b70f258d691f59609d31b4502e1ba5199942d3bc5d35d17a4ce771d"
    19  	expectedSHA512 = "7f70e439ba8c52025c1f06cdf6ae443c4b8ed2e90059cdb9bbbf8adf80846f185a24acca9245b128b226d61753b0d7ed46580a69c8999eeff3bc13a4d0bd816c"
    20  )
    21  
    22  func TestMultiHasherSums(t *testing.T) {
    23  	t.Run("Sums", func(t *testing.T) {
    24  		h := NewMultiHasher()
    25  		h.Write([]byte("gitea"))
    26  
    27  		hashMD5, hashSHA1, hashSHA256, hashSHA512 := h.Sums()
    28  
    29  		assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
    30  		assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
    31  		assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
    32  		assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
    33  	})
    34  
    35  	t.Run("State", func(t *testing.T) {
    36  		h := NewMultiHasher()
    37  		h.Write([]byte("git"))
    38  
    39  		state, err := h.MarshalBinary()
    40  		assert.NoError(t, err)
    41  
    42  		h2 := NewMultiHasher()
    43  		err = h2.UnmarshalBinary(state)
    44  		assert.NoError(t, err)
    45  
    46  		h2.Write([]byte("ea"))
    47  
    48  		hashMD5, hashSHA1, hashSHA256, hashSHA512 := h2.Sums()
    49  
    50  		assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
    51  		assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
    52  		assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
    53  		assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
    54  	})
    55  }