Skip to content

Commit

Permalink
Merge pull request gocsaf#545 from csaf-poc/expand-util-tests
Browse files Browse the repository at this point in the history
Extend unit test coverage in util
  • Loading branch information
JanHoefelmeyer authored Jun 24, 2024
2 parents 5c6736b + 3084cdb commit cb1ed60
Show file tree
Hide file tree
Showing 6 changed files with 578 additions and 1 deletion.
38 changes: 38 additions & 0 deletions util/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This file is Free Software under the Apache-2.0 License
// without warranty, see README.md and LICENSES/Apache-2.0.txt for details.
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>

package util

import (
"bytes"
"testing"
)

func TestCSV(t *testing.T) {
buf := new(bytes.Buffer)
csvWriter := NewFullyQuotedCSWWriter(buf)
for _, x := range [][]string{{"a", "b", "c"}, {"d", "e", "f"}} {
if err := csvWriter.Write(x); err != nil {
t.Error(err)
}
}

csvWriter.Flush()
if err := csvWriter.Error(); err != nil {
t.Error(err)
}
for _, want := range []string{`"a","b","c"`, `"d","e","f"`} {
got, err := buf.ReadString('\n')
if err != nil {
t.Error(err)
}
if got[:len(got)-1] != want {
t.Errorf("FullyQuotedCSWWriter: Expected %q but got %q.", want, got)
}
}
}
137 changes: 136 additions & 1 deletion util/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package util

import (
"bytes"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -55,8 +57,51 @@ func TestConformingFileName(t *testing.T) {
}
}

func TestNWriter(t *testing.T) {
func TestIDMatchesFilename(t *testing.T) {
pathEval := NewPathEval()

doc := make(map[string]any)
doc["document"] = map[string]any{
"tracking": map[string]any{
"id": "valid.json",
},
}

if err := IDMatchesFilename(pathEval, doc, "valid.json"); err != nil {
t.Errorf("IDMatchesFilename: Expected nil, got %q", err)
}

if err := IDMatchesFilename(pathEval, doc, "different_file_name.json"); err == nil {
t.Error("IDMatchesFilename: Expected error, got nil")
}

doc["document"] = map[string]any{
"tracking": map[string]any{},
}
if err := IDMatchesFilename(pathEval, doc, "valid.json"); err == nil {
t.Error("IDMatchesFilename: Expected error, got nil")
}
}

func TestPathExists(t *testing.T) {
got, err := PathExists("/this/path/does/not/exist")
if err != nil {
t.Error(err)
}
if got != false {
t.Error("PathExists: Expected false, got true")
}
dir := t.TempDir()
got, err = PathExists(dir)
if err != nil {
t.Error(err)
}
if got != true {
t.Error("PathExists: Expected true, got false")
}
}

func TestNWriter(t *testing.T) {
msg := []byte("Gruß!\n")

first, second := msg[:len(msg)/2], msg[len(msg)/2:]
Expand All @@ -78,3 +123,93 @@ func TestNWriter(t *testing.T) {
t.Errorf("Expected %q, but got %q", msg, out)
}
}

func TestWriteToFile(t *testing.T) {
filename := filepath.Join(t.TempDir(), "test_file")
wt := bytes.NewBufferString("test_data")
if err := WriteToFile(filename, wt); err != nil {
t.Error(err)
}
fileData, err := os.ReadFile(filename)
if err != nil {
t.Error(err)
}
if !bytes.Equal(fileData, []byte("test_data")) {
t.Errorf("DeepCopy: Expected test_data, got %v", fileData)
}
}

func TestMakeUniqFile(t *testing.T) {
dir := t.TempDir()
_, file, err := MakeUniqFile(dir)
if err != nil {
t.Error(err)
}
if _, err = file.Write([]byte("test_data")); err != nil {
t.Error(err)
}
if err = file.Close(); err != nil {
t.Error(err)
}
}

func Test_mkUniq(t *testing.T) {
dir := t.TempDir()
name, err := mkUniq(dir+"/", func(name string) error {
return nil
})
if err != nil {
t.Error(err)
}
firstTime := true
name1, err := mkUniq(dir+"/", func(_ string) error {
if firstTime {
firstTime = false
return os.ErrExist
}
return nil
})
if err != nil {
t.Error(err)
}
if name == name1 {
t.Errorf("mkUniq: Expected unique names, got %v and %v", name, name1)
}
}

func TestDeepCopy(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "src/folder0"), 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dir, "dst"), 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dir, "dst1"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "src/folder0/test_file"), []byte("test_data"), 0755); err != nil {
t.Fatal(err)
}

