func TestProcessFmt134(t *testing.T) { b := New() config.SetDistance(1000) config.SetRange(500) config.SetChoices(3) b.addSignature(tests.TestFmts[134]) saver := persist.NewLoadSaver(nil) b.Save(saver) loader := persist.NewLoadSaver(saver.Bytes()) b = Load(loader) if len(b.keyFrames[0]) != 8 { for _, v := range b.keyFrames[0] { t.Errorf("%s\n", v) } } for _, t := range b.tests { t.maxLeftDistance = maxLength(t.left) t.maxRightDistance = maxLength(t.right) } if len(b.tests) != 8 { for _, v := range b.tests { t.Error(v.maxRightDistance) t.Error(v.right) } } }
func TestMatcher(t *testing.T) { ctypes = []ctype{ctype{testTrigger, newTestReader}} // test adding count++ _, err := testMatcher.Add( SignatureSet{ 0, [][]string{[]string{"one", "two"}, []string{"one"}}, [][]frames.Signature{[]frames.Signature{tests.TestSignatures[3], tests.TestSignatures[4]}, []frames.Signature{tests.TestSignatures[2]}}, }, nil, ) if err != nil { t.Fatal(err) } // test IO str := testMatcher.String() saver := persist.NewLoadSaver(nil) testMatcher.Save(saver) if len(saver.Bytes()) < 100 { t.Errorf("Save container: too small, only got %v", len(saver.Bytes())) } newcm := Load(persist.NewLoadSaver(saver.Bytes())) str2 := newcm.String() if len(str) != len(str2) { t.Errorf("Load container: expecting first matcher (%v), to equal second matcher (%v)", str, str2) } }
func TestSet(t *testing.T) { m := make(Map) m.Add("apple", "orange") m.Add("orange", "banana") m.Add("orange", "grapes") m.Add("banana", "grapes") m.Add("banana", "grapefruit") m.Complete() list := m.List([]string{"apple", "grapes", "grapes", "banana", "banana", "apple"}) list2 := m.List([]string{"grapefruit", "banana", "grapes"}) s := &Set{} s.Add(list, len(list)) s.Add(list2, len(list2)) // test save/load saver := persist.NewLoadSaver(nil) s.Save(saver) loader := persist.NewLoadSaver(saver.Bytes()) s = Load(loader) // now test the waitset w := s.WaitSet() if !w.Check(8) { t.Error("Priority: should get continue signal") } if w.Put(8) { t.Error("Priority: should not be satisfied") } if !w.Put(1) { t.Error("Priority: should be satisfied") } w.Put(7) if !w.Check(6) { t.Error("Priority: expecting to be waiting on grapefruits") } wo := w.WaitingOn() if len(wo) != 2 { t.Error("Priority: expecting to be waiting on two") } if wo[0] != 6 { t.Error("Priority: expecting to be waiting on grapefruits") } l := w.Filter([]int{5, 6}) if len(l) != 1 { t.Error("Priority: bad filter, expecting to be waiting on grapefruits") } if l[0] != 6 { t.Error("Priority: bad filter, expecting to be waiting on grapefruits") } l = w.Filter([]int{1, 2}) if l != nil { t.Error("Priority: bad filter, nil list") } }
func TestMaxLength(t *testing.T) { test := &testTree{} test.add([2]int{0, 0}, []frames.Frame{}, []frames.Frame{tests.TestFrames[0], tests.TestFrames[3], tests.TestFrames[6]}) test.add([2]int{0, 0}, []frames.Frame{}, []frames.Frame{tests.TestFrames[1], tests.TestFrames[3]}) saver := persist.NewLoadSaver(nil) saveTests(saver, []*testTree{test, test}) loader := persist.NewLoadSaver(saver.Bytes()) tests := loadTests(loader) test = tests[1] if maxLength(test.right) != 33 { t.Errorf("maxLength fail: expecting 33 got %v", maxLength(test.right)) } }
func TestProcessFmt363(t *testing.T) { b := New() b.addSignature(tests.TestFmts[363]) saver := persist.NewLoadSaver(nil) b.Save(saver) loader := persist.NewLoadSaver(saver.Bytes()) b = Load(loader) if len(b.keyFrames[0]) != 2 { for _, v := range b.keyFrames[0] { t.Errorf("%s\n", v) } } }
func TestProcess(t *testing.T) { b := New() config.SetDistance(8192)() config.SetRange(2059)() config.SetChoices(9)() for i, v := range tests.TestSignatures { err := b.addSignature(v) if err != nil { t.Errorf("Unexpected error adding persist; sig %v; error %v", i, v) } } saver := persist.NewLoadSaver(nil) b.Save(saver) loader := persist.NewLoadSaver(saver.Bytes()) b = Load(loader) if len(b.keyFrames) != 6 { t.Errorf("Expecting 6 keyframe slices, got %d", len(b.keyFrames)) } var tl int for _, v := range b.keyFrames { tl += len(v) } if tl != 12 { t.Errorf("Expecting a total of 12 keyframes, got %d", tl) } if len(b.tests) != 9 { t.Errorf("Expecting a total of 9 tests, got %d", len(b.tests)) } if len(b.bofSeq.set) != 4 { t.Errorf("Expecting 4 BOF seqs, got %d", len(b.bofSeq.set)) } e1 := wac.Seq{[]int64{0}, []wac.Choice{wac.Choice{[]byte{'t', 'e', 's', 't'}}}} if !seqEquals(b.bofSeq.set[0], e1) { t.Errorf("Expecting %v to equal %v", b.bofSeq.set[0], e1) } e2 := wac.Seq{[]int64{-1}, []wac.Choice{wac.Choice{[]byte{'t', 'e', 's', 't'}}}} if seqEquals(b.bofSeq.set[0], e2) { t.Errorf("Not expecting %v to equal %v", b.bofSeq.set[0], e2) } if len(b.eofSeq.set) != 2 { t.Errorf("Expecting two EOF seqs, got %d, first is %v", len(b.eofSeq.set), b.eofSeq.set[0]) } if len(b.bofFrames.set) != 1 { t.Errorf("Expecting one BOF Frame, got %d", len(b.bofFrames.set)) } if len(b.eofFrames.set) != 0 { t.Errorf("Expecting no EOF frame, got %d", len(b.eofFrames.set)) } }
func TestProcessFmt418(t *testing.T) { b := New() config.SetDistance(2000)() config.SetRange(500)() config.SetChoices(10)() b.addSignature(tests.TestFmts[418]) saver := persist.NewLoadSaver(nil) b.Save(saver) loader := persist.NewLoadSaver(saver.Bytes()) b = Load(loader) if len(b.keyFrames[0]) != 2 { for _, v := range b.keyFrames[0] { t.Errorf("%s\n", v) } } }
// Save persists a Siegfried struct to disk (path) func (s *Siegfried) Save(path string) error { ls := persist.NewLoadSaver(nil) ls.SaveTime(s.C) s.em.Save(ls) s.cm.Save(ls) s.bm.Save(ls) s.tm.Save(ls) ls.SaveTinyUInt(len(s.ids)) for _, i := range s.ids { i.Save(ls) } if ls.Err != nil { return ls.Err } f, err := os.Create(path) if err != nil { return err } defer f.Close() _, err = f.Write(append(config.Magic(), byte(config.Version()[0]), byte(config.Version()[1]))) if err != nil { return err } z, err := flate.NewWriter(f, 1) if err != nil { return err } _, err = z.Write(ls.Bytes()) z.Close() return err }
func TestIO(t *testing.T) { sm := New() sm.Add(SignatureSet{[]string{".bla"}, []string{".doc"}, []string{".ppt"}}, nil) str := sm.String() saver := persist.NewLoadSaver(nil) sm.Save(saver) if len(saver.Bytes()) < 10 { t.Errorf("Save string matcher: too small, only got %v", saver.Bytes()) } loader := persist.NewLoadSaver(saver.Bytes()) newsm := Load(loader) str2 := newsm.String() if str != str2 { t.Errorf("Load string matcher: expecting first matcher (%v), to equal second matcher (%v)", str, str2) } }
func TestIO(t *testing.T) { bm := New() _, err := bm.Add(SignatureSet(tests.TestSignatures), nil) if err != nil { t.Error(err) } saver := persist.NewLoadSaver(nil) bm.Save(saver) if len(saver.Bytes()) < 100 { t.Errorf("Save bytematcher: too small, only got %v", len(saver.Bytes())) } newbm := Load(persist.NewLoadSaver(saver.Bytes())) nsaver := persist.NewLoadSaver(nil) newbm.Save(nsaver) if len(nsaver.Bytes()) != len(saver.Bytes()) { t.Fatalf("expecting the bms to match length: %d and %d", len(saver.Bytes()), len(nsaver.Bytes())) } if string(nsaver.Bytes()) != string(saver.Bytes()) { t.Errorf("Load bytematcher: expecting first bytematcher (%v), to equal second bytematcher (%v)", bm.String(), newbm.String()) } }
func TestSequence(t *testing.T) { if !TestSequences[0].Equals(TestSequences[1]) { t.Error("Seq fail: Equality") } if r, _ := TestSequences[0].Test([]byte{'t', 'o', 'o', 't'}); r { t.Error("Sequence fail: shouldn't match") } if _, l := TestSequences[2].Test([]byte{'t', 'e', 's', 't', 'y'}); l != 5 { t.Error("Sequence fail: should match") } reverseSeq := TestSequences[2].Reverse() if reverseSeq[1] != 't' || reverseSeq[2] != 's' || reverseSeq[3] != 'e' || reverseSeq[4] != 't' { t.Error("Sequence fail: Reverse") } saver := persist.NewLoadSaver(nil) TestSequences[0].Save(saver) loader := persist.NewLoadSaver(saver.Bytes()) _ = loader.LoadByte() p := loader.LoadBytes() if len(p) != 4 { t.Errorf("expecting %v, got %v", TestSequences[0], string(p)) } }
// Load creates a Siegfried struct and loads content from path func Load(path string) (*Siegfried, error) { errOpening := "siegfried: error opening signature file, got %v; try running `sf -update`" errNotSig := "siegfried: not a siegfried signature file; try running `sf -update`" errUpdateSig := "siegfried: signature file is incompatible with this version of sf; try running `sf -update`" fbuf, err := ioutil.ReadFile(path) if err != nil { return nil, fmt.Errorf(errOpening, err) } if len(fbuf) < len(config.Magic())+2 { return nil, fmt.Errorf(errNotSig) } if string(fbuf[:len(config.Magic())]) != string(config.Magic()) { return nil, fmt.Errorf(errNotSig) } if major, minor := fbuf[len(config.Magic())], fbuf[len(config.Magic())+1]; major < byte(config.Version()[0]) || (major == byte(config.Version()[0]) && minor < byte(config.Version()[1])) { return nil, fmt.Errorf(errUpdateSig) } r := bytes.NewBuffer(fbuf[len(config.Magic())+2:]) rc := flate.NewReader(r) defer rc.Close() buf, err := ioutil.ReadAll(rc) if err != nil { return nil, fmt.Errorf(errOpening, err) } ls := persist.NewLoadSaver(buf) return &Siegfried{ C: ls.LoadTime(), em: stringmatcher.Load(ls), mm: stringmatcher.Load(ls), cm: containermatcher.Load(ls), bm: bytematcher.Load(ls), tm: textmatcher.Load(ls), ids: func() []core.Identifier { ids := make([]core.Identifier, ls.LoadTinyUInt()) for i := range ids { ids[i] = core.LoadIdentifier(ls) } return ids }(), buffers: siegreader.New(), }, ls.Err }
// Load creates a Siegfried struct and loads content from path func Load(path string) (*Siegfried, error) { errOpening := "siegfried: error opening signature file; got %v\nTry running `sf -update`" errNotSig := "siegfried: not a siegfried signature file" fbuf, err := ioutil.ReadFile(path) if err != nil { return nil, fmt.Errorf(errOpening, err) } if string(fbuf[:len(config.Magic())]) != string(config.Magic()) { return nil, fmt.Errorf(errNotSig) } r := bytes.NewBuffer(fbuf[len(config.Magic()):]) rc := flate.NewReader(r) defer rc.Close() buf, err := ioutil.ReadAll(rc) if err != nil { return nil, fmt.Errorf(errOpening, err) } ls := persist.NewLoadSaver(buf) if ls.LoadString() != "siegfried" { return nil, fmt.Errorf(errNotSig) } return &Siegfried{ C: ls.LoadTime(), em: extensionmatcher.Load(ls), cm: containermatcher.Load(ls), bm: bytematcher.Load(ls), tm: textmatcher.Load(ls), ids: func() []core.Identifier { ids := make([]core.Identifier, ls.LoadTinyUInt()) for i := range ids { ids[i] = core.LoadIdentifier(ls) } return ids }(), buffers: siegreader.New(), }, ls.Err }