lib/security: change ordering of blessing patterns in DebugString to favor ...

This change ensures that the '...' (AllPrincipals) blessing pattern entries
appear first when dumping the blessing store; with all the other patterns
sorted alphabetically afterwards.

There's no strong philosophical argument in favor of this, other than the
convenience of seeing the entry corresponding to 'all principals' in a
recognizable position.  My self-serving impetus to make this change is
v.io/i/680.

Change-Id: I0d653d9c2d767b6aa3d4d22e21af4ddc2b7b7132
diff --git a/lib/security/blessingstore.go b/lib/security/blessingstore.go
index 0b09ee8..587a38b 100644
--- a/lib/security/blessingstore.go
+++ b/lib/security/blessingstore.go
@@ -232,14 +232,20 @@
 	buff := bytes.NewBufferString(fmt.Sprintf(format, "Default Blessings", bs.state.DefaultBlessings))
 
 	buff.WriteString(fmt.Sprintf(format, "Peer pattern", "Blessings"))
-
+	writePattern := func(pattern security.BlessingPattern) {
+		buff.WriteString(fmt.Sprintf(format, pattern, bs.state.PeerBlessings[security.BlessingPattern(pattern)]))
+	}
 	sorted := make([]string, 0, len(bs.state.PeerBlessings))
 	for k, _ := range bs.state.PeerBlessings {
-		sorted = append(sorted, string(k))
+		if k == security.AllPrincipals {
+			writePattern(k)
+		} else {
+			sorted = append(sorted, string(k))
+		}
 	}
 	sort.Strings(sorted)
 	for _, pattern := range sorted {
-		buff.WriteString(fmt.Sprintf(format, pattern, bs.state.PeerBlessings[security.BlessingPattern(pattern)]))
+		writePattern(security.BlessingPattern(pattern))
 	}
 	return buff.String()
 }
diff --git a/lib/security/blessingstore_test.go b/lib/security/blessingstore_test.go
index 01cc3df..8f4d1cc 100644
--- a/lib/security/blessingstore_test.go
+++ b/lib/security/blessingstore_test.go
@@ -9,6 +9,7 @@
 	"io/ioutil"
 	"os"
 	"reflect"
+	"regexp"
 	"testing"
 
 	"v.io/v23/security"
@@ -266,3 +267,26 @@
 		t.Errorf("Got (%v, %v) want (%v, nil)", old, err, bob)
 	}
 }
+
+func TestBlessingStoreDebugString(t *testing.T) {
+	p, err := NewPrincipal()
+	if err != nil {
+		t.Fatal(err)
+	}
+	var (
+		alice = blessSelf(p, "alice")
+		bob   = blessSelf(p, "bob")
+		s     = p.BlessingStore()
+	)
+	if _, err := s.Set(alice, security.AllPrincipals); err != nil {
+		t.Errorf("Got error %v", err)
+	}
+	if _, err := s.Set(bob, "-patternstartingwithdash"); err != nil {
+		t.Errorf("Got error %v", err)
+	}
+	blessingPatternsRE := regexp.MustCompile("Peer pattern[^\n]*\n...[^\n]*\n-patternstartingwithdash")
+	if ds := s.DebugString(); !blessingPatternsRE.MatchString(ds) {
+		t.Errorf("DebugString (%s) doesn't match", ds)
+	}
+
+}