xref: /netbsd-src/external/bsd/zstd/dist/contrib/linux-kernel/test/static_test.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos #include <stddef.h>
11*3117ece4Schristos #include <stdio.h>
12*3117ece4Schristos #include <stdlib.h>
13*3117ece4Schristos #include <string.h>
14*3117ece4Schristos 
15*3117ece4Schristos #include "decompress_sources.h"
16*3117ece4Schristos #include <linux/zstd.h>
17*3117ece4Schristos 
18*3117ece4Schristos #define CONTROL(x)                                                             \
19*3117ece4Schristos   do {                                                                         \
20*3117ece4Schristos     if (!(x)) {                                                                \
21*3117ece4Schristos       fprintf(stderr, "%s:%u: %s failed!\n", __FUNCTION__, __LINE__, #x);      \
22*3117ece4Schristos       abort();                                                                 \
23*3117ece4Schristos     }                                                                          \
24*3117ece4Schristos   } while (0)
25*3117ece4Schristos 
26*3117ece4Schristos 
27*3117ece4Schristos static const char kEmptyZstdFrame[] = {
28*3117ece4Schristos     0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x00, 0x01, 0x00, 0x00, 0x99, 0xe9, 0xd8, 0x51
29*3117ece4Schristos };
30*3117ece4Schristos 
31*3117ece4Schristos static void test_decompress_unzstd(void) {
32*3117ece4Schristos     fprintf(stderr, "Testing decompress unzstd... ");
33*3117ece4Schristos     {
34*3117ece4Schristos         size_t const wkspSize = zstd_dctx_workspace_bound();
35*3117ece4Schristos         void* wksp = malloc(wkspSize);
36*3117ece4Schristos         ZSTD_DCtx* dctx = zstd_init_dctx(wksp, wkspSize);
37*3117ece4Schristos         CONTROL(wksp != NULL);
38*3117ece4Schristos         CONTROL(dctx != NULL);
39*3117ece4Schristos         {
40*3117ece4Schristos           size_t const dSize = zstd_decompress_dctx(dctx, NULL, 0, kEmptyZstdFrame, sizeof(kEmptyZstdFrame));
41*3117ece4Schristos           CONTROL(!zstd_is_error(dSize));
42*3117ece4Schristos           CONTROL(dSize == 0);
43*3117ece4Schristos         }
44*3117ece4Schristos         free(wksp);
45*3117ece4Schristos     }
46*3117ece4Schristos     fprintf(stderr, "Ok\n");
47*3117ece4Schristos }
48*3117ece4Schristos 
49*3117ece4Schristos int main(void) {
50*3117ece4Schristos   test_decompress_unzstd();
51*3117ece4Schristos   return 0;
52*3117ece4Schristos }
53