naming:  Turn on endpoint type v3 and use ep.ServesMountTable().

The default if we're using a previous version endpoint is to
assume it serves a mount table.

Since we've been parsing v3 endpoints for weeks, this should
be compatible with current servers.

Change-Id: I46b1facb83c1f05a741a1ee0b4ee29943ff0be3f
diff --git a/runtimes/google/ipc/sort_internal_test.go b/runtimes/google/ipc/sort_internal_test.go
index c234557..1995c16 100644
--- a/runtimes/google/ipc/sort_internal_test.go
+++ b/runtimes/google/ipc/sort_internal_test.go
@@ -73,10 +73,10 @@
 
 	// Just foobar and tcp4
 	want := []string{
-		"/@2@foobar@127.0.0.10@00000000000000000000000000000000@@@@",
-		"/@2@foobar@127.0.0.11@00000000000000000000000000000000@@@@",
-		"/@2@tcp4@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@tcp4@127.0.0.2@00000000000000000000000000000000@@@@",
+		"/@3@foobar@127.0.0.10@00000000000000000000000000000000@@@m@@",
+		"/@3@foobar@127.0.0.11@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp4@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp4@127.0.0.2@00000000000000000000000000000000@@@m@@",
 	}
 	if !reflect.DeepEqual(got, want) {
 		t.Errorf("got: %v, want %v", got, want)
@@ -88,10 +88,10 @@
 	}
 	// tcp or tcp4
 	want = []string{
-		"/@2@tcp4@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@tcp4@127.0.0.2@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.3@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.4@00000000000000000000000000000000@@@@",
+		"/@3@tcp4@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp4@127.0.0.2@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.4@00000000000000000000000000000000@@@m@@",
 	}
 
 	// Everything, since we didn't specify a protocol
