Close on unknown message type
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"compress/flate"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -193,6 +194,7 @@ func (c *Connection) readerLoop() {
|
||||
break
|
||||
}
|
||||
if hdr.version != 0 {
|
||||
log.Printf("Protocol error: %s: unknown message version %#x", c.ID, hdr.version)
|
||||
c.close()
|
||||
break
|
||||
}
|
||||
@@ -258,6 +260,10 @@ func (c *Connection) readerLoop() {
|
||||
delete(c.awaiting, hdr.msgID)
|
||||
c.wLock.Unlock()
|
||||
}
|
||||
|
||||
default:
|
||||
log.Printf("Protocol error: %s: unknown message type %#x", c.ID, hdr.msgType)
|
||||
c.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,3 +157,25 @@ func TestVersionErr(t *testing.T) {
|
||||
t.Error("Connection should close due to unknown version")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypeErr(t *testing.T) {
|
||||
m0 := &TestModel{}
|
||||
m1 := &TestModel{}
|
||||
|
||||
ar, aw := io.Pipe()
|
||||
br, bw := io.Pipe()
|
||||
|
||||
c0 := NewConnection("c0", ar, bw, m0)
|
||||
NewConnection("c1", br, aw, m1)
|
||||
|
||||
c0.mwriter.writeHeader(header{
|
||||
version: 0,
|
||||
msgID: 0,
|
||||
msgType: 42,
|
||||
})
|
||||
c0.flush()
|
||||
|
||||
if !m1.closed {
|
||||
t.Error("Connection should close due to unknown message type")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user