...

Source file src/github.com/Microsoft/go-winio/pkg/guid/guid_test.go

Documentation: github.com/Microsoft/go-winio/pkg/guid

     1  package guid
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  func mustNewV4(t *testing.T) GUID {
    10  	g, err := NewV4()
    11  	if err != nil {
    12  		t.Fatal(err)
    13  	}
    14  	return g
    15  }
    16  
    17  func mustNewV5(t *testing.T, namespace GUID, name []byte) GUID {
    18  	g, err := NewV5(namespace, name)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	return g
    23  }
    24  
    25  func mustFromString(t *testing.T, s string) GUID {
    26  	g, err := FromString(s)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	return g
    31  }
    32  
    33  func Test_Variant(t *testing.T) {
    34  	type testCase struct {
    35  		g GUID
    36  		v Variant
    37  	}
    38  	testCases := []testCase{
    39  		{mustFromString(t, "f5cbc1a9-4cba-45a0-0fdd-b6761fc7dcc0"), VariantNCS},
    40  		{mustFromString(t, "f5cbc1a9-4cba-45a0-7fdd-b6761fc7dcc0"), VariantNCS},
    41  		{mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0"), VariantRFC4122},
    42  		{mustFromString(t, "f5cbc1a9-4cba-45a0-9fdd-b6761fc7dcc0"), VariantRFC4122},
    43  		{mustFromString(t, "f5cbc1a9-4cba-45a0-cfdd-b6761fc7dcc0"), VariantMicrosoft},
    44  		{mustFromString(t, "f5cbc1a9-4cba-45a0-dfdd-b6761fc7dcc0"), VariantMicrosoft},
    45  		{mustFromString(t, "f5cbc1a9-4cba-45a0-efdd-b6761fc7dcc0"), VariantFuture},
    46  		{mustFromString(t, "f5cbc1a9-4cba-45a0-ffdd-b6761fc7dcc0"), VariantFuture},
    47  	}
    48  	for _, tc := range testCases {
    49  		t.Run(tc.v.String()+"/"+tc.g.String(), func(t *testing.T) {
    50  			actualVariant := tc.g.Variant()
    51  			if actualVariant != tc.v {
    52  				t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func Test_SetVariant(t *testing.T) {
    59  	g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
    60  	for i := 0; i < len(_Variant_index)-1; i++ {
    61  		v := Variant(i)
    62  		if v == VariantUnknown {
    63  			// Unknown is not a valid variant
    64  			continue
    65  		}
    66  		t.Run(v.String(), func(t *testing.T) {
    67  			g.setVariant(v)
    68  			if g.Variant() != v {
    69  				t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", v, g.Variant())
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func Test_Version(t *testing.T) {
    76  	type testCase struct {
    77  		g GUID
    78  		v Version
    79  	}
    80  	testCases := []testCase{
    81  		{mustFromString(t, "f5cbc1a9-4cba-15a0-0fdd-b6761fc7dcc0"), 1},
    82  		{mustFromString(t, "f5cbc1a9-4cba-25a0-0fdd-b6761fc7dcc0"), 2},
    83  		{mustFromString(t, "f5cbc1a9-4cba-35a0-0fdd-b6761fc7dcc0"), 3},
    84  		{mustFromString(t, "f5cbc1a9-4cba-45a0-0fdd-b6761fc7dcc0"), 4},
    85  		{mustFromString(t, "f5cbc1a9-4cba-55a0-0fdd-b6761fc7dcc0"), 5},
    86  	}
    87  	for _, tc := range testCases {
    88  		t.Run(tc.v.String()+"-"+tc.g.String(), func(t *testing.T) {
    89  			actualVersion := tc.g.Version()
    90  			if actualVersion != tc.v {
    91  				t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g)
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  func Test_SetVersion(t *testing.T) {
    98  	g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
    99  	for tc := 0; tc < 16; tc++ {
   100  		v := Version(tc)
   101  		t.Run(v.String(), func(t *testing.T) {
   102  			g.setVersion(v)
   103  			if g.Version() != v {
   104  				t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version())
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func Test_NewV4IsUnique(t *testing.T) {
   111  	g := mustNewV4(t)
   112  	g2 := mustNewV4(t)
   113  	if g == g2 {
   114  		t.Fatalf("GUIDs are equal: %s, %s", g, g2)
   115  	}
   116  }
   117  
   118  func Test_V4HasCorrectVersionAndVariant(t *testing.T) {
   119  	g := mustNewV4(t)
   120  	if g.Version() != 4 {
   121  		t.Fatalf("Version is not 4: %s", g)
   122  	}
   123  	if g.Variant() != VariantRFC4122 {
   124  		t.Fatalf("Variant is not RFC4122: %s", g)
   125  	}
   126  }
   127  
   128  func Test_V5HasCorrectVersionAndVariant(t *testing.T) {
   129  	namespace := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
   130  	g := mustNewV5(t, namespace, []byte("Foo"))
   131  	if g.Version() != 5 {
   132  		t.Fatalf("Version is not 5: %s", g)
   133  	}
   134  	if g.Variant() != VariantRFC4122 {
   135  		t.Fatalf("Variant is not RFC4122: %s", g)
   136  	}
   137  }
   138  
   139  func Test_V5KnownValues(t *testing.T) {
   140  	type testCase struct {
   141  		ns   GUID
   142  		name string
   143  		g    GUID
   144  	}
   145  	testCases := []testCase{
   146  		{
   147  			mustFromString(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c8"),
   148  			"www.sample.com",
   149  			mustFromString(t, "4e4463eb-b0e8-54fa-8c28-12d1ab1d45b3"),
   150  		},
   151  		{
   152  			mustFromString(t, "6ba7b811-9dad-11d1-80b4-00c04fd430c8"),
   153  			"https://www.sample.com/test",
   154  			mustFromString(t, "9e44625a-0d85-5e0a-99bc-8e8a77df5ea2"),
   155  		},
   156  		{
   157  			mustFromString(t, "6ba7b812-9dad-11d1-80b4-00c04fd430c8"),
   158  			"1.3.6.1.4.1.343",
   159  			mustFromString(t, "6aab0456-7392-582a-b92a-ba5a7096945d"),
   160  		},
   161  		{
   162  			mustFromString(t, "6ba7b814-9dad-11d1-80b4-00c04fd430c8"),
   163  			"CN=John Smith, ou=People, o=FakeCorp, L=Seattle, S=Washington, C=US",
   164  			mustFromString(t, "badff8dd-c869-5b64-a260-00092e66be00"),
   165  		},
   166  	}
   167  	for _, tc := range testCases {
   168  		t.Run(tc.name, func(t *testing.T) {
   169  			g := mustNewV5(t, tc.ns, []byte(tc.name))
   170  			if g != tc.g {
   171  				t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g)
   172  			}
   173  		})
   174  	}
   175  }
   176  
   177  func Test_ToArray(t *testing.T) {
   178  	g := mustFromString(t, "73c39589-192e-4c64-9acf-6c5d0aa18528")
   179  	b := g.ToArray()
   180  	expected := [16]byte{0x73, 0xc3, 0x95, 0x89, 0x19, 0x2e, 0x4c, 0x64, 0x9a, 0xcf, 0x6c, 0x5d, 0x0a, 0xa1, 0x85, 0x28}
   181  	if b != expected {
   182  		t.Fatalf("GUID does not match array form: %x, %x", expected, b)
   183  	}
   184  }
   185  
   186  func Test_FromArrayAndBack(t *testing.T) {
   187  	b := [16]byte{0x73, 0xc3, 0x95, 0x89, 0x19, 0x2e, 0x4c, 0x64, 0x9a, 0xcf, 0x6c, 0x5d, 0x0a, 0xa1, 0x85, 0x28}
   188  	b2 := FromArray(b).ToArray()
   189  	if b != b2 {
   190  		t.Fatalf("Arrays do not match: %x, %x", b, b2)
   191  	}
   192  }
   193  
   194  func Test_ToWindowsArray(t *testing.T) {
   195  	g := mustFromString(t, "73c39589-192e-4c64-9acf-6c5d0aa18528")
   196  	b := g.ToWindowsArray()
   197  	expected := [16]byte{0x89, 0x95, 0xc3, 0x73, 0x2e, 0x19, 0x64, 0x4c, 0x9a, 0xcf, 0x6c, 0x5d, 0x0a, 0xa1, 0x85, 0x28}
   198  	if b != expected {
   199  		t.Fatalf("GUID does not match array form: %x, %x", expected, b)
   200  	}
   201  }
   202  
   203  func Test_FromWindowsArrayAndBack(t *testing.T) {
   204  	b := [16]byte{0x73, 0xc3, 0x95, 0x89, 0x19, 0x2e, 0x4c, 0x64, 0x9a, 0xcf, 0x6c, 0x5d, 0x0a, 0xa1, 0x85, 0x28}
   205  	b2 := FromWindowsArray(b).ToWindowsArray()
   206  	if b != b2 {
   207  		t.Fatalf("Arrays do not match: %x, %x", b, b2)
   208  	}
   209  }
   210  
   211  func Test_FromString(t *testing.T) {
   212  	orig := "8e35239e-2084-490e-a3db-ab18ee0744cb"
   213  	g := mustFromString(t, orig)
   214  	s := g.String()
   215  	if orig != s {
   216  		t.Fatalf("GUIDs not equal: %s, %s", orig, s)
   217  	}
   218  }
   219  
   220  func Test_MarshalJSON(t *testing.T) {
   221  	g := mustNewV4(t)
   222  	j, err := json.Marshal(g)
   223  	if err != nil {
   224  		t.Fatal(err)
   225  	}
   226  	gj := fmt.Sprintf("\"%s\"", g.String())
   227  	if string(j) != gj {
   228  		t.Fatalf("JSON not equal: %s, %s", j, gj)
   229  	}
   230  }
   231  
   232  func Test_MarshalJSON_Nested(t *testing.T) {
   233  	type test struct {
   234  		G GUID
   235  	}
   236  	g := mustNewV4(t)
   237  	t1 := test{g}
   238  	j, err := json.Marshal(t1)
   239  	if err != nil {
   240  		t.Fatal(err)
   241  	}
   242  	gj := fmt.Sprintf("{\"G\":\"%s\"}", g.String())
   243  	if string(j) != gj {
   244  		t.Fatalf("JSON not equal: %s, %s", j, gj)
   245  	}
   246  }
   247  
   248  func Test_UnmarshalJSON(t *testing.T) {
   249  	g := mustNewV4(t)
   250  	j, err := json.Marshal(g)
   251  	if err != nil {
   252  		t.Fatal(err)
   253  	}
   254  	var g2 GUID
   255  	if err := json.Unmarshal(j, &g2); err != nil {
   256  		t.Fatal(err)
   257  	}
   258  	if g != g2 {
   259  		t.Fatalf("GUIDs not equal: %s, %s", g, g2)
   260  	}
   261  }
   262  
   263  func Test_UnmarshalJSON_Nested(t *testing.T) {
   264  	type test struct {
   265  		G GUID
   266  	}
   267  	g := mustNewV4(t)
   268  	t1 := test{g}
   269  	j, err := json.Marshal(t1)
   270  	if err != nil {
   271  		t.Fatal(err)
   272  	}
   273  	var t2 test
   274  	if err := json.Unmarshal(j, &t2); err != nil {
   275  		t.Fatal(err)
   276  	}
   277  	if t1.G != t2.G {
   278  		t.Fatalf("GUIDs not equal: %v, %v", t1.G, t2.G)
   279  	}
   280  }
   281  

View as plain text