]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-zlib.c
eaa48369db90fe0f0f80f8df6a4af694425aee8c
[mirror_spl-debian.git] / module / splat / splat-zlib.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Zlib Compression Tests.
25 \*****************************************************************************/
26
27 #include <sys/zmod.h>
28 #include <sys/random.h>
29 #include <sys/kmem.h>
30 #include <sys/vmem.h>
31 #include "splat-internal.h"
32
33 #define SPLAT_ZLIB_NAME "zlib"
34 #define SPLAT_ZLIB_DESC "Zlib Compression Tests"
35
36 #define SPLAT_ZLIB_TEST1_ID 0x0f01
37 #define SPLAT_ZLIB_TEST1_NAME "compress/uncompress"
38 #define SPLAT_ZLIB_TEST1_DESC "Compress/Uncompress Test"
39
40 #define BUFFER_SIZE (128 * 1024)
41
42 static int
43 splat_zlib_test1_check(struct file *file, void *src, void *dst, void *chk,
44 int level)
45 {
46 size_t dst_len = BUFFER_SIZE;
47 size_t chk_len = BUFFER_SIZE;
48 int rc;
49
50 memset(dst, 0, BUFFER_SIZE);
51 memset(chk, 0, BUFFER_SIZE);
52
53 rc = z_compress_level(dst, &dst_len, src, BUFFER_SIZE, level);
54 if (rc != Z_OK) {
55 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
56 "Failed level %d z_compress_level(), %d\n", level, rc);
57 return -EINVAL;
58 }
59
60 rc = z_uncompress(chk, &chk_len, dst, dst_len);
61 if (rc != Z_OK) {
62 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
63 "Failed level %d z_uncompress(), %d\n", level, rc);
64 return -EINVAL;
65 }
66
67 rc = memcmp(src, chk, BUFFER_SIZE);
68 if (rc) {
69 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
70 "Failed level %d memcmp()), %d\n", level, rc);
71 return -EINVAL;
72 }
73
74 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
75 "Passed level %d, compressed %d bytes to %d bytes\n",
76 level, BUFFER_SIZE, (int)dst_len);
77
78 return 0;
79 }
80
81 /*
82 * Compress a buffer, uncompress the newly compressed buffer, then
83 * compare it to the original. Do this for all 9 compression levels.
84 */
85 static int
86 splat_zlib_test1(struct file *file, void *arg)
87 {
88 void *src = NULL, *dst = NULL, *chk = NULL;
89 int i, rc, level;
90
91 src = vmalloc(BUFFER_SIZE);
92 if (src == NULL) {
93 rc = -ENOMEM;
94 goto out;
95 }
96
97 dst = vmalloc(BUFFER_SIZE);
98 if (dst == NULL) {
99 rc = -ENOMEM;
100 goto out;
101 }
102
103 chk = vmalloc(BUFFER_SIZE);
104 if (chk == NULL) {
105 rc = -ENOMEM;
106 goto out;
107 }
108
109 /* Source buffer is a repeating 1024 byte random pattern. */
110 random_get_pseudo_bytes(src, sizeof(uint8_t) * 1024);
111 for (i = 1; i < 128; i++)
112 memcpy(src + (i * 1024), src, 1024);
113
114 for (level = 1; level <= 9; level++)
115 if ((rc = splat_zlib_test1_check(file, src, dst, chk, level)))
116 break;
117 out:
118 if (src)
119 vfree(src);
120
121 if (dst)
122 vfree(dst);
123
124 if (chk)
125 vfree(chk);
126
127 return rc;
128 }
129
130 splat_subsystem_t *
131 splat_zlib_init(void)
132 {
133 splat_subsystem_t *sub;
134
135 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
136 if (sub == NULL)
137 return NULL;
138
139 memset(sub, 0, sizeof(*sub));
140 strncpy(sub->desc.name, SPLAT_ZLIB_NAME, SPLAT_NAME_SIZE);
141 strncpy(sub->desc.desc, SPLAT_ZLIB_DESC, SPLAT_DESC_SIZE);
142 INIT_LIST_HEAD(&sub->subsystem_list);
143 INIT_LIST_HEAD(&sub->test_list);
144 spin_lock_init(&sub->test_lock);
145 sub->desc.id = SPLAT_SUBSYSTEM_ZLIB;
146
147 SPLAT_TEST_INIT(sub, SPLAT_ZLIB_TEST1_NAME, SPLAT_ZLIB_TEST1_DESC,
148 SPLAT_ZLIB_TEST1_ID, splat_zlib_test1);
149
150 return sub;
151 }
152
153 void
154 splat_zlib_fini(splat_subsystem_t *sub)
155 {
156 ASSERT(sub);
157
158 SPLAT_TEST_FINI(sub, SPLAT_ZLIB_TEST1_ID);
159
160 kfree(sub);
161 }
162
163 int
164 splat_zlib_id(void) {
165 return SPLAT_SUBSYSTEM_ZLIB;
166 }