torture_base64.c 4.59 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
 * 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>
42
#include <stdlib.h>
Pekka Pessi's avatar
Pekka Pessi committed
43

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

int tstflags = 0;
#define TSTFLAGS tstflags

char const *name = "torture_base64";

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

char const constant[] = "not changed";

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

  BEGIN();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  END();
}

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

  BEGIN();

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

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

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

157
  TEST_SIZE(base64_d((void *)buffer, 3, "A B C D !!"), 3);
Pekka Pessi's avatar
Pekka Pessi committed
158 159 160 161 162 163
  TEST_M(buffer, "\0\020\203", 3);

  END();
}


164
void usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
165 166
{
  fprintf(stderr,
167
	  "usage: %s [-v] [-a]\n",
Pekka Pessi's avatar
Pekka Pessi committed
168
	  name);
169
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
170 171 172 173 174 175 176 177 178 179
}

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;
180 181
    else if (strcmp(argv[i], "-a") == 0)
      tstflags |= tst_abort;
Pekka Pessi's avatar
Pekka Pessi committed
182
    else
183
      usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
184 185 186 187 188 189 190
  }

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

  return retval;
}