if err := DeepCopy(filepath.Join(dir, "dst"), filepath.Join(dir, "src")); err != nil {
t.Error(err)
}

fileData, err := os.ReadFile(filepath.Join(dir, "dst/folder0/test_file"))
if err != nil {
t.Error(err)
}

if !bytes.Equal(fileData, []byte("test_data")) {
t.Errorf("DeepCopy: Expected test_data, got %v", fileData)
}

if err = DeepCopy("/path/does/not/exist", filepath.Join(dir, "src")); err == nil {
t.Error("DeepCopy: Expected error, got nil")
}

if err = DeepCopy(filepath.Join(dir, "dst1"), "/path/does/not/exist"); err == nil {
t.Error("DeepCopy: Expected error, got nil")
}
}
107 changes: 107 additions & 0 deletions util/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This file is Free Software under the Apache-2.0 License
// without warranty, see README.md and LICENSES/Apache-2.0.txt for details.
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>

package util

import (
"hash"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)

func TestHashFromReader(t *testing.T) {
r := strings.NewReader("deadbeef")
want := []byte{0xde, 0xad, 0xbe, 0xef}
if got, err := HashFromReader(r); !reflect.DeepEqual(want, got) {
if err != nil {
t.Error(err)
}
t.Errorf("HashFromReader: Expected %v, got %v", want, got)
}
}

func TestHashFromFile(t *testing.T) {
dir := t.TempDir()
filePath := filepath.Join(dir, "test_file")
testFile, err := os.Create(filePath)
if err != nil {
t.Error(err)
}

testFile.WriteString("deadbeef")
want := []byte{0xde, 0xad, 0xbe, 0xef}

testFile.Close()

if got, err := HashFromFile(filePath); !reflect.DeepEqual(want, got) {
if err != nil {
t.Error(err)
}
t.Errorf("HashFromFile: Expected %v, got %v", want, got)
}
}

type deadbeefHash struct {
hash.Hash
}

func (deadbeefHash) Write(p []byte) (int, error) { return len(p), nil }
func (deadbeefHash) Sum(_ []byte) []byte { return []byte{0xde, 0xad, 0xbe, 0xef} }

func TestWriteHashToFile(t *testing.T) {
dir := t.TempDir()
filePath := filepath.Join(dir, "test_file")

hashArg := deadbeefHash{}
nameArg := "name"
want := "deadbeef " + nameArg + "\n"

if err := WriteHashToFile(filePath, nameArg, hashArg, []byte{}); err != nil {
t.Error(err)
}
testFile, err := os.Open(filePath)
if err != nil {
t.Error(err)
}
defer testFile.Close()
fileContent, err := os.ReadFile(filePath)
if err != nil {
t.Error(err)
}
if got := string(fileContent); got != want {
t.Errorf("WriteHashToFile: Expected %v, got %v", want, got)
}
}

func TestWriteHashSumToFile(t *testing.T) {
dir := t.TempDir()
filePath := filepath.Join(dir, "test_file")

sum := []byte{0xde, 0xad, 0xbe, 0xef}
nameArg := "name"
want := "deadbeef " + nameArg + "\n"

if err := WriteHashSumToFile(filePath, nameArg, sum); err != nil {
t.Error(err)
}
testFile, err := os.Open(filePath)
if err != nil {
t.Error(err)
}
defer testFile.Close()
fileContent, err := os.ReadFile(filePath)
if err != nil {
t.Error(err)
}
if got := string(fileContent); got != want {
t.Errorf("WriteHashSumToFile: Expected %v, got %v", want, got)
}
}
Loading

0 comments on commit cb1ed60

Please sign in to comment.