github.phpd.cn/cilium/cilium@v1.6.12/pkg/byteorder/byteorder_test.go (about)

     1  // Copyright 2017 Authors of Cilium
     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  // +build !privileged_tests
    16  
    17  package byteorder
    18  
    19  import (
    20  	"net"
    21  	"reflect"
    22  	"testing"
    23  
    24  	. "gopkg.in/check.v1"
    25  )
    26  
    27  // Hook up gocheck into the "go test" runner.
    28  func Test(t *testing.T) {
    29  	TestingT(t)
    30  }
    31  
    32  type ByteorderSuite struct{}
    33  
    34  var _ = Suite(&ByteorderSuite{})
    35  
    36  func (b *ByteorderSuite) TestNativeIsInitialized(c *C) {
    37  	c.Assert(Native, NotNil)
    38  }
    39  
    40  func (b *ByteorderSuite) TestHostToNetworkSlice(c *C) {
    41  	ip := net.ParseIP("b007::aaaa:bbbb:0:0")
    42  	c.Assert(HostToNetworkSlice(ip[14:], reflect.Uint16), Equals, uint16(0))
    43  
    44  	ip = net.ParseIP("b007::aaaa:bbbb:0:0")
    45  	c.Assert(HostToNetworkSlice(ip[8:12], reflect.Uint32), Equals, uint32(0xaaaabbbb))
    46  }
    47  
    48  func (b *ByteorderSuite) TestHostToNetworkPutShort(c *C) {
    49  	ip := net.ParseIP("b007::")
    50  	HostToNetworkPut(ip[12:14], uint16(0xaabb))
    51  	c.Assert(HostToNetworkSlice(ip[12:14], reflect.Uint16), Equals, uint16(0xaabb))
    52  }
    53  
    54  func (b *ByteorderSuite) TestHostToNetwork(c *C) {
    55  	c.Assert(HostToNetwork(uint16(0xAABB)), Equals, uint16(0xBBAA),
    56  		Commentf("TestHostToNetwork failed: HostToNetwork(0xAABB) != 0xBBAA"))
    57  
    58  	c.Assert(HostToNetwork(uint32(0xAABBCCDD)), Equals, uint32(0xDDCCBBAA),
    59  		Commentf("TestHostToNetwork failed: HostToNetwork(0xAABBCCDD) != 0xDDCCBBAA"))
    60  }
    61  
    62  func (b *ByteorderSuite) TestHostSliceToNetworkU32(c *C) {
    63  	c.Assert(uint32(0x5b810b0a), Equals, HostSliceToNetwork(net.ParseIP("10.11.129.91"), reflect.Uint32).(uint32))
    64  	c.Assert(uint32(0xd68a0b0a), Equals, HostSliceToNetwork(net.ParseIP("10.11.138.214"), reflect.Uint32).(uint32))
    65  }