github.com/kubearmor/cilium@v1.6.12/pkg/mac/mac_test.go (about)

     1  // Copyright 2016-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 mac
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  // Hook up gocheck into the "go test" runner.
    29  func Test(t *testing.T) {
    30  	TestingT(t)
    31  }
    32  
    33  type MACSuite struct{}
    34  
    35  var _ = Suite(&MACSuite{})
    36  
    37  func (s *MACSuite) TestUint64(c *C) {
    38  	m := MAC([]byte{0x11, 0x12, 0x23, 0x34, 0x45, 0x56})
    39  	v, err := m.Uint64()
    40  	c.Assert(err, IsNil)
    41  	c.Assert(v, Equals, uint64(0x564534231211))
    42  }
    43  
    44  func (s *MACSuite) TestUnmarshalJSON(c *C) {
    45  	m := MAC([]byte{0x11, 0x12, 0x23, 0x34, 0x45, 0x56})
    46  	w := MAC([]byte{0x11, 0x12, 0x23, 0x34, 0x45, 0xAB})
    47  	d, err := json.Marshal(m)
    48  	c.Assert(err, IsNil)
    49  	c.Assert(d, checker.DeepEquals, []byte(`"11:12:23:34:45:56"`))
    50  	var t MAC
    51  	err = json.Unmarshal([]byte(`"11:12:23:34:45:AB"`), &t)
    52  	c.Assert(err, IsNil)
    53  	c.Assert(t, checker.DeepEquals, w)
    54  	err = json.Unmarshal([]byte(`"11:12:23:34:45:A"`), &t)
    55  	c.Assert(err, NotNil)
    56  
    57  	m = MAC([]byte{})
    58  	w = MAC([]byte{})
    59  	d, err = json.Marshal(m)
    60  	c.Assert(err, Equals, nil)
    61  	c.Assert(d, checker.DeepEquals, []byte(`""`))
    62  	var t2 MAC
    63  	err = json.Unmarshal([]byte(`""`), &t2)
    64  	c.Assert(err, IsNil)
    65  	c.Assert(t2, checker.DeepEquals, w)
    66  }