Skip to content

Commit

Permalink
WIP fixing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
juagargi committed Nov 30, 2024
1 parent 64dcbc1 commit 0dd419d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 4 additions & 2 deletions router/dataplane_hbird.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ func (p *scionPacketProcessor) determinePeerHbird() (processResult, error) {
return processResult{}, err
}

var getTimeNow func() time.Time = time.Now

func (p *scionPacketProcessor) validateHopExpiryHbird() (processResult, error) {
expiration := util.SecsToTime(p.infoField.Timestamp).
Add(path.ExpTimeToDuration(p.hopField.ExpTime))
expired := expiration.Before(time.Now())
expired := expiration.Before(getTimeNow())
if !expired {
return processResult{}, nil
}
Expand All @@ -137,7 +139,7 @@ func (p *scionPacketProcessor) validateHopExpiryHbird() (processResult, error) {
func (p *scionPacketProcessor) validateReservationExpiry() (processResult, error) {
startTime := util.SecsToTime(p.hbirdPath.PathMeta.BaseTS - uint32(p.flyoverField.ResStartTime))
endTime := startTime.Add(time.Duration(p.flyoverField.Duration) * time.Second)
now := time.Now()
now := getTimeNow()
if startTime.Before(now) && now.Before(endTime) {
return processResult{}, nil
}
Expand Down
12 changes: 7 additions & 5 deletions router/dataplane_hbird_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestProcessHbirdPacket(t *testing.T) {

key := []byte("testkey_xxxxxxxx")
sv := []byte("test_secretvalue")
now := time.Now()
now := util.SecsToTime(1000)

testCases := map[string]struct {
mockMsg func(bool) *ipv4.Message
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestProcessHbirdPacket(t *testing.T) {
dpath.HopFields[2].HopField)
ret := toMsg(t, spkt, dpath)
if afterProcessing {
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: topology.EndhostPort}
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: dstUDPPort}
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil

}
Expand Down Expand Up @@ -659,7 +659,7 @@ func TestProcessHbirdPacket(t *testing.T) {
dpath.HopFields[2].HopField.Mac = computeMAC(t, key, dpath.InfoFields[0],
dpath.HopFields[2].HopField)
ret := toMsg(t, spkt, dpath)
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: topology.EndhostPort}
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: dstUDPPort}
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
return ret
}
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestProcessHbirdPacket(t *testing.T) {
spkt.PayloadLen, dpath.InfoFields[0], dpath.HopFields[2], dpath.PathMeta)
ret := toMsg(t, spkt, dpath)
if afterProcessing {
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: topology.EndhostPort}
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: dstUDPPort}
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
}
return ret
Expand Down Expand Up @@ -1340,6 +1340,8 @@ func TestProcessHbirdPacket(t *testing.T) {
},
}

router.SetNowFunction(func() time.Time { return now })

for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -3838,7 +3840,7 @@ func prepHbirdMsg(now time.Time) (*slayers.SCION, *hummingbird.Decoded) {
DstIA: xtest.MustParseIA("4-ff00:0:411"),
SrcIA: xtest.MustParseIA("2-ff00:0:222"),
Path: &hummingbird.Raw{},
PayloadLen: 18,
PayloadLen: 26, // scionudpLayer + len("actualpayloadbytes")
}

dpath := &hummingbird.Decoded{
Expand Down
7 changes: 7 additions & 0 deletions router/export2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package router

import "time"

func SetNowFunction(nowFun func() time.Time) {
getTimeNow = nowFun
}

0 comments on commit 0dd419d

Please sign in to comment.