github.com/kaydxh/golang@v0.0.131/go/crypto/md5/md5_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package md5_test
    23  
    24  import (
    25  	"fmt"
    26  	"io/ioutil"
    27  	"os"
    28  	"testing"
    29  
    30  	md5_ "github.com/kaydxh/golang/go/crypto/md5"
    31  	"gotest.tools/assert"
    32  )
    33  
    34  func TestMd5File(t *testing.T) {
    35  	file, err := ioutil.TempFile(".", "file")
    36  	if err != nil {
    37  		t.Errorf("expect nil, got %v", err)
    38  	}
    39  	defer os.RemoveAll(file.Name())
    40  
    41  	strContext := "hello world"
    42  	//	n, err := io.WriteString(file, "hello world")
    43  	n, err := file.Write([]byte(strContext))
    44  	if err != nil {
    45  		t.Errorf("expect nil, got %v", err)
    46  	}
    47  	assert.Equal(t, len(strContext), n)
    48  	fmt.Printf("fileName: %v, n: %v\n", file.Name(), n)
    49  
    50  	sum, err := md5_.SumFile(file.Name())
    51  	if err != nil {
    52  		t.Errorf("expect nil, got %v", err)
    53  	}
    54  	assert.Equal(t, sum, md5_.SumString(strContext))
    55  }
    56  
    57  func TestMd5FileAt(t *testing.T) {
    58  	file, err := ioutil.TempFile(".", "file")
    59  	if err != nil {
    60  		t.Errorf("expect nil, got %v", err)
    61  	}
    62  	defer os.RemoveAll(file.Name())
    63  
    64  	testCases := []struct {
    65  		name     string
    66  		words    []byte
    67  		expected string
    68  	}{
    69  		{
    70  			name:     "write one word",
    71  			words:    []byte("test1"),
    72  			expected: "test1",
    73  		},
    74  		{
    75  			name:     "write one word",
    76  			words:    []byte("test2"),
    77  			expected: "test2",
    78  		},
    79  
    80  		{
    81  			name:     "write one word",
    82  			words:    []byte("test3"),
    83  			expected: "test3",
    84  		},
    85  	}
    86  
    87  	var offset int64
    88  	for i, testCase := range testCases {
    89  		_, err := file.Write(testCase.words)
    90  		if err != nil {
    91  			t.Errorf("expect nil, got %v", err)
    92  		}
    93  
    94  		if i > 0 {
    95  			offset += int64(len(testCases[i-1].words))
    96  		}
    97  		fmt.Printf("i: %d, offset: %v, testCase: %s\n", i, offset, testCase.words)
    98  		sum, err := md5_.SumFileAt(file.Name(), offset, int64(len(testCase.words)))
    99  		if err != nil {
   100  			t.Errorf("expect nil, got %v", err)
   101  		}
   102  		fmt.Println("sum: ", sum)
   103  		assert.Equal(t, sum, md5_.SumBytes(testCase.words))
   104  	}
   105  
   106  }