github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/header/eth_test.go (about) 1 // Copyright 2018 The gVisor Authors. 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 header 16 17 import ( 18 "testing" 19 20 "github.com/SagerNet/gvisor/pkg/tcpip" 21 "github.com/SagerNet/gvisor/pkg/tcpip/testutil" 22 ) 23 24 func TestIsValidUnicastEthernetAddress(t *testing.T) { 25 tests := []struct { 26 name string 27 addr tcpip.LinkAddress 28 expected bool 29 }{ 30 { 31 "Nil", 32 tcpip.LinkAddress([]byte(nil)), 33 false, 34 }, 35 { 36 "Empty", 37 tcpip.LinkAddress(""), 38 false, 39 }, 40 { 41 "InvalidLength", 42 tcpip.LinkAddress("\x01\x02\x03"), 43 false, 44 }, 45 { 46 "Unspecified", 47 unspecifiedEthernetAddress, 48 false, 49 }, 50 { 51 "Multicast", 52 tcpip.LinkAddress("\x01\x02\x03\x04\x05\x06"), 53 false, 54 }, 55 { 56 "Valid", 57 tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06"), 58 true, 59 }, 60 } 61 62 for _, test := range tests { 63 t.Run(test.name, func(t *testing.T) { 64 if got := IsValidUnicastEthernetAddress(test.addr); got != test.expected { 65 t.Fatalf("got IsValidUnicastEthernetAddress = %t, want = %t", got, test.expected) 66 } 67 }) 68 } 69 } 70 71 func TestIsMulticastEthernetAddress(t *testing.T) { 72 tests := []struct { 73 name string 74 addr tcpip.LinkAddress 75 expected bool 76 }{ 77 { 78 "Nil", 79 tcpip.LinkAddress([]byte(nil)), 80 false, 81 }, 82 { 83 "Empty", 84 tcpip.LinkAddress(""), 85 false, 86 }, 87 { 88 "InvalidLength", 89 tcpip.LinkAddress("\x01\x02\x03"), 90 false, 91 }, 92 { 93 "Unspecified", 94 unspecifiedEthernetAddress, 95 false, 96 }, 97 { 98 "Multicast", 99 tcpip.LinkAddress("\x01\x02\x03\x04\x05\x06"), 100 true, 101 }, 102 { 103 "Unicast", 104 tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06"), 105 false, 106 }, 107 } 108 109 for _, test := range tests { 110 t.Run(test.name, func(t *testing.T) { 111 if got := IsMulticastEthernetAddress(test.addr); got != test.expected { 112 t.Fatalf("got IsMulticastEthernetAddress = %t, want = %t", got, test.expected) 113 } 114 }) 115 } 116 } 117 118 func TestEthernetAddressFromMulticastIPv4Address(t *testing.T) { 119 tests := []struct { 120 name string 121 addr tcpip.Address 122 expectedLinkAddr tcpip.LinkAddress 123 }{ 124 { 125 name: "IPv4 Multicast without 24th bit set", 126 addr: "\xe0\x7e\xdc\xba", 127 expectedLinkAddr: "\x01\x00\x5e\x7e\xdc\xba", 128 }, 129 { 130 name: "IPv4 Multicast with 24th bit set", 131 addr: "\xe0\xfe\xdc\xba", 132 expectedLinkAddr: "\x01\x00\x5e\x7e\xdc\xba", 133 }, 134 } 135 136 for _, test := range tests { 137 t.Run(test.name, func(t *testing.T) { 138 if got := EthernetAddressFromMulticastIPv4Address(test.addr); got != test.expectedLinkAddr { 139 t.Fatalf("got EthernetAddressFromMulticastIPv4Address(%s) = %s, want = %s", test.addr, got, test.expectedLinkAddr) 140 } 141 }) 142 } 143 } 144 145 func TestEthernetAddressFromMulticastIPv6Address(t *testing.T) { 146 addr := testutil.MustParse6("ff02:304:506:708:90a:b0c:d0e:f1a") 147 if got, want := EthernetAddressFromMulticastIPv6Address(addr), tcpip.LinkAddress("\x33\x33\x0d\x0e\x0f\x1a"); got != want { 148 t.Fatalf("got EthernetAddressFromMulticastIPv6Address(%s) = %s, want = %s", addr, got, want) 149 } 150 }