Просмотр исходного кода

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 лет назад
Родитель
Сommit
c9c0e06eed
2 измененных файлов с 21 добавлено и 0 удалено
  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")
 	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")
 var fragmentError = errors.New("otr: invalid OTR fragment")
 
 
 // processFragment processes a fragmented OTR message and possibly returns a
 // 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
 	var err error
 	alicesMessage = append(alicesMessage, []byte(QueryMessage))
 	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++ {
 	for round := 0; len(alicesMessage) > 0 || len(bobsMessage) > 0; round++ {
 		bobsMessage = nil
 		bobsMessage = nil
 		for i, msg := range alicesMessage {
 		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[:])
 		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")
 	var testMessage = []byte("hello Bob")
 	alicesMessage, err = alice.Send(testMessage)
 	alicesMessage, err = alice.Send(testMessage)
 	for i, msg := range alicesMessage {
 	for i, msg := range alicesMessage {