gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/pkg/uuid/uuid_test.go (about) 1 // Copyright (c) 2017 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package uuid 7 8 import ( 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // Test UUID parsing and string conversation. 15 // 16 // This test simply converts a set of strings to UUIDs and back again. 17 // 18 // The original strings and the strings generated from the UUIDs match. 19 func TestUUID(t *testing.T) { 20 assert := assert.New(t) 21 testUUIDs := []string{ 22 "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", 23 "30dedd5c-48d9-45d3-8b44-f973e4f35e48", 24 "69e84267-ed01-4738-b15f-b47de06b62e7", 25 "e35ed972-c46c-4aad-a1e7-ef103ae079a2", 26 "eba04826-62a5-48bd-876f-9119667b1487", 27 "ca957444-fa46-11e5-94f9-38607786d9ec", 28 "ab68111c-03a6-11e6-87de-001320fb6e31", 29 } 30 31 for _, s := range testUUIDs { 32 uuid, err := Parse(s) 33 assert.NoError(err) 34 s2 := uuid.String() 35 assert.Equal(s, s2) 36 } 37 } 38 39 // Test UUID generation. 40 // 41 // This test generates 100 new UUIDs and then verifies that those UUIDs 42 // can be parsed. 43 // 44 // The UUIDs are generated correctly, their version number is correct, 45 // and they can be parsed. 46 func TestGenUUID(t *testing.T) { 47 assert := assert.New(t) 48 for i := 0; i < 100; i++ { 49 u := Generate() 50 s := u.String() 51 assert.EqualValues(s[14], '4') 52 u2, err := Parse(s) 53 assert.NoError(err) 54 assert.Equal(u, u2) 55 } 56 } 57 58 // Test uuid.Parse on invalid input. 59 // 60 // This test attempts to parse a set of invalid UUIDs. 61 // 62 // uuid.Parse should return an error for each invalid UUID. 63 func TestBadUUID(t *testing.T) { 64 badTestUUIDs := []string{ 65 "", 66 "48d9-45d3-8b44-f973e4f35e48", 67 "69e8426--ed01-4738-b15f-b47de06b62e7", 68 "e35ed972-46c-4aad-a1e7-ef103ae079a2", 69 "sba04826-62a5-48bd-876f-9119667b1487", 70 "ca957444fa4611e594f938607786d9ec0000", 71 "ab68111c-03a6-11e6-87de-001320fb6e31a", 72 } 73 74 for _, s := range badTestUUIDs { 75 _, err := Parse(s) 76 assert.Error(t, err) 77 } 78 }