-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the bug #6022 #6075
Fix the bug #6022 #6075
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright (C) 2024 The "MysteriumNetwork/node" Authors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package netstack_provider | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/netip" | ||
"strings" | ||
"testing" | ||
|
||
"golang.zx2c4.com/wireguard/conn" | ||
"golang.zx2c4.com/wireguard/device" | ||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||
|
||
"github.com/mysteriumnetwork/node/config" | ||
netstack "github.com/mysteriumnetwork/node/services/wireguard/endpoint/netstack" | ||
) | ||
|
||
func startClient(t *testing.T, priv, pubServ wgtypes.Key) { | ||
|
||
tun, tnet, err := netstack.CreateNetTUN( | ||
[]netip.Addr{netip.MustParseAddr("192.168.4.100")}, | ||
[]netip.Addr{netip.MustParseAddr("8.8.8.8")}, | ||
device.DefaultMTU) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelError, "C> ")) | ||
|
||
wgConf := &bytes.Buffer{} | ||
wgConf.WriteString("private_key=" + hex.EncodeToString(priv[:]) + "\n") | ||
wgConf.WriteString("public_key=" + hex.EncodeToString(pubServ[:]) + "\n") | ||
wgConf.WriteString("allowed_ip=0.0.0.0/0\n") | ||
wgConf.WriteString("endpoint=127.0.0.1:58120\n") | ||
|
||
if err = dev.IpcSetOperation(wgConf); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if err = dev.Up(); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: tnet.DialContext, | ||
}, | ||
} | ||
|
||
resp, err := client.Get("http://107.173.23.19:8080/test") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
log.Println("Reply:", string(body)) | ||
|
||
res := strings.HasPrefix(string(body), "Hello,") | ||
ok := "success" | ||
if !res { | ||
ok = "failed" | ||
} | ||
log.Println("Test result:", ok) | ||
// dev.Down() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be cleaned. |
||
// tun.Close() | ||
} | ||
|
||
func startServer(t *testing.T, privKey, pubClinet wgtypes.Key) { | ||
tun, _, _, err := CreateNetTUNWithStack( | ||
[]netip.Addr{netip.MustParseAddr("192.168.4.1")}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a static IP, or it can be anything? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 192.168.4.1 - wireguard "server" in gvisor net |
||
53, | ||
device.DefaultMTU, | ||
) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelError, "S> ")) | ||
|
||
wgConf := &bytes.Buffer{} | ||
wgConf.WriteString("private_key=" + hex.EncodeToString(privKey[:]) + "\n") | ||
wgConf.WriteString("listen_port=58120\n") | ||
wgConf.WriteString("public_key=" + hex.EncodeToString(pubClinet[:]) + "\n") | ||
wgConf.WriteString("allowed_ip=0.0.0.0/0\n") | ||
|
||
if err = dev.IpcSetOperation(wgConf); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if err = dev.Up(); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
} | ||
|
||
func TestShaperEnabled(t *testing.T) { | ||
log.Default().SetFlags(0) | ||
|
||
config.Current.SetDefault(config.FlagShaperBandwidth.Name, "6250") | ||
config.Current.SetDefault(config.FlagShaperEnabled.Name, "true") | ||
InitUserspaceShaper(nil) | ||
|
||
privKey1, err := wgtypes.GeneratePrivateKey() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
privKey2, err := wgtypes.GeneratePrivateKey() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
startServer(t, privKey1, privKey2.PublicKey()) | ||
startClient(t, privKey2, privKey1.PublicKey()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a part of the local environment, we cannot depend on external service in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came to a conclusion that a local address can not be used (for the endpoint), it must be an external address,
b/c we test the path: wireguard client->wireguard server->gvisor tcp/ip stack->external web server.
This test turnes out to be an integrational test by its nature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will try to transform into a integrational test and run using docker compose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soffokl
fixed!