Direct tests

This commit is contained in:
Simeng He
2021-05-13 13:05:28 -04:00
parent 73bb80e42b
commit c56829eff7
2 changed files with 38 additions and 4 deletions

View File

@@ -765,7 +765,7 @@ func (c *Direct) sendMapRequest(ctx context.Context, maxPolls int, cb func(*netm
if pr := resp.PingRequest; pr != nil {
log.Println("Ping Triggered")
CustomPing(pr)
go CustomPing(&resp)
// go answerPing(c.logf, c.httpc, pr)
}
@@ -1220,7 +1220,8 @@ func sleepAsRequested(ctx context.Context, logf logger.Logf, timeoutReset chan<-
// Run the ping suite from this client to another one
// Send the ping results via http to the adminhttp handlers.
func CustomPing(pr *tailcfg.PingRequest) {
log.Println("Custom Ping Triggered")
log.Println(pr)
func CustomPing(mr *tailcfg.MapResponse) bool {
log.Printf("Custom Ping Triggered with %d number of peers\n", len(mr.Peers))
log.Println(mr.PingRequest)
return len(mr.Peers) > 0
}

View File

@@ -103,3 +103,36 @@ func TestNewHostinfo(t *testing.T) {
}
t.Logf("Got: %s", j)
}
func TestPingFromMapResponse(t *testing.T) {
hi := NewHostinfo()
ni := tailcfg.NetInfo{LinkType: "wired"}
hi.NetInfo = &ni
key, err := wgkey.NewPrivate()
if err != nil {
t.Error(err)
}
opts := Options{
ServerURL: "https://example.com",
Hostinfo: hi,
GetMachinePrivateKey: func() (wgkey.Private, error) {
return key, nil
},
}
c, err := NewDirect(opts)
if c == nil || err != nil {
t.Errorf("Direct not created %w", err)
}
peers := []*tailcfg.Node{
{ID: 1},
{ID: 2},
{ID: 3},
}
pingRequest := tailcfg.PingRequest{URL: "localhost:3040", Log: true, PayloadSize: 10}
mr := &tailcfg.MapResponse{Peers: peers, Domain: "DumbTest", PingRequest: &pingRequest}
if !CustomPing(mr) {
t.Errorf("Custom ping failed!\n")
}
t.Log("Successfull ping")
}