go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/connection/fs/fsutil/hash_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fsutil_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/spf13/afero"
    10  	"github.com/stretchr/testify/assert"
    11  	"go.mondoo.com/cnquery/providers/os/connection"
    12  	"go.mondoo.com/cnquery/providers/os/connection/fs/fsutil"
    13  )
    14  
    15  func TestFileResource(t *testing.T) {
    16  	path := "/tmp/test_hash"
    17  
    18  	conn := connection.NewLocalConnection(0, nil, nil)
    19  	assert.NotNil(t, conn)
    20  
    21  	fs := conn.FileSystem()
    22  	afutil := afero.Afero{Fs: fs}
    23  
    24  	// create the file and set the content
    25  	err := afutil.WriteFile(path, []byte("hello world"), 0o666)
    26  	assert.Nil(t, err)
    27  
    28  	f, err := fs.Open(path)
    29  	assert.Nil(t, err)
    30  	if assert.NotNil(t, f) {
    31  		assert.Equal(t, path, f.Name(), "they should be equal")
    32  
    33  		md5, err := fsutil.Md5(f)
    34  		assert.Nil(t, err)
    35  		assert.Equal(t, "5eb63bbbe01eeed093cb22bb8f5acdc3", md5)
    36  
    37  		sha256, err := fsutil.Sha256(f)
    38  		assert.Nil(t, err)
    39  		assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", sha256)
    40  	}
    41  }