summaryrefslogtreecommitdiffstats
path: root/include/qemu/timed-average.h
diff options
context:
space:
mode:
authorEmilio G. Cota2018-08-15 23:56:26 +0200
committerRichard Henderson2018-09-26 17:55:54 +0200
commitf44641bbf2dc9a2849d17797b2d342d1af853712 (patch)
treeb293f0513bc978524d6a76c8dea1222e0e103c8a /include/qemu/timed-average.h
parenttest-qht: test qht_iter_remove (diff)
downloadqemu-f44641bbf2dc9a2849d17797b2d342d1af853712.tar.gz
qemu-f44641bbf2dc9a2849d17797b2d342d1af853712.tar.xz
qemu-f44641bbf2dc9a2849d17797b2d342d1af853712.zip
test-qht: test removal of non-existent entries
This improves qht.c code coverage from 89.44% to 90.00%. Reviewed-by: Alex Bennée <alex.bennee@linaro.org> Signed-off-by: Emilio G. Cota <cota@braap.org> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Diffstat (limited to 'include/qemu/timed-average.h')
0 files changed, 0 insertions, 0 deletions
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/*
 * QEMU Crypto block encryption
 *
 * Copyright (c) 2016 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "qemu/osdep.h"
#include "qapi/error.h"
#include "crypto/init.h"
#include "crypto/block.h"
#include "qemu/buffer.h"
#include "qemu/module.h"
#include "crypto/secret.h"
#ifndef _WIN32
#include <sys/resource.h>
#endif

#if (defined(_WIN32) || defined RUSAGE_THREAD) && \
    (defined(CONFIG_NETTLE) || defined(CONFIG_GCRYPT))
#define TEST_LUKS
#else
#undef TEST_LUKS
#endif

static QCryptoBlockCreateOptions qcow_create_opts = {
    .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
    .u.qcow = {
        .has_key_secret = true,
        .key_secret = (char *)"sec0",
    },
};

static QCryptoBlockOpenOptions qcow_open_opts = {
    .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
    .u.qcow = {
        .has_key_secret = true,
        .key_secret = (char *)"sec0",
    },
};


#ifdef TEST_LUKS
static QCryptoBlockOpenOptions luks_open_opts = {
    .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
    .u.luks = {
        .has_key_secret = true,
        .key_secret = (char *)"sec0",
    },
};


/* Creation with all default values */
static QCryptoBlockCreateOptions luks_create_opts_default = {
    .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
    .u.luks = {
        .has_key_secret = true,
        .key_secret = (char *)"sec0",
    },
};


/* ...and with explicit values */
static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_plain64 = {
    .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
    .u.luks = {
        .has_key_secret = true,
        .key_secret = (char *)"sec0",
        .has_cipher_alg = true,
        .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
        .has_cipher_mode = true,
        .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
        .has_ivgen_alg = true,
        .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
    },
};


static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_essiv = {
    .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
    .u.luks = {
        .has_key_secret = true,
        .key_secret = (char *)"sec0",
        .has_cipher_alg = true,
        .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
        .has_cipher_mode = true,
        .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
        .has_ivgen_alg = true,
        .ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
        .has_ivgen_hash_alg = true,
        .ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256,
        .has_hash_alg = true,
        .hash_alg = QCRYPTO_HASH_ALG_SHA1,
    },
};
#endif /* TEST_LUKS */


static struct QCryptoBlockTestData {
    const char *path;
    QCryptoBlockCreateOptions *create_opts;
    QCryptoBlockOpenOptions *open_opts;

    bool expect_header;

    QCryptoCipherAlgorithm cipher_alg;
    QCryptoCipherMode cipher_mode;
    QCryptoHashAlgorithm hash_alg;

    QCryptoIVGenAlgorithm ivgen_alg;
    QCryptoHashAlgorithm ivgen_hash;

    bool slow;
} test_data[] = {
    {
        .path = "/crypto/block/qcow",
        .create_opts = &qcow_create_opts,
        .open_opts = &qcow_open_opts,

        .expect_header = false,

        .cipher_alg = QCRYPTO_CIPHER_ALG_AES_128,
        .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,

        .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
    },
#ifdef TEST_LUKS
    {
        .path = "/crypto/block/luks/default",
        .create_opts = &luks_create_opts_default,
        .open_opts = &luks_open_opts,

        .expect_header = true,

        .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
        .cipher_mode = QCRYPTO_CIPHER_MODE_XTS,
        .hash_alg = QCRYPTO_HASH_ALG_SHA256,

        .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,

        .slow = true,
    },
    {
        .path = "/crypto/block/luks/aes-256-cbc-plain64",
        .create_opts = &luks_create_opts_aes256_cbc_plain64,
        .open_opts = &luks_open_opts,

        .expect_header = true,

        .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
        .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
        .hash_alg = QCRYPTO_HASH_ALG_SHA256,

        .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,

        .slow = true,
    },
    {
        .path = "/crypto/block/luks/aes-256-cbc-essiv",
        .create_opts = &luks_create_opts_aes256_cbc_essiv,
        .open_opts = &luks_open_opts,

        .expect_header = true,

        .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
        .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
        .hash_alg = QCRYPTO_HASH_ALG_SHA1,

        .ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
        .ivgen_hash = QCRYPTO_HASH_ALG_SHA256,

        .slow = true,
    },
#endif
};


static ssize_t test_block_read_func(QCryptoBlock *block,
                                    size_t offset,
                                    uint8_t *buf,
                                    size_t buflen,
                                    void *opaque,
                                    Error **errp)
{
    Buffer *header = opaque;

    g_assert_cmpint(offset + buflen, <=, header->capacity);

    memcpy(buf, header->buffer + offset, buflen);

    return buflen;
}


static ssize_t test_block_init_func(QCryptoBlock *block,
                                    size_t headerlen,
                                    void *opaque,
                                    Error **errp)
{
    Buffer *header = opaque;

    g_assert_cmpint(header->capacity, ==, 0);

    buffer_reserve(header, headerlen);

    return headerlen;
}


static ssize_t test_block_write_func(QCryptoBlock *block,
                                     size_t offset,
                                     const uint8_t *buf,
                                     size_t buflen,
                                     void *opaque,
                                     Error **errp)
{
    Buffer *header = opaque;

    g_assert_cmpint(buflen + offset, <=, header->capacity);

    memcpy(header->buffer + offset, buf, buflen);
    header->offset = offset + buflen;

    return buflen;
}


static Object *test_block_secret(void)
{
    return object_new_with_props(
        TYPE_QCRYPTO_SECRET,
        object_get_objects_root(),
        "sec0",
        &error_abort,