|
| 1 | +// Copyright 2022 CFC4N <cfc4n.cs@gmail.com>. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gotls |
| 16 | + |
| 17 | +import ( |
| 18 | + "net" |
| 19 | + "testing" |
| 20 | +) |
| 21 | + |
| 22 | +func TestIfaceHasAddr_Loopback(t *testing.T) { |
| 23 | + // The loopback interface (lo) should always be present and have an address |
| 24 | + // on Linux. On non-Linux or unusual environments, skip. |
| 25 | + iface, err := net.InterfaceByName("lo") |
| 26 | + if err != nil { |
| 27 | + t.Skip("loopback interface 'lo' not available on this host") |
| 28 | + } |
| 29 | + if iface.Flags&net.FlagUp == 0 { |
| 30 | + t.Skip("loopback interface 'lo' is not UP") |
| 31 | + } |
| 32 | + if !ifaceHasAddr("lo") { |
| 33 | + t.Error("ifaceHasAddr returned false for loopback interface 'lo' which is UP") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestIfaceHasAddr_NonExistent(t *testing.T) { |
| 38 | + // A completely made-up interface name should return false. |
| 39 | + if ifaceHasAddr("nonexistent_iface_xyz") { |
| 40 | + t.Error("ifaceHasAddr returned true for a non-existent interface") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestFirstUpNonLoopbackInterface(t *testing.T) { |
| 45 | + name := firstUpNonLoopbackInterface() |
| 46 | + if name == "" { |
| 47 | + t.Skip("no active non-loopback interface found on this host") |
| 48 | + } |
| 49 | + |
| 50 | + // The returned interface must actually be up and have addresses. |
| 51 | + iface, err := net.InterfaceByName(name) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("firstUpNonLoopbackInterface returned %q but InterfaceByName failed: %v", name, err) |
| 54 | + } |
| 55 | + if iface.Flags&net.FlagUp == 0 { |
| 56 | + t.Errorf("firstUpNonLoopbackInterface returned %q which is not UP", name) |
| 57 | + } |
| 58 | + if iface.Flags&net.FlagLoopback != 0 { |
| 59 | + t.Errorf("firstUpNonLoopbackInterface returned loopback interface %q", name) |
| 60 | + } |
| 61 | + addrs, err := iface.Addrs() |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("cannot get addresses for %q: %v", name, err) |
| 64 | + } |
| 65 | + if len(addrs) == 0 { |
| 66 | + t.Errorf("firstUpNonLoopbackInterface returned %q which has no addresses", name) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestSetDefaultIfname(t *testing.T) { |
| 71 | + // When Ifname is already set, setDefaultIfname should not change it. |
| 72 | + c := NewConfig() |
| 73 | + c.Ifname = "eth99" |
| 74 | + c.setDefaultIfname() |
| 75 | + if c.Ifname != "eth99" { |
| 76 | + t.Errorf("setDefaultIfname changed pre-set Ifname from 'eth99' to %q", c.Ifname) |
| 77 | + } |
| 78 | + |
| 79 | + // When Ifname is empty, setDefaultIfname should set it to something non-empty. |
| 80 | + c2 := NewConfig() |
| 81 | + c2.Ifname = "" |
| 82 | + c2.setDefaultIfname() |
| 83 | + if c2.Ifname == "" { |
| 84 | + t.Error("setDefaultIfname left Ifname empty") |
| 85 | + } |
| 86 | +} |
0 commit comments