|
| 1 | +/* |
| 2 | +Copyright 2023 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package streams |
| 15 | + |
| 16 | +import ( |
| 17 | + "io" |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestLimitReadCloser(t *testing.T) { |
| 25 | + t.Run("stream shorter than limit", func(t *testing.T) { |
| 26 | + s := LimitReadCloser(io.NopCloser(strings.NewReader("e ho guardato dentro un'emozione")), 1000) |
| 27 | + read, err := io.ReadAll(s) |
| 28 | + require.NoError(t, err) |
| 29 | + require.Equal(t, "e ho guardato dentro un'emozione", string(read)) |
| 30 | + |
| 31 | + // Reading again should return io.EOF |
| 32 | + n, err := s.Read(read) |
| 33 | + require.ErrorIs(t, err, io.EOF) |
| 34 | + require.Equal(t, 0, n) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("stream has same length as limit", func(t *testing.T) { |
| 38 | + s := LimitReadCloser(io.NopCloser(strings.NewReader("e ci ho visto dentro tanto amore")), 32) |
| 39 | + read, err := io.ReadAll(s) |
| 40 | + require.NoError(t, err) |
| 41 | + require.Equal(t, "e ci ho visto dentro tanto amore", string(read)) |
| 42 | + |
| 43 | + // Reading again should return io.EOF |
| 44 | + n, err := s.Read(read) |
| 45 | + require.ErrorIs(t, err, io.EOF) |
| 46 | + require.Equal(t, 0, n) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("stream longer than limit", func(t *testing.T) { |
| 50 | + s := LimitReadCloser(io.NopCloser(strings.NewReader("che ho capito perche' non si comanda al cuore")), 21) |
| 51 | + read, err := io.ReadAll(s) |
| 52 | + require.Error(t, err) |
| 53 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 54 | + require.Equal(t, "che ho capito perche'", string(read)) |
| 55 | + |
| 56 | + // Reading again should return ErrStreamTooLarge again |
| 57 | + n, err := s.Read(read) |
| 58 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 59 | + require.Equal(t, 0, n) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("stream longer than limit, read with byte slice", func(t *testing.T) { |
| 63 | + s := LimitReadCloser(io.NopCloser(strings.NewReader("e va bene cosi'")), 4) |
| 64 | + |
| 65 | + read := make([]byte, 100) |
| 66 | + n, err := s.Read(read) |
| 67 | + require.Error(t, err) |
| 68 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 69 | + require.Equal(t, "e va", string(read[0:n])) |
| 70 | + |
| 71 | + // Reading again should return ErrStreamTooLarge again |
| 72 | + n, err = s.Read(read) |
| 73 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 74 | + require.Equal(t, 0, n) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("read in two segments", func(t *testing.T) { |
| 78 | + s := LimitReadCloser(io.NopCloser(strings.NewReader("senza parole")), 9) |
| 79 | + |
| 80 | + read := make([]byte, 5) |
| 81 | + |
| 82 | + n, err := s.Read(read) |
| 83 | + require.NoError(t, err) |
| 84 | + require.Equal(t, "senza", string(read[0:n])) |
| 85 | + |
| 86 | + n, err = s.Read(read) |
| 87 | + require.Error(t, err) |
| 88 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 89 | + require.Equal(t, " par", string(read[0:n])) |
| 90 | + |
| 91 | + // Reading again should return ErrStreamTooLarge again |
| 92 | + n, err = s.Read(read) |
| 93 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 94 | + require.Equal(t, 0, n) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("close early", func(t *testing.T) { |
| 98 | + s := LimitReadCloser(io.NopCloser(strings.NewReader("senza parole")), 10) |
| 99 | + |
| 100 | + // Read 5 bytes then close |
| 101 | + read := make([]byte, 5) |
| 102 | + n, err := s.Read(read) |
| 103 | + require.NoError(t, err) |
| 104 | + require.Equal(t, "senza", string(read[0:n])) |
| 105 | + |
| 106 | + // Reading should now return io.EOF |
| 107 | + err = s.Close() |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + n, err = s.Read(read) |
| 111 | + require.Error(t, err) |
| 112 | + require.ErrorIs(t, err, io.EOF) |
| 113 | + require.Equal(t, 0, n) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("stream is nil", func(t *testing.T) { |
| 117 | + s := LimitReadCloser(nil, 10) |
| 118 | + |
| 119 | + // Reading should return ErrStreamTooLarge again |
| 120 | + n, err := s.Read(make([]byte, 10)) |
| 121 | + require.ErrorIs(t, err, ErrStreamTooLarge) |
| 122 | + require.Equal(t, 0, n) |
| 123 | + }) |
| 124 | +} |
0 commit comments