github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/nat/service_ipforward_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 nat 19 20 import ( 21 "testing" 22 23 "github.com/pkg/errors" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 type mockCommand struct { 28 CombinedOutputRes []byte 29 CombinedOutputError error 30 OutputRes []byte 31 OutputError error 32 } 33 34 func (mc *mockCommand) CombinedOutput() ([]byte, error) { 35 return mc.CombinedOutputRes, mc.CombinedOutputError 36 } 37 38 func (mc *mockCommand) Output() ([]byte, error) { 39 return mc.OutputRes, mc.OutputError 40 } 41 42 type mockCommandFactory struct { 43 MockCommand Command 44 } 45 46 func (mcf *mockCommandFactory) Create(name string, arg ...string) Command { 47 return mcf.MockCommand 48 } 49 50 func Test_ServiceIPForward_Enabled(t *testing.T) { 51 mc := &mockCommand{ 52 OutputRes: []byte("1"), 53 } 54 mf := &mockCommandFactory{ 55 MockCommand: mc, 56 } 57 service := &serviceIPForward{ 58 CommandFactory: mf.Create, 59 CommandRead: []string{"doesnt", "matter"}, 60 } 61 62 assert.True(t, service.Enabled()) 63 64 mc.OutputRes = []byte("calm waters") 65 assert.False(t, service.Enabled()) 66 67 mc.OutputError = errors.New("mass panic") 68 mc.OutputRes = []byte("1") 69 assert.True(t, service.Enabled()) 70 } 71 72 func Test_ServiceIPForward_Enable(t *testing.T) { 73 mc := &mockCommand{ 74 CombinedOutputRes: []byte("1"), 75 OutputRes: []byte("1"), 76 } 77 mf := &mockCommandFactory{ 78 MockCommand: mc, 79 } 80 service := &serviceIPForward{ 81 CommandFactory: mf.Create, 82 CommandRead: []string{"doesnt", "matter"}, 83 CommandEnable: []string{"doesnt", "matter"}, 84 } 85 86 assert.Nil(t, service.Enable()) 87 assert.True(t, service.forward) 88 service.forward = false 89 90 mc.OutputRes = []byte("people screaming") 91 mc.CombinedOutputError = errors.New("explosions everywhere") 92 93 assert.Equal(t, mc.CombinedOutputError, service.Enable()) 94 95 mc.CombinedOutputError = nil 96 97 assert.Nil(t, mc.CombinedOutputError, service.Enable()) 98 } 99 100 func Test_ServiceIPForward_Disable(t *testing.T) { 101 mc := &mockCommand{ 102 CombinedOutputRes: []byte("1"), 103 CombinedOutputError: errors.New("explosions everywhere"), 104 } 105 mf := &mockCommandFactory{ 106 MockCommand: mc, 107 } 108 service := &serviceIPForward{ 109 CommandFactory: mf.Create, 110 CommandDisable: []string{"doesnt", "matter"}, 111 } 112 service.Disable() 113 114 service.forward = true 115 service.Disable() 116 }