-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Claude Hähni
committed
Sep 6, 2019
1 parent
61ef82b
commit db4a991
Showing
4 changed files
with
206 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
go/hidden_path_srv/internal/registration/handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters