-
Notifications
You must be signed in to change notification settings - Fork 9
/
guid_test.go
61 lines (50 loc) · 1.33 KB
/
guid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package disko_test
import (
"regexp"
"testing"
"machinerun.io/disko"
"machinerun.io/disko/partid"
)
func TestStringRoundtrip(t *testing.T) {
guidfmt := "^[0-9A-F]{8}-([0-9A-F]{4}-){3}[0-9A-F]{12}$"
matcher := regexp.MustCompile(guidfmt)
myGUID := disko.GenGUID()
asStr := disko.GUIDToString(myGUID)
if !matcher.MatchString(asStr) {
t.Errorf(
"guid %#v as a string (%s) did not match format %s",
myGUID, asStr, guidfmt)
}
back, err := disko.StringToGUID(asStr)
if err != nil {
t.Errorf("StringToGUID failed %#v -> %s: %s)", myGUID, asStr, back)
}
if back != myGUID {
t.Errorf("Round trip failed. %#v -> %#v", myGUID, back)
}
}
func TestStringKnown(t *testing.T) {
for _, td := range []struct {
guid disko.GUID
asStr string
}{
{partid.LinuxFS, "0FC63DAF-8483-4772-8E79-3D69D8477DE4"},
{disko.GUID{0x67, 0x45, 0x23, 0x1, 0xab, 0x89, 0xef, 0xcd, 0x1,
0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
"01234567-89AB-CDEF-0123-456789ABCDEF"},
} {
found := td.guid.String()
if found != td.asStr {
t.Errorf("GUIDToString(%#v) got %s. expected %s",
td.guid, found, td.asStr)
}
back, err := disko.StringToGUID(found)
if err != nil {
t.Errorf("Failed StringToGUID(%#v): %s", found, err)
}
if td.guid != back {
t.Errorf("StringToGuid(%s) returned %#v. expected %#v",
found, back, td.guid)
}
}
}