github.com/linuxboot/fiano@v1.2.0/pkg/guid/guid_test.go (about) 1 // Copyright 2018 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 guid 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 var ( 13 // GUID examples 14 exampleGUID GUID = [16]byte{0x67, 0x45, 0x23, 0x01, 0xAB, 0x89, 0xEF, 0xCD, 15 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF} 16 // GUID string examples 17 exampleGUIDString = "01234567-89AB-CDEF-0123-456789ABCDEF" 18 shortGUIDString = "0123456789ABCDEF0123456789ABCDEF" 19 badGUIDStringLength = "01234567" 20 badHex = "GHGHGHGHGHGHGH" 21 22 // GUID JSON examples 23 exampleJSON = `{"GUID" : "` + exampleGUIDString + `"}` 24 exampleJSONBadHex = `{"GUID" : "` + badHex + `"}` 25 exampleJSONBadKey = `{"UU" : "` + exampleGUIDString + `"}` 26 ) 27 28 func TestParse(t *testing.T) { 29 var tests = []struct { 30 s string 31 u *GUID 32 msg string 33 }{ 34 {exampleGUIDString, &exampleGUID, ""}, 35 {shortGUIDString, &exampleGUID, ""}, 36 {badGUIDStringLength, nil, fmt.Sprintf("guid string has incorrect length, need string of the format \n%v\n, got \n%v", 37 UExample, badGUIDStringLength)}, 38 {badHex, nil, fmt.Sprintf("guid string not correct, need string of the format \n%v\n, got \n%v", 39 UExample, badHex)}, 40 } 41 for _, test := range tests { 42 u, err := Parse(test.s) 43 if u == nil { 44 if test.u != nil { 45 t.Errorf("GUID was expected: %v, got nil", test.u) 46 } 47 if err == nil { 48 t.Errorf("Error was not returned, expected %v", test.msg) 49 } else if err.Error() != test.msg { 50 t.Errorf("Mismatched Error returned, expected \n%v\n got \n%v\n", test.msg, err.Error()) 51 } 52 } else if *u != *test.u { 53 t.Errorf("GUID was parsed incorrectly, expected %v\n, got %v\n, string was %v", test.u, u, test.s) 54 } 55 } 56 } 57 58 func TestMarshal(t *testing.T) { 59 var tests = []struct { 60 j string 61 u *GUID 62 }{ 63 {exampleJSON, &exampleGUID}, 64 } 65 for _, test := range tests { 66 j, err := test.u.MarshalJSON() 67 if err != nil { 68 t.Errorf("No error was expected, got %v", err) 69 } 70 if test.j != string(j) { 71 t.Errorf("JSON strings are not equal. Expected:\n%v\ngot:\n%v", test.j, string(j)) 72 } 73 } 74 } 75 76 func TestUnmarshal(t *testing.T) { 77 var tests = []struct { 78 j string 79 u *GUID 80 msg string 81 }{ 82 {exampleJSON, &exampleGUID, ""}, 83 {exampleJSONBadHex, nil, fmt.Sprintf("guid string not correct, need string of the format \n%v\n, got \n%v", 84 UExample, badHex)}, 85 {exampleJSONBadKey, nil, fmt.Sprintf("guid string has incorrect length, need string of the format \n%v\n, got \n%v", 86 UExample, "")}, 87 } 88 for _, test := range tests { 89 var g GUID 90 err := g.UnmarshalJSON([]byte(test.j)) 91 if test.msg == "" && err != nil { 92 t.Errorf("No error was expected, got %v", err) 93 } 94 if test.msg != "" && err == nil { 95 t.Errorf("Error was expected: %v, got nil", test.msg) 96 } 97 if err != nil && err.Error() != test.msg { 98 t.Errorf("Got Error msg %v, was expecting %v", err.Error(), test.msg) 99 } 100 if test.u != nil && *test.u != g { 101 t.Errorf("GUIDs are not equal. Expected:\n%v\ngot:\n%v", test.u, g) 102 } 103 } 104 }