github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/hashed_buffer_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  	"io"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestHashedBuffer(t *testing.T) {
    18  	cases := []struct {
    19  		MaxMemorySize int
    20  		Data          string
    21  		HashMD5       string
    22  		HashSHA1      string
    23  		HashSHA256    string
    24  		HashSHA512    string
    25  	}{
    26  		{
    27  			5,
    28  			"test",
    29  			"098f6bcd4621d373cade4e832627b4f6",
    30  			"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
    31  			"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
    32  			"ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff",
    33  		},
    34  		{
    35  			5,
    36  			"testtest",
    37  			"05a671c66aefea124cc08b76ea6d30bb",
    38  			"51abb9636078defbf888d8457a7c76f85c8f114c",
    39  			"37268335dd6931045bdcdf92623ff819a64244b53d0e746d438797349d4da578",
    40  			"125d6d03b32c84d492747f79cf0bf6e179d287f341384eb5d6d3197525ad6be8e6df0116032935698f99a09e265073d1d6c32c274591bf1d0a20ad67cba921bc",
    41  		},
    42  	}
    43  
    44  	for _, c := range cases {
    45  		buf, err := CreateHashedBufferFromReader(strings.NewReader(c.Data), c.MaxMemorySize)
    46  		assert.NoError(t, err)
    47  
    48  		assert.EqualValues(t, len(c.Data), buf.Size())
    49  
    50  		data, err := io.ReadAll(buf)
    51  		assert.NoError(t, err)
    52  		assert.Equal(t, c.Data, string(data))
    53  
    54  		hashMD5, hashSHA1, hashSHA256, hashSHA512 := buf.Sums()
    55  		assert.Equal(t, c.HashMD5, fmt.Sprintf("%x", hashMD5))
    56  		assert.Equal(t, c.HashSHA1, fmt.Sprintf("%x", hashSHA1))
    57  		assert.Equal(t, c.HashSHA256, fmt.Sprintf("%x", hashSHA256))
    58  		assert.Equal(t, c.HashSHA512, fmt.Sprintf("%x", hashSHA512))
    59  
    60  		assert.NoError(t, buf.Close())
    61  	}
    62  }