Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package checksum
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestStrings(t *testing.T) {
|
|
h := sha256.Sum256([]byte("abc"))
|
|
require.Equal(t, fmt.Sprintf("%x", h[:]), Strings("a", "b", "c"))
|
|
}
|
|
|
|
func TestSum(t *testing.T) {
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "a.txt"), []byte("alpha"), 0o600))
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "b.txt"), []byte("beta"), 0o600))
|
|
|
|
out := filepath.Join(t.TempDir(), "checksums.txt")
|
|
require.NoError(t, Sum(dir, out))
|
|
|
|
content, err := os.ReadFile(out)
|
|
require.NoError(t, err)
|
|
text := string(content)
|
|
require.Contains(t, text, " a.txt\n")
|
|
require.Contains(t, text, " b.txt\n")
|
|
}
|
|
|
|
func TestBinary(t *testing.T) {
|
|
bin := filepath.Join(t.TempDir(), "fake-bin")
|
|
data := []byte("#!/bin/sh\necho test\n")
|
|
require.NoError(t, os.WriteFile(bin, data, 0o700))
|
|
|
|
want := sha256.Sum256(data)
|
|
got, err := Binary(bin)
|
|
require.NoError(t, err)
|
|
require.Equal(t, fmt.Sprintf("%x", want[:]), got)
|
|
}
|
|
|
|
func TestBinaryReturnsErrorForMissingFile(t *testing.T) {
|
|
_, err := Binary(strings.TrimSpace(filepath.Join(t.TempDir(), "missing-bin")))
|
|
require.Error(t, err)
|
|
}
|