github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/wireguard/service_config_test.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package wireguard 19 20 import ( 21 "encoding/json" 22 "net" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestServiceConfig_MarshalJSON(t *testing.T) { 29 endpoint, _ := net.ResolveUDPAddr("udp4", "127.0.0.1:51001") 30 config := ServiceConfig{ 31 LocalPort: 51000, 32 RemotePort: 51001, 33 Provider: struct { 34 PublicKey string 35 Endpoint net.UDPAddr 36 }{ 37 PublicKey: "wg1", 38 Endpoint: *endpoint, 39 }, 40 Consumer: struct { 41 IPAddress net.IPNet 42 DNSIPs string 43 }{ 44 IPAddress: net.IPNet{ 45 IP: net.IPv4(127, 0, 0, 1), 46 Mask: net.IPv4Mask(255, 255, 255, 128), 47 }, 48 DNSIPs: "128.0.0.1", 49 }, 50 } 51 52 configBytes, err := json.Marshal(config) 53 assert.NoError(t, err) 54 assert.Equal(t, 55 `{"local_port":51000,"remote_port":51001,"ports":null,"provider":{"public_key":"wg1","endpoint":"127.0.0.1:51001"},"consumer":{"ip_address":"127.0.0.1/25","dns_ips":"128.0.0.1"}}`, 56 string(configBytes), 57 ) 58 } 59 60 func TestServiceConfig_UnmarshalJSON(t *testing.T) { 61 configJSON := json.RawMessage(`{"local_port":51000,"remote_port":51001,"provider":{"public_key":"wg1","endpoint":"127.0.0.1:51001"},"consumer":{"ip_address":"127.0.0.1/25","dns_ips":"128.0.0.1"}}`) 62 63 endpoint, _ := net.ResolveUDPAddr("udp4", "127.0.0.1:51001") 64 expecteConfig := ServiceConfig{ 65 LocalPort: 51000, 66 RemotePort: 51001, 67 Provider: struct { 68 PublicKey string 69 Endpoint net.UDPAddr 70 }{ 71 PublicKey: "wg1", 72 Endpoint: *endpoint, 73 }, 74 Consumer: struct { 75 IPAddress net.IPNet 76 DNSIPs string 77 }{ 78 IPAddress: net.IPNet{ 79 IP: net.IPv4(127, 0, 0, 1), 80 Mask: net.IPv4Mask(255, 255, 255, 128), 81 }, 82 DNSIPs: "128.0.0.1", 83 }, 84 } 85 86 var actualConfig ServiceConfig 87 err := json.Unmarshal(configJSON, &actualConfig) 88 89 assert.NoError(t, err) 90 assert.Equal(t, expecteConfig, actualConfig) 91 }