Skip to content

Commit

Permalink
Add test for combinator.filterDuplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
matzf committed Nov 25, 2020
1 parent 706c558 commit 2bc3fde
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 8 deletions.
2 changes: 2 additions & 0 deletions go/lib/infra/modules/combinator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_test(
srcs = [
"combinator_test.go",
"expiry_test.go",
"export_test.go",
"staticinfo_accumulator_test.go",
],
data = glob(["testdata/**"]),
Expand All @@ -40,6 +41,7 @@ go_test(
"//go/lib/slayers/path:go_default_library",
"//go/lib/slayers/path/scion:go_default_library",
"//go/lib/snet:go_default_library",
"//go/lib/spath:go_default_library",
"//go/lib/xtest:go_default_library",
"//go/lib/xtest/graph:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
Expand Down
147 changes: 139 additions & 8 deletions go/lib/infra/modules/combinator/combinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package combinator
package combinator_test

import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"io"
"io/ioutil"
"testing"
"time"

"github.com/golang/mock/gomock"
. "github.com/smartystreets/goconvey/convey"
Expand All @@ -29,8 +31,11 @@ import (
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/infra/modules/combinator"
"github.com/scionproto/scion/go/lib/slayers/path"
"github.com/scionproto/scion/go/lib/slayers/path/scion"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/spath"
"github.com/scionproto/scion/go/lib/xtest"
"github.com/scionproto/scion/go/lib/xtest/graph"
)
Expand Down Expand Up @@ -80,7 +85,7 @@ func TestBadPeering(t *testing.T) {
Convey("main", t, func() {
for _, tc := range testCases {
Convey(tc.Name, func() {
result := Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
result := combinator.Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
txtResult := writePaths(result)
if *update {
err := ioutil.WriteFile(xtest.ExpandPath(tc.FileName), txtResult.Bytes(), 0644)
Expand Down Expand Up @@ -127,7 +132,7 @@ func TestMultiPeering(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
result := Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
result := combinator.Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
txtResult := writePaths(result)
if *update {
err := ioutil.WriteFile(xtest.ExpandPath(tc.FileName), txtResult.Bytes(), 0644)
Expand Down Expand Up @@ -171,7 +176,7 @@ func TestSameCoreParent(t *testing.T) {
Convey("main", t, func() {
for _, tc := range testCases {
Convey(tc.Name, func() {
result := Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
result := combinator.Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
txtResult := writePaths(result)
if *update {
err := ioutil.WriteFile(xtest.ExpandPath(tc.FileName), txtResult.Bytes(), 0644)
Expand Down Expand Up @@ -223,7 +228,7 @@ func TestLoops(t *testing.T) {
Convey("TestLoops", t, func() {
for _, tc := range testCases {
Convey(tc.Name, func() {
result := Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
result := combinator.Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
txtResult := writePaths(result)
if *update {
err := ioutil.WriteFile(xtest.ExpandPath(tc.FileName), txtResult.Bytes(), 0644)
Expand Down Expand Up @@ -532,7 +537,7 @@ func TestComputePath(t *testing.T) {
Convey("main", t, func() {
for _, tc := range testCases {
Convey(tc.Name, func() {
result := Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
result := combinator.Combine(tc.SrcIA, tc.DstIA, tc.Ups, tc.Cores, tc.Downs, false)
txtResult := writePaths(result)
if *update {
err := ioutil.WriteFile(xtest.ExpandPath(tc.FileName), txtResult.Bytes(), 0644)
Expand All @@ -545,8 +550,134 @@ func TestComputePath(t *testing.T) {
}
})
}
func TestFilterDuplicates(t *testing.T) {
// Define three different path interface sequences for the test cases below.
// These look somewhat valid, but that doesn't matter at all -- we only look
// at the fingerprint anyway.
path0 := []snet.PathInterface{
{IA: xtest.MustParseIA("1-ff00:0:110"), ID: common.IFIDType(10)},
{IA: xtest.MustParseIA("1-ff00:0:111"), ID: common.IFIDType(10)},
}
path1 := []snet.PathInterface{
{IA: xtest.MustParseIA("1-ff00:0:110"), ID: common.IFIDType(11)},
{IA: xtest.MustParseIA("1-ff00:0:112"), ID: common.IFIDType(11)},
{IA: xtest.MustParseIA("1-ff00:0:112"), ID: common.IFIDType(12)},
{IA: xtest.MustParseIA("1-ff00:0:111"), ID: common.IFIDType(12)},
}
path2 := []snet.PathInterface{
{IA: xtest.MustParseIA("1-ff00:0:110"), ID: common.IFIDType(11)},
{IA: xtest.MustParseIA("1-ff00:0:112"), ID: common.IFIDType(11)},
{IA: xtest.MustParseIA("1-ff00:0:112"), ID: common.IFIDType(22)},
{IA: xtest.MustParseIA("1-ff00:0:111"), ID: common.IFIDType(22)},
}

// Define two expiry times for the paths: paths with latest expiry will be kept
timeEarly := time.Time{}
timeLater := timeEarly.Add(time.Hour) // just later than timeEarly

testPath := func(id uint32, interfaces []snet.PathInterface, expiry time.Time) combinator.Path {
idBuf := make([]byte, 4)
binary.LittleEndian.PutUint32(idBuf, id)
return combinator.Path{
// hide an id in the (otherwise unused) raw path
SPath: spath.Path{Raw: idBuf},
Metadata: snet.PathMetadata{
Interfaces: interfaces,
Expiry: expiry,
},
}
}

testCases := []struct {
Name string
Paths []combinator.Path
Expected []uint32
}{
{
Name: "single path",
Paths: []combinator.Path{
testPath(1, path0, timeEarly),
},
Expected: []uint32{1},
},
{
Name: "different paths",
Paths: []combinator.Path{
testPath(1, path0, timeEarly),
testPath(2, path1, timeEarly),
},
Expected: []uint32{1, 2},
},
{
Name: "triple",
Paths: []combinator.Path{
testPath(1, path0, timeEarly),
testPath(2, path0, timeLater),
testPath(3, path0, timeEarly),
},
Expected: []uint32{2},
},
{
Name: "triple, same expiry",
Paths: []combinator.Path{
testPath(1, path0, timeEarly),
testPath(2, path0, timeLater),
testPath(3, path0, timeLater),
},
Expected: []uint32{2},
},
{
Name: "triple and double",
Paths: []combinator.Path{
testPath(1, path0, timeEarly),
testPath(2, path0, timeLater),
testPath(3, path0, timeEarly),
testPath(5, path1, timeLater),
testPath(6, path1, timeEarly),
},
Expected: []uint32{2, 5},
},
{
Name: "triple, double, single",
Paths: []combinator.Path{
testPath(1, path0, timeEarly),
testPath(2, path0, timeLater),
testPath(3, path0, timeEarly),
testPath(5, path1, timeLater),
testPath(6, path1, timeEarly),
testPath(7, path2, timeEarly),
},
Expected: []uint32{2, 5, 7},
},
{
Name: "triple, double, single, mixed",
Paths: []combinator.Path{
testPath(1, path1, timeEarly),
testPath(2, path2, timeEarly),
testPath(3, path0, timeEarly),
testPath(4, path0, timeLater),
testPath(5, path1, timeLater),
testPath(6, path0, timeEarly),
},
Expected: []uint32{2, 4, 5},
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {

filtered := combinator.FilterDuplicates(tc.Paths)
// extract IDs hidden in the raw paths:
filteredIds := make([]uint32, len(filtered))
for i, path := range filtered {
filteredIds[i] = binary.LittleEndian.Uint32(path.SPath.Raw)
}
assert.Equal(t, tc.Expected, filteredIds)
})
}
}

func writePaths(paths []Path) *bytes.Buffer {
func writePaths(paths []combinator.Path) *bytes.Buffer {
buffer := &bytes.Buffer{}
for i, p := range paths {
fmt.Fprintf(buffer, "Path #%d:\n", i)
Expand All @@ -555,7 +686,7 @@ func writePaths(paths []Path) *bytes.Buffer {
return buffer
}

func writeTestString(p Path, w io.Writer) {
func writeTestString(p combinator.Path, w io.Writer) {
fmt.Fprintf(w, " Weight: %d\n", p.Weight)

sp := scion.Decoded{}
Expand Down
19 changes: 19 additions & 0 deletions go/lib/infra/modules/combinator/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 ETH Zurich
//
// 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 combinator

var (
FilterDuplicates = filterDuplicates
)

0 comments on commit 2bc3fde

Please sign in to comment.