File: indexfile_test.go

package info (click to toggle)
stenographer 0.0~git20180422.0.73ce5dd-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 19,112 kB
  • sloc: cpp: 2,062; sh: 782; yacc: 257; makefile: 102
file content (141 lines) | stat: -rw-r--r-- 3,948 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package indexfile

import (
	"bytes"
	"encoding/hex"
	"reflect"
	"testing"

	"golang.org/x/net/context"

	"github.com/google/stenographer/base"
	"github.com/google/stenographer/filecache"
)

var ctx = context.Background()

func testIndexFile(t *testing.T, filename string) *IndexFile {
	idx, err := NewIndexFile(filename, filecache.NewCache(10))
	if err != nil {
		t.Fatal(err)
	}
	return idx
}

func TestIPPositions(t *testing.T) {
	idx := testIndexFile(t, "/tmp/stenographer-testdata/IDX0/dhcp")
	defer idx.Close()
	for _, test := range []struct {
		start string
		end   string
		want  base.Positions
	}{
		{"192.168.0.1", "192.168.0.254", base.Positions{1049024, 1049848}},
		{"10.0.0.1", "10.0.0.254", nil},
	} {
		if got, err := idx.IPPositions(ctx, parseIP(test.start), parseIP(test.end)); err != nil {
			t.Fatal(err)
		} else if !reflect.DeepEqual(got, test.want) {
			t.Errorf("wrong IP positions.\nwant: %v\n got: %v\n", test.want, got)
		}
	}
}

func TestMPLSPositions(t *testing.T) {
	idx := testIndexFile(t, "/tmp/stenographer-testdata/IDX0/mpls")
	defer idx.Close()
	for _, test := range []struct {
		label uint32
		want  base.Positions
	}{
		{29, base.Positions{1051144, 1054304, 1054592, 1054736, 1054888, 1055184, 1055328, 1055472, 1055800, 1056824, 1056968}},
		{55, nil},
	} {
		if got, err := idx.MPLSPositions(ctx, test.label); err != nil {
			t.Fatal(err)
		} else if !reflect.DeepEqual(got, test.want) {
			t.Errorf("wrong MPLS positions.\nwant: %v\n got: %v\n", test.want, got)
		}
	}
}

func TestVLANPositions(t *testing.T) {
	idx := testIndexFile(t, "/tmp/stenographer-testdata/IDX0/vlan")
	defer idx.Close()
	for _, test := range []struct {
		id   uint16
		want base.Positions
	}{
		{7, base.Positions{1123648, 1126248, 1178544, 1192552, 1208680}},
		{8, nil},
	} {
		if got, err := idx.VLANPositions(ctx, test.id); err != nil {
			t.Fatal(err)
		} else if !reflect.DeepEqual(got, test.want) {
			t.Errorf("wrong VLAN positions.\nwant: %v\n got: %v\n", test.want, got)
		}
	}
}

func TestProtoPositions(t *testing.T) {
	idx := testIndexFile(t, "/tmp/stenographer-testdata/IDX0/dhcp")
	defer idx.Close()
	for _, test := range []struct {
		proto byte
		want  base.Positions
	}{
		{'\x11', base.Positions{1048624, 1049024, 1049448, 1049848}},
		{'\x12', nil},
	} {
		if got, err := idx.ProtoPositions(ctx, test.proto); err != nil {
			t.Fatal(err)
		} else if !reflect.DeepEqual(got, test.want) {
			t.Errorf("wrong proto positions.\nwant: %v\n got: %v\n", test.want, got)
		}
	}
}

func TestPortPositions(t *testing.T) {
	idx := testIndexFile(t, "/tmp/stenographer-testdata/IDX0/dhcp")
	defer idx.Close()
	for _, test := range []struct {
		port uint16
		want base.Positions
	}{
		{67, base.Positions{1048624, 1049024, 1049448, 1049848}},
		{69, nil},
	} {
		if got, err := idx.PortPositions(ctx, test.port); err != nil {
			t.Fatal(err)
		} else if !reflect.DeepEqual(got, test.want) {
			t.Errorf("wrong proto positions.\nwant: %v\n got: %v\n", test.want, got)
		}
	}
}

func TestDump(t *testing.T) {
	idx := testIndexFile(t, "/tmp/stenographer-testdata/IDX0/dhcp")
	want := "00\n0111\n013a\n"
	var w bytes.Buffer
	start, _ := hex.DecodeString("00")
	end, _ := hex.DecodeString("02")
	idx.Dump(&w, start, end)
	got := w.String()
	if got != want {
		t.Fatalf("invalid dump.\nwant %q\n got: %q\n", want, got)
	}
}