mirror of
https://github.com/openziti/ziti.git
synced 2026-10-12 15:28:05 +00:00
222 lines
6.7 KiB
Go
222 lines
6.7 KiB
Go
/*
|
|
Copyright NetFoundry, Inc.
|
|
|
|
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
|
|
|
|
https://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 network
|
|
|
|
import (
|
|
"github.com/openziti/fabric/controller/db"
|
|
"github.com/openziti/fabric/controller/models"
|
|
"github.com/openziti/foundation/channel2"
|
|
"github.com/openziti/foundation/common"
|
|
"github.com/openziti/foundation/identity/identity"
|
|
"github.com/openziti/foundation/transport"
|
|
"github.com/openziti/foundation/transport/tcp"
|
|
"github.com/stretchr/testify/assert"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSimpleCircuit2(t *testing.T) {
|
|
ctx := db.NewTestContext(t)
|
|
defer ctx.Cleanup()
|
|
|
|
nodeId := &identity.TokenId{Token: "test"}
|
|
network, err := NewNetwork(nodeId, nil, ctx.GetDb(), nil, NewVersionProviderTest())
|
|
assert.Nil(t, err)
|
|
|
|
addr := "tcp:0.0.0.0:0"
|
|
transportAddr, err := tcp.AddressParser{}.Parse(addr)
|
|
assert.Nil(t, err)
|
|
|
|
r0 := newRouterForTest("r0", "", transportAddr, nil)
|
|
network.Routers.markConnected(r0)
|
|
|
|
r1 := newRouterForTest("r1", "", transportAddr, nil)
|
|
network.Routers.markConnected(r1)
|
|
|
|
l0 := newLink(&identity.TokenId{Token: "l0"})
|
|
l0.Src = r0
|
|
l0.Dst = r1
|
|
l0.addState(newLinkState(Connected))
|
|
network.linkController.add(l0)
|
|
|
|
circuit, err := network.CreateCircuit(r0, r1)
|
|
assert.NotNil(t, circuit)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 2, len(circuit.Path))
|
|
assert.Equal(t, r0, circuit.Path[0])
|
|
assert.Equal(t, r1, circuit.Path[1])
|
|
assert.Equal(t, 1, len(circuit.Links))
|
|
assert.Equal(t, l0, circuit.Links[0])
|
|
assert.Equal(t, r1, circuit.EgressRouter())
|
|
|
|
s0 := &identity.TokenId{Token: "s0"}
|
|
routeMessages, err := circuit.CreateRouteMessages(s0, addr)
|
|
assert.NotNil(t, routeMessages)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 2, len(routeMessages))
|
|
|
|
// ingress route message
|
|
rm0 := routeMessages[0]
|
|
assert.Equal(t, s0.Token, rm0.SessionId)
|
|
assert.Nil(t, rm0.Egress)
|
|
assert.Equal(t, 2, len(rm0.Forwards))
|
|
assert.Equal(t, circuit.IngressId, rm0.Forwards[0].SrcAddress)
|
|
assert.Equal(t, l0.Id.Token, rm0.Forwards[0].DstAddress)
|
|
assert.Equal(t, l0.Id.Token, rm0.Forwards[1].SrcAddress)
|
|
assert.Equal(t, circuit.IngressId, rm0.Forwards[1].DstAddress)
|
|
|
|
// egress route message
|
|
rm1 := routeMessages[1]
|
|
assert.Equal(t, s0.Token, rm1.SessionId)
|
|
assert.NotNil(t, rm1.Egress)
|
|
assert.Equal(t, circuit.EgressId, rm1.Egress.Address)
|
|
assert.Equal(t, addr, rm1.Egress.Destination)
|
|
assert.Equal(t, circuit.EgressId, rm1.Forwards[0].SrcAddress)
|
|
assert.Equal(t, l0.Id.Token, rm1.Forwards[0].DstAddress)
|
|
assert.Equal(t, l0.Id.Token, rm1.Forwards[1].SrcAddress)
|
|
assert.Equal(t, circuit.EgressId, rm1.Forwards[1].DstAddress)
|
|
}
|
|
|
|
func TestTransitCircuit2(t *testing.T) {
|
|
ctx := db.NewTestContext(t)
|
|
defer ctx.Cleanup()
|
|
|
|
nodeId := &identity.TokenId{Token: "test"}
|
|
network, err := NewNetwork(nodeId, nil, ctx.GetDb(), nil, NewVersionProviderTest())
|
|
assert.Nil(t, err)
|
|
|
|
addr := "tcp:0.0.0.0:0"
|
|
transportAddr, err := tcp.AddressParser{}.Parse(addr)
|
|
assert.Nil(t, err)
|
|
|
|
r0 := newRouterForTest("r0", "", transportAddr, nil)
|
|
network.Routers.markConnected(r0)
|
|
|
|
r1 := newRouterForTest("r1", "", transportAddr, nil)
|
|
network.Routers.markConnected(r1)
|
|
|
|
r2 := newRouterForTest("r2", "", transportAddr, nil)
|
|
network.Routers.markConnected(r2)
|
|
|
|
l0 := newLink(&identity.TokenId{Token: "l0"})
|
|
l0.Src = r0
|
|
l0.Dst = r1
|
|
l0.addState(newLinkState(Connected))
|
|
network.linkController.add(l0)
|
|
|
|
l1 := newLink(&identity.TokenId{Token: "l1"})
|
|
l1.Src = r1
|
|
l1.Dst = r2
|
|
l1.addState(newLinkState(Connected))
|
|
network.linkController.add(l1)
|
|
|
|
circuit, err := network.CreateCircuit(r0, r2)
|
|
assert.NotNil(t, circuit)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 3, len(circuit.Path))
|
|
assert.Equal(t, r0, circuit.Path[0])
|
|
assert.Equal(t, r1, circuit.Path[1])
|
|
assert.Equal(t, r2, circuit.Path[2])
|
|
assert.Equal(t, 2, len(circuit.Links))
|
|
assert.Equal(t, l0, circuit.Links[0])
|
|
assert.Equal(t, l1, circuit.Links[1])
|
|
assert.Equal(t, r2, circuit.EgressRouter())
|
|
|
|
s0 := &identity.TokenId{Token: "s0"}
|
|
routeMessages, err := circuit.CreateRouteMessages(s0, addr)
|
|
assert.NotNil(t, routeMessages)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 3, len(routeMessages))
|
|
|
|
// ingress route message
|
|
rm0 := routeMessages[0]
|
|
assert.Equal(t, s0.Token, rm0.SessionId)
|
|
assert.Nil(t, rm0.Egress)
|
|
assert.Equal(t, 2, len(rm0.Forwards))
|
|
assert.Equal(t, circuit.IngressId, rm0.Forwards[0].SrcAddress)
|
|
assert.Equal(t, l0.Id.Token, rm0.Forwards[0].DstAddress)
|
|
assert.Equal(t, l0.Id.Token, rm0.Forwards[1].SrcAddress)
|
|
assert.Equal(t, circuit.IngressId, rm0.Forwards[1].DstAddress)
|
|
|
|
// transit route message
|
|
rm1 := routeMessages[1]
|
|
assert.Equal(t, s0.Token, rm1.SessionId)
|
|
assert.Nil(t, rm1.Egress)
|
|
assert.Equal(t, 2, len(rm1.Forwards))
|
|
assert.Equal(t, l0.Id.Token, rm1.Forwards[0].SrcAddress)
|
|
assert.Equal(t, l1.Id.Token, rm1.Forwards[0].DstAddress)
|
|
assert.Equal(t, l1.Id.Token, rm1.Forwards[1].SrcAddress)
|
|
assert.Equal(t, l0.Id.Token, rm1.Forwards[1].DstAddress)
|
|
|
|
// egress route message
|
|
rm2 := routeMessages[2]
|
|
assert.Equal(t, s0.Token, rm2.SessionId)
|
|
assert.NotNil(t, rm2.Egress)
|
|
assert.Equal(t, circuit.EgressId, rm2.Egress.Address)
|
|
assert.Equal(t, transportAddr.String(), rm2.Egress.Destination)
|
|
assert.Equal(t, circuit.EgressId, rm2.Forwards[0].SrcAddress)
|
|
assert.Equal(t, l1.Id.Token, rm2.Forwards[0].DstAddress)
|
|
assert.Equal(t, l1.Id.Token, rm2.Forwards[1].SrcAddress)
|
|
assert.Equal(t, circuit.EgressId, rm2.Forwards[1].DstAddress)
|
|
}
|
|
|
|
func newRouterForTest(id string, fingerprint string, advLstnr transport.Address, ctrl channel2.Channel) *Router {
|
|
r := &Router{
|
|
BaseEntity: models.BaseEntity{Id: id},
|
|
Fingerprint: &fingerprint,
|
|
Control: ctrl,
|
|
}
|
|
if advLstnr != nil {
|
|
r.AdvertisedListener = advLstnr.String()
|
|
}
|
|
return r
|
|
}
|
|
|
|
type VersionProviderTest struct {
|
|
}
|
|
|
|
func (v VersionProviderTest) EncoderDecoder() common.VersionEncDec {
|
|
return &common.StdVersionEncDec
|
|
}
|
|
|
|
func (v VersionProviderTest) Version() string {
|
|
return "v0.0.0"
|
|
}
|
|
|
|
func (v VersionProviderTest) BuildDate() string {
|
|
return time.Now().String()
|
|
}
|
|
|
|
func (v VersionProviderTest) Revision() string {
|
|
return ""
|
|
}
|
|
|
|
func (v VersionProviderTest) AsVersionInfo() *common.VersionInfo {
|
|
return &common.VersionInfo{
|
|
Version: v.Version(),
|
|
Revision: v.Revision(),
|
|
BuildDate: v.BuildDate(),
|
|
OS: runtime.GOOS,
|
|
Arch: runtime.GOARCH,
|
|
}
|
|
}
|
|
|
|
func NewVersionProviderTest() common.VersionProvider {
|
|
return &VersionProviderTest{}
|
|
}
|