LCOV - code coverage report
Current view: top level - egg - test-padding.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 98.3 % 116 114
Test Date: 2024-05-07 18:02:03 Functions: 100.0 % 17 17

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-padding.c: Test padding functionality
       3              : 
       4              :    Copyright (C) 2007 Stefan Walter
       5              : 
       6              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       7              :    modify it under the terms of the GNU Library General Public License as
       8              :    published by the Free Software Foundation; either version 2 of the
       9              :    License, or (at your option) any later version.
      10              : 
      11              :    The Gnome Keyring 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              :    Library General Public License for more details.
      15              : 
      16              :    You should have received a copy of the GNU Library General Public
      17              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      18              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "egg/egg-padding.h"
      26              : #include "egg/egg-testing.h"
      27              : 
      28              : #include <gcrypt.h>
      29              : 
      30              : #include <stdlib.h>
      31              : #include <stdio.h>
      32              : #include <string.h>
      33              : 
      34              : static void
      35           21 : check_padding (EggPadding padding, gsize block, gconstpointer input,
      36              :                gsize n_input, gconstpointer output, gsize n_output)
      37              : {
      38              :         gpointer result;
      39              :         gsize n_result;
      40              : 
      41           21 :         if (!(padding) (NULL, block, input, n_input, &result, &n_result)) {
      42            8 :                 g_assert (output == NULL);
      43            8 :                 return;
      44              :         }
      45              : 
      46           13 :         g_assert (result != NULL);
      47           13 :         egg_assert_cmpsize (n_output, ==, n_result);
      48           13 :         g_assert (memcmp (output, result, n_output) == 0);
      49           13 :         g_free (result);
      50              : 
      51              :         /* Now make sure it can tell us the right length */
      52           13 :         if (!(padding) (NULL, block, input, n_input, NULL, &n_result))
      53            0 :                 g_assert_not_reached ();
      54              : 
      55           13 :         egg_assert_cmpsize (n_output, ==, n_result);
      56              : }
      57              : 
      58              : static void
      59            1 : test_zero_padding (void)
      60              : {
      61            1 :         guchar padded[] = { 0x00, 0x00, 0x00, 0x00, 'T', 'E', 'S', 'T' };
      62            1 :         gchar raw[] = "TEST";
      63            1 :         check_padding (egg_padding_zero_pad, 8, raw, 4, padded, 8);
      64            1 : }
      65              : 
      66              : static void
      67            1 : test_zero_padding_no_data (void)
      68              : {
      69              :         guchar padded[] = { };
      70            1 :         gchar raw[] = "";
      71            1 :         check_padding (egg_padding_zero_pad, 8, raw, 0, padded, 0);
      72            1 : }
      73              : 
      74              : static void
      75            1 : test_pkcs1_one_padding (void)
      76              : {
      77            1 :         guchar padded[] = { 0x00, 0x01, 0xFF, 0x00, 'T', 'E', 'S', 'T' };
      78            1 :         gchar raw[] = "TEST";
      79            1 :         check_padding (egg_padding_pkcs1_pad_01, 8, raw, 4, padded, 8);
      80            1 :         check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 4);
      81            1 : }
      82              : 
      83              : static void
      84            1 : test_pkcs1_one_padding_no_data (void)
      85              : {
      86            1 :         guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
      87            1 :         gchar raw[] = "";
      88            1 :         check_padding (egg_padding_pkcs1_pad_01, 8, raw, 0, padded, 8);
      89            1 :         check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 0);
      90            1 : }
      91              : 
      92              : static void
      93            1 : test_pkcs1_two_padding (void)
      94              : {
      95            1 :         guchar padded[] = { 0x00, 0x02, 0x77, 0x66, 0x55, 0x00, 'T', 'E', };
      96            1 :         gchar raw[] = "TE";
      97              :         guchar *result;
      98              :         gpointer vesult;
      99              :         gsize n_result;
     100              : 
     101            1 :         check_padding (egg_padding_pkcs1_unpad_02, 8, padded, 8, raw, 2);
     102              : 
     103              :         /* PKCS#1 02 padding is unpredictable */
     104            1 :         if (!egg_padding_pkcs1_pad_02 (NULL, 8, raw, 2, &vesult, &n_result))
     105            0 :                 g_assert_not_reached ();
     106            1 :         result = vesult;
     107            1 :         g_assert (result != NULL);
     108            1 :         egg_assert_cmpsize (n_result, ==, 8);
     109            1 :         g_assert (result[0] == 0x00);
     110            1 :         g_assert (result[1] == 0x02);
     111            1 :         g_assert (result[2] != 0x00);
     112            1 :         g_assert (result[3] != 0x00);
     113            1 :         g_assert (result[4] != 0x00);
     114            1 :         g_assert (result[5] == 0x00);
     115            1 :         g_assert (result[6] == 'T');
     116            1 :         g_assert (result[7] == 'E');
     117            1 :         g_free (vesult);
     118            1 : }
     119              : 
     120              : static void
     121            1 : test_pkcs1_padding_invalid_prefix (void)
     122              : {
     123            1 :         guchar padded[] = { 0x01, 0x04, 0x04, 0x04 };
     124            1 :         check_padding (egg_padding_pkcs1_unpad_01, 4, padded, 4, NULL, 0);
     125            1 : }
     126              : 
     127              : static void
     128            1 : test_pkcs1_padding_invalid_type (void)
     129              : {
     130            1 :         guchar padded[] = { 0x00, 0x03, 0xFF, 0x00, 'T', 'E', 'S', 'T' };
     131            1 :         check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0);
     132            1 : }
     133              : 
     134              : static void
     135            1 : test_pkcs1_padding_invalid_no_zero (void)
     136              : {
     137            1 :         guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 'T', 'E', 'S', 'T' };
     138            1 :         check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0);
     139            1 : }
     140              : 
     141              : static void
     142            1 : test_pkcs1_padding_invalid_length (void)
     143              : {
     144            1 :         guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 'T', 'E', 'S' };
     145            1 :         check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 7, NULL, 0);
     146            1 : }
     147              : 
     148              : static void
     149            1 : test_pkcs7_padding (void)
     150              : {
     151            1 :         guchar padded[] = { 'T', 'E', 'S', 'T', 0x04, 0x04, 0x04, 0x04 };
     152            1 :         gchar raw[] = "TEST";
     153              : 
     154            1 :         check_padding (egg_padding_pkcs7_pad, 8, raw, 4, padded, 8);
     155            1 :         check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 4);
     156            1 : }
     157              : 
     158              : static void
     159            1 : test_pkcs7_padding_equal_block (void)
     160              : {
     161            1 :         guchar padded[] = { 'T', 'E', 'S', 'T', 'T', 'E', 'S', 'T', 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
     162            1 :         gchar raw[] = "TESTTEST";
     163              : 
     164            1 :         check_padding (egg_padding_pkcs7_pad, 8, raw, 8, padded, 16);
     165            1 :         check_padding (egg_padding_pkcs7_unpad, 8, padded, 16, raw, 8);
     166            1 : }
     167              : 
     168              : static void
     169            1 : test_pkcs7_padding_zero (void)
     170              : {
     171            1 :         guchar padded[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
     172            1 :         gchar raw[] = "";
     173              : 
     174            1 :         check_padding (egg_padding_pkcs7_pad, 8, raw, 0, padded, 8);
     175            1 :         check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 0);
     176            1 : }
     177              : 
     178              : static void
     179            1 : test_pkcs7_padding_invalid_zero (void)
     180              : {
     181            1 :         guchar padded[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     182            1 :         check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0);
     183            1 : }
     184              : 
     185              : static void
     186            1 : test_pkcs7_padding_invalid_too_long (void)
     187              : {
     188            1 :         guchar padded[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
     189            1 :         check_padding (egg_padding_pkcs7_unpad, 4, padded, 8, NULL, 0);
     190            1 :         check_padding (egg_padding_pkcs7_unpad, 4, padded, 4, NULL, 0);
     191            1 : }
     192              : 
     193              : static void
     194            1 : test_pkcs7_padding_invalid_different (void)
     195              : {
     196            1 :         guchar padded[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
     197            1 :         check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0);
     198            1 : }
     199              : 
     200              : int
     201            1 : main (int argc, char **argv)
     202              : {
     203            1 :         g_test_init (&argc, &argv, NULL);
     204              : 
     205            1 :         g_test_add_func ("/padding/zero_padding", test_zero_padding);
     206            1 :         g_test_add_func ("/padding/zero_padding_no_data", test_zero_padding_no_data);
     207            1 :         g_test_add_func ("/padding/pkcs1_one_padding", test_pkcs1_one_padding);
     208            1 :         g_test_add_func ("/padding/pkcs1_one_padding_no_data", test_pkcs1_one_padding_no_data);
     209            1 :         g_test_add_func ("/padding/pkcs1_two_padding", test_pkcs1_two_padding);
     210            1 :         g_test_add_func ("/padding/pkcs1_padding_invalid_prefix", test_pkcs1_padding_invalid_prefix);
     211            1 :         g_test_add_func ("/padding/pkcs1_padding_invalid_type", test_pkcs1_padding_invalid_type);
     212            1 :         g_test_add_func ("/padding/pkcs1_padding_invalid_no_zero", test_pkcs1_padding_invalid_no_zero);
     213            1 :         g_test_add_func ("/padding/pkcs1_padding_invalid_length", test_pkcs1_padding_invalid_length);
     214            1 :         g_test_add_func ("/padding/pkcs7_padding", test_pkcs7_padding);
     215            1 :         g_test_add_func ("/padding/pkcs7_padding_equal_block", test_pkcs7_padding_equal_block);
     216            1 :         g_test_add_func ("/padding/pkcs7_padding_zero", test_pkcs7_padding_zero);
     217            1 :         g_test_add_func ("/padding/pkcs7_padding_invalid_zero", test_pkcs7_padding_invalid_zero);
     218            1 :         g_test_add_func ("/padding/pkcs7_padding_invalid_too_long", test_pkcs7_padding_invalid_too_long);
     219            1 :         g_test_add_func ("/padding/pkcs7_padding_invalid_different", test_pkcs7_padding_invalid_different);
     220              : 
     221            1 :         return g_test_run ();
     222              : }
        

Generated by: LCOV version 2.0-1