コード例 #1
0
func TestMatch(t *testing.T) {
	bm, _, err := Add(nil, 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{{testTrigger, newTestReader}}
	// test adding
	count++
	testMatcher, _, err := Add(Matcher{testContainerMatcher},
		SignatureSet{
			0,
			[][]string{{"one", "two"}, {"one"}},
			[][]frames.Signature{{tests.TestSignatures[3], tests.TestSignatures[4]}, {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) {
	m, _, _ := Add(nil, SignatureSet(tests.TestSignatures), nil)
	bm := m.(*Matcher)
	bufs := siegreader.New()
	buf, _ := bufs.Get(bytes.NewBuffer(TestSample1))
	buf.SizeNow()
	res := make(chan core.Result)
	return bm.scorer(buf, bm.priorities.WaitSet(), make(chan struct{}), res), res
}
コード例 #4
0
ファイル: container.go プロジェクト: richardlehane/siegfried
func newMscfb() *ContainerMatcher {
	return &ContainerMatcher{
		ctype:      ctypes[1],
		conType:    Mscfb,
		nameCTest:  make(map[string]*cTest),
		priorities: &priority.Set{},
		entryBufs:  siegreader.New(),
	}
}
コード例 #5
0
ファイル: container.go プロジェクト: richardlehane/siegfried
func newZip() *ContainerMatcher {
	return &ContainerMatcher{
		ctype:      ctypes[0],
		conType:    Zip,
		nameCTest:  make(map[string]*cTest),
		priorities: &priority.Set{},
		extension:  "zip",
		entryBufs:  siegreader.New(),
	}
}
コード例 #6
0
ファイル: container.go プロジェクト: richardlehane/siegfried
func Load(ls *persist.LoadSaver) core.Matcher {
	if !ls.LoadBool() {
		return nil
	}
	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
}
コード例 #7
0
func identifyString(m Matcher, s string) ([]core.Result, error) {
	rdr := strings.NewReader(s)
	bufs := siegreader.New()
	buf, _ := bufs.Get(rdr)
	res, err := m.Identify("", buf)
	if err != nil {
		return nil, err
	}
	ret := []core.Result{}
	for r := range res {
		ret = append(ret, r)
	}
	return ret, nil
}
コード例 #8
0
ファイル: siegfried.go プロジェクト: richardlehane/siegfried
// 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{
		path: filepath.Base(path),
		C:    ls.LoadTime(),
		nm:   namematcher.Load(ls),
		mm:   mimematcher.Load(ls),
		cm:   containermatcher.Load(ls),
		xm:   xmlmatcher.Load(ls),
		rm:   riffmatcher.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
}
コード例 #9
0
func TestMatch(t *testing.T) {
	f, err := os.Open(filepath.Join(*testdata, "benchmark", "Benchmark.wav"))
	if err != nil {
		t.Fatal(err)
	}
	bufs := siegreader.New()
	b, _ := bufs.Get(f)
	res, err := rm.Identify("", b)
	if err != nil {
		t.Fatal(err)
	}
	var hits []int
	for h := range res {
		hits = append(hits, h.Index())
	}
	if len(hits) != len(fmts) {
		t.Fatalf("Expecting %d hits, got %d", len(fmts), len(hits))
	}
}
コード例 #10
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)
		}
	}
}
コード例 #11
0
	"github.com/richardlehane/siegfried/internal/persist"
	"github.com/richardlehane/siegfried/internal/priority"
	"github.com/richardlehane/siegfried/internal/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 count int

func TestMatcher(t *testing.T) {
	ctypes = []ctype{{testTrigger, newTestReader}}
	// test adding
	count++
	testMatcher, _, err := Add(Matcher{testContainerMatcher},
		SignatureSet{
			0,
			[][]string{{"one", "two"}, {"one"}},
			[][]frames.Signature{{tests.TestSignatures[3], tests.TestSignatures[4]}, {tests.TestSignatures[2]}},
		},
		nil,
コード例 #12
0
ファイル: siegfried.go プロジェクト: richardlehane/siegfried
// 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 {
	return &Siegfried{
		C:       time.Now(),
		buffers: siegreader.New(),
	}
}