github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/netutil/subnet_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 netutil
    19  
    20  import (
    21  	"net"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestFirstIP(t *testing.T) {
    28  	tests := []struct {
    29  		name string
    30  		args net.IPNet
    31  		want net.IP
    32  	}{
    33  		{
    34  			name: "Normal case",
    35  			args: net.IPNet{IP: net.ParseIP("1.2.3.4").To4(), Mask: net.IPv4Mask(255, 255, 255, 0)},
    36  			want: net.ParseIP("1.2.3.1").To4(),
    37  		},
    38  		{
    39  			name: "Normal case non /24",
    40  			args: net.IPNet{IP: net.ParseIP("1.2.3.218").To4(), Mask: net.IPv4Mask(255, 255, 255, 128)},
    41  			want: net.ParseIP("1.2.3.129").To4(),
    42  		},
    43  		{
    44  			name: "Normal case /32",
    45  			args: net.IPNet{IP: net.ParseIP("1.2.3.218").To4(), Mask: net.IPv4Mask(255, 255, 255, 255)},
    46  			want: net.ParseIP("1.2.3.218").To4(),
    47  		},
    48  		{
    49  			name: "Empty subnet not panic",
    50  			args: net.IPNet{},
    51  			want: net.IP{},
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			assert.Equal(t, tt.want, FirstIP(tt.args))
    57  		})
    58  	}
    59  }