discovery: add simple util functions for generating uuids.

  Add util functions to generate UUIDs for services and discovery
  instances.

Change-Id: I0e243954a5a6d7db83ad331101c15bc06cca6c9f
diff --git a/runtime/internal/discovery/uuid.go b/runtime/internal/discovery/uuid.go
new file mode 100644
index 0000000..221ef5a
--- /dev/null
+++ b/runtime/internal/discovery/uuid.go
@@ -0,0 +1,26 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package discovery
+
+import (
+	"github.com/pborman/uuid"
+)
+
+var (
+	// UUID of Vanadium namespace.
+	// Generated from UUID5("00000000-0000-0000-0000-000000000000", "v.io").
+	v23UUID uuid.UUID = uuid.UUID{0x3d, 0xd1, 0xd5, 0xa8, 0x2e, 0xef, 0x58, 0x16, 0xa7, 0x20, 0xf8, 0x8b, 0x9b, 0xcf, 0x6e, 0xe4}
+)
+
+// NewServiceUUID returns a version 5 UUID for the given interface name.
+func NewServiceUUID(interfaceName string) uuid.UUID {
+	return uuid.NewSHA1(v23UUID, []byte(interfaceName))
+}
+
+// NewInstanceUUID returns a version 4 (random) UUID. Mostly used for
+// uniquely identifying the discovery service instance.
+func NewInstanceUUID() uuid.UUID {
+	return uuid.NewRandom()
+}
diff --git a/runtime/internal/discovery/uuid_test.go b/runtime/internal/discovery/uuid_test.go
new file mode 100644
index 0000000..114c8e4
--- /dev/null
+++ b/runtime/internal/discovery/uuid_test.go
@@ -0,0 +1,38 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package discovery_test
+
+import (
+	"testing"
+
+	"v.io/x/ref/runtime/internal/discovery"
+)
+
+func TestServiceUUID(t *testing.T) {
+	tests := []struct {
+		in, want string
+	}{
+		{"v.io", "2101363c-688d-548a-a600-34d506e1aad0"},
+		{"v.io/v23/abc", "6726c4e5-b6eb-5547-9228-b2913f4fad52"},
+		{"v.io/v23/abc/xyz", "be8a57d7-931d-5ee4-9243-0bebde0029a5"},
+	}
+
+	for _, test := range tests {
+		if got := discovery.NewServiceUUID(test.in).String(); got != test.want {
+			t.Errorf("ServiceUUID for %q mismatch; got %q, want %q", test.in, got, test.want)
+		}
+	}
+}
+
+func TestInstanceUUID(t *testing.T) {
+	uuids := make(map[string]struct{})
+	for x := 0; x < 100; x++ {
+		uuid := discovery.NewInstanceUUID().String()
+		if _, ok := uuids[uuid]; ok {
+			t.Errorf("InstanceUUID returned duplicated UUID %q", uuid)
+		}
+		uuids[uuid] = struct{}{}
+	}
+}