-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
s2: Document and test how to peek the stream for skippable blocks (#918)
Co-authored-by: Klaus Post <klauspost@gmail.com>
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2019+ Klaus Post. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package s2 | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestLeadingSkippableBlock(t *testing.T) { | ||
var buf bytes.Buffer | ||
w := NewWriter(&buf) | ||
if err := w.AddSkippableBlock(0x80, []byte("skippable block")); err != nil { | ||
t.Fatalf("w.AddSkippableBlock: %v", err) | ||
} | ||
if _, err := w.Write([]byte("some data")); err != nil { | ||
t.Fatalf("w.Write: %v", err) | ||
} | ||
if err := w.Close(); err != nil { | ||
t.Fatalf("w.Close: %v", err) | ||
} | ||
r := NewReader(&buf) | ||
var sb []byte | ||
r.SkippableCB(0x80, func(sr io.Reader) error { | ||
var err error | ||
sb, err = io.ReadAll(sr) | ||
return err | ||
}) | ||
if _, err := r.Read([]byte{}); err != nil { | ||
t.Errorf("empty read failed: %v", err) | ||
} | ||
if !bytes.Equal(sb, []byte("skippable block")) { | ||
t.Errorf("didn't get correct data from skippable block: %q", string(sb)) | ||
} | ||
data, err := io.ReadAll(r) | ||
if err != nil { | ||
t.Fatalf("r.Read: %v", err) | ||
} | ||
if !bytes.Equal(data, []byte("some data")) { | ||
t.Errorf("didn't get correct compressed data: %q", string(data)) | ||
} | ||
} |