github.com/cilium/cilium@v1.16.2/pkg/policy/api/icmp_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package api
     5  
     6  import (
     7  	"encoding/json"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"k8s.io/apimachinery/pkg/util/intstr"
    13  )
    14  
    15  func TestICMPFieldUnmarshal(t *testing.T) {
    16  	setUpSuite(t)
    17  
    18  	var i ICMPField
    19  
    20  	value1 := []byte("{\"family\": \"IPv4\", \"type\": 8}")
    21  	err := json.Unmarshal(value1, &i)
    22  
    23  	icmpType := intstr.FromInt(8)
    24  	require.EqualValues(t, ICMPField{Family: IPv4Family, Type: &icmpType}, i)
    25  	require.Nil(t, err)
    26  
    27  	// Check ICMPFIeld can treat ICMP type name
    28  	value2 := []byte("{\"family\": \"IPv4\", \"type\": \"EchoRequest\"}")
    29  	err = json.Unmarshal(value2, &i)
    30  
    31  	icmpType = intstr.FromString("EchoRequest")
    32  	require.EqualValues(t, ICMPField{Family: IPv4Family, Type: &icmpType}, i)
    33  	require.Nil(t, err)
    34  
    35  	// ICMP Node Information Query is only for IPv6
    36  	value3 := []byte("{\"family\": \"IPv4\", \"type\": \"ICMPNodeInformationQuery\"}")
    37  	err = json.Unmarshal(value3, &i)
    38  
    39  	require.Error(t, err)
    40  }