@@ -103,14 +103,14 @@
 	// original ordering within each protocol, with protocols that
 	// are not in the default ordering list at the end.
 	want = []string{
-		"/@2@tcp@127.0.0.3@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.4@00000000000000000000000000000000@@@@",
-		"/@2@tcp4@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@tcp4@127.0.0.2@00000000000000000000000000000000@@@@",
-		"/@2@tcp6@127.0.0.7@00000000000000000000000000000000@@@@",
-		"/@2@tcp6@127.0.0.8@00000000000000000000000000000000@@@@",
-		"/@2@foobar@127.0.0.10@00000000000000000000000000000000@@@@",
-		"/@2@foobar@127.0.0.11@00000000000000000000000000000000@@@@",
+		"/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.4@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp4@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp4@127.0.0.2@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp6@127.0.0.7@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp6@127.0.0.8@00000000000000000000000000000000@@@m@@",
+		"/@3@foobar@127.0.0.10@00000000000000000000000000000000@@@m@@",
+		"/@3@foobar@127.0.0.11@00000000000000000000000000000000@@@m@@",
 	}
 	if !reflect.DeepEqual(got, want) {
 		t.Errorf("got: %v, want %v", got, want)
@@ -132,13 +132,13 @@
 		t.Fatalf("unexpected error: %s", err)
 	}
 	want = []string{
-		"/@2@tcp@127.0.0.3@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@tcp@74.125.69.139@00000000000000000000000000000000@@@@",
-		"/@2@tcp@192.168.1.10@00000000000000000000000000000000@@@@",
-		"/@2@tcp@74.125.142.83@00000000000000000000000000000000@@@@",
-		"/@2@foobar@127.0.0.10@00000000000000000000000000000000@@@@",
-		"/@2@foobar@127.0.0.11@00000000000000000000000000000000@@@@",
+		"/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@74.125.69.139@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@192.168.1.10@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@74.125.142.83@00000000000000000000000000000000@@@m@@",
+		"/@3@foobar@127.0.0.10@00000000000000000000000000000000@@@m@@",
+		"/@3@foobar@127.0.0.11@00000000000000000000000000000000@@@m@@",
 	}
 	if !reflect.DeepEqual(got, want) {
 		t.Errorf("got: %v, want %v", got, want)
@@ -156,11 +156,11 @@
 		t.Fatalf("unexpected error: %s", err)
 	}
 	want := []string{
-		"/@2@tcp@127.0.0.3@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@tcp@74.125.69.139@00000000000000000000000000000000@@@@",
-		"/@2@tcp@192.168.1.10@00000000000000000000000000000000@@@@",
-		"/@2@tcp@74.125.142.83@00000000000000000000000000000000@@@@",
+		"/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@74.125.69.139@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@192.168.1.10@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@74.125.142.83@00000000000000000000000000000000@@@m@@",
 	}
 	if !reflect.DeepEqual(got, want) {
 		t.Errorf("got: %v, want %v", got, want)
@@ -175,16 +175,16 @@
 		t.Fatalf("unexpected error: %s", err)
 	}
 	want = []string{
-		"/@2@ws@127.0.0.3:123@00000000000000000000000000000000@@@@",
-		"/@2@ws@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@ws@74.125.69.139@00000000000000000000000000000000@@@@",
-		"/@2@ws@192.168.1.10@00000000000000000000000000000000@@@@",
-		"/@2@ws@74.125.142.83@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.3@00000000000000000000000000000000@@@@",
-		"/@2@tcp@127.0.0.1@00000000000000000000000000000000@@@@",
-		"/@2@tcp@74.125.69.139@00000000000000000000000000000000@@@@",
-		"/@2@tcp@192.168.1.10@00000000000000000000000000000000@@@@",
-		"/@2@tcp@74.125.142.83@00000000000000000000000000000000@@@@",
+		"/@3@ws@127.0.0.3:123@00000000000000000000000000000000@@@m@@",
+		"/@3@ws@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@ws@74.125.69.139@00000000000000000000000000000000@@@m@@",
+		"/@3@ws@192.168.1.10@00000000000000000000000000000000@@@m@@",
+		"/@3@ws@74.125.142.83@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@127.0.0.1@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@74.125.69.139@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@192.168.1.10@00000000000000000000000000000000@@@m@@",
+		"/@3@tcp@74.125.142.83@00000000000000000000000000000000@@@m@@",
 	}
 	if !reflect.DeepEqual(got, want) {
 		t.Errorf("got: %v, want %v", got, want)
diff --git a/runtimes/google/naming/endpoint.go b/runtimes/google/naming/endpoint.go
index e2fefd9..2552bc2 100644
--- a/runtimes/google/naming/endpoint.go
+++ b/runtimes/google/naming/endpoint.go
@@ -36,6 +36,9 @@
 func NewEndpoint(input string) (*Endpoint, error) {
 	var ep Endpoint
 
+	// We have to guess this is a mount table if we don't know.
+	ep.IsMountTable = true
+
 	// The prefix and suffix are optional.
 	input = strings.TrimPrefix(strings.TrimSuffix(input, suffix), separator)
 
@@ -116,7 +119,10 @@
 }
 
 func parseMountTableFlag(input string) (bool, error) {
-	if len(input) == 1 {
+	switch len(input) {
+	case 0:
+		return true, nil
+	case 1:
 		switch f := input[0]; f {
 		case 'm':
 			return true, nil
@@ -169,7 +175,7 @@
 	return Network
 }
 
-var defaultVersion = 2
+var defaultVersion = 3
 
 func (ep *Endpoint) VersionedString(version int) string {
 	switch version {
@@ -205,7 +211,7 @@
 
 func (ep *Endpoint) ServesMountTable() bool {
 	//nologcall
-	return true
+	return ep.IsMountTable
 }
 
 type addr struct {
diff --git a/runtimes/google/naming/endpoint_test.go b/runtimes/google/naming/endpoint_test.go
index 00135c5..1aaca1b 100644
--- a/runtimes/google/naming/endpoint_test.go
+++ b/runtimes/google/naming/endpoint_test.go
@@ -11,9 +11,10 @@
 
 func TestEndpoint(t *testing.T) {
 	v1 := &Endpoint{
-		Protocol: "tcp",
-		Address:  "batman.com:1234",
-		RID:      naming.FixedRoutingID(0xdabbad00),
+		Protocol:     "tcp",
+		Address:      "batman.com:1234",
+		RID:          naming.FixedRoutingID(0xdabbad00),
+		IsMountTable: true,
 	}
 	v2 := &Endpoint{
 		Protocol:      "tcp",
@@ -21,6 +22,7 @@
 		RID:           naming.FixedRoutingID(0xdabbad00),
 		MinIPCVersion: 1,
 		MaxIPCVersion: 10,
+		IsMountTable:  true,
 	}
 	v2hp := &Endpoint{
 		Protocol:      "tcp",
@@ -28,13 +30,22 @@
 		RID:           naming.FixedRoutingID(0x0),
 		MinIPCVersion: 2,
 		MaxIPCVersion: 3,
+		IsMountTable:  true,
 	}
-	v3 := &Endpoint{
+	v3s := &Endpoint{
 		Protocol:      "tcp",
 		Address:       "batman.com:2345",
 		RID:           naming.FixedRoutingID(0x0),
 		MinIPCVersion: 2,
 		MaxIPCVersion: 3,
+		IsMountTable:  false,
+	}
+	v3m := &Endpoint{
+		Protocol:      "tcp",
+		Address:       "batman.com:2345",
+		RID:           naming.FixedRoutingID(0xdabbad00),
+		MinIPCVersion: 2,
+		MaxIPCVersion: 3,
 		IsMountTable:  true,
 	}
 
@@ -56,6 +67,45 @@
 		}
 	}
 
+	// Test v3 endpoints.
+	defaultVersion = 3
+	testcasesC := []struct {
+		Endpoint naming.Endpoint
+		String   string
+		Input    string
+		min, max version.IPCVersion
+		servesMT bool
+	}{
+		{v3s, "@3@tcp@batman.com:2345@00000000000000000000000000000000@2@3@s@@", "", 2, 3, false},
+		{v3m, "@3@tcp@batman.com:2345@000000000000000000000000dabbad00@2@3@m@@", "", 2, 3, true},
+	}
+
+	for _, test := range testcasesC {
+		if got, want := test.Endpoint.String(), test.String; got != want {
+			t.Errorf("Got %q want %q for endpoint %T = %#v", got, want, test.Endpoint, test.Endpoint)
+		}
+		str := test.Input
+		var ep naming.Endpoint
+		var err error
+		if str == "" {
+			str = test.String
+			ep, err = NewEndpoint(str)
+		} else {
+			ep, err = NewEndpoint(naming.FormatEndpoint("tcp", str,
+				version.IPCVersionRange{test.min, test.max},
+				naming.ServesMountTableOpt(test.servesMT)))
+		}
+		if err != nil {
+			t.Errorf("Endpoint(%q) failed with %v", str, err)
+			continue
+		}
+		if !reflect.DeepEqual(ep, test.Endpoint) {
+			t.Errorf("Got endpoint %T = %#v, want %T = %#v for string %q", ep, ep, test.Endpoint, test.Endpoint, str)
+		}
+	}
+
+	// Make sure we can continue to parse and create v2 endpoints.
+	defaultVersion = 2
 	testcasesB := []struct {
 		Endpoint naming.Endpoint
 		String   string
@@ -63,11 +113,9 @@
 		min, max version.IPCVersion
 		servesMT bool
 	}{
-		{v1, "@2@tcp@batman.com:1234@000000000000000000000000dabbad00@@@@", "", version.UnknownIPCVersion, version.UnknownIPCVersion, false},
-		{v2, "@2@tcp@batman.com:2345@000000000000000000000000dabbad00@1@10@@", "", 1, 10, false},
-		{v2hp, "@2@tcp@batman.com:2345@00000000000000000000000000000000@2@3@@", "batman.com:2345", 2, 3, false},
-		// the v3 format is ignored unless explicitly enabled.
-		{v3, "@2@tcp@batman.com:2345@00000000000000000000000000000000@2@3@@", "batman.com:2345", 2, 3, true},
+		{v1, "@2@tcp@batman.com:1234@000000000000000000000000dabbad00@@@@", "", version.UnknownIPCVersion, version.UnknownIPCVersion, true},
+		{v2, "@2@tcp@batman.com:2345@000000000000000000000000dabbad00@1@10@@", "", 1, 10, true},
+		{v2hp, "@2@tcp@batman.com:2345@00000000000000000000000000000000@2@3@@", "batman.com:2345", 2, 3, true},
 	}
 
 	for _, test := range testcasesB {
@@ -93,25 +141,7 @@
 			t.Errorf("Got endpoint %T = %#v, want %T = %#v for string %q", ep, ep, test.Endpoint, test.Endpoint, str)
 		}
 	}
-
-	// Enable V3 endpoints.
 	defaultVersion = 3
-	for _, c := range []struct {
-		servesMT bool
-		str      string
-	}{
-		{true, "@3@tcp@a:10@00000000000000000000000000000000@1@3@m@@"},
-		{false, "@3@tcp@a:10@00000000000000000000000000000000@1@3@s@@"},
-	} {
-		ep, _ := NewEndpoint(naming.FormatEndpoint("tcp", "a:10",
-			version.IPCVersionRange{1, 3},
-			naming.ServesMountTableOpt(c.servesMT)))
-		if got, want := ep.String(), c.str; got != want {
-			t.Errorf("got: %s, want: %s", got, want)
-		}
-	}
-	// Disable V3 endpoints.
-	defaultVersion = 2
 }
 
 type endpointTest struct {
@@ -121,15 +151,17 @@
 
 func TestEndpointDefaults(t *testing.T) {
 	testcases := []endpointTest{
-		{"@1@tcp@batman@@@", "@2@tcp@batman@00000000000000000000000000000000@@@@", nil},
-		{"@2@tcp@robin@@@@@", "@2@tcp@robin@00000000000000000000000000000000@@@@", nil},
-		{"@1@@@@@", "@2@tcp@:0@00000000000000000000000000000000@@@@", nil},
-		{"@2@@@@@@@", "@2@tcp@:0@00000000000000000000000000000000@@@@", nil},
-		{"@1@tcp@batman:12@@@", "@2@tcp@batman:12@00000000000000000000000000000000@@@@", nil},
-		{"@host:10@@", "@2@tcp@host:10@00000000000000000000000000000000@@@@", nil},
-		{"@2@tcp@foo:12@@9@@@", "@2@tcp@foo:12@00000000000000000000000000000000@9@@@", nil},
-		{"@2@tcp@foo:12@@@4@@", "@2@tcp@foo:12@00000000000000000000000000000000@@4@@", nil},
-		{"@2@tcp@foo:12@@2@4@@", "@2@tcp@foo:12@00000000000000000000000000000000@2@4@@", nil},
+		{"@1@tcp@batman@@@", "@3@tcp@batman@00000000000000000000000000000000@@@m@@", nil},
+		{"@2@tcp@robin@@@@@", "@3@tcp@robin@00000000000000000000000000000000@@@m@@", nil},
+		{"@1@@@@@", "@3@tcp@:0@00000000000000000000000000000000@@@m@@", nil},
+		{"@2@@@@@@@", "@3@tcp@:0@00000000000000000000000000000000@@@m@@", nil},
+		{"@1@tcp@batman:12@@@", "@3@tcp@batman:12@00000000000000000000000000000000@@@m@@", nil},
+		{"@host:10@@", "@3@tcp@host:10@00000000000000000000000000000000@@@m@@", nil},
+		{"@2@tcp@foo:12@@9@@@", "@3@tcp@foo:12@00000000000000000000000000000000@9@@m@@", nil},
+		{"@2@tcp@foo:12@@@4@@", "@3@tcp@foo:12@00000000000000000000000000000000@@4@m@@", nil},
+		{"@2@tcp@foo:12@@2@4@@", "@3@tcp@foo:12@00000000000000000000000000000000@2@4@m@@", nil},
+		{"@3@@host:11@@@@m@@", "@3@tcp@host:11@00000000000000000000000000000000@@@m@@", nil},
+		{"@3@@host:12@@@@@@", "@3@tcp@host:12@00000000000000000000000000000000@@@m@@", nil},
 	}
 	runEndpointTests(t, testcases)
 }
@@ -156,8 +188,8 @@
 
 func TestHostPortEndpoint(t *testing.T) {
 	testcases := []endpointTest{
-		{"localhost:10", "@2@tcp@localhost:10@00000000000000000000000000000000@@@@", nil},
-		{"localhost:", "@2@tcp@localhost:@00000000000000000000000000000000@@@@", nil},
+		{"localhost:10", "@3@tcp@localhost:10@00000000000000000000000000000000@@@m@@", nil},
+		{"localhost:", "@3@tcp@localhost:@00000000000000000000000000000000@@@m@@", nil},
 		{"localhost", "", errInvalidEndpointString},
 	}
 	runEndpointTests(t, testcases)
@@ -170,18 +202,21 @@
 		Address:       "batman.com:4444",
 		MinIPCVersion: min,
 		MaxIPCVersion: max,
+		IsMountTable:  true,
 	}
 	ipv4 := &Endpoint{
 		Protocol:      "tcp",
 		Address:       "192.168.1.1:4444",
 		MinIPCVersion: min,
 		MaxIPCVersion: max,
+		IsMountTable:  true,
 	}
 	ipv6 := &Endpoint{
 		Protocol:      "tcp",
 		Address:       "[01:02::]:4444",
 		MinIPCVersion: min,
 		MaxIPCVersion: max,
+		IsMountTable:  true,
 	}
 	testcases := []struct {
 		Endpoint   naming.Endpoint
diff --git a/runtimes/google/naming/namespace/namespace.go b/runtimes/google/naming/namespace/namespace.go
index 26cc469..9d58742 100644
--- a/runtimes/google/naming/namespace/namespace.go
+++ b/runtimes/google/naming/namespace/namespace.go
@@ -4,6 +4,8 @@
 	"sync"
 	"time"
 
+	inaming "veyron.io/veyron/veyron/runtimes/google/naming"
+
 	"veyron.io/veyron/veyron2"
 	"veyron.io/veyron/veyron2/naming"
 	"veyron.io/veyron/veyron2/verror"
@@ -125,9 +127,11 @@
 		}
 		return e, false
 	}
-	// TODO(p): right now I assume any address handed to me to be resolved is a mount table.
-	// Eventually we should do something like the following:
-	e.SetServesMountTable(true)
+	servesMT := true
+	if ep, err := inaming.NewEndpoint(address); err == nil {
+		servesMT = ep.ServesMountTable()
+	}
+	e.SetServesMountTable(servesMT)
 	e.Name = suffix
 	e.Servers = append(e.Servers, naming.MountedServer{Server: naming.JoinAddressName(address, ""), Expires: expiration})
 	return e, true