This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TCPCL: Keepalive/Session Upkeep Message
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package tcpcl | ||
|
||
import "fmt" | ||
|
||
// KEEPALIVE is the Message Header code for a Keepalive Message. | ||
const KEEPALIVE uint8 = 0x04 | ||
|
||
// KeepaliveMessage is the KEEPALIVE message for session upkeep. | ||
type KeepaliveMessage uint8 | ||
|
||
// NewKeepaliveMessage creates a new KeepaliveMessage. | ||
func NewKeepaliveMessage() KeepaliveMessage { | ||
return KeepaliveMessage(KEEPALIVE) | ||
} | ||
|
||
func (_ KeepaliveMessage) String() string { | ||
return "KEEPALIVE" | ||
} | ||
|
||
// MarshalBinary encodes this KeepaliveMessage into its binary form. | ||
func (km KeepaliveMessage) MarshalBinary() (data []byte, err error) { | ||
if uint8(km) != KEEPALIVE { | ||
err = fmt.Errorf("KEEPALIVE's value is %d instead of %d", uint8(km), KEEPALIVE) | ||
return | ||
} | ||
|
||
data = []byte{KEEPALIVE} | ||
return | ||
} | ||
|
||
// UnmarshalBinary decodes a KeepaliveMessage from its binary form. | ||
func (km *KeepaliveMessage) UnmarshalBinary(data []byte) error { | ||
if len(data) != 1 { | ||
return fmt.Errorf("KEEPALIVE's octet length is %d instead of 1", len(data)) | ||
} | ||
|
||
if x := uint8(data[0]); x != KEEPALIVE { | ||
return fmt.Errorf("KEEPALIVE's value is %d instead of %d", x, KEEPALIVE) | ||
} else { | ||
*km = KeepaliveMessage(x) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tcpcl | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestKeepaliveMessage(t *testing.T) { | ||
tests := []struct { | ||
valid bool | ||
data []byte | ||
}{ | ||
{true, []byte{KEEPALIVE}}, | ||
{false, []byte{0x21}}, | ||
{false, []byte{}}, | ||
{false, []byte{0x23, 0x42}}, | ||
} | ||
|
||
for _, test := range tests { | ||
var km KeepaliveMessage | ||
|
||
if err := km.UnmarshalBinary(test.data); (err == nil) != test.valid { | ||
t.Fatalf("Error state was not expected; valid := %t, got := %v", test.valid, err) | ||
} else if !test.valid { | ||
continue | ||
} | ||
|
||
if data, err := NewKeepaliveMessage().MarshalBinary(); err != nil { | ||
t.Fatal(err) | ||
} else if !bytes.Equal(data, test.data) { | ||
t.Fatalf("Data does not match, expected %x and got %x", test.data, data) | ||
} | ||
|
||
} | ||
} |