Nothing
# zstd (RFC 8878), built in since #55. The framed-codec behaviour shared with
# lz4-frame -- frame continuation, trailing bytes, held magic prefixes -- is
# in test-frames.R; this file is zstd's own.
test_that("zstd round-trips every payload kind", {
withr::local_seed(17L)
for (kind in payload_kinds()) {
x <- new_payload(kind, 4096L)
expect_roundtrip(x, "zstd")
expect_identical(komp_decompress(komp_compress(x, "zstd"), "zstd"), x,
info = kind)
}
})
test_that("zstd round-trips at the edges of its level range", {
x <- new_payload("structured", 6000L)
row <- komp_codecs()[komp_codecs()$id == "zstd", ]
for (lvl in c(row$level_min, -5L, 0L, 1L, row$level_default, 19L,
row$level_max)) {
expect_identical(
komp_decompress(komp_compress(x, "zstd", level = lvl), "zstd"), x,
info = paste("level", lvl)
)
}
for (name in c("fast", "default", "best")) {
expect_identical(
komp_decompress(komp_compress(x, "zstd", level = name), "zstd"), x,
info = name
)
}
})
test_that("zstd's advertised levels are libzstd's", {
# level_min is a literal in the vtable; this is the check that it is
# still ZSTD_minCLevel() for the vendored release.
row <- komp_codecs()[komp_codecs()$id == "zstd", ]
expect_identical(row$level_min, -131072L)
expect_identical(row$level_max, 22L)
expect_identical(row$level_default, 3L)
expect_identical(row$level_fast, 1L)
expect_identical(row$level_best, 19L)
expect_codec_error(komp_compress(raw(4), "zstd", level = 23L),
"zukomp_invalid_argument")
expect_codec_error(komp_compress(raw(4), "zstd", level = -131073L),
"zukomp_invalid_argument")
})
test_that("zstd streams correctly at every chunk boundary", {
x <- new_payload("structured", 3001L)
for (n in chunk_sizes()) {
expect_chunked_roundtrip(x, "zstd", n, n)
}
expect_chunked_roundtrip(x, "zstd", 1L, 4096L)
expect_chunked_roundtrip(x, "zstd", 4096L, 1L)
})
test_that("zstd output is a standard frame with a checksum", {
x <- new_payload("ascii", 1000L)
z <- komp_compress(x, "zstd")
expect_identical(z[1:4], as.raw(c(0x28, 0xb5, 0x2f, 0xfd)))
# Frame header descriptor (RFC 8878 3.1.1.1.1): bit 2 is the content
# checksum flag, and the whole input arrived at once so bits 6-7 record a
# content size.
fhd <- as.integer(z[5])
expect_true(bitwAnd(fhd, 0x04L) != 0L)
expect_true(bitwAnd(fhd, 0xC0L) != 0L || bitwAnd(fhd, 0x20L) != 0L)
})
test_that("zstd compression is deterministic", {
x <- new_payload("structured", 10000L)
expect_identical(komp_compress(x, "zstd"), komp_compress(x, "zstd"))
expect_identical(komp_compress(x, "zstd", level = 19L),
komp_compress(x, "zstd", level = 19L))
})
test_that("decodes frames made by the zstd command-line tool", {
m <- fixture_manifest("zstd")
expect_gt(nrow(m), 5L)
for (i in seq_len(nrow(m))) {
row <- m[i, ]
bytes <- fixture_bytes("zstd", row$file)
expect_identical(zu_test_stream(bytes, "zstd", "decode"),
fixture_plaintext(row), info = row$file)
expect_identical(zu_test_stream(bytes, "zstd", "decode",
in_chunk = 1L, out_chunk = 1L),
fixture_plaintext(row), info = paste(row$file, "1-byte"))
}
})
test_that("a corrupted content checksum is a checksum error", {
x <- new_payload("ascii", 2048L)
z <- komp_compress(x, "zstd")
for (off in 0:3) {
bad <- z
i <- length(z) - off
bad[i] <- as.raw(bitwXor(as.integer(bad[i]), 0xff))
expect_codec_error(komp_decompress(bad, "zstd"), "zukomp_checksum_error",
info = paste("offset", off))
}
})
test_that("a zstd frame cut short is truncated, at every position", {
# libzstd sees the frame structure, so unlike a DEFLATE body it can tell
# "needs more" from "is wrong" (design 7).
x <- new_payload("structured", 3000L)
z <- komp_compress(x, "zstd")
# Every position under ZUKOMP_SLOW_TESTS, a sample otherwise.
withr::local_seed(41L)
for (cut in c(0L, truncation_positions(length(z)))) {
expect_codec_error(komp_decompress(z[seq_len(cut)], "zstd"),
"zukomp_truncated", info = paste("cut at", cut))
}
})
test_that("garbage is invalid data, not a crash", {
expect_codec_error(komp_decompress(as.raw(1:64), "zstd"),
"zukomp_invalid_data")
# The right magic followed by nonsense.
bad <- c(as.raw(c(0x28, 0xb5, 0x2f, 0xfd)), as.raw(rep(0xff, 32)))
expect_error(komp_decompress(bad, "zstd"), class = "zukomp_error")
})
test_that("bit flips in a zstd frame never decode silently wrong", {
withr::local_seed(23L)
x <- new_payload("structured", 4000L)
z <- komp_compress(x, "zstd")
for (i in seq_len(80)) {
bad <- z
pos <- sample(seq_along(bad), 1L)
bad[pos] <- as.raw(bitwXor(as.integer(bad[pos]), 2^sample(0:7, 1L)))
res <- tryCatch(komp_decompress(bad, "zstd", max_output = 1e6),
zukomp_error = function(e) NULL)
# The content checksum is what makes this hold for zstd: whatever
# decodes must be the original.
expect_true(is.null(res) || identical(res, x), info = paste("pos", pos))
}
})
test_that("max_output binds on a zstd bomb", {
# 1 MiB of zeros compresses to a few dozen bytes.
z <- komp_compress(raw(2^20), "zstd")
expect_lt(length(z), 200L)
expect_codec_error(komp_decompress(z, "zstd", max_output = 1000),
"zukomp_output_limit")
for (n in c(1L, 7L, 4096L)) {
expect_codec_error(
zu_test_stream(z, "zstd", "decode", in_chunk = n, out_chunk = n,
max_output = 1000),
"zukomp_output_limit", info = paste("chunk", n))
}
# Exactly at the cap is not an error.
expect_identical(komp_decompress(z, "zstd", max_output = 2^20), raw(2^20))
})
test_that("max_ratio binds on a zstd bomb", {
z <- komp_compress(raw(2^20), "zstd")
expect_codec_error(komp_decompress(z, "zstd", max_ratio = 10),
"zukomp_ratio_limit")
})
test_that("a frame claiming a huge window is refused under max_output", {
# Frame header with Single_Segment_Flag clear and Window_Descriptor
# 0xC0: exponent 24, a 2^34-byte window that libzstd would have to
# allocate before the first byte of output. Under a max_output that is the
# limit refusing the stream; with no limit, libzstd's own 128 MiB ceiling.
hdr <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x00, 0xC0))
body <- as.raw(c(0x01, 0x00, 0x00)) # a last, empty raw block
z <- c(hdr, body)
expect_codec_error(komp_decompress(z, "zstd", max_output = 2^20),
"zukomp_output_limit")
expect_codec_error(komp_decompress(z, "zstd", max_output = NULL),
"zukomp_unsupported_codec")
})
test_that("a lying frame content size is invalid data", {
# Single-segment frame declaring 5 bytes of content (FHD 0x20, FCS 1 byte)
# whose only block is a raw block of 3 bytes.
z <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x05,
0x19, 0x00, 0x00, 0x61, 0x62, 0x63))
expect_codec_error(komp_decompress(z, "zstd"), "zukomp_invalid_data")
# ...and the same frame declaring 3 decodes.
ok <- z
ok[6] <- as.raw(0x03)
expect_identical(komp_decompress(ok, "zstd"), charToRaw("abc"))
})
test_that("the empty payload is a valid zstd frame", {
z <- komp_compress(raw(0), "zstd")
expect_gt(length(z), 4L)
expect_identical(komp_decompress(z, "zstd"), raw(0))
expect_identical(zu_test_decompress_one(z, "zstd", 0), raw(0))
# No bytes at all is not a frame.
expect_codec_error(komp_decompress(raw(0), "zstd"), "zukomp_truncated")
})
test_that("zstd supports a mid-stream flush", {
x <- new_payload("ascii", 9000L)
z <- zu_test_stream(x, "zstd", "encode", in_chunk = 100L, flush_every = 3)
expect_identical(komp_decompress(z, "zstd"), x)
})
test_that("the one-shot zstd paths agree with the whole-buffer ones", {
x <- new_payload("structured", 5000L)
z <- komp_compress(x, "zstd")
expect_identical(zu_test_decompress_one(z, "zstd", length(x)), x)
expect_codec_error(zu_test_decompress_one(z, "zstd", length(x) - 1L),
"zukomp_output_limit")
# Incompressible input fits the advertised bound in one shot.
withr::local_seed(3L)
r <- new_payload("random", 70000L)
expect_lte(length(komp_compress(r, "zstd")),
zu_test_compress_bound(length(r), "zstd"))
})
test_that("a frame ending in an empty last block is held to its declared content size", {
# Frame_Content_Size 4312 (two-byte field: 0x0FD8 + 256), single segment,
# no checksum, then one empty raw block marked last. libzstd's streaming
# decoder checked the content size only after a non-empty last block, so
# this was an empty frame whenever the output room was smaller than the
# declared size, and invalid otherwise (tools/patches/zstd/0002, #68).
frame <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x60, 0xd8, 0x0f, 0x01, 0x00, 0x00))
for (in_chunk in chunk_sizes()) {
for (out_chunk in c(1, 255, 1e6)) {
expect_error(zu_test_stream(frame, "zstd", "decode", in_chunk, out_chunk),
class = "zukomp_invalid_data",
info = paste(in_chunk, out_chunk))
}
}
expect_error(komp_decompress(frame, "zstd"), class = "zukomp_invalid_data")
# The control: the same frame declaring a content size of 0 (a one-byte
# field, as single-segment allows) is a valid empty frame.
empty <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x00, 0x01, 0x00, 0x00))
for (out_chunk in c(1, 1e6)) {
expect_identical(zu_test_stream(empty, "zstd", "decode", 1, out_chunk), raw(0))
}
})
test_that("a block larger than its frame's Block_Maximum_Size is refused", {
# A 1 KiB window (Window_Descriptor 0), so Block_Maximum_Size is 1 KiB
# (RFC 8878 3.1.1.2.3), and one last RLE block declaring 1350655 bytes.
# libzstd's streaming decoder, which codec_zstd.c drives, refuses it; its
# one-shot decoder never checks, which is why the fuzz oracle's reference
# now applies the rule itself (fuzz/corpus/regressions/, #68).
frame <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x00, 0x00, 0xfb, 0xdf, 0xa4, 0x05))
for (in_chunk in chunk_sizes()) {
for (out_chunk in c(1, 1e6)) {
expect_error(zu_test_stream(frame, "zstd", "decode", in_chunk, out_chunk),
class = "zukomp_invalid_data",
info = paste(in_chunk, out_chunk))
}
}
# The control: the same RLE block at exactly 1024 bytes decodes.
ok <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x00, 0x00, 0x03, 0x20, 0x00, 0x05))
expect_identical(komp_decompress(ok, "zstd"), rep(as.raw(0x05), 1024))
})
test_that("an RLE block is measured by its regenerated size, not its body", {
# Single segment with Frame_Content_Size 0, so Window_Size and therefore
# Block_Maximum_Size are 0 (RFC 8878 3.1.1.2.3), then one last RLE block
# of size 0 and the checksum of no bytes (XXH64, low 32 bits). It is a
# valid empty frame. libzstd's streaming decoder compared the RLE block's
# one-byte body with the maximum of 0 and called it corrupt whenever the
# frame arrived a few bytes at a time (tools/patches/zstd/0003, #68).
frame <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x00, 0x03, 0x00, 0x00,
0x00, 0x99, 0xe9, 0xd8, 0x51))
for (in_chunk in chunk_sizes()) {
for (out_chunk in c(1, 1e6)) {
expect_identical(zu_test_stream(frame, "zstd", "decode", in_chunk, out_chunk),
raw(0), info = paste(in_chunk, out_chunk))
}
}
# With a wrong checksum it is a checksum failure at every chunking, not
# corrupt data at some and a checksum failure at others.
bad <- frame
bad[11] <- as.raw(0x00)
for (in_chunk in chunk_sizes()) {
expect_error(zu_test_stream(bad, "zstd", "decode", in_chunk, 1e6),
class = "zukomp_checksum_error", info = in_chunk)
}
})
test_that("a compressed block of size 0 is corrupt at every chunking", {
# Single segment, Frame_Content_Size 0, no checksum, then one last block
# of type Compressed and size 0. A compressed block holds at least its
# literals section header, so this is corrupt -- libzstd's one-shot path
# and the zstd CLI say so -- but its streaming path took it for an empty
# block and accepted the frame (tools/patches/zstd/0004, #68).
frame <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x00, 0x05, 0x00, 0x00))
for (in_chunk in chunk_sizes()) {
for (out_chunk in c(1, 1e6)) {
expect_error(zu_test_stream(frame, "zstd", "decode", in_chunk, out_chunk),
class = "zukomp_invalid_data",
info = paste(in_chunk, out_chunk))
}
}
expect_error(komp_decompress(frame, "zstd"), class = "zukomp_invalid_data")
# The control: the same frame with a raw block of size 0 is a valid empty
# frame.
raw_frame <- frame
raw_frame[7] <- as.raw(0x01)
expect_identical(zu_test_stream(raw_frame, "zstd", "decode", 1, 1), raw(0))
})
test_that("a reserved header bit is invalid data, a dictionary is unsupported", {
# Aligned with lz4-frame (design 7): a bit the format says must be zero is
# malformed input; a dictionary is a feature this build lacks. libzstd
# names the first frameParameter_unsupported, which zukomp used to report
# as unsupported too.
empty <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x00, 0x01, 0x00, 0x00))
expect_identical(komp_decompress(empty, "zstd"), raw(0)) # the control
reserved <- empty
reserved[5] <- as.raw(0x28) # bit 3 set
dict <- as.raw(c(0x28, 0xb5, 0x2f, 0xfd, 0x01, 0x00, 0x07, 0x01, 0x00, 0x00))
no_dict <- dict[-7]
no_dict[5] <- as.raw(0x00)
expect_identical(komp_decompress(no_dict, "zstd"), raw(0)) # the control
for (k in c(1L, 64L)) {
expect_identical(
decode_outcome(zu_test_stream(reserved, "zstd", "decode", in_chunk = k)),
"zukomp_invalid_data", info = paste("reserved, in_chunk", k))
expect_identical(
decode_outcome(zu_test_stream(dict, "zstd", "decode", in_chunk = k)),
"zukomp_unsupported_codec", info = paste("dictionary, in_chunk", k))
}
})
test_that("a legacy-format zstd frame is unsupported, not invalid data", {
# zstd v0.1 (0xFD2FB51E) and v0.2-v0.7 (0xFD2FB522..27) are well-formed
# formats this build has no decoder for (ZSTD_LEGACY_SUPPORT=0), so libzstd
# calls them prefix_unknown and zukomp recognises the magic itself (#138).
# The neighbours on either side of each range are not formats at all.
frame <- function(b0) {
as.raw(c(b0, 0xb5, 0x2f, 0xfd, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00))
}
cases <- list(
list(0x1d, "zukomp_invalid_data"),
list(0x1e, "zukomp_unsupported_codec"),
list(0x21, "zukomp_invalid_data"),
list(0x22, "zukomp_unsupported_codec"),
list(0x25, "zukomp_unsupported_codec"),
list(0x27, "zukomp_unsupported_codec")
)
for (case in cases) {
for (k in c(1L, 3L, 64L)) {
expect_identical(
decode_outcome(zu_test_stream(frame(case[[1]]), "zstd", "decode",
in_chunk = k)),
case[[2]],
info = sprintf("magic byte 0x%02x, in_chunk %d", case[[1]], k)
)
}
}
expect_codec_error(komp_decompress(frame(0x27), "zstd"),
"zukomp_unsupported_codec")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.