|
| 1 | +/* |
| 2 | +Copyright 2024 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 cbor_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "errors" |
| 22 | + "io" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/runtime/serializer/cbor" |
| 26 | + |
| 27 | + "github.com/google/go-cmp/cmp" |
| 28 | +) |
| 29 | + |
| 30 | +// TestFrameReaderReadError tests that the frame reader does not resume after encountering a |
| 31 | +// well-formedness error in the input stream. According to RFC 8742 Section 2.8: "[...] if any data |
| 32 | +// item in the sequence is not well formed, it is not possible to reliably decode the rest of the |
| 33 | +// sequence." |
| 34 | +func TestFrameReaderReadError(t *testing.T) { |
| 35 | + input := []byte{ |
| 36 | + 0xff, // ill-formed initial break |
| 37 | + 0xa0, // followed by well-formed empty map |
| 38 | + } |
| 39 | + fr := cbor.NewFramer().NewFrameReader(io.NopCloser(bytes.NewReader(input))) |
| 40 | + for i := 0; i < 3; i++ { |
| 41 | + n, err := fr.Read(nil) |
| 42 | + if err == nil || errors.Is(err, io.ErrShortBuffer) { |
| 43 | + t.Fatalf("expected a non-nil error other than io.ErrShortBuffer, got: %v", err) |
| 44 | + } |
| 45 | + if n != 0 { |
| 46 | + t.Fatalf("expected 0 bytes read on error, got %d", n) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestFrameReaderRead(t *testing.T) { |
| 52 | + type ChunkedFrame [][]byte |
| 53 | + |
| 54 | + for _, tc := range []struct { |
| 55 | + Name string |
| 56 | + Frames []ChunkedFrame |
| 57 | + }{ |
| 58 | + { |
| 59 | + Name: "consecutive frames", |
| 60 | + Frames: []ChunkedFrame{ |
| 61 | + [][]byte{{0xa0}}, |
| 62 | + [][]byte{{0xa0}}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + Name: "zero-length destination buffer", |
| 67 | + Frames: []ChunkedFrame{ |
| 68 | + [][]byte{{}, {0xa0}}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + Name: "overflow", |
| 73 | + Frames: []ChunkedFrame{ |
| 74 | + [][]byte{ |
| 75 | + {0x43}, |
| 76 | + {'x'}, |
| 77 | + {'y', 'z'}, |
| 78 | + }, |
| 79 | + [][]byte{ |
| 80 | + {0xa1, 0x43, 'f', 'o', 'o'}, |
| 81 | + {'b'}, |
| 82 | + {'a', 'r'}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } { |
| 87 | + t.Run(tc.Name, func(t *testing.T) { |
| 88 | + var concatenation []byte |
| 89 | + for _, f := range tc.Frames { |
| 90 | + for _, c := range f { |
| 91 | + concatenation = append(concatenation, c...) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fr := cbor.NewFramer().NewFrameReader(io.NopCloser(bytes.NewReader(concatenation))) |
| 96 | + |
| 97 | + for _, frame := range tc.Frames { |
| 98 | + var want, got []byte |
| 99 | + for i, chunk := range frame { |
| 100 | + dst := make([]byte, len(chunk), 2*len(chunk)) |
| 101 | + for i := len(dst); i < cap(dst); i++ { |
| 102 | + dst[:cap(dst)][i] = 0xff |
| 103 | + } |
| 104 | + n, err := fr.Read(dst) |
| 105 | + if n != len(chunk) { |
| 106 | + t.Errorf("expected %d bytes read, got %d", len(chunk), n) |
| 107 | + } |
| 108 | + if i == len(frame)-1 && err != nil { |
| 109 | + t.Errorf("unexpected non-nil error on last read of frame: %v", err) |
| 110 | + } else if i < len(frame)-1 && !errors.Is(err, io.ErrShortBuffer) { |
| 111 | + t.Errorf("expected io.ErrShortBuffer on all but the last read of a frame, got: %v", err) |
| 112 | + } |
| 113 | + for i := len(dst); i < cap(dst); i++ { |
| 114 | + if dst[:cap(dst)][i] != 0xff { |
| 115 | + t.Errorf("read mutated underlying array beyond slice length: %#v", dst[len(dst):cap(dst)]) |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + want = append(want, chunk...) |
| 120 | + got = append(got, dst...) |
| 121 | + } |
| 122 | + if diff := cmp.Diff(want, got); diff != "" { |
| 123 | + t.Errorf("reassembled frame differs:\n%s", diff) |
| 124 | + } |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +type fakeReadCloser struct { |
| 131 | + err error |
| 132 | +} |
| 133 | + |
| 134 | +func (rc fakeReadCloser) Read(_ []byte) (int, error) { |
| 135 | + return 0, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (rc fakeReadCloser) Close() error { |
| 139 | + return rc.err |
| 140 | +} |
| 141 | + |
| 142 | +func TestFrameReaderClose(t *testing.T) { |
| 143 | + want := errors.New("test") |
| 144 | + if got := cbor.NewFramer().NewFrameReader(fakeReadCloser{err: want}).Close(); !errors.Is(got, want) { |
| 145 | + t.Errorf("got error %v, want %v", got, want) |
| 146 | + } |
| 147 | +} |
0 commit comments