github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/framer/framer_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package framer
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"io/ioutil"
    23  	"testing"
    24  )
    25  
    26  func TestRead(t *testing.T) {
    27  	data := []byte{
    28  		0x00, 0x00, 0x00, 0x04,
    29  		0x01, 0x02, 0x03, 0x04,
    30  		0x00, 0x00, 0x00, 0x03,
    31  		0x05, 0x06, 0x07,
    32  		0x00, 0x00, 0x00, 0x00,
    33  		0x00, 0x00, 0x00, 0x01,
    34  		0x08,
    35  	}
    36  	b := bytes.NewBuffer(data)
    37  	r := NewLengthDelimitedFrameReader(ioutil.NopCloser(b))
    38  	buf := make([]byte, 1)
    39  	if n, err := r.Read(buf); err != io.ErrShortBuffer && n != 1 && bytes.Equal(buf, []byte{0x01}) {
    40  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    41  	}
    42  	if n, err := r.Read(buf); err != io.ErrShortBuffer && n != 1 && bytes.Equal(buf, []byte{0x02}) {
    43  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    44  	}
    45  	// read the remaining frame
    46  	buf = make([]byte, 2)
    47  	if n, err := r.Read(buf); err != nil && n != 2 && bytes.Equal(buf, []byte{0x03, 0x04}) {
    48  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    49  	}
    50  	// read with buffer equal to frame
    51  	buf = make([]byte, 3)
    52  	if n, err := r.Read(buf); err != nil && n != 3 && bytes.Equal(buf, []byte{0x05, 0x06, 0x07}) {
    53  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    54  	}
    55  	// read empty frame
    56  	buf = make([]byte, 3)
    57  	if n, err := r.Read(buf); err != nil && n != 0 && bytes.Equal(buf, []byte{}) {
    58  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    59  	}
    60  	// read with larger buffer than frame
    61  	buf = make([]byte, 3)
    62  	if n, err := r.Read(buf); err != nil && n != 1 && bytes.Equal(buf, []byte{0x08}) {
    63  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    64  	}
    65  	// read EOF
    66  	if n, err := r.Read(buf); err != io.EOF && n != 0 {
    67  		t.Fatalf("unexpected: %v %d", err, n)
    68  	}
    69  }
    70  
    71  func TestReadLarge(t *testing.T) {
    72  	data := []byte{
    73  		0x00, 0x00, 0x00, 0x04,
    74  		0x01, 0x02, 0x03, 0x04,
    75  		0x00, 0x00, 0x00, 0x03,
    76  		0x05, 0x06, 0x07,
    77  		0x00, 0x00, 0x00, 0x00,
    78  		0x00, 0x00, 0x00, 0x01,
    79  		0x08,
    80  	}
    81  	b := bytes.NewBuffer(data)
    82  	r := NewLengthDelimitedFrameReader(ioutil.NopCloser(b))
    83  	buf := make([]byte, 40)
    84  	if n, err := r.Read(buf); err != nil && n != 4 && bytes.Equal(buf, []byte{0x01, 0x02, 0x03, 0x04}) {
    85  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    86  	}
    87  	if n, err := r.Read(buf); err != nil && n != 3 && bytes.Equal(buf, []byte{0x05, 0x06, 0x7}) {
    88  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    89  	}
    90  	if n, err := r.Read(buf); err != nil && n != 0 && bytes.Equal(buf, []byte{}) {
    91  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    92  	}
    93  	if n, err := r.Read(buf); err != nil && n != 1 && bytes.Equal(buf, []byte{0x08}) {
    94  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
    95  	}
    96  	// read EOF
    97  	if n, err := r.Read(buf); err != io.EOF && n != 0 {
    98  		t.Fatalf("unexpected: %v %d", err, n)
    99  	}
   100  }
   101  func TestReadInvalidFrame(t *testing.T) {
   102  	data := []byte{
   103  		0x00, 0x00, 0x00, 0x04,
   104  		0x01, 0x02,
   105  	}
   106  	b := bytes.NewBuffer(data)
   107  	r := NewLengthDelimitedFrameReader(ioutil.NopCloser(b))
   108  	buf := make([]byte, 1)
   109  	if n, err := r.Read(buf); err != io.ErrShortBuffer && n != 1 && bytes.Equal(buf, []byte{0x01}) {
   110  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
   111  	}
   112  	// read the remaining frame
   113  	buf = make([]byte, 3)
   114  	if n, err := r.Read(buf); err != io.ErrUnexpectedEOF && n != 1 && bytes.Equal(buf, []byte{0x02}) {
   115  		t.Fatalf("unexpected: %v %d %v", err, n, buf)
   116  	}
   117  	// read EOF
   118  	if n, err := r.Read(buf); err != io.EOF && n != 0 {
   119  		t.Fatalf("unexpected: %v %d", err, n)
   120  	}
   121  }
   122  
   123  func TestJSONFrameReader(t *testing.T) {
   124  	b := bytes.NewBufferString("{\"test\":true}\n1\n[\"a\"]")
   125  	r := NewJSONFramedReader(ioutil.NopCloser(b))
   126  	buf := make([]byte, 20)
   127  	if n, err := r.Read(buf); err != nil || n != 13 || string(buf[:n]) != `{"test":true}` {
   128  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   129  	}
   130  	if n, err := r.Read(buf); err != nil || n != 1 || string(buf[:n]) != `1` {
   131  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   132  	}
   133  	if n, err := r.Read(buf); err != nil || n != 5 || string(buf[:n]) != `["a"]` {
   134  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   135  	}
   136  	if n, err := r.Read(buf); err != io.EOF || n != 0 {
   137  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   138  	}
   139  }
   140  
   141  func TestJSONFrameReaderShortBuffer(t *testing.T) {
   142  	b := bytes.NewBufferString("{\"test\":true}\n1\n[\"a\"]")
   143  	r := NewJSONFramedReader(ioutil.NopCloser(b))
   144  	buf := make([]byte, 3)
   145  
   146  	if n, err := r.Read(buf); err != io.ErrShortBuffer || n != 3 || string(buf[:n]) != `{"t` {
   147  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   148  	}
   149  	if n, err := r.Read(buf); err != io.ErrShortBuffer || n != 3 || string(buf[:n]) != `est` {
   150  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   151  	}
   152  	if n, err := r.Read(buf); err != io.ErrShortBuffer || n != 3 || string(buf[:n]) != `":t` {
   153  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   154  	}
   155  	if n, err := r.Read(buf); err != io.ErrShortBuffer || n != 3 || string(buf[:n]) != `rue` {
   156  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   157  	}
   158  	if n, err := r.Read(buf); err != nil || n != 1 || string(buf[:n]) != `}` {
   159  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   160  	}
   161  
   162  	if n, err := r.Read(buf); err != nil || n != 1 || string(buf[:n]) != `1` {
   163  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   164  	}
   165  
   166  	if n, err := r.Read(buf); err != io.ErrShortBuffer || n != 3 || string(buf[:n]) != `["a` {
   167  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   168  	}
   169  	if n, err := r.Read(buf); err != nil || n != 2 || string(buf[:n]) != `"]` {
   170  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   171  	}
   172  
   173  	if n, err := r.Read(buf); err != io.EOF || n != 0 {
   174  		t.Fatalf("unexpected: %v %d %q", err, n, buf)
   175  	}
   176  }