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

     1  /*
     2   * Copyright (C) 2018 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 openvpn
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/mysteriumnetwork/node/core/connection/connectionstate"
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/mysteriumnetwork/go-openvpn/openvpn"
    28  )
    29  
    30  func TestGetStateCallbackReturnsCorrectState(t *testing.T) {
    31  	channel := make(chan connectionstate.State, 1)
    32  	callback := getStateCallback(channel)
    33  	callback(openvpn.ConnectedState)
    34  	assert.Equal(t, connectionstate.Connected, <-channel)
    35  }
    36  
    37  func TestGetStateCallbackClosesChannelOnProcessExit(t *testing.T) {
    38  	channel := make(chan connectionstate.State, 1)
    39  	callback := getStateCallback(channel)
    40  	callback(openvpn.ExitingState)
    41  	res, ok := <-channel
    42  	assert.Equal(t, connectionstate.Disconnecting, res)
    43  	assert.True(t, ok)
    44  }
    45  
    46  func TestOpenVpnStateCallbackToConnectionState(t *testing.T) {
    47  	type args struct {
    48  		input openvpn.State
    49  	}
    50  	tests := []struct {
    51  		name string
    52  		args args
    53  		want connectionstate.State
    54  	}{
    55  		{
    56  			name: "Maps openvpn.connectedState to connection.Connected",
    57  			args: args{
    58  				input: openvpn.ConnectedState,
    59  			},
    60  			want: connectionstate.Connected,
    61  		},
    62  		{
    63  			name: "Maps openvpn.exitingState to connection.Disconnecting",
    64  			args: args{
    65  				input: openvpn.ExitingState,
    66  			},
    67  			want: connectionstate.Disconnecting,
    68  		},
    69  		{
    70  			name: "Maps openvpn.reconnectingState to connection.Reconnecting",
    71  			args: args{
    72  				input: openvpn.ReconnectingState,
    73  			},
    74  			want: connectionstate.Reconnecting,
    75  		},
    76  		{
    77  			name: "Maps openvpn.getConfigState to connection.Unknown",
    78  			args: args{
    79  				input: openvpn.GetConfigState,
    80  			},
    81  			want: connectionstate.Unknown,
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			if got := openVpnStateCallbackToConnectionState(tt.args.input); !reflect.DeepEqual(got, tt.want) {
    87  				t.Errorf("OpenVpnStateCallbackToConnectionState() = %v, want %v", got, tt.want)
    88  			}
    89  		})
    90  	}
    91  }