예제 #1
0
func TestMatch(t *testing.T) {
	bm := New()
	_, err := bm.Add(SignatureSet(tests.TestSignatures), nil)
	if err != nil {
		t.Error(err)
	}
	bufs := siegreader.New()
	buf, err := bufs.Get(bytes.NewBuffer(TestSample1))
	if err != nil && err != io.EOF {
		t.Error(err)
	}
	res, _ := bm.Identify("", buf)
	results := make([]core.Result, 0)
	for i := range res {
		results = append(results, i)
	}
	if !contains(results, []int{0, 2, 3, 4}) {
		t.Errorf("Missing result, got: %v, expecting:%v\n", results, bm)
	}
	buf, err = bufs.Get(bytes.NewBuffer(TestSample2))
	if err != nil && err != io.EOF {
		t.Error(err)
	}
	res, _ = bm.Identify("", buf)
	results = results[:0]
	for i := range res {
		results = append(results, i)
	}
	if !contains(results, []int{0, 1, 2, 3, 4}) {
		t.Errorf("Missing result, got: %v, expecting:%v\n", results, bm)
	}
}
예제 #2
0
func TestIdentify(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)
	}
	r := bytes.NewBuffer([]byte("012345678"))
	bufs := siegreader.New()
	b, err := bufs.Get(r)
	if err != nil && err != io.EOF {
		t.Fatal(err)
	}
	res, _ := testMatcher.Identify("example.tt", b)
	var collect []core.Result
	for r := range res {
		collect = append(collect, r)
	}
	expect := count * 2
	if len(collect) != expect {
		t.Errorf("Expecting %d results, got %d", expect, len(collect))
		for _, r := range collect {
			t.Error(r.Basis())
		}
	}
}
예제 #3
0
func setup() (chan<- strike, <-chan core.Result) {
	bm := New()
	bm.Add(SignatureSet(tests.TestSignatures), nil)
	bufs := siegreader.New()
	buf, _ := bufs.Get(bytes.NewBuffer(TestSample1))
	res := make(chan core.Result)
	return bm.newScorer(buf, make(chan struct{}), res), res
}
예제 #4
0
func Load(ls *persist.LoadSaver) Matcher {
	ret := make(Matcher, ls.LoadTinyUInt())
	for i := range ret {
		ret[i] = loadCM(ls)
		ret[i].ctype = ctypes[ret[i].conType]
		ret[i].entryBufs = siegreader.New()
	}
	return ret
}
예제 #5
0
func newMscfb() *ContainerMatcher {
	return &ContainerMatcher{
		ctype:      ctypes[1],
		conType:    Mscfb,
		nameCTest:  make(map[string]*cTest),
		priorities: &priority.Set{},
		entryBufs:  siegreader.New(),
	}
}
예제 #6
0
// New creates a new Siegfried struct. It initializes the three matchers.
//
// Example:
//  s := New()
//  p, err := pronom.New() // create a new PRONOM identifier
//  if err != nil {
//  	log.Fatal(err)
//  }
//  err = s.Add(p) // add the identifier to the Siegfried
//  if err != nil {
//  	log.Fatal(err)
//  }
//  err = s.Save("pronom.sig") // save the Siegfried
func New() *Siegfried {
	s := &Siegfried{}
	s.C = time.Now()
	s.em = extensionmatcher.New()
	s.cm = containermatcher.New()
	s.bm = bytematcher.New()
	s.tm = textmatcher.New()
	s.buffers = siegreader.New()
	return s
}
예제 #7
0
func newZip() *ContainerMatcher {
	return &ContainerMatcher{
		ctype:      ctypes[0],
		conType:    Zip,
		nameCTest:  make(map[string]*cTest),
		priorities: &priority.Set{},
		extension:  "zip",
		entryBufs:  siegreader.New(),
	}
}
예제 #8
0
func TestSuite(t *testing.T) {
	ids := 3
	m, _ := new(ids)
	bufs := siegreader.New()
	for _, u := range suite {
		buf, _ := bufs.Get(u.rdr)
		res, _ := m.Identify("", buf)
		var i int
		for r := range res {
			i++
			if r.Index() != i || r.Basis() != u.expect {
				t.Fatalf("Expecting result %d for %s, got %d with %s", i, u.label, r.Index(), r.Basis())
			}
		}
		if i != u.results {
			t.Fatalf("Expecting a total of %d results, got %d", u.results, i)
		}
	}
}
예제 #9
0
// 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
}
예제 #10
0
// 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
}
예제 #11
0
	"github.com/richardlehane/siegfried/pkg/core/persist"
	"github.com/richardlehane/siegfried/pkg/core/priority"
	"github.com/richardlehane/siegfried/pkg/core/siegreader"
)

func testTrigger([]byte) bool {
	return true
}

var testContainerMatcher *ContainerMatcher = &ContainerMatcher{
	ctype:        ctype{testTrigger, newTestReader},
	conType:      0,
	nameCTest:    make(map[string]*cTest),
	priorities:   &priority.Set{},
	startIndexes: []int{0},
	entryBufs:    siegreader.New(),
}

var testMatcher Matcher = []*ContainerMatcher{testContainerMatcher}

var count int

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]}},