cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

test-base64.c (3287B)


      1/*
      2 * QEMU base64 helper test
      3 *
      4 * Copyright (c) 2015 Red Hat, Inc.
      5 *
      6 * This library is free software; you can redistribute it and/or
      7 * modify it under the terms of the GNU Lesser General Public
      8 * License as published by the Free Software Foundation; either
      9 * version 2.1 of the License, or (at your option) any later version.
     10 *
     11 * This library is distributed in the hope that it will be useful,
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14 * Lesser General Public License for more details.
     15 *
     16 * You should have received a copy of the GNU Lesser General Public
     17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
     18 *
     19 */
     20
     21#include "qemu/osdep.h"
     22
     23#include "qapi/error.h"
     24#include "qemu/base64.h"
     25
     26static void test_base64_good(void)
     27{
     28    const char input[] =
     29        "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
     30        "lzc2VkIHRoZSBzY29ycGlvbi4=";
     31    const char expect[] = "Because we focused on the snake, "
     32        "we missed the scorpion.";
     33
     34    size_t len;
     35    uint8_t *actual = qbase64_decode(input,
     36                                     -1,
     37                                     &len,
     38                                     &error_abort);
     39
     40    g_assert(actual != NULL);
     41    g_assert_cmpint(len, ==, strlen(expect));
     42    g_assert_cmpstr((char *)actual, ==, expect);
     43    g_free(actual);
     44}
     45
     46
     47static void test_base64_bad(const char *input,
     48                            size_t input_len)
     49{
     50    size_t len;
     51    Error *err = NULL;
     52    uint8_t *actual = qbase64_decode(input,
     53                                     input_len,
     54                                     &len,
     55                                     &err);
     56
     57    error_free_or_abort(&err);
     58    g_assert(actual == NULL);
     59    g_assert_cmpint(len, ==, 0);
     60}
     61
     62
     63static void test_base64_embedded_nul(void)
     64{
     65    /* We put a NUL character in the middle of the base64
     66     * text which is invalid data, given the expected length */
     67    const char input[] =
     68        "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\0"
     69        "lzc2VkIHRoZSBzY29ycGlvbi4=";
     70
     71    test_base64_bad(input, G_N_ELEMENTS(input) - 1);
     72}
     73
     74
     75static void test_base64_not_nul_terminated(void)
     76{
     77    const char input[] =
     78        "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
     79        "lzc2VkIHRoZSBzY29ycGlvbi4=";
     80
     81    /* Using '-2' to make us drop the trailing NUL, thus
     82     * creating an invalid base64 sequence for decoding */
     83    test_base64_bad(input, G_N_ELEMENTS(input) - 2);
     84}
     85
     86
     87static void test_base64_invalid_chars(void)
     88{
     89    /* We put a single quote character in the middle
     90     * of the base64 text which is invalid data */
     91    const char input[] =
     92        "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW'"
     93        "lzc2VkIHRoZSBzY29ycGlvbi4=";
     94
     95    test_base64_bad(input, strlen(input));
     96}
     97
     98
     99int main(int argc, char **argv)
    100{
    101    g_test_init(&argc, &argv, NULL);
    102    g_test_add_func("/util/base64/good", test_base64_good);
    103    g_test_add_func("/util/base64/embedded-nul", test_base64_embedded_nul);
    104    g_test_add_func("/util/base64/not-nul-terminated",
    105                    test_base64_not_nul_terminated);
    106    g_test_add_func("/util/base64/invalid-chars", test_base64_invalid_chars);
    107    return g_test_run();
    108}