test/testautomation_stdlib.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 19 Mar 2014 21:39:55 -0700
changeset 8645 d69fdbefeecf
parent 7219 c577dfb69153
child 8646 0a20fff4de3c
permissions -rw-r--r--
Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
slouken@7216
     1
/**
slouken@7216
     2
 * Standard C library routine test suite
slouken@7216
     3
 */
slouken@7216
     4
slouken@7216
     5
#include <stdio.h>
slouken@7216
     6
slouken@7216
     7
#include "SDL.h"
slouken@7216
     8
#include "SDL_test.h"
slouken@7216
     9
slouken@7216
    10
slouken@7216
    11
/* Test case functions */
slouken@7216
    12
slouken@7216
    13
/**
slouken@7216
    14
 * @brief Call to SDL_strlcpy
slouken@7216
    15
 */
slouken@7216
    16
#undef SDL_strlcpy
slouken@7216
    17
int
slouken@7216
    18
stdlib_strlcpy(void *arg)
slouken@7216
    19
{
slouken@7216
    20
  size_t result;
slouken@7216
    21
  char text[1024];
slouken@7216
    22
  const char *expected;
slouken@7216
    23
slouken@7216
    24
  result = SDL_strlcpy(text, "foo", sizeof(text));
slouken@7216
    25
  expected = "foo";
slouken@7216
    26
  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
slouken@7216
    27
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    28
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    29
slouken@7216
    30
  result = SDL_strlcpy(text, "foo", 2);
slouken@7216
    31
  expected = "f";
slouken@7216
    32
  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
slouken@7216
    33
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    34
  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
slouken@7216
    35
slouken@7216
    36
  return TEST_COMPLETED;
slouken@7216
    37
}
slouken@7216
    38
slouken@7216
    39
/**
slouken@7216
    40
 * @brief Call to SDL_snprintf
slouken@7216
    41
 */
slouken@7216
    42
#undef SDL_snprintf
slouken@7216
    43
int
slouken@7216
    44
stdlib_snprintf(void *arg)
slouken@7216
    45
{
slouken@7216
    46
  int result;
slouken@7216
    47
  char text[1024];
slouken@7216
    48
  const char *expected;
slouken@7216
    49
slouken@7216
    50
  result = SDL_snprintf(text, sizeof(text), "%s", "foo");
slouken@7216
    51
  expected = "foo";
slouken@7216
    52
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
slouken@7216
    53
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    54
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    55
slouken@7216
    56
  result = SDL_snprintf(text, 2, "%s", "foo");
slouken@7216
    57
  expected = "f";
slouken@7216
    58
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
slouken@7216
    59
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    60
  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
slouken@7216
    61
slouken@7216
    62
  result = SDL_snprintf(NULL, 0, "%s", "foo");
slouken@7216
    63
  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
slouken@7216
    64
slouken@7216
    65
  result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
slouken@7216
    66
  expected = "1.000000";
slouken@7216
    67
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
slouken@7216
    68
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    69
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    70
slouken@7216
    71
  result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
slouken@7216
    72
  expected = "1";
slouken@7216
    73
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
slouken@7216
    74
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    75
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    76
slouken@7216
    77
  result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
slouken@7216
    78
  expected = "1.";
slouken@7216
    79
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
slouken@7216
    80
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    81
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    82
slouken@7216
    83
  result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
slouken@7216
    84
  expected = "1.333333";
slouken@7216
    85
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
slouken@7216
    86
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    87
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    88
slouken@7216
    89
  result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
slouken@7216
    90
  expected = "+1.333333";
slouken@7216
    91
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
slouken@7216
    92
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    93
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
    94
slouken@7216
    95
  result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
slouken@7216
    96
  expected = "1.33";
slouken@7216
    97
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
slouken@7216
    98
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    99
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
   100
slouken@7216
   101
  result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
slouken@7216
   102
  expected = "  1.33";
slouken@7216
   103
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
slouken@7216
   104
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
slouken@7216
   105
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
   106
slouken@7216
   107
  result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
slouken@7216
   108
  expected = "001.33";
slouken@7216
   109
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
slouken@7216
   110
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
slouken@7216
   111
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
slouken@7216
   112
slouken@7216
   113
  result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
slouken@7216
   114
  expected = "001.";
slouken@7216
   115
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
slouken@7216
   116
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
slouken@7216
   117
  SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
slouken@7216
   118
slouken@7216
   119
  return TEST_COMPLETED;
slouken@7216
   120
}
slouken@7216
   121
