package cop_test import ( "errors" "fmt" "os" "os/user" "path" "testing" "gitea.mmo.to/ppForge/ppforge/cop" "gitea.mmo.to/ppForge/ppforge/globals" "gitea.mmo.to/ppForge/ppforge/protocol" ) func setupSuite(t *testing.T) func(t *testing.T) { // setup // setup testing home dir repo, err := globals.GetRepoDir(globals.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) { td := setupSuite(t) defer td(t) fcop := cop.FileCOP{} wd, err := globals.GetRepoDir(globals.GetCurrentDir()) if err != nil { fmt.Printf("Os error: %s", err) t.Fail() } fcop.Open(path.Join(wd, "test", "cop")) 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) { td := setupSuite(t) defer td(t) fcop := cop.FileCOP{} wd, err := globals.GetRepoDir(globals.GetCurrentDir()) if err != nil { fmt.Printf("Os error: %s", err) t.Fail() } fcop.Open(path.Join(wd, "test", "cop")) fcop.Close() 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() } err = fcop.Sync() if err == nil { fmt.Println("UpdateCollection did not deliver error") t.Fail() } } 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, err := globals.GetRepoDir(globals.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) 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 // reset to zero because of other tests gcop.PCs = []cop.COPer{} cop.Init() c := gcop.PCs[0] protos, err := c.GetProtocols() if len(protos) != 4 { t.Fail() } err = gcop.WriteCache() 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() } // reset again and check if reading the cache works gcop.PCs = []cop.COPer{} err = gcop.ReadCache() if err != nil { fmt.Println("Reading the cache file failed", err) t.Fail() } c = gcop.PCs[0] protos, err = c.GetProtocols() 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() 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) }