Browse Source

go.crypto/otr: expose IsEncrypted.

It's useful to expose this bit of state because, although callers can
keep track of it themselves, it's already in the Conversation so it
seems wasteful to make them do so.

R=golang-dev, ioerror
CC=golang-dev
https://golang.org/cl/6724056
Adam Langley 13 years ago
parent
commit
c9c0e06eed
2 changed files with 21 additions and 0 deletions
  1. 7 0
      otr/otr.go
  2. 14 0
      otr/otr_test.go

+ 7 - 0
otr/otr.go

@@ -508,6 +508,13 @@ func (c *Conversation) End() (toSend [][]byte) {
 	panic("unreachable")
 }
 
+// IsEncrypted returns true if a message passed to Send would be encrypted
+// before transmission. This result remains valid until the next call to
+// Receive or End, which may change the state of the Conversation.
+func (c *Conversation) IsEncrypted() bool {
+	return c.state == stateEncrypted
+}
+
 var fragmentError = errors.New("otr: invalid OTR fragment")
 
 // processFragment processes a fragmented OTR message and possibly returns a

+ 14 - 0
otr/otr_test.go

@@ -139,6 +139,13 @@ func TestConversation(t *testing.T) {
 	var err error
 	alicesMessage = append(alicesMessage, []byte(QueryMessage))
 
+	if alice.IsEncrypted() {
+		t.Error("Alice believes that the conversation is secure before we've started")
+	}
+	if bob.IsEncrypted() {
+		t.Error("Bob believes that the conversation is secure before we've started")
+	}
+
 	for round := 0; len(alicesMessage) > 0 || len(bobsMessage) > 0; round++ {
 		bobsMessage = nil
 		for i, msg := range alicesMessage {
@@ -180,6 +187,13 @@ func TestConversation(t *testing.T) {
 		t.Errorf("Session identifiers don't match. Alice has %x, Bob has %x", alice.SSID[:], bob.SSID[:])
 	}
 
+	if !alice.IsEncrypted() {
+		t.Error("Alice doesn't believe that the conversation is secure")
+	}
+	if !bob.IsEncrypted() {
+		t.Error("Bob doesn't believe that the conversation is secure")
+	}
+
 	var testMessage = []byte("hello Bob")
 	alicesMessage, err = alice.Send(testMessage)
 	for i, msg := range alicesMessage {