ppforge/cop/collection_test.go

202 lines
4.2 KiB
Go
Raw Normal View History

2023-10-27 13:38:35 +02:00
package cop_test
import (
2023-10-27 13:38:35 +02:00
"errors"
"fmt"
"os"
2023-10-27 13:38:35 +02:00
"os/user"
"path"
2023-10-27 13:38:35 +02:00
"runtime"
"strings"
"testing"
2023-10-27 13:38:35 +02:00
"gitea.mmo.to/ppForge/ppforge/cop"
"gitea.mmo.to/ppForge/ppforge/globals"
"gitea.mmo.to/ppForge/ppforge/protocol"
)
2023-10-27 13:38:35 +02:00
func getCurrentDir() string {
_, filename, _, _ := runtime.Caller(0)
return path.Dir(filename)
}
func getRepoDir(start string) (string, error) {
var repoPath string
entries, err := os.ReadDir(start)
for _, v := range entries {
if strings.HasSuffix(v.Name(), ".git") {
repoPath = start
}
}
if repoPath == "" {
return getRepoDir(path.Dir(start))
}
return repoPath, err
}
func setupSuite(t *testing.T) func(t *testing.T) {
// setup
// setup testing home dir
repo, err := getRepoDir(getCurrentDir())
if err != nil {
t.Error(err)
}
testUserHome := path.Join(repo, "test", "home")
globals.ConfigDirectoryList = []string{testUserHome, ".config", "ppforge"}
globals.ConfigDirectory = path.Join(globals.ConfigDirectoryList...)
globals.CollectionOfProtocolsDir = path.Join(globals.ConfigDirectory, "cop")
globals.MockUserCurrent = func() (*user.User, error) {
mu := user.User{}
mu.HomeDir = testUserHome
return &mu, nil
}
// teardown
return func(t *testing.T) {
globals.MockUserCurrent = user.Current
}
}
func TestOpen(t *testing.T) {
2023-10-27 13:38:35 +02:00
td := setupSuite(t)
defer td(t)
fcop := cop.FileCOP{}
wd, err := getRepoDir(getCurrentDir())
if err != nil {
fmt.Printf("Os error: %s", err)
t.Fail()
}
2023-10-27 13:38:35 +02:00
fcop.Open(path.Join(wd, "test", "cop"))
2023-10-30 22:39:53 +01:00
l, err := fcop.GetProtocols()
if err != nil {
t.Fail()
}
for _, pce := range l {
fmt.Printf("%s//%s\n", pce.Path, pce.Protocol.Name)
}
}
func TestClosed(t *testing.T) {
2023-10-27 13:38:35 +02:00
td := setupSuite(t)
defer td(t)
fcop := cop.FileCOP{}
wd, err := getRepoDir(getCurrentDir())
if err != nil {
fmt.Printf("Os error: %s", err)
t.Fail()
}
2023-10-27 13:38:35 +02:00
fcop.Open(path.Join(wd, "test", "cop"))
fcop.Close()
2023-10-30 22:39:53 +01:00
l, err := fcop.GetProtocols()
if err == nil || l != nil {
fmt.Println("Protocols did not deliver error")
t.Fail()
}
err = fcop.AddOrUpdate(nil)
if err == nil {
fmt.Println("AddOrUpdate did not deliver error")
t.Fail()
}
2023-10-13 16:13:33 +02:00
err = fcop.Sync()
if err == nil {
fmt.Println("UpdateCollection did not deliver error")
t.Fail()
}
}
2023-10-27 13:38:35 +02:00
func TestGlobalCOP(t *testing.T) {
td := setupSuite(t)
defer td(t)
cop.Init()
gcop := cop.GlobalCOP
pcs := gcop.PCs
fmt.Printf("Length of all Protocol Collections: %d\n", len(pcs))
if len(pcs) != 1 {
fmt.Println("Length != 1")
t.Fail()
}
wd, _ := getRepoDir(getCurrentDir())
fcop, err := cop.NewFileCOP(path.Join(wd, "test", "cop"))
if err != nil {
fmt.Println("Error on loading new FileCOP", err)
t.Fail()
}
gcop.PCs = append(gcop.PCs, fcop)
2023-10-27 13:38:35 +02:00
fmt.Printf("Length of all Protocol Collections: %d\n", len(gcop.PCs))
if len(gcop.PCs) != 2 {
fmt.Println("Amount of COPs is not 2")
t.Fail()
}
}
func TestCOPCache(t *testing.T) {
td := setupSuite(t)
defer td(t)
gcop := cop.GlobalCOP
2023-10-30 22:39:53 +01:00
// reset to zero because of other tests
gcop.PCs = []cop.COPer{}
2023-10-30 22:39:53 +01:00
cop.Init()
c := gcop.PCs[0]
protos, err := c.GetProtocols()
2023-10-30 22:39:53 +01:00
if len(protos) != 4 {
t.Fail()
}
err = gcop.WriteCache()
2023-10-27 13:38:35 +02:00
if err != nil {
fmt.Println("Writing the cache file failed")
t.Fail()
}
fpath := path.Join(globals.CollectionOfProtocolsDir, globals.COPCacheFileName)
if _, staterr := os.Stat(fpath); errors.Is(staterr, os.ErrNotExist) {
fmt.Println("File not existing ", fpath)
t.Fail()
}
2023-10-30 22:39:53 +01:00
// reset again and check if reading the cache works
gcop.PCs = []cop.COPer{}
2023-10-30 22:39:53 +01:00
2023-10-27 13:38:35 +02:00
err = gcop.ReadCache()
if err != nil {
fmt.Println("Reading the cache file failed", err)
2023-10-27 13:38:35 +02:00
t.Fail()
}
2023-10-30 22:39:53 +01:00
c = gcop.PCs[0]
protos, err = c.GetProtocols()
2023-10-30 22:39:53 +01:00
if len(protos) != 4 {
t.Fail()
}
}
func TestAddOrUpdate(t *testing.T) {
td := setupSuite(t)
defer td(t)
gcop := cop.GlobalCOP
gcop.PCs = []cop.COPer{}
cop.Init()
2023-10-27 13:38:35 +02:00
prot := protocol.NewProtocolStructure()
prot.Metadata.Name = "TestAddOrUpdate"
prot.Metadata.Description = "A protocol of the unit tests to test AddOrUpdate of a protocol collection"
prot.Metadata.Version = "1.0"
prot.Metadata.Revision = 0
prot.Metadata.TCPIPLayer = 1
f := protocol.NewField(
"Testfield",
"A field for the unit tests of protocol collections...",
"",
32,
nil,
false,
false,
)
prot.AddField(0, f)
col := gcop.PCs[0]
col.AddOrUpdate(prot)
2023-10-27 13:38:35 +02:00
}