go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/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-sdk/v1/inventory"
    12  	"go.mondoo.com/cnquery/providers/os/connection"
    13  	"go.mondoo.com/cnquery/providers/os/fsutil"
    14  )
    15  
    16  func TestFileResource(t *testing.T) {
    17  	path := "/tmp/test_hash"
    18  
    19  	conn := connection.NewLocalConnection(0, &inventory.Config{
    20  		Path: path,
    21  	}, &inventory.Asset{})
    22  
    23  	fs := conn.FileSystem()
    24  	afutil := afero.Afero{Fs: fs}
    25  
    26  	// create the file and set the content
    27  	err := afutil.WriteFile(path, []byte("hello world"), 0o666)
    28  	assert.Nil(t, err)
    29  
    30  	f, err := fs.Open(path)
    31  	assert.Nil(t, err)
    32  	if assert.NotNil(t, f) {
    33  		assert.Equal(t, path, f.Name(), "they should be equal")
    34  
    35  		md5, err := fsutil.Md5(f)
    36  		assert.Nil(t, err)
    37  		assert.Equal(t, "5eb63bbbe01eeed093cb22bb8f5acdc3", md5)
    38  
    39  		sha256, err := fsutil.Sha256(f)
    40  		assert.Nil(t, err)
    41  		assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", sha256)
    42  	}
    43  }