2 netstring -- netstring format serialization library
3 Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 func TestTrivial(t *testing.T) {
28 buf := bytes.NewBuffer(nil)
30 if n, err := w.Write([]byte{}); err != nil || n != 3 || string(buf.Next(3)) != "0:," {
33 if n, err := w.Write([]byte("foo")); err != nil || n != 6 {
36 if n, err := w.Write([]byte("barz")); err != nil || n != 7 {
39 if string(buf.Bytes()) != "3:foo,4:barz," {
43 if n, err := r.Iter(); err != nil || n != 3 {
46 if r.Discard() != nil {
49 if n, err := r.Iter(); err != nil || n != 4 {
52 m := make([]byte, int(4))
56 if bytes.Compare(m, []byte("barz")) != 0 {
61 func TestSymmetric(t *testing.T) {
62 buf := bytes.NewBuffer(nil)
65 f := func(i uint8, data []byte) bool {
67 for n = 0; n < i; n++ {
68 if _, err := w.Write(data); err != nil {
72 for n = 0; n < i; n++ {
74 if err != nil || int(size) != len(data) {
77 read := make([]byte, size)
78 if r.Read(read) != nil {
81 if bytes.Compare(read, data) != 0 {
87 if err := quick.Check(f, nil); err != nil {
92 func TestErrors(t *testing.T) {
93 b := bytes.NewBufferString("junk")
95 if _, err := r.Iter(); err == nil {
99 b = bytes.NewBufferString("1111111111111111111111:")
101 if _, err := r.Iter(); err == nil {
105 b = bytes.NewBufferString("6foobar")
107 if _, err := r.Iter(); err == nil {
111 data := make([]byte, 1<<10)
113 b = bytes.NewBufferString("0:foobar,")
115 if _, err := r.Iter(); err != nil {
118 if r.Read(data) == nil {
122 b = bytes.NewBufferString("0:foobar")
124 if _, err := r.Iter(); err != nil {
127 if r.Read(data) == nil {
131 b = bytes.NewBufferString("6:foobar")
133 if _, err := r.Iter(); err != nil {
136 if r.Read(data) == nil {