2 netstring -- netstring format serialization library
3 Copyright (C) 2015-2020 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 func TestTrivial(t *testing.T) {
27 buf := bytes.NewBuffer(nil)
29 if n, err := w.Write([]byte{}); err != nil || n != 3 || string(buf.Next(3)) != "0:," {
32 if n, err := w.Write([]byte("foo")); err != nil || n != 6 {
35 if n, err := w.Write([]byte("barz")); err != nil || n != 7 {
38 if string(buf.Bytes()) != "3:foo,4:barz," {
42 if n, err := r.Iter(); err != nil || n != 3 {
45 if r.Discard() != nil {
48 if n, err := r.Iter(); err != nil || n != 4 {
51 m := make([]byte, int(4))
55 if bytes.Compare(m, []byte("barz")) != 0 {
60 func TestSymmetric(t *testing.T) {
61 buf := bytes.NewBuffer(nil)
64 f := func(i uint8, data []byte) bool {
66 for n = 0; n < i; n++ {
67 if _, err := w.Write(data); err != nil {
71 for n = 0; n < i; n++ {
73 if err != nil || int(size) != len(data) {
76 read := make([]byte, size)
77 if r.Read(read) != nil {
80 if bytes.Compare(read, data) != 0 {
86 if err := quick.Check(f, nil); err != nil {
91 func TestErrors(t *testing.T) {
92 b := bytes.NewBufferString("junk")
94 if _, err := r.Iter(); err == nil {
98 b = bytes.NewBufferString("1111111111111111111111:")
100 if _, err := r.Iter(); err == nil {
104 b = bytes.NewBufferString("6foobar")
106 if _, err := r.Iter(); err == nil {
110 data := make([]byte, 1<<10)
112 b = bytes.NewBufferString("0:foobar,")
114 if _, err := r.Iter(); err != nil {
117 if r.Read(data) == nil {
121 b = bytes.NewBufferString("0:foobar")
123 if _, err := r.Iter(); err != nil {
126 if r.Read(data) == nil {
130 b = bytes.NewBufferString("6:foobar")
132 if _, err := r.Iter(); err != nil {
135 if r.Read(data) == nil {