mirror of
https://github.com/google/uuid.git
synced 2025-09-16 09:16:30 +02:00
uuid: clean to use more idiomatic names
Convert constants to more Go like names (e.g,. DOMAIN_PERSON -> Person) Use the hash.Hash interface instead of our own. R=rcs CC=borman http://codereview.appspot.com/4832058
This commit is contained in:
36
uuid/dce.go
36
uuid/dce.go
@@ -15,17 +15,17 @@ type Domain byte
|
||||
|
||||
// Domain constants for DCE Security (Version 2) UUIDs.
|
||||
const (
|
||||
DOMAIN_PERSON = 0
|
||||
DOMAIN_GROUP = 1
|
||||
DOMAIN_ORG = 2
|
||||
Person = Domain(0)
|
||||
Group = Domain(1)
|
||||
Org = Domain(2)
|
||||
)
|
||||
|
||||
// NewDCESecurity returns a DCE Security (Version 2) UUID.
|
||||
//
|
||||
// The domain should be one of DOMAIN_PERSON, DOMAIN_GROUP or DOMAIN_ORG.
|
||||
// On a POSIX system the id should be the users UID for the DOMAIN_PERSON
|
||||
// domain and the users GID for the DOMAIN_GROUP. The meaning of id for
|
||||
// the domain DOMAIN_ORG or on non-POSIX systems is site defined.
|
||||
// The domain should be one of Person, Group or Org.
|
||||
// On a POSIX system the id should be the users UID for the Person
|
||||
// domain and the users GID for the Group. The meaning of id for
|
||||
// the domain Org or on non-POSIX systems is site defined.
|
||||
//
|
||||
// For a given domain/id pair the same token may be returned for up to
|
||||
// 7 minutes and 10 seconds.
|
||||
@@ -42,17 +42,17 @@ func NewDCESecurity(domain Domain, id uint32) UUID {
|
||||
// NewDCEGroup returns a DCE Security (Version 2) UUID in the group
|
||||
// domain with the id returned by os.Getuid.
|
||||
//
|
||||
// NewDCESecurity(DOMAIN_PERSON, uint32(os.Getuid()))
|
||||
// NewDCESecurity(Person, uint32(os.Getuid()))
|
||||
func NewDCEPerson() UUID {
|
||||
return NewDCESecurity(DOMAIN_PERSON, uint32(os.Getuid()))
|
||||
return NewDCESecurity(Person, uint32(os.Getuid()))
|
||||
}
|
||||
|
||||
// NewDCEPerson returns a DCE Security (Version 2) UUID in the group
|
||||
// domain with the id returned by os.Getgid.
|
||||
//
|
||||
// NewDCESecurity(DOMAIN_GROUP, uint32(os.Getgid()))
|
||||
// NewDCESecurity(Group, uint32(os.Getgid()))
|
||||
func NewDCEGroup() UUID {
|
||||
return NewDCESecurity(DOMAIN_GROUP, uint32(os.Getgid()))
|
||||
return NewDCESecurity(Group, uint32(os.Getgid()))
|
||||
}
|
||||
|
||||
// Domain returns the domain for a Version 2 UUID or false.
|
||||
@@ -73,12 +73,12 @@ func (uuid UUID) Id() (uint32, bool) {
|
||||
|
||||
func (d Domain) String() string {
|
||||
switch d {
|
||||
case DOMAIN_PERSON:
|
||||
return "DOMAIN_PERSON"
|
||||
case DOMAIN_GROUP:
|
||||
return "DOMAIN_GROUP"
|
||||
case DOMAIN_ORG:
|
||||
return "DOMAIN_ORG"
|
||||
case Person:
|
||||
return "Person"
|
||||
case Group:
|
||||
return "Group"
|
||||
case Org:
|
||||
return "Org"
|
||||
}
|
||||
return fmt.Sprintf("DOMAIN_%d", d)
|
||||
return fmt.Sprintf("Domain%d", int(d))
|
||||
}
|
||||
|
21
uuid/hash.go
21
uuid/hash.go
@@ -7,23 +7,16 @@ package uuid
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"os"
|
||||
"hash"
|
||||
)
|
||||
|
||||
// A Hash defines a hashing function.
|
||||
type Hash interface {
|
||||
Reset()
|
||||
Write([]byte) (int, os.Error)
|
||||
Sum() []byte
|
||||
}
|
||||
|
||||
// Well known Name Space IDs and UUIDs
|
||||
var (
|
||||
NameSpace_DNS = Decode("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||
NameSpace_URL = Decode("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
|
||||
NameSpace_OID = Decode("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
|
||||
NameSpace_X500 = Decode("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
|
||||
NIL = Decode("00000000-0000-0000-0000-000000000000")
|
||||
NameSpace_DNS = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||
NameSpace_URL = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
|
||||
NameSpace_OID = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
|
||||
NameSpace_X500 = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
|
||||
NIL = Parse("00000000-0000-0000-0000-000000000000")
|
||||
)
|
||||
|
||||
// NewHash returns a new UUID dervied from the hash of space concatenated with
|
||||
@@ -31,7 +24,7 @@ var (
|
||||
// first 16 bytes of the hash are used to form the UUID. The version of the
|
||||
// UUID will be the lower 4 bits of version. NewHash is used to implement
|
||||
// NewMD5 and NewSHA1.
|
||||
func NewHash(h Hash, space UUID, data []byte, version int) UUID {
|
||||
func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {
|
||||
h.Reset()
|
||||
h.Write(space)
|
||||
h.Write([]byte(data))
|
||||
|
44
uuid/uuid.go
44
uuid/uuid.go
@@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// The uuid package generates and inspects UUIDs based on RFC 4122 and
|
||||
// DCE 1.1: Authentication and Security Services.
|
||||
package uuid
|
||||
|
||||
import (
|
||||
@@ -26,11 +24,11 @@ type Variant byte
|
||||
|
||||
// Constants returned by Variant.
|
||||
const (
|
||||
INVALID = iota // Invalid UUID
|
||||
RFC4122 // The variant specified in RFC4122
|
||||
RESERVED // Reserved, NCS backward compatibility.
|
||||
MICROSOFT // Reserved, Microsoft Corporation backward compatibility.
|
||||
FUTURE // Reserved for future definition.
|
||||
Invalid = Variant(iota) // Invalid UUID
|
||||
RFC4122 // The variant specified in RFC4122
|
||||
Reserved // Reserved, NCS backward compatibility.
|
||||
Microsoft // Reserved, Microsoft Corporation backward compatibility.
|
||||
Future // Reserved for future definition.
|
||||
)
|
||||
|
||||
var rander = rand.Reader // random function
|
||||
@@ -41,10 +39,10 @@ func New() string {
|
||||
return NewRandom().String()
|
||||
}
|
||||
|
||||
// Decode decodes s into a UUID or returns nil. Both the UUID form of
|
||||
// Parse decodes s into a UUID or returns nil. Both the UUID form of
|
||||
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
|
||||
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
|
||||
func Decode(s string) UUID {
|
||||
func Parse(s string) UUID {
|
||||
if len(s) == 36+9 {
|
||||
if strings.ToLower(s[:9]) != "urn:uuid:" {
|
||||
return nil
|
||||
@@ -99,21 +97,21 @@ func (uuid UUID) URN() string {
|
||||
b[:4], b[4:6], b[6:8], b[8:10], b[10:])
|
||||
}
|
||||
|
||||
// Variant returns the variant encoded in uuid. It returns INVALID if
|
||||
// Variant returns the variant encoded in uuid. It returns Invalid if
|
||||
// uuid is invalid.
|
||||
func (uuid UUID) Variant() Variant {
|
||||
if len(uuid) != 16 {
|
||||
return INVALID
|
||||
return Invalid
|
||||
}
|
||||
switch {
|
||||
case (uuid[8] & 0xc0) == 0x80:
|
||||
return RFC4122
|
||||
case (uuid[8] & 0xe0) == 0xc0:
|
||||
return MICROSOFT
|
||||
return Microsoft
|
||||
case (uuid[8] & 0xe0) == 0xe0:
|
||||
return FUTURE
|
||||
return Future
|
||||
default:
|
||||
return RESERVED
|
||||
return Reserved
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
@@ -138,16 +136,16 @@ func (v Variant) String() string {
|
||||
switch v {
|
||||
case RFC4122:
|
||||
return "RFC4122"
|
||||
case RESERVED:
|
||||
return "RESERVED"
|
||||
case MICROSOFT:
|
||||
return "MICROSOFT"
|
||||
case FUTURE:
|
||||
return "FUTURE"
|
||||
case INVALID:
|
||||
return "INVALID"
|
||||
case Reserved:
|
||||
return "Reserved"
|
||||
case Microsoft:
|
||||
return "Microsoft"
|
||||
case Future:
|
||||
return "Future"
|
||||
case Invalid:
|
||||
return "Invalid"
|
||||
}
|
||||
return fmt.Sprintf("BAD_VARIANT_%d", v)
|
||||
return fmt.Sprintf("BadVariant%d", int(v))
|
||||
}
|
||||
|
||||
// SetRand sets the random number generator to r, which implents io.Reader.
|
||||
|
@@ -6,6 +6,7 @@ package uuid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -37,37 +38,53 @@ var tests = []test{
|
||||
{"f47ac10b-58cc-e372-8567-0e02b2c3d479", 14, RFC4122, true},
|
||||
{"f47ac10b-58cc-f372-8567-0e02b2c3d479", 15, RFC4122, true},
|
||||
|
||||
{"urn:uuid:f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"URN:UUID:f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-1567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-2567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-3567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-4567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-5567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-6567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"f47ac10b-58cc-4372-7567-0e02b2c3d479", 4, RESERVED, true},
|
||||
{"urn:uuid:f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"URN:UUID:f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-1567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-2567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-3567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-4567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-5567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-6567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-7567-0e02b2c3d479", 4, Reserved, true},
|
||||
{"f47ac10b-58cc-4372-8567-0e02b2c3d479", 4, RFC4122, true},
|
||||
{"f47ac10b-58cc-4372-9567-0e02b2c3d479", 4, RFC4122, true},
|
||||
{"f47ac10b-58cc-4372-a567-0e02b2c3d479", 4, RFC4122, true},
|
||||
{"f47ac10b-58cc-4372-b567-0e02b2c3d479", 4, RFC4122, true},
|
||||
{"f47ac10b-58cc-4372-c567-0e02b2c3d479", 4, MICROSOFT, true},
|
||||
{"f47ac10b-58cc-4372-d567-0e02b2c3d479", 4, MICROSOFT, true},
|
||||
{"f47ac10b-58cc-4372-e567-0e02b2c3d479", 4, FUTURE, true},
|
||||
{"f47ac10b-58cc-4372-f567-0e02b2c3d479", 4, FUTURE, true},
|
||||
{"f47ac10b-58cc-4372-c567-0e02b2c3d479", 4, Microsoft, true},
|
||||
{"f47ac10b-58cc-4372-d567-0e02b2c3d479", 4, Microsoft, true},
|
||||
{"f47ac10b-58cc-4372-e567-0e02b2c3d479", 4, Future, true},
|
||||
{"f47ac10b-58cc-4372-f567-0e02b2c3d479", 4, Future, true},
|
||||
|
||||
{"f47ac10b158cc-5372-a567-0e02b2c3d479", 0, INVALID, false},
|
||||
{"f47ac10b-58cc25372-a567-0e02b2c3d479", 0, INVALID, false},
|
||||
{"f47ac10b-58cc-53723a567-0e02b2c3d479", 0, INVALID, false},
|
||||
{"f47ac10b-58cc-5372-a56740e02b2c3d479", 0, INVALID, false},
|
||||
{"f47ac10b-58cc-5372-a567-0e02-2c3d479", 0, INVALID, false},
|
||||
{"g47ac10b-58cc-4372-a567-0e02b2c3d479", 0, INVALID, false},
|
||||
{"f47ac10b158cc-5372-a567-0e02b2c3d479", 0, Invalid, false},
|
||||
{"f47ac10b-58cc25372-a567-0e02b2c3d479", 0, Invalid, false},
|
||||
{"f47ac10b-58cc-53723a567-0e02b2c3d479", 0, Invalid, false},
|
||||
{"f47ac10b-58cc-5372-a56740e02b2c3d479", 0, Invalid, false},
|
||||
{"f47ac10b-58cc-5372-a567-0e02-2c3d479", 0, Invalid, false},
|
||||
{"g47ac10b-58cc-4372-a567-0e02b2c3d479", 0, Invalid, false},
|
||||
}
|
||||
|
||||
var constants = []struct {
|
||||
c interface{}
|
||||
name string
|
||||
} {
|
||||
{Person, "Person"},
|
||||
{Group, "Group"},
|
||||
{Org, "Org"},
|
||||
{Invalid, "Invalid"},
|
||||
{RFC4122, "RFC4122"},
|
||||
{Reserved, "Reserved"},
|
||||
{Microsoft, "Microsoft"},
|
||||
{Future, "Future"},
|
||||
{Domain(17), "Domain17"},
|
||||
{Variant(42), "BadVariant42"},
|
||||
}
|
||||
|
||||
func testTest(t *testing.T, in string, tt test) {
|
||||
uuid := Decode(in)
|
||||
uuid := Parse(in)
|
||||
if ok := (uuid != nil); ok != tt.isuuid {
|
||||
t.Errorf("Decode(%s) got %v expected %v\b", in, ok, tt.isuuid)
|
||||
t.Errorf("Parse(%s) got %v expected %v\b", in, ok, tt.isuuid)
|
||||
}
|
||||
if uuid == nil {
|
||||
return
|
||||
@@ -88,6 +105,18 @@ func TestUUID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConstants(t *testing.T) {
|
||||
for x, tt := range constants {
|
||||
v, ok := tt.c.(fmt.Stringer)
|
||||
if !ok {
|
||||
t.Errorf("%x: %v: not a stringer", x, v)
|
||||
} else if s := v.String(); s != tt.name {
|
||||
v, _ := tt.c.(int)
|
||||
t.Errorf("%x: Constant %T:%d gives %q, expected %q\n", x, tt.c, v, s, tt.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomUUID(t *testing.T) {
|
||||
m := make(map[string]bool)
|
||||
for x := 1; x < 32; x++ {
|
||||
@@ -114,7 +143,7 @@ func TestNew(t *testing.T) {
|
||||
t.Errorf("New returned duplicated UUID %s\n", s)
|
||||
}
|
||||
m[s] = true
|
||||
uuid := Decode(s)
|
||||
uuid := Parse(s)
|
||||
if uuid == nil {
|
||||
t.Errorf("New returned %q which does not decode\n", s)
|
||||
continue
|
||||
@@ -182,7 +211,7 @@ func TestCoding(t *testing.T) {
|
||||
t.Errorf("%x: urn is %s, expected %s\n", data, v, urn)
|
||||
}
|
||||
|
||||
uuid := Decode(text)
|
||||
uuid := Parse(text)
|
||||
if !Equal(uuid, data) {
|
||||
t.Errorf("%s: decoded to %s, expected %s\n", text, uuid, data)
|
||||
}
|
||||
@@ -236,7 +265,7 @@ func TestVersion1(t *testing.T) {
|
||||
func TestNodeAndTime(t *testing.T) {
|
||||
// Time is February 5, 1998 12:30:23.136364800 AM GMT
|
||||
|
||||
uuid := Decode("7d444840-9dc0-11d1-b245-5ffdce74fad2")
|
||||
uuid := Parse("7d444840-9dc0-11d1-b245-5ffdce74fad2")
|
||||
node := []byte{0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2}
|
||||
|
||||
ts, ok := uuid.Time()
|
||||
@@ -330,8 +359,8 @@ func testDCE(t *testing.T, name string, uuid UUID, domain Domain, id uint32) {
|
||||
|
||||
func TestDCE(t *testing.T) {
|
||||
testDCE(t, "NewDCESecurity", NewDCESecurity(42, 12345678), 42, 12345678)
|
||||
testDCE(t, "NewDCEPerson", NewDCEPerson(), DOMAIN_PERSON, uint32(os.Getuid()))
|
||||
testDCE(t, "NewDCEGroup", NewDCEGroup(), DOMAIN_GROUP, uint32(os.Getgid()))
|
||||
testDCE(t, "NewDCEPerson", NewDCEPerson(), Person, uint32(os.Getuid()))
|
||||
testDCE(t, "NewDCEGroup", NewDCEGroup(), Group, uint32(os.Getgid()))
|
||||
}
|
||||
|
||||
type badRand struct{}
|
||||
|
Reference in New Issue
Block a user