forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to crypto secure random number generation (Azure#15190)
- Loading branch information
1 parent
47927cd
commit 8702aa5
Showing
4 changed files
with
116 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package uuid | ||
|
||
import ( | ||
"reflect" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
u, err := New() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if reflect.ValueOf(u).IsZero() { | ||
t.Fatal("unexpected zero-value UUID") | ||
} | ||
s := u.String() | ||
match, err := regexp.MatchString(`[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}`, s) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !match { | ||
t.Fatalf("invalid UUID string %s", s) | ||
} | ||
} | ||
|
||
func TestParse(t *testing.T) { | ||
testCases := []string{ | ||
"72d0f24f-82be-4016-729d-31fd13bd681e", | ||
"{72d0f24f-82be-4016-729d-31fd13bd681e}", | ||
} | ||
for _, input := range testCases { | ||
t.Run(input, func(t *testing.T) { | ||
u, err := Parse(input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if reflect.ValueOf(u).IsZero() { | ||
t.Fatal("unexpected zero-value UUID") | ||
} | ||
if len(input) > 36 { | ||
// strip off the {} as String() doesn't output them | ||
input = input[1:37] | ||
} | ||
if s := u.String(); s != input { | ||
t.Fatalf("didn't round trip: %s", s) | ||
} | ||
}) | ||
} | ||
} |