github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/wireguard/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  	"net"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/urfave/cli/v2"
    28  
    29  	"github.com/mysteriumnetwork/node/config"
    30  )
    31  
    32  func Test_ParseJSONOptions_HandlesNil(t *testing.T) {
    33  	configureDefaults()
    34  	options, err := ParseJSONOptions(nil)
    35  
    36  	assert.NoError(t, err)
    37  	assert.Equal(t, DefaultOptions, options)
    38  }
    39  
    40  func Test_ParseJSONOptions_HandlesEmptyRequest(t *testing.T) {
    41  	configureDefaults()
    42  	request := json.RawMessage(`{}`)
    43  	options, err := ParseJSONOptions(&request)
    44  
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, DefaultOptions, options)
    47  }
    48  
    49  func Test_ParseJSONOptions_ValidRequest(t *testing.T) {
    50  	configureDefaults()
    51  	request := json.RawMessage(`{"subnet":"10.10.0.0/16"}`)
    52  	options, err := ParseJSONOptions(&request)
    53  
    54  	assert.NoError(t, err)
    55  	assert.Equal(t, Options{
    56  		Subnet: net.IPNet{
    57  			IP:   net.ParseIP("10.10.0.0").To4(),
    58  			Mask: net.IPv4Mask(255, 255, 0, 0),
    59  		},
    60  	}, options)
    61  }
    62  
    63  func configureDefaults() {
    64  	ctx := emptyContext()
    65  	config.ParseFlagsServiceWireguard(ctx)
    66  }
    67  
    68  func emptyContext() *cli.Context {
    69  	return cli.NewContext(nil, flag.NewFlagSet("", flag.ContinueOnError), nil)
    70  }