aschiffler@8645
   122
/**
aschiffler@8645
   123
 * @brief Call to SDL_getenv and SDL_setenv
aschiffler@8645
   124
 */
aschiffler@8645
   125
int
aschiffler@8645
   126
stdlib_getsetenv(void *arg)
aschiffler@8645
   127
{
aschiffler@8645
   128
  const int nameLen = 16;
aschiffler@8645
   129
  int counter;
aschiffler@8645
   130
  int result;
aschiffler@8645
   131
  char name[nameLen + 1];
aschiffler@8645
   132
  char * value1;
aschiffler@8645
   133
  char * value2;
aschiffler@8645
   134
  char * expected;
aschiffler@8645
   135
  int overwrite;
aschiffler@8645
   136
  char * text;
aschiffler@8645
   137
aschiffler@8645
   138
  /* Create a random name. This tests SDL_getenv, since we need to */
aschiffler@8645
   139
  /* make sure the variable is not set yet (it shouldn't). */
aschiffler@8645
   140
  do {
aschiffler@8645
   141
    for(counter = 0; counter < nameLen; counter++) {
aschiffler@8645
   142
      name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
aschiffler@8645
   143
    }
aschiffler@8645
   144
    name[nameLen] = '\0';
aschiffler@8645
   145
    
aschiffler@8645
   146
    text = SDL_getenv(name);
aschiffler@8645
   147
    SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   148
    if (text != NULL) {
aschiffler@8645
   149
      SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, SDL_strlen(text));
aschiffler@8645
   150
    }
aschiffler@8645
   151
  } while (text != NULL);
aschiffler@8645
   152
   
aschiffler@8645
   153
  /* Create random values to set */                    
aschiffler@8645
   154
  value1 = SDLTest_RandomAsciiStringOfSize(10);
aschiffler@8645
   155
  value2 = SDLTest_RandomAsciiStringOfSize(10);
aschiffler@8645
   156
aschiffler@8645
   157
  /* Set value 1 without overwrite */
aschiffler@8645
   158
  overwrite = 0;
aschiffler@8645
   159
  expected = value1;
aschiffler@8645
   160
  result = SDL_setenv(name, value1, overwrite);
aschiffler@8645
   161
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
aschiffler@8645
   162
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   163
aschiffler@8645
   164
  /* Check value */
aschiffler@8645
   165
  text = SDL_getenv(name);
aschiffler@8645
   166
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   167
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   168
  if (text != NULL) {
aschiffler@8645
   169
    SDLTest_AssertCheck(
aschiffler@8645
   170
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   171
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   172
      expected,
aschiffler@8645
   173
      text);
aschiffler@8645
   174
  }
aschiffler@8645
   175
  
aschiffler@8645
   176
  /* Set value 2 with overwrite */
aschiffler@8645
   177
  overwrite = 1;
aschiffler@8645
   178
  expected = value2;    
aschiffler@8645
   179
  result = SDL_setenv(name, value2, overwrite);
aschiffler@8645
   180
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
aschiffler@8645
   181
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   182
aschiffler@8645
   183
  /* Check value */
aschiffler@8645
   184
  text = SDL_getenv(name);
aschiffler@8645
   185
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   186
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   187
  if (text != NULL) {
aschiffler@8645
   188
    SDLTest_AssertCheck(
aschiffler@8645
   189
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   190
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   191
      expected,
aschiffler@8645
   192
      text);
aschiffler@8645
   193
  }
aschiffler@8645
   194
