github.com/linuxboot/fiano@v1.2.0/pkg/uefi/meregion_test.go (about)

     1  // Copyright 2019 the LinuxBoot Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uefi
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestMEName_MarshalText(t *testing.T) {
    13  	var tests = []struct {
    14  		name string
    15  		me   MEName
    16  	}{
    17  		{"NAME", MEName{'N', 'A', 'M', 'E'}},
    18  		{"NAM", MEName{'N', 'A', 'M', 0}},
    19  		{"NA", MEName{'N', 'A', 0, 0}},
    20  		{"N", MEName{'N', 0, 0, 0}},
    21  	}
    22  	for _, test := range tests {
    23  		t.Run(test.name, func(t *testing.T) {
    24  			b, err := test.me.MarshalText()
    25  			if err != nil {
    26  				t.Errorf("Unexpected error %v", err)
    27  			}
    28  			if string(b) != test.name {
    29  				t.Errorf("error got %q want %q", b, test.name)
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestMEName_UnmarshalText(t *testing.T) {
    36  	var tests = []struct {
    37  		name string
    38  		me   MEName
    39  		msg  string
    40  	}{
    41  		{"NAME", MEName{'N', 'A', 'M', 'E'}, ""},
    42  		{"NAM", MEName{'N', 'A', 'M', 0}, ""},
    43  		{"NA", MEName{'N', 'A', 0, 0}, ""},
    44  		{"N", MEName{'N', 0, 0, 0}, ""},
    45  		{"NAME1", MEName{'N', 'A', 'M', 'E'}, "can’t unmarshal \"NAME1\" to MEName, 5 > 4"},
    46  	}
    47  	for _, test := range tests {
    48  		t.Run(test.name, func(t *testing.T) {
    49  			me := MEName{'F', 'U', 'L', 'L'}
    50  			err := me.UnmarshalText([]byte(test.name))
    51  			if err == nil && test.msg != "" {
    52  				t.Errorf("Error was not returned, expected %v", test.msg)
    53  			} else if err != nil && err.Error() != test.msg {
    54  				t.Errorf("Mismatched Error returned, expected \n%v\n got \n%v\n", test.msg, err.Error())
    55  			} else if !reflect.DeepEqual(me, test.me) {
    56  				t.Errorf("error got %q want %q", me, test.me)
    57  			}
    58  
    59  		})
    60  	}
    61  }