|
4 | 4 | "context"
|
5 | 5 | "net/netip"
|
6 | 6 | "testing"
|
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/google/uuid"
|
9 | 10 | "github.com/stretchr/testify/assert"
|
@@ -412,6 +413,63 @@ parentLoop:
|
412 | 413 | require.True(t, client2.AwaitReachable(awaitReachableCtx4, ip))
|
413 | 414 | }
|
414 | 415 |
|
| 416 | +func TestConn_BlockEndpoints(t *testing.T) { |
| 417 | + t.Parallel() |
| 418 | + logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug) |
| 419 | + |
| 420 | + derpMap, _ := tailnettest.RunDERPAndSTUN(t) |
| 421 | + |
| 422 | + // Setup conn 1. |
| 423 | + ip1 := tailnet.IP() |
| 424 | + conn1, err := tailnet.NewConn(&tailnet.Options{ |
| 425 | + Addresses: []netip.Prefix{netip.PrefixFrom(ip1, 128)}, |
| 426 | + Logger: logger.Named("w1"), |
| 427 | + DERPMap: derpMap, |
| 428 | + BlockEndpoints: true, |
| 429 | + }) |
| 430 | + require.NoError(t, err) |
| 431 | + defer func() { |
| 432 | + err := conn1.Close() |
| 433 | + assert.NoError(t, err) |
| 434 | + }() |
| 435 | + |
| 436 | + // Setup conn 2. |
| 437 | + ip2 := tailnet.IP() |
| 438 | + conn2, err := tailnet.NewConn(&tailnet.Options{ |
| 439 | + Addresses: []netip.Prefix{netip.PrefixFrom(ip2, 128)}, |
| 440 | + Logger: logger.Named("w2"), |
| 441 | + DERPMap: derpMap, |
| 442 | + BlockEndpoints: true, |
| 443 | + }) |
| 444 | + require.NoError(t, err) |
| 445 | + defer func() { |
| 446 | + err := conn2.Close() |
| 447 | + assert.NoError(t, err) |
| 448 | + }() |
| 449 | + |
| 450 | + // Connect them together and wait for them to be reachable. |
| 451 | + stitch(t, conn2, conn1) |
| 452 | + stitch(t, conn1, conn2) |
| 453 | + awaitReachableCtx, awaitReachableCancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 454 | + defer awaitReachableCancel() |
| 455 | + require.True(t, conn1.AwaitReachable(awaitReachableCtx, ip2)) |
| 456 | + |
| 457 | + // Wait 10s for endpoints to potentially be sent over Disco. There's no way |
| 458 | + // to force Disco to send endpoints immediately. |
| 459 | + time.Sleep(10 * time.Second) |
| 460 | + |
| 461 | + // Double check that both peers don't have endpoints for the other peer |
| 462 | + // according to magicsock. |
| 463 | + conn1Status, ok := conn1.Status().Peer[conn2.Node().Key] |
| 464 | + require.True(t, ok) |
| 465 | + require.Empty(t, conn1Status.Addrs) |
| 466 | + require.Empty(t, conn1Status.CurAddr) |
| 467 | + conn2Status, ok := conn2.Status().Peer[conn1.Node().Key] |
| 468 | + require.True(t, ok) |
| 469 | + require.Empty(t, conn2Status.Addrs) |
| 470 | + require.Empty(t, conn2Status.CurAddr) |
| 471 | +} |
| 472 | + |
415 | 473 | // stitch sends node updates from src Conn as peer updates to dst Conn. Sort of
|
416 | 474 | // like the Coordinator would, but without actually needing a Coordinator.
|
417 | 475 | func stitch(t *testing.T, dst, src *tailnet.Conn) {
|
|
0 commit comments