github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/openvpn/service/options_test.go (about)

     1  /*
     2   * Copyright (C) 2019 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 service
    19  
    20  import (
    21  	"encoding/json"
    22  	"flag"
    23  	"testing"
    24  
    25  	"github.com/mysteriumnetwork/node/config"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/urfave/cli/v2"
    28  )
    29  
    30  var DefaultOptionsOpenvpn = Options{
    31  	Protocol: config.FlagOpenvpnProtocol.Value,
    32  	Port:     config.FlagOpenvpnPort.Value,
    33  	Subnet:   config.FlagOpenvpnSubnet.Value,
    34  	Netmask:  config.FlagOpenvpnNetmask.Value,
    35  }
    36  
    37  func Test_ParseJSONOptions_HandlesNil(t *testing.T) {
    38  	configureDefaults()
    39  	options, err := ParseJSONOptions(nil)
    40  
    41  	assert.NoError(t, err)
    42  	assert.Equal(t, DefaultOptionsOpenvpn, options)
    43  }
    44  
    45  func Test_ParseJSONOptions_HandlesEmptyRequest(t *testing.T) {
    46  	configureDefaults()
    47  	request := json.RawMessage(`{}`)
    48  	options, err := ParseJSONOptions(&request)
    49  
    50  	assert.NoError(t, err)
    51  	assert.Equal(t, DefaultOptionsOpenvpn, options)
    52  }
    53  
    54  func Test_ParseJSONOptions_ValidRequest(t *testing.T) {
    55  	configureDefaults()
    56  	request := json.RawMessage(`{"port": 1123, "protocol": "udp", "subnet": "10.10.10.0", "netmask": "255.255.255.0"}`)
    57  	options, err := ParseJSONOptions(&request)
    58  
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, Options{
    61  		Protocol: "udp",
    62  		Port:     1123,
    63  		Subnet:   "10.10.10.0",
    64  		Netmask:  "255.255.255.0",
    65  	}, options)
    66  }
    67  
    68  func configureDefaults() {
    69  	ctx := emptyContext()
    70  	config.ParseFlagsServiceOpenvpn(ctx)
    71  }
    72  
    73  func emptyContext() *cli.Context {
    74  	return cli.NewContext(nil, flag.NewFlagSet("", flag.ContinueOnError), nil)
    75  }