Skip to content

Commit 20f5f00

Browse files
committed
[client] Add unit tests for engine synchronization and Info flag copying
- Introduced tests for the Engine's handleSync method to verify behavior when SkipNetworkMapUpdate is true and when NetworkMap is nil. - Added a test for the Info struct to ensure correct copying of flag values from one instance to another, while preserving unrelated fields.
1 parent fc141cf commit 20f5f00

File tree

3 files changed

+168
-4
lines changed

3 files changed

+168
-4
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
8+
9+
"github.com/netbirdio/netbird/client/iface"
10+
"github.com/netbirdio/netbird/client/internal/peer"
11+
"github.com/netbirdio/netbird/shared/management/client"
12+
mgmtProto "github.com/netbirdio/netbird/shared/management/proto"
13+
)
14+
15+
// Ensures handleSync exits early when SkipNetworkMapUpdate is true
16+
func TestEngine_HandleSync_SkipNetworkMapUpdate(t *testing.T) {
17+
key, err := wgtypes.GeneratePrivateKey()
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
22+
ctx, cancel := context.WithCancel(context.Background())
23+
defer cancel()
24+
25+
engine := NewEngine(ctx, cancel, nil, &client.MockClient{}, nil, &EngineConfig{
26+
WgIfaceName: "utun199",
27+
WgAddr: "100.70.0.1/24",
28+
WgPrivateKey: key,
29+
WgPort: 33100,
30+
MTU: iface.DefaultMTU,
31+
}, MobileDependency{}, peer.NewRecorder("https://mgm"), nil)
32+
engine.ctx = ctx
33+
34+
// Precondition
35+
if engine.networkSerial != 0 {
36+
t.Fatalf("unexpected initial serial: %d", engine.networkSerial)
37+
}
38+
39+
resp := &mgmtProto.SyncResponse{
40+
NetworkMap: &mgmtProto.NetworkMap{Serial: 42},
41+
SkipNetworkMapUpdate: true,
42+
}
43+
44+
if err := engine.handleSync(resp); err != nil {
45+
t.Fatalf("handleSync returned error: %v", err)
46+
}
47+
48+
if engine.networkSerial != 0 {
49+
t.Fatalf("networkSerial changed despite SkipNetworkMapUpdate; got %d, want 0", engine.networkSerial)
50+
}
51+
}
52+
53+
// Ensures handleSync exits early when NetworkMap is nil
54+
func TestEngine_HandleSync_NilNetworkMap(t *testing.T) {
55+
key, err := wgtypes.GeneratePrivateKey()
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
ctx, cancel := context.WithCancel(context.Background())
61+
defer cancel()
62+
63+
engine := NewEngine(ctx, cancel, nil, &client.MockClient{}, nil, &EngineConfig{
64+
WgIfaceName: "utun198",
65+
WgAddr: "100.70.0.2/24",
66+
WgPrivateKey: key,
67+
WgPort: 33101,
68+
MTU: iface.DefaultMTU,
69+
}, MobileDependency{}, peer.NewRecorder("https://mgm"), nil)
70+
engine.ctx = ctx
71+
72+
resp := &mgmtProto.SyncResponse{NetworkMap: nil}
73+
74+
if err := engine.handleSync(resp); err != nil {
75+
t.Fatalf("handleSync returned error: %v", err)
76+
}
77+
}
78+
79+

client/system/info_test.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,72 @@
11
package system
22

33
import (
4-
"context"
5-
"testing"
4+
"context"
5+
"testing"
66

7-
"github.com/stretchr/testify/assert"
8-
"google.golang.org/grpc/metadata"
7+
"github.com/stretchr/testify/assert"
8+
"google.golang.org/grpc/metadata"
99
)
1010

11+
func TestInfo_CopyFlagsFrom(t *testing.T) {
12+
origin := &Info{}
13+
serverSSHAllowed := true
14+
origin.SetFlags(
15+
true, // RosenpassEnabled
16+
false, // RosenpassPermissive
17+
&serverSSHAllowed,
18+
true, // DisableClientRoutes
19+
false, // DisableServerRoutes
20+
true, // DisableDNS
21+
false, // DisableFirewall
22+
true, // BlockLANAccess
23+
false, // BlockInbound
24+
true, // LazyConnectionEnabled
25+
)
26+
27+
got := &Info{}
28+
got.CopyFlagsFrom(origin)
29+
30+
if got.RosenpassEnabled != true {
31+
t.Fatalf("RosenpassEnabled not copied: got %v", got.RosenpassEnabled)
32+
}
33+
if got.RosenpassPermissive != false {
34+
t.Fatalf("RosenpassPermissive not copied: got %v", got.RosenpassPermissive)
35+
}
36+
if got.ServerSSHAllowed != true {
37+
t.Fatalf("ServerSSHAllowed not copied: got %v", got.ServerSSHAllowed)
38+
}
39+
if got.DisableClientRoutes != true {
40+
t.Fatalf("DisableClientRoutes not copied: got %v", got.DisableClientRoutes)
41+
}
42+
if got.DisableServerRoutes != false {
43+
t.Fatalf("DisableServerRoutes not copied: got %v", got.DisableServerRoutes)
44+
}
45+
if got.DisableDNS != true {
46+
t.Fatalf("DisableDNS not copied: got %v", got.DisableDNS)
47+
}
48+
if got.DisableFirewall != false {
49+
t.Fatalf("DisableFirewall not copied: got %v", got.DisableFirewall)
50+
}
51+
if got.BlockLANAccess != true {
52+
t.Fatalf("BlockLANAccess not copied: got %v", got.BlockLANAccess)
53+
}
54+
if got.BlockInbound != false {
55+
t.Fatalf("BlockInbound not copied: got %v", got.BlockInbound)
56+
}
57+
if got.LazyConnectionEnabled != true {
58+
t.Fatalf("LazyConnectionEnabled not copied: got %v", got.LazyConnectionEnabled)
59+
}
60+
61+
// ensure CopyFlagsFrom does not touch unrelated fields
62+
origin.Hostname = "host-a"
63+
got.Hostname = "host-b"
64+
got.CopyFlagsFrom(origin)
65+
if got.Hostname != "host-b" {
66+
t.Fatalf("CopyFlagsFrom should not overwrite non-flag fields, got Hostname=%q", got.Hostname)
67+
}
68+
}
69+
1170
func Test_LocalWTVersion(t *testing.T) {
1271
got := GetInfo(context.TODO())
1372
want := "development"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package client
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGrpcClient_LastNetworkMapSerial_SetGet(t *testing.T) {
8+
c := &GrpcClient{}
9+
10+
if got := c.getLastNetworkMapSerial(); got != 0 {
11+
t.Fatalf("initial serial should be 0, got %d", got)
12+
}
13+
14+
c.setLastNetworkMapSerial(123)
15+
if got := c.getLastNetworkMapSerial(); got != 123 {
16+
t.Fatalf("serial after set should be 123, got %d", got)
17+
}
18+
19+
// overwrite should work
20+
c.setLastNetworkMapSerial(5)
21+
if got := c.getLastNetworkMapSerial(); got != 5 {
22+
t.Fatalf("serial after overwrite should be 5, got %d", got)
23+
}
24+
}
25+
26+

0 commit comments

Comments
 (0)