github.com/cilium/cilium@v1.16.2/pkg/safeio/safeio_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package safeio
     5  
     6  import (
     7  	"errors"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestReadLimitExceeds(t *testing.T) {
    13  	str := "this string is 28 bytes long"
    14  	r := strings.NewReader(str)
    15  	buf, err := ReadAllLimit(r, ByteSize(30))
    16  	if err != nil {
    17  		t.Fatalf("did not exepect error: %v", err)
    18  	}
    19  	if string(buf) != str {
    20  		t.Fatalf("returned buffer %s did not match the expected value %s", string(buf), str)
    21  	}
    22  }
    23  
    24  func TestReadLimitIsLess(t *testing.T) {
    25  	limit := int64(27)
    26  	str := "this string is 28 bytes long"
    27  	r := strings.NewReader(str)
    28  	buf, err := ReadAllLimit(r, ByteSize(limit))
    29  
    30  	if !errors.Is(err, ErrLimitReached) {
    31  		t.Fatalf("expected err, %v, got: %v", ErrLimitReached, err)
    32  	}
    33  	if string(buf) != str[:limit] {
    34  		t.Fatalf("returned buffer %q did not match the expected value %q", string(buf), str[:limit])
    35  	}
    36  }
    37  
    38  func TestReadLimitIsEqual(t *testing.T) {
    39  	limit := int64(28)
    40  	str := "this string is 28 bytes long"
    41  	r := strings.NewReader(str)
    42  	buf, err := ReadAllLimit(r, ByteSize(limit))
    43  	if err != nil {
    44  		t.Fatalf("did not exepect error: %v", err)
    45  	}
    46  	if string(buf) != str {
    47  		t.Fatalf("returned buffer %s did not match the expected value %s", string(buf), str)
    48  	}
    49  }
    50  
    51  func TestReadLimitExceedsLargeBuffer(t *testing.T) {
    52  	limit := KB * 2
    53  	str := strings.Repeat(" ", 1024)
    54  	r := strings.NewReader(str)
    55  	buf, err := ReadAllLimit(r, limit)
    56  	if err != nil {
    57  		t.Fatalf("did not exepect error: %v", err)
    58  	}
    59  	if string(buf) != str {
    60  		t.Fatal("returned buffer did not match the expected value")
    61  	}
    62  }
    63  
    64  func TestReadLimitIsLessLargeBuffer(t *testing.T) {
    65  	limit := KB
    66  	str := strings.Repeat(" ", 2048)
    67  	r := strings.NewReader(str)
    68  	buf, err := ReadAllLimit(r, limit)
    69  	if !errors.Is(err, ErrLimitReached) {
    70  		t.Fatalf("expected err, %v, got: %v", ErrLimitReached, err)
    71  	}
    72  	if string(buf) == str {
    73  		t.Fatal("returned buffer did not match the expected value")
    74  	}
    75  }
    76  
    77  func TestReadLimitIsEqualLargeBuffer(t *testing.T) {
    78  	limit := KB
    79  	str := strings.Repeat(" ", int(limit))
    80  	r := strings.NewReader(str)
    81  	buf, err := ReadAllLimit(r, limit)
    82  	if err != nil {
    83  		t.Fatalf("did not exepect error: %v", err)
    84  	}
    85  	if string(buf) != str {
    86  		t.Fatal("returned buffer did not match the expected value")
    87  	}
    88  }