]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-zlib.c
852cf818fb35cad8def4923002d067e4e7d523bc
[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://github.com/behlendorf/spl/>.
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 "splat-internal.h"
31
32 #define SPLAT_ZLIB_NAME "zlib"
33 #define SPLAT_ZLIB_DESC "Zlib Compression Tests"
34
35 #define SPLAT_ZLIB_TEST1_ID 0x0f01
36 #define SPLAT_ZLIB_TEST1_NAME "compress/uncompress"
37 #define SPLAT_ZLIB_TEST1_DESC "Compress/Uncompress Test"
38
39 #define BUFFER_SIZE (128 * 1024)
40
41 static int
42 splat_zlib_test1_check(struct file *file, void *src, void *dst, void *chk,
43 int level)
44 {
45 size_t dst_len = BUFFER_SIZE;
46 size_t chk_len = BUFFER_SIZE;
47 int rc;
48
49 memset(dst, 0, BUFFER_SIZE);
50 memset(chk, 0, BUFFER_SIZE);
51
52 rc = z_compress_level(dst, &dst_len, src, BUFFER_SIZE, level);
53 if (rc != Z_OK) {
54 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
55 "Failed level %d z_compress_level(), %d\n", level, rc);
56 return -EINVAL;
57 }
58
59 rc = z_uncompress(chk, &chk_len, dst, dst_len);
60 if (rc != Z_OK) {
61 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
62 "Failed level %d z_uncompress(), %d\n", level, rc);
63 return -EINVAL;
64 }
65
66 rc = memcmp(src, chk, BUFFER_SIZE);
67 if (rc) {
68 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
69 "Failed level %d memcmp()), %d\n", level, rc);
70 return -EINVAL;
71 }
72
73 splat_vprint(file, SPLAT_ZLIB_TEST1_NAME,
74 "Passed level %d, compressed %d bytes to %d bytes\n",
75 level, BUFFER_SIZE, (int)dst_len);
76
77 return 0;
78 }
79
80 /*
81 * Compress a buffer, uncompress the newly compressed buffer, then
82 * compare it to the original. Do this for all 9 compression levels.
83 */
84 static int
85 splat_zlib_test1(struct file *file, void *arg)
86 {
87 void *src = NULL, *dst = NULL, *chk = NULL;
88 int i, rc, level;
89
90 src = vmalloc(BUFFER_SIZE);
91 if (src == NULL) {
92 rc = -ENOMEM;
93 goto out;
94 }
95
96 dst = vmalloc(BUFFER_SIZE);
97 if (dst == NULL) {
98 rc = -ENOMEM;
99 goto out;
100 }
101
102 chk = vmalloc(BUFFER_SIZE);
103 if (chk == NULL) {
104 rc = -ENOMEM;
105 goto out;
106 }
107
108 /* Source buffer is a repeating 1024 byte random pattern. */
109 random_get_pseudo_bytes(src, sizeof(uint8_t) * 1024);
110 for (i = 1; i < 128; i++)
111 memcpy(src + (i * 1024), src, 1024);
112
113 for (level = 1; level <= 9; level++)
114 if ((rc = splat_zlib_test1_check(file, src, dst, chk, level)))
115 break;
116 out:
117 if (src)
118 vfree(src);
119
120 if (dst)
121 vfree(dst);
122
123 if (chk)
124 vfree(chk);
125
126 return rc;
127 }
128
129 splat_subsystem_t *
130 splat_zlib_init(void)
131 {
132 splat_subsystem_t *sub;
133
134 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
135 if (sub == NULL)
136 return NULL;
137
138 memset(sub, 0, sizeof(*sub));
139 strncpy(sub->desc.name, SPLAT_ZLIB_NAME, SPLAT_NAME_SIZE);
140 strncpy(sub->desc.desc, SPLAT_ZLIB_DESC, SPLAT_DESC_SIZE);
141 INIT_LIST_HEAD(&sub->subsystem_list);
142 INIT_LIST_HEAD(&sub->test_list);
143 spin_lock_init(&sub->test_lock);
144 sub->desc.id = SPLAT_SUBSYSTEM_ZLIB;
145
146 SPLAT_TEST_INIT(sub, SPLAT_ZLIB_TEST1_NAME, SPLAT_ZLIB_TEST1_DESC,
147 SPLAT_ZLIB_TEST1_ID, splat_zlib_test1);
148
149 return sub;
150 }
151
152 void
153 splat_zlib_fini(splat_subsystem_t *sub)
154 {
155 ASSERT(sub);
156
157 SPLAT_TEST_FINI(sub, SPLAT_ZLIB_TEST1_ID);
158
159 kfree(sub);
160 }
161
162 int
163 splat_zlib_id(void) {
164 return SPLAT_SUBSYSTEM_ZLIB;
165 }