MCPcopy Create free account
hub / github.com/PasarGuard/node / TestUpdateUsersChangesAllowedIP

Function TestUpdateUsersChangesAllowedIP

backend/wireguard/user_sync_test.go:724–778  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

722}
723
724func TestUpdateUsersChangesAllowedIP(t *testing.T) {
725 cfg, err := NewConfig(`{
726 "interface_name":"wg-test",
727 "listen_port":51820,
728 "address":["10.70.0.1/24"]
729 }`)
730 if err != nil {
731 t.Fatalf("failed to create config: %v", err)
732 }
733
734 _, key, _ := GenerateKeyPair()
735
736 ps := NewPeerStore()
737 ps.ReplaceAll([]*PeerInfo{mustPeerInfo("keep@example.com", key, []string{"10.70.0.2/32"})})
738
739 applyCalls := 0
740 wg := &WireGuard{
741 config: cfg,
742 peerStore: ps,
743 statsTracker: stats.New(),
744 manager: &Manager{
745 iFaceName: "wg-test",
746 client: &fakeWGClient{
747 configureDeviceFn: func(interfaceName string, cfg wgtypes.Config) error {
748 applyCalls++
749 if cfg.Peers[0].AllowedIPs[0].String() != "10.70.0.3/32" {
750 t.Fatal("expected new IP in config")
751 }
752 return nil
753 },
754 },
755 },
756 state: lifecycleRunning,
757 }
758
759 users := []*common.User{
760 {
761 Email: "keep@example.com",
762 Inbounds: []string{"wg-test"},
763 Proxies: &common.Proxy{
764 Wireguard: &common.Wireguard{
765 PublicKey: key, PeerIps: []string{"10.70.0.3/32"},
766 },
767 },
768 },
769 }
770
771 if err := wg.UpdateUsers(context.Background(), users); err != nil {
772 t.Fatalf("UpdateUsers failed: %v", err)
773 }
774
775 if applyCalls != 1 {
776 t.Fatalf("expected one ConfigureDevice calls for IP change partial sync, got %d", applyCalls)
777 }
778}
779
780func TestUpdateUsersAndRestartReappliesKeepaliveToAllPeers(t *testing.T) {
781 privateKey, _, err := GenerateKeyPair()

Callers

nothing calls this directly

Calls 8

ReplaceAllMethod · 0.95
UpdateUsersMethod · 0.95
NewFunction · 0.92
GenerateKeyPairFunction · 0.85
NewPeerStoreFunction · 0.85
mustPeerInfoFunction · 0.85
NewConfigFunction · 0.70
StringMethod · 0.45

Tested by

no test coverage detected