Skip to content

Commit

Permalink
Extend unittest coverage in util
Browse files Browse the repository at this point in the history
  • Loading branch information
koplas committed Jun 12, 2024
1 parent 7a53478 commit 33bd6bd
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 1 deletion.
40 changes: 40 additions & 0 deletions util/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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"}} {
err := csvWriter.Write(x)
if err != nil {
t.Error(err)
}
}

csvWriter.Flush()
err := csvWriter.Error()
if 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)
}
}
}
141 changes: 140 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,54 @@ func TestConformingFileName(t *testing.T) {
}
}

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

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

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

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

doc["document"] = map[string]interface{}{
"tracking": map[string]interface{}{},
}
err = IDMatchesFilename(pathEval, doc, "valid.json")
if 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 +126,94 @@ 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")
err := WriteToFile(filename, wt)
if 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)
}
_, err = file.Write([]byte("test_data"))
if err != nil {
t.Error(err)
}
err = file.Close()
if 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()
os.MkdirAll(filepath.Join(dir, "src/folder0"), 0755)
os.MkdirAll(filepath.Join(dir, "dst"), 0755)
os.MkdirAll(filepath.Join(dir, "dst1"), 0755)
err := os.WriteFile(filepath.Join(dir, "src/folder0/test_file"), []byte("test_data"), 0755)
if err != nil {
t.Error(err)
}

err = DeepCopy(filepath.Join(dir, "dst"), filepath.Join(dir, "src"))
if 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)
}

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

err = DeepCopy(filepath.Join(dir, "dst1"), "/path/does/not/exist")
if err == nil {
t.Error("DeepCopy: Expected error, got nil")
}
}
109 changes: 109 additions & 0 deletions util/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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"

err := WriteHashToFile(filePath, nameArg, hashArg, []byte{})
if 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"

err := WriteHashSumToFile(filePath, nameArg, sum)
if 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 33bd6bd

Please sign in to comment.