Skip to content

Instantly share code, notes, and snippets.

@hugelgupf
Created February 20, 2023 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugelgupf/fc7ea7c735017acfbcad61ef36632d55 to your computer and use it in GitHub Desktop.
Save hugelgupf/fc7ea7c735017acfbcad61ef36632d55 to your computer and use it in GitHub Desktop.
From 38d43e436f9401cc38f2a4d78ca9d17dd521a611 Mon Sep 17 00:00:00 2001
From: Chris Koch <chrisko@google.com>
Date: Sun, 19 Feb 2023 16:38:50 -0800
Subject: [PATCH] Test case for failure to legacy-compress and
non-legacy-decompress
---
reader_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/reader_test.go b/reader_test.go
index 143642c..9b2e008 100644
--- a/reader_test.go
+++ b/reader_test.go
@@ -6,11 +6,13 @@ import (
"fmt"
"io"
"io/ioutil"
+ "math/rand"
"os"
"reflect"
"runtime"
"strings"
"testing"
+ "time"
"github.com/pierrec/lz4/v4"
)
@@ -326,3 +328,44 @@ func TestValidFrameHeader(t *testing.T) {
})
}
}
+
+const choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+// randomString generates random string of fixed length in a fast and simple way.
+func randomString(l int) string {
+ rand.Seed(time.Now().UnixNano())
+ r := make([]byte, l)
+ for i := 0; i < l; i++ {
+ r[i] = byte(choices[rand.Intn(len(choices))])
+ }
+ return string(r)
+}
+
+func TestLegacyCompressionNonLegacyReader(t *testing.T) {
+ var b bytes.Buffer
+ lz4w := lz4.NewWriter(&b)
+ lz4w.Apply(lz4.LegacyOption(true))
+
+ data := randomString(1024)
+
+ n, err := io.Copy(lz4w, strings.NewReader(data))
+ if err != nil {
+ t.Fatalf("io.Copy = %v", err)
+ }
+ if n != int64(len(data)) {
+ t.Fatalf("Got %d bytes compressed, want %d", n, len(data))
+ }
+ if err := lz4w.Close(); err != nil {
+ t.Fatalf("Failed to close lz4 writer: %v", err)
+ }
+
+ src := lz4.NewReader(bytes.NewReader(b.Bytes()))
+ var buf strings.Builder
+ if _, err := io.Copy(&buf, src); err != nil {
+ t.Errorf("io.Copy = %v", err)
+ }
+ if buf.String() != data {
+ t.Errorf("got %s, want %s", buf.String(), data)
+ }
+
+}
--
2.39.2.637.g21b0678d19-goog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment