aboutsummaryrefslogtreecommitdiffstats
path: root/pktline
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2015-10-23 12:05:02 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2015-10-23 12:05:02 +0200
commit214e1dca024fb6da5ed65564d2de734df5dc2127 (patch)
tree0c2805a0676006690a8053ae8ccee388ccfc83fe /pktline
parent70923099e61fa33f0bc5256d2f938fa44c4df10e (diff)
downloadgo-git-214e1dca024fb6da5ed65564d2de734df5dc2127.tar.gz
format/pktline: review and improving coverage
Diffstat (limited to 'pktline')
-rw-r--r--pktline/decoder.go106
-rw-r--r--pktline/decoder_test.go70
-rw-r--r--pktline/doc.go56
-rw-r--r--pktline/encoder.go56
-rw-r--r--pktline/encoder_test.go34
5 files changed, 0 insertions, 322 deletions
diff --git a/pktline/decoder.go b/pktline/decoder.go
deleted file mode 100644
index 76be108..0000000
--- a/pktline/decoder.go
+++ /dev/null
@@ -1,106 +0,0 @@
-package pktline
-
-import (
- "errors"
- "fmt"
- "io"
- "strconv"
-)
-
-var (
- ErrUnderflow = errors.New("unexpected string length (underflow)")
- ErrInvalidHeader = errors.New("invalid header")
- ErrInvalidLen = errors.New("invalid length")
-)
-
-type Decoder struct {
- r io.Reader
-}
-
-func NewDecoder(r io.Reader) *Decoder {
- return &Decoder{r}
-}
-
-func (d *Decoder) readLine() (string, error) {
- raw := make([]byte, HEADER_LENGTH)
- if _, err := d.r.Read(raw); err != nil {
- return "", err
- }
- header, err := strconv.ParseInt(string(raw), 16, 16)
- if err != nil {
- fmt.Println(err)
-
- return "", ErrInvalidHeader
- }
-
- if header == 0 {
- return "", nil
- }
-
- exp := int(header - HEADER_LENGTH)
- if exp < 0 {
- return "", ErrInvalidLen
- }
-
- line := make([]byte, exp)
- if read, err := d.r.Read(line); err != nil {
- return "", err
- } else if read != exp {
- return "", ErrUnderflow
- }
-
- return string(line), nil
-}
-
-func (d *Decoder) ReadLine() (string, error) {
- return d.readLine()
-}
-
-func (d *Decoder) ReadBlock() ([]string, error) {
- o := make([]string, 0)
-
- for {
- line, err := d.readLine()
- if err == io.EOF {
- return o, nil
- }
-
- if err != nil {
- return o, err
- }
-
- if err == nil && line == "" {
- return o, nil
- }
-
- o = append(o, line)
- }
-
- return o, nil
-}
-
-func (d *Decoder) ReadAll() ([]string, error) {
- result, err := d.ReadBlock()
- if err != nil {
- return result, err
- }
-
- for {
- lines, err := d.ReadBlock()
- if err == io.EOF {
- return result, nil
- }
-
- if err != nil {
- return result, err
- }
-
- if err == nil && len(lines) == 0 {
- return result, nil
- }
-
- result = append(result, lines...)
- }
-
- return result, nil
-}
diff --git a/pktline/decoder_test.go b/pktline/decoder_test.go
deleted file mode 100644
index 7899cc8..0000000
--- a/pktline/decoder_test.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package pktline
-
-import (
- "strings"
- "testing"
-
- . "gopkg.in/check.v1"
-)
-
-func Test(t *testing.T) { TestingT(t) }
-
-type DecoderSuite struct{}
-
-var _ = Suite(&DecoderSuite{})
-
-func (s *DecoderSuite) TestReadLine(c *C) {
- j := &Decoder{strings.NewReader("0006a\n")}
-
- line, err := j.ReadLine()
- c.Assert(err, IsNil)
- c.Assert(line, Equals, "a\n")
-}
-
-func (s *DecoderSuite) TestReadLineBufferUnderflow(c *C) {
- j := &Decoder{strings.NewReader("00e7a\n")}
-
- line, err := j.ReadLine()
- c.Assert(err, Equals, ErrUnderflow)
- c.Assert(line, Equals, "")
-}
-
-func (s *DecoderSuite) TestReadLineBufferInvalidLen(c *C) {
- j := &Decoder{strings.NewReader("0001foo\n")}
-
- line, err := j.ReadLine()
- c.Assert(err, Equals, ErrInvalidLen)
- c.Assert(line, Equals, "")
-}
-
-func (s *DecoderSuite) TestReadBlock(c *C) {
- j := &Decoder{strings.NewReader("0006a\n")}
-
- lines, err := j.ReadBlock()
- c.Assert(err, IsNil)
- c.Assert(lines, HasLen, 1)
- c.Assert(lines[0], Equals, "a\n")
-}
-
-func (s *DecoderSuite) TestReadBlockWithFlush(c *C) {
- j := &Decoder{strings.NewReader("0006a\n0006b\n00000006c\n")}
-
- lines, err := j.ReadBlock()
- c.Assert(err, IsNil)
- c.Assert(lines, HasLen, 2)
- c.Assert(lines[0], Equals, "a\n")
- c.Assert(lines[1], Equals, "b\n")
-}
-
-func (s *DecoderSuite) TestReadAll(c *C) {
- j := &Decoder{strings.NewReader("0006a\n0006b\n00000006c\n0006d\n0006e\n")}
-
- lines, err := j.ReadAll()
- c.Assert(err, IsNil)
- c.Assert(lines, HasLen, 5)
- c.Assert(lines[0], Equals, "a\n")
- c.Assert(lines[1], Equals, "b\n")
- c.Assert(lines[2], Equals, "c\n")
- c.Assert(lines[3], Equals, "d\n")
- c.Assert(lines[4], Equals, "e\n")
-}
diff --git a/pktline/doc.go b/pktline/doc.go
deleted file mode 100644
index a976b54..0000000
--- a/pktline/doc.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package pktline
-
-// pkt-line Format
-// ---------------
-//
-// Much (but not all) of the payload is described around pkt-lines.
-//
-// A pkt-line is a variable length binary string. The first four bytes
-// of the line, the pkt-len, indicates the total length of the line,
-// in hexadecimal. The pkt-len includes the 4 bytes used to contain
-// the length's hexadecimal representation.
-//
-// A pkt-line MAY contain binary data, so implementors MUST ensure
-// pkt-line parsing/formatting routines are 8-bit clean.
-//
-// A non-binary line SHOULD BE terminated by an LF, which if present
-// MUST be included in the total length.
-//
-// The maximum length of a pkt-line's data component is 65520 bytes.
-// Implementations MUST NOT send pkt-line whose length exceeds 65524
-// (65520 bytes of payload + 4 bytes of length data).
-//
-// Implementations SHOULD NOT send an empty pkt-line ("0004").
-//
-// A pkt-line with a length field of 0 ("0000"), called a flush-pkt,
-// is a special case and MUST be handled differently than an empty
-// pkt-line ("0004").
-//
-// ----
-// pkt-line = data-pkt / flush-pkt
-//
-// data-pkt = pkt-len pkt-payload
-// pkt-len = 4*(HEXDIG)
-// pkt-payload = (pkt-len - 4)*(OCTET)
-//
-// flush-pkt = "0000"
-// ----
-//
-// Examples (as C-style strings):
-//
-// ----
-// pkt-line actual value
-// ---------------------------------
-// "0006a\n" "a\n"
-// "0005a" "a"
-// "000bfoobar\n" "foobar\n"
-// "0004" ""
-// ----
-//
-// Extracted from:
-// https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt
-
-const (
- HEADER_LENGTH = 4
- MAX_LENGTH = 65524
-)
diff --git a/pktline/encoder.go b/pktline/encoder.go
deleted file mode 100644
index 9c00126..0000000
--- a/pktline/encoder.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package pktline
-
-import (
- "errors"
- "fmt"
- "strings"
-)
-
-var (
- ErrOverflow = errors.New("unexpected string length (overflow)")
-)
-
-type Encoder struct {
- lines []string
-}
-
-func NewEncoder() *Encoder {
- return &Encoder{make([]string, 0)}
-}
-
-func (e *Encoder) AddLine(line string) error {
- le, err := EncodeFromString(line + "\n")
- if err != nil {
- return err
- }
-
- e.lines = append(e.lines, le)
- return nil
-}
-
-func (e *Encoder) AddFlush() {
- e.lines = append(e.lines, "0000")
-}
-
-func (e *Encoder) GetReader() *strings.Reader {
- data := strings.Join(e.lines, "")
-
- return strings.NewReader(data)
-}
-
-func EncodeFromString(line string) (string, error) {
- return Encode([]byte(line))
-}
-
-func Encode(line []byte) (string, error) {
- if line == nil {
- return "0000", nil
- }
-
- l := len(line) + HEADER_LENGTH
- if l > MAX_LENGTH {
- return "", ErrOverflow
- }
-
- return fmt.Sprintf("%04x%s", l, line), nil
-}
diff --git a/pktline/encoder_test.go b/pktline/encoder_test.go
deleted file mode 100644
index 091ad1c..0000000
--- a/pktline/encoder_test.go
+++ /dev/null
@@ -1,34 +0,0 @@
-package pktline
-
-import (
- "io/ioutil"
-
- . "gopkg.in/check.v1"
-)
-
-type EncoderSuite struct{}
-
-var _ = Suite(&EncoderSuite{})
-
-func (s *EncoderSuite) TestEncode(c *C) {
- line, err := Encode([]byte("a\n"))
- c.Assert(err, IsNil)
- c.Assert(string(line), Equals, "0006a\n")
-}
-
-func (s *EncoderSuite) TestEncodeFromString(c *C) {
- line, err := EncodeFromString("a\n")
- c.Assert(err, IsNil)
- c.Assert(string(line), Equals, "0006a\n")
-}
-
-func (s *EncoderSuite) TestEncoder(c *C) {
- e := NewEncoder()
- e.AddLine("a")
- e.AddFlush()
- e.AddLine("b")
-
- r := e.GetReader()
- a, _ := ioutil.ReadAll(r)
- c.Assert(string(a), Equals, "0006a\n00000006b\n")
-}