1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Extend unittest coverage in util

This commit is contained in:
koplas 2024-06-12 10:08:06 +02:00
parent 7a5347803a
commit 33bd6bd787
6 changed files with 599 additions and 1 deletions

View file

@ -10,6 +10,8 @@ package util
import (
"bytes"
"os"
"path/filepath"
"testing"
)
@ -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:]
@ -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")
}
}