Skip to content

Commit 429f4e4

Browse files
benluddyk8s-publishing-bot
authored andcommitted
Implement runtime.Framer for CBOR Sequences.
Kubernetes-commit: e2b36a0f0c3801085d765ad5155c8d08be9ed09c
1 parent d7e1c53 commit 429f4e4

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

pkg/runtime/serializer/cbor/framer.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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
18+
19+
import (
20+
"io"
21+
22+
"k8s.io/apimachinery/pkg/runtime"
23+
24+
"github.com/fxamacker/cbor/v2"
25+
)
26+
27+
// NewFramer returns a runtime.Framer based on RFC 8742 CBOR Sequences. Each frame contains exactly
28+
// one encoded CBOR data item.
29+
func NewFramer() runtime.Framer {
30+
return framer{}
31+
}
32+
33+
var _ runtime.Framer = framer{}
34+
35+
type framer struct{}
36+
37+
func (framer) NewFrameReader(rc io.ReadCloser) io.ReadCloser {
38+
return &frameReader{
39+
decoder: cbor.NewDecoder(rc),
40+
closer: rc,
41+
}
42+
}
43+
44+
func (framer) NewFrameWriter(w io.Writer) io.Writer {
45+
// Each data item in a CBOR sequence is self-delimiting (like JSON objects).
46+
return w
47+
}
48+
49+
type frameReader struct {
50+
decoder *cbor.Decoder
51+
closer io.Closer
52+
53+
overflow []byte
54+
}
55+
56+
func (fr *frameReader) Read(dst []byte) (int, error) {
57+
if len(fr.overflow) > 0 {
58+
// We read a frame that was too large for the destination slice in a previous call
59+
// to Read and have bytes left over.
60+
n := copy(dst, fr.overflow)
61+
if n < len(fr.overflow) {
62+
fr.overflow = fr.overflow[n:]
63+
return n, io.ErrShortBuffer
64+
}
65+
fr.overflow = nil
66+
return n, nil
67+
}
68+
69+
// The Reader contract allows implementations to use all of dst[0:len(dst)] as scratch
70+
// space, even if n < len(dst), but it does not allow implementations to use
71+
// dst[len(dst):cap(dst)]. Slicing it up-front allows us to append to it without worrying
72+
// about overwriting dst[len(dst):cap(dst)].
73+
m := cbor.RawMessage(dst[0:0:len(dst)])
74+
if err := fr.decoder.Decode(&m); err != nil {
75+
return 0, err
76+
}
77+
78+
if len(m) > len(dst) {
79+
// The frame was too big, m has a newly-allocated underlying array to accommodate
80+
// it.
81+
fr.overflow = m[len(dst):]
82+
return copy(dst, m), io.ErrShortBuffer
83+
}
84+
85+
return len(m), nil
86+
}
87+
88+
func (fr *frameReader) Close() error {
89+
return fr.closer.Close()
90+
}
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)