torture_base64.c 4.48 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * @file torture_base64.c
 * @brief Test BASE64 encoding/decoding
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com> \n
 *
 * @date Created: Tue Feb  1 13:29:09 EET 2005 ppessi
 *
 */

#include "config.h"

#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>

43
#include "sofia-sip/base64.h"
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47 48 49

int tstflags = 0;
#define TSTFLAGS tstflags

char const *name = "torture_base64";

50
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
51 52 53 54 55 56 57 58 59

char const constant[] = "not changed";

int test_encoding(void)
{
  char buffer[32];

  BEGIN();

60
  TEST_SIZE(base64_e(buffer, sizeof buffer, "\0\020\203", 3), 4);
Pekka Pessi's avatar
Pekka Pessi committed
61 62 63
  TEST_S(buffer, "ABCD");

  strcpy(buffer + 5, "not changed");
64
  TEST_SIZE(base64_e(buffer, 5, "\0\020\203", 3), 4);
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68
  TEST_S(buffer + 5, "not changed");
  TEST_S(buffer, "ABCD");

  strcpy(buffer + 4, "not changed");
69
  TEST_SIZE(base64_e(buffer, 4, "\0\020\203", 3), 4);
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73
  TEST_S(buffer + 4, "not changed");
  TEST_S(buffer, "ABC");

  strcpy(buffer + 3, "not changed");
74
  TEST_SIZE(base64_e(buffer, 3, "\0\020\203", 3), 4);
Pekka Pessi's avatar
Pekka Pessi committed
75 76 77 78
  TEST_S(buffer + 3, "not changed");
  TEST_S(buffer, "AB");

  strcpy(buffer + 2, "not changed");
79
  TEST_SIZE(base64_e(buffer, 2, "\0\020\203", 3), 4);
Pekka Pessi's avatar
Pekka Pessi committed
80 81 82 83
  TEST_S(buffer + 2, "not changed");
  TEST_S(buffer, "A");

  strcpy(buffer + 1, "not changed");
84
  TEST_SIZE(base64_e(buffer, 1, "\0\020\203", 3), 4);
Pekka Pessi's avatar
Pekka Pessi committed
85 86 87 88
  TEST_S(buffer + 1, "not changed");
  TEST_S(buffer, "");

  strcpy(buffer + 5, "not changed");
89
  TEST_SIZE(base64_e(buffer, 5, "\0\020", 2), 4);
Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93
  TEST_S(buffer + 5, "not changed");
  TEST_S(buffer, "ABA=");

  strcpy(buffer + 4, "not changed");
94
  TEST_SIZE(base64_e(buffer, 4, "\0\020", 2), 4);
Pekka Pessi's avatar
Pekka Pessi committed
95 96 97 98
  TEST_S(buffer + 4, "not changed");
  TEST_S(buffer, "ABA");

  strcpy(buffer + 3, "not changed");
99
  TEST_SIZE(base64_e(buffer, 3, "\0\020", 2), 4);
Pekka Pessi's avatar
Pekka Pessi committed
100 101 102 103
  TEST_S(buffer + 3, "not changed");
  TEST_S(buffer, "AB");

  strcpy(buffer + 2, "not changed");
104
  TEST_SIZE(base64_e(buffer, 2, "\0\020", 2), 4);
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108
  TEST_S(buffer + 2, "not changed");
  TEST_S(buffer, "A");

  strcpy(buffer + 1, "not changed");
109
  TEST_SIZE(base64_e(buffer, 1, "\0\020", 2), 4);
Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113
  TEST_S(buffer + 1, "not changed");
  TEST_S(buffer, "");

  strcpy(buffer + 5, "not changed");
114
  TEST_SIZE(base64_e(buffer, 5, "\0", 1), 4);
Pekka Pessi's avatar
Pekka Pessi committed
115 116 117 118
  TEST_S(buffer + 5, "not changed");
  TEST_S(buffer, "AA==");

  strcpy(buffer + 4, "not changed");
119
  TEST_SIZE(base64_e(buffer, 4, "\0", 1), 4);
Pekka Pessi's avatar
Pekka Pessi committed
120 121 122 123
  TEST_S(buffer + 4, "not changed");
  TEST_S(buffer, "AA=");

  strcpy(buffer + 3, "not changed");
124
  TEST_SIZE(base64_e(buffer, 3, "\0", 1), 4);
Pekka Pessi's avatar
Pekka Pessi committed
125 126 127 128
  TEST_S(buffer + 3, "not changed");
  TEST_S(buffer, "AA");

  strcpy(buffer + 2, "not changed");
129
  TEST_SIZE(base64_e(buffer, 2, "\0", 1), 4);
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133
  TEST_S(buffer + 2, "not changed");
  TEST_S(buffer, "A");

  strcpy(buffer + 1, "not changed");
134
  TEST_SIZE(base64_e(buffer, 1, "\0", 1), 4);
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137 138 139 140 141 142 143 144 145 146 147
  TEST_S(buffer + 1, "not changed");
  TEST_S(buffer, "");

  END();
}

int test_decoding(void)
{
  char buffer[32];

  BEGIN();

  strcpy(buffer + 0, "not changed");
148
  TEST_SIZE(base64_d((void *)buffer, 0, "ABCD"), 3);
Pekka Pessi's avatar
Pekka Pessi committed
149 150
  TEST_S(buffer + 0, "not changed");

151
  TEST_SIZE(base64_d((void *)buffer, 3, "ABCD"), 3);
Pekka Pessi's avatar
Pekka Pessi committed
152 153
  TEST_M(buffer, "\0\020\203", 3);

154
  TEST_SIZE(base64_d(NULL, 3, "ABCD"), 3);
Pekka Pessi's avatar
Pekka Pessi committed
155

156
  TEST_SIZE(base64_d((void *)buffer, 3, "A B C D !!"), 3);
Pekka Pessi's avatar
Pekka Pessi committed
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
  TEST_M(buffer, "\0\020\203", 3);

  END();
}


void usage(void)
{
  fprintf(stderr,
	  "usage: %s [-v]\n",
	  name);
}

int main(int argc, char *argv[])
{
  int retval = 0;
  int i;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
    else
      usage();
  }

  retval |= test_encoding(); fflush(stdout);
  retval |= test_decoding(); fflush(stdout);

  return retval;
}