Skip to content

Commit

Permalink
add HP registration handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Claude Hähni committed Sep 6, 2019
1 parent 61ef82b commit db4a991
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 3 deletions.
14 changes: 13 additions & 1 deletion go/hidden_path_srv/internal/registration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,29 @@ go_library(

go_test(
name = "go_default_test",
srcs = ["validator_test.go"],
srcs = [
"handler_test.go",
"validator_test.go",
],
embed = [":go_default_library"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/ctrl/ack:go_default_library",
"//go/lib/ctrl/path_mgmt:go_default_library",
"//go/lib/ctrl/seg:go_default_library",
"//go/lib/hiddenpath:go_default_library",
"//go/lib/infra:go_default_library",
"//go/lib/infra/messenger:go_default_library",
"//go/lib/infra/mock_infra:go_default_library",
"//go/lib/infra/modules/seghandler:go_default_library",
"//go/lib/infra/modules/seghandler/mock_seghandler:go_default_library",
"//go/lib/log: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",
"//go/lib/xtest/matchers:go_default_library",
"//go/proto:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
Expand Down
10 changes: 8 additions & 2 deletions go/hidden_path_srv/internal/registration/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (h *hpSegRegHandler) handle(logger log.Logger) (*infra.HandlerResult, error
ctx := h.request.Context()
hpSegReg, ok := h.request.Message.(*path_mgmt.HPSegReg)
if !ok {
logger.Error("[hpSegRegHandler] wrong message type, expected path_mgmt.HPSegReg",
logger.Error("[hpSegRegHandler] Wrong message type, expected path_mgmt.HPSegReg",
"msg", h.request.Message, "type", common.TypeOf(h.request.Message))
return infra.MetricsErrInternal, nil
}
Expand All @@ -83,7 +83,13 @@ func (h *hpSegRegHandler) handle(logger log.Logger) (*infra.HandlerResult, error
logger.Debug("[hpSegRegHandler] Received HPSegRecs", "src",
h.request.Peer, "data", hpSegReg.HPSegRecs)

snetPeer := h.request.Peer.(*snet.Addr)
snetPeer, ok := h.request.Peer.(*snet.Addr)
if !ok {
logger.Error("[hpSegRegHandler] Invalid peer address type, expected *snet.Addr", nil,
"peer", h.request.Peer, "type", common.TypeOf(h.request.Peer))
sendAck(proto.Ack_ErrCode_reject, messenger.AckRejectFailedToParse)
return infra.MetricsErrInvalid, nil
}
peerPath, err := snetPeer.GetPath()
if err != nil {
logger.Error("[hpSegRegHandler] Failed to initialize path", "err", err)
Expand Down
176 changes: 176 additions & 0 deletions go/hidden_path_srv/internal/registration/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright 2019 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 registration_test

import (
"context"
"net"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/hidden_path_srv/internal/registration"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/ack"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/infra/messenger"
"github.com/scionproto/scion/go/lib/infra/mock_infra"
"github.com/scionproto/scion/go/lib/infra/modules/seghandler"
"github.com/scionproto/scion/go/lib/infra/modules/seghandler/mock_seghandler"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/spath"
"github.com/scionproto/scion/go/lib/xtest/matchers"
"github.com/scionproto/scion/go/proto"
)

func TestSegReg(t *testing.T) {
log.Root().SetHandler(log.DiscardHandler())
newTestGraph(t, gomock.NewController(t))
tests := map[string]func(*testing.T, context.Context, infra.Handler, *mocks){
"valid request": func(t *testing.T, ctx context.Context, handler infra.Handler, m *mocks) {
msg := &path_mgmt.HPSegReg{
HPSegRecs: &path_mgmt.HPSegRecs{
GroupId: group.Id.ToMsg(),
Recs: []*seg.Meta{seg110_133},
},
}
peer := &snet.Addr{
Host: addr.NewSVCUDPAppAddr(addr.SvcBS),
}
req := infra.NewRequest(ctx, msg, nil, peer, 0)
ack := ack.Ack{
Err: proto.Ack_ErrCode_ok,
ErrDesc: "",
}
segments := seghandler.Segments{
Segs: msg.HPSegRecs.Recs,
HPGroupID: group.Id,
}
m.verifier.EXPECT().Verify(gomock.Any(), segments, peer)
m.rw.EXPECT().SendAckReply(gomock.Any(), &matchers.AckMsg{Ack: ack})
res := handler.Handle(req)
assert.Equal(t, infra.MetricsResultOk, res)
},
"wrong message type": func(t *testing.T, ctx context.Context,
handler infra.Handler, m *mocks) {

req := infra.NewRequest(ctx, &path_mgmt.HPSegReq{}, nil, nil, 0)
res := handler.Handle(req)
assert.Equal(t, infra.MetricsErrInternal, res)
},
"no messenger": func(t *testing.T, ctx context.Context,
handler infra.Handler, m *mocks) {

req := infra.NewRequest(context.Background(),
&path_mgmt.HPSegReg{}, nil, nil, 0)
res := handler.Handle(req)
assert.Equal(t, infra.MetricsErrInternal, res)
},
"corrupt segment data": func(t *testing.T, ctx context.Context,
handler infra.Handler, m *mocks) {

segment := seg110_133.Segment.ShallowCopy()
segment.RawSData = common.RawBytes("abc")
req := infra.NewRequest(ctx, &path_mgmt.HPSegReg{
HPSegRecs: &path_mgmt.HPSegRecs{
Recs: []*seg.Meta{{
Segment: segment,
}},
},
}, nil, nil, 0)
ack := ack.Ack{
Err: proto.Ack_ErrCode_reject,
ErrDesc: messenger.AckRejectFailedToParse,
}
m.rw.EXPECT().SendAckReply(gomock.Any(), &matchers.AckMsg{Ack: ack})
res := handler.Handle(req)
assert.Equal(t, infra.MetricsErrInvalid, res)
},
"invalid peer address type": func(t *testing.T, ctx context.Context,
handler infra.Handler, m *mocks) {

msg := &path_mgmt.HPSegReg{
HPSegRecs: &path_mgmt.HPSegRecs{
Recs: []*seg.Meta{seg110_133},
},
}
peer := &net.IPNet{}
req := infra.NewRequest(ctx, msg, nil, peer, 0)
ack := ack.Ack{
Err: proto.Ack_ErrCode_reject,
ErrDesc: messenger.AckRejectFailedToParse,
}
m.rw.EXPECT().SendAckReply(gomock.Any(), &matchers.AckMsg{Ack: ack})
res := handler.Handle(req)
assert.Equal(t, infra.MetricsErrInvalid, res)
},
"invalid peer hop field": func(t *testing.T, ctx context.Context,
handler infra.Handler, m *mocks) {

msg := &path_mgmt.HPSegReg{
HPSegRecs: &path_mgmt.HPSegRecs{
Recs: []*seg.Meta{seg110_133},
},
}
peer := &snet.Addr{
Path: &spath.Path{},
}
req := infra.NewRequest(ctx, msg, nil, peer, 0)
ack := ack.Ack{
Err: proto.Ack_ErrCode_reject,
ErrDesc: messenger.AckRejectFailedToParse,
}
m.rw.EXPECT().SendAckReply(gomock.Any(), &matchers.AckMsg{Ack: ack})
res := handler.Handle(req)
assert.Equal(t, infra.MetricsErrInvalid, res)
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mocks := createMocks(ctrl)
ctx := infra.NewContextWithResponseWriter(
context.Background(), mocks.rw)
handler := registration.NewSegRegHandler(
registration.NullValidator,
seghandler.Handler{
Verifier: mocks.verifier,
Storage: mocks.storage,
},
)
test(t, ctx, handler, mocks)
})
}
}

type mocks struct {
verifier *mock_seghandler.MockVerifier
storage *mock_seghandler.MockStorage
rw *mock_infra.MockResponseWriter
}

func createMocks(ctrl *gomock.Controller) *mocks {
return &mocks{
verifier: mock_seghandler.NewMockVerifier(ctrl),
storage: mock_seghandler.NewMockStorage(ctrl),
rw: mock_infra.NewMockResponseWriter(ctrl),
}
}
9 changes: 9 additions & 0 deletions go/hidden_path_srv/internal/registration/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ func checkHiddenSegExtn(s *seg.Meta) bool {
}
return lastASEntry.Exts.HiddenPathSeg.Set
}

// NullValidator validates all input
var NullValidator = nullValidator{}

type nullValidator struct{}

func (v nullValidator) Validate(hpSegReg *path_mgmt.HPSegReg, peer addr.IA) error {
return nil
}

0 comments on commit db4a991

Please sign in to comment.