aschiffler@8645
   195
  /* Set value 1 without overwrite */
aschiffler@8645
   196
  overwrite = 0;
aschiffler@8645
   197
  expected = value2;    
aschiffler@8645
   198
  result = SDL_setenv(name, value1, overwrite);
aschiffler@8645
   199
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
aschiffler@8645
   200
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   201
aschiffler@8645
   202
  /* Check value */
aschiffler@8645
   203
  text = SDL_getenv(name);
aschiffler@8645
   204
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   205
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   206
  if (text != NULL) {
aschiffler@8645
   207
    SDLTest_AssertCheck(
aschiffler@8645
   208
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   209
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   210
      expected,
aschiffler@8645
   211
      text);
aschiffler@8645
   212
  }
aschiffler@8645
   213
  
aschiffler@8645
   214
  /* Set value 1 without overwrite */
aschiffler@8645
   215
  overwrite = 1;
aschiffler@8645
   216
  expected = value1;
aschiffler@8645
   217
  result = SDL_setenv(name, value1, overwrite);
aschiffler@8645
   218
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
aschiffler@8645
   219
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   220
aschiffler@8645
   221
  /* Check value */
aschiffler@8645
   222
  text = SDL_getenv(name);
aschiffler@8645
   223
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   224
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   225
  if (text != NULL) {
aschiffler@8645
   226
    SDLTest_AssertCheck(
aschiffler@8645
   227
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   228
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   229
      expected,
aschiffler@8645
   230
      text);
aschiffler@8645
   231
  }
aschiffler@8645
   232
aschiffler@8645
   233
  /* Negative cases */
aschiffler@8645
   234
  for (overwrite=0; overwrite <= 1; overwrite++) { 
aschiffler@8645
   235
    result = SDL_setenv(NULL, value1, overwrite);
aschiffler@8645
   236
    SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
aschiffler@8645
   237
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   238
    result = SDL_setenv("", value1, overwrite);
aschiffler@8645
   239
    SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
aschiffler@8645
   240
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   241
    result = SDL_setenv("=", value1, overwrite);
aschiffler@8645
   242
    SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
aschiffler@8645
   243
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   244
    result = SDL_setenv(name, NULL, overwrite);
aschiffler@8645
   245
    SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
aschiffler@8645
   246
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   247
  }
aschiffler@8645
   248
aschiffler@8645
   249
  /* Clean up */
aschiffler@8645
   250
  SDL_free(value1);
aschiffler@8645
   251
  SDL_free(value2);
aschiffler@8645
   252
    
aschiffler@8645
   253
  return TEST_COMPLETED;
aschiffler@8645
   254
}
aschiffler@8645
   255
slouken@7216
   256
/* ================= Test References ================== */
slouken@7216
   257
slouken@7216
   258
/* Standard C routine test cases */
slouken@7216
   259
static const SDLTest_TestCaseReference stdlibTest1 =
slouken@7216
   260
        { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED };
slouken@7216
   261
slouken@7216
   262
static const SDLTest_TestCaseReference stdlibTest2 =
philipp@7219
   263
        { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED };
slouken@7216
   264
aschiffler@8645
   265
static const SDLTest_TestCaseReference stdlibTest3 =
aschiffler@8645
   266
        { (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED };
aschiffler@8645
   267
slouken@7216
   268
/* Sequence of Standard C routine test cases */
slouken@7216
   269
static const SDLTest_TestCaseReference *stdlibTests[] =  {
aschiffler@8645
   270
    &stdlibTest1, &stdlibTest2, &stdlibTest3, NULL
slouken@7216
   271
};
slouken@7216
   272
slouken@7216
   273
/* Timer test suite (global) */
slouken@7216
   274
SDLTest_TestSuiteReference stdlibTestSuite = {
aschiffler@8645
   275
    "Stdlib",
slouken@7216
   276
    NULL,
slouken@7216
   277
    stdlibTests,
slouken@7216
   278
    NULL
slouken@7216
   279
};