...

Source file src/github.com/mdlayher/ethernet/vlan_test.go

Documentation: github.com/mdlayher/ethernet

     1  package ethernet
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestVLANMarshalBinary(t *testing.T) {
    11  	tests := []struct {
    12  		desc string
    13  		v    *VLAN
    14  		b    []byte
    15  		err  error
    16  	}{
    17  		{
    18  			desc: "VLAN priority too large",
    19  			v: &VLAN{
    20  				Priority: 8,
    21  			},
    22  			err: ErrInvalidVLAN,
    23  		},
    24  		{
    25  			desc: "VLAN ID too large",
    26  			v: &VLAN{
    27  				ID: 4095,
    28  			},
    29  			err: ErrInvalidVLAN,
    30  		},
    31  		{
    32  			desc: "empty VLAN",
    33  			v:    &VLAN{},
    34  			b:    []byte{0x00, 0x00},
    35  		},
    36  		{
    37  			desc: "VLAN: PRI 1, ID 101",
    38  			v: &VLAN{
    39  				Priority: 1,
    40  				ID:       101,
    41  			},
    42  			b: []byte{0x20, 0x65},
    43  		},
    44  		{
    45  			desc: "VLANs: PRI 0, DROP, ID 100",
    46  			v: &VLAN{
    47  				DropEligible: true,
    48  				ID:           100,
    49  			},
    50  			b: []byte{0x10, 0x64},
    51  		},
    52  	}
    53  
    54  	for _, tt := range tests {
    55  		t.Run(tt.desc, func(t *testing.T) {
    56  			b, err := tt.v.MarshalBinary()
    57  			if err != nil {
    58  				if want, got := tt.err, err; want != got {
    59  					t.Fatalf("unexpected error: %v != %v", want, got)
    60  				}
    61  
    62  				return
    63  			}
    64  
    65  			if want, got := tt.b, b; !bytes.Equal(want, got) {
    66  				t.Fatalf("unexpected VLAN bytes:\n- want: %v\n-  got: %v", want, got)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestVLANUnmarshalBinary(t *testing.T) {
    73  	tests := []struct {
    74  		desc string
    75  		b    []byte
    76  		v    *VLAN
    77  		err  error
    78  	}{
    79  		{
    80  			desc: "nil buffer",
    81  			err:  io.ErrUnexpectedEOF,
    82  		},
    83  		{
    84  			desc: "short buffer",
    85  			b:    []byte{0},
    86  			err:  io.ErrUnexpectedEOF,
    87  		},
    88  		{
    89  			desc: "VLAN ID too large",
    90  			b:    []byte{0xff, 0xff},
    91  			err:  ErrInvalidVLAN,
    92  		},
    93  		{
    94  			desc: "VLAN: PRI 1, ID 101",
    95  			b:    []byte{0x20, 0x65},
    96  			v: &VLAN{
    97  				Priority: 1,
    98  				ID:       101,
    99  			},
   100  		},
   101  		{
   102  			desc: "VLAN: PRI 0, DROP, ID 100",
   103  			b:    []byte{0x10, 0x64},
   104  			v: &VLAN{
   105  				DropEligible: true,
   106  				ID:           100,
   107  			},
   108  		},
   109  	}
   110  
   111  	for _, tt := range tests {
   112  		t.Run(tt.desc, func(t *testing.T) {
   113  			v := new(VLAN)
   114  			if err := v.UnmarshalBinary(tt.b); err != nil {
   115  				if want, got := tt.err, err; want != got {
   116  					t.Fatalf("unexpected error: %v != %v", want, got)
   117  				}
   118  
   119  				return
   120  			}
   121  
   122  			if want, got := tt.v, v; !reflect.DeepEqual(want, got) {
   123  				t.Fatalf("unexpected VLAN:\n- want: %v\n-  got: %v", want, got)
   124  			}
   125  		})
   126  	}
   127  }
   128  
   129  // Benchmarks for VLAN.MarshalBinary
   130  
   131  func BenchmarkVLANMarshalBinary(b *testing.B) {
   132  	v := &VLAN{
   133  		Priority: PriorityBackground,
   134  		ID:       10,
   135  	}
   136  
   137  	b.ResetTimer()
   138  	b.ReportAllocs()
   139  	for i := 0; i < b.N; i++ {
   140  		if _, err := v.MarshalBinary(); err != nil {
   141  			b.Fatal(err)
   142  		}
   143  	}
   144  }
   145  
   146  // Benchmarks for VLAN.UnmarshalBinary
   147  
   148  func BenchmarkVLANUnmarshalBinary(b *testing.B) {
   149  	v := &VLAN{
   150  		Priority: PriorityBestEffort,
   151  		ID:       20,
   152  	}
   153  
   154  	vb, err := v.MarshalBinary()
   155  	if err != nil {
   156  		b.Fatal(err)
   157  	}
   158  
   159  	b.ResetTimer()
   160  	b.ReportAllocs()
   161  	for i := 0; i < b.N; i++ {
   162  		if err := v.UnmarshalBinary(vb); err != nil {
   163  			b.Fatal(err)
   164  		}
   165  	}
   166  }
   167  

View as plain text