toolkit/mozapps/update/tests/TestAUSReadStrings.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/update/tests/TestAUSReadStrings.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/**
    1.10 + * This binary tests the updater's ReadStrings ini parser and should run in a
    1.11 + * directory with a Unicode character to test bug 473417.
    1.12 + */
    1.13 +#ifdef XP_WIN
    1.14 +  #include <windows.h>
    1.15 +  #define NS_main wmain
    1.16 +  #define NS_tstrrchr wcsrchr
    1.17 +  #define NS_T(str) L ## str
    1.18 +  #define PATH_SEPARATOR_CHAR L'\\'
    1.19 +  // On Windows, argv[0] can also have forward slashes instead
    1.20 +  #define ALT_PATH_SEPARATOR_CHAR L'/'
    1.21 +#else
    1.22 +  #include <unistd.h>
    1.23 +  #define NS_main main
    1.24 +  #define NS_tstrrchr strrchr
    1.25 +  #define NS_T(str) str
    1.26 +  #define PATH_SEPARATOR_CHAR '/'
    1.27 +#endif
    1.28 +
    1.29 +#include <stdio.h>
    1.30 +#include <stdarg.h>
    1.31 +#include <string.h>
    1.32 +
    1.33 +#include "updater/resource.h"
    1.34 +#include "updater/progressui.h"
    1.35 +#include "common/readstrings.h"
    1.36 +#include "common/errors.h"
    1.37 +#include "mozilla/ArrayUtils.h"
    1.38 +
    1.39 +#ifndef MAXPATHLEN
    1.40 +# ifdef PATH_MAX
    1.41 +#  define MAXPATHLEN PATH_MAX
    1.42 +# elif defined(MAX_PATH)
    1.43 +#  define MAXPATHLEN MAX_PATH
    1.44 +# elif defined(_MAX_PATH)
    1.45 +#  define MAXPATHLEN _MAX_PATH
    1.46 +# elif defined(CCHMAXPATH)
    1.47 +#  define MAXPATHLEN CCHMAXPATH
    1.48 +# else
    1.49 +#  define MAXPATHLEN 1024
    1.50 +# endif
    1.51 +#endif
    1.52 +
    1.53 +#define TEST_NAME "Updater ReadStrings"
    1.54 +
    1.55 +using namespace mozilla;
    1.56 +
    1.57 +static int gFailCount = 0;
    1.58 +
    1.59 +/**
    1.60 + * Prints the given failure message and arguments using printf, prepending
    1.61 + * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and
    1.62 + * appending "\n" to eliminate having to type it at each call site.
    1.63 + */
    1.64 +void fail(const char* msg, ...)
    1.65 +{
    1.66 +  va_list ap;
    1.67 +
    1.68 +  printf("TEST-UNEXPECTED-FAIL | ");
    1.69 +
    1.70 +  va_start(ap, msg);
    1.71 +  vprintf(msg, ap);
    1.72 +  va_end(ap);
    1.73 +
    1.74 +  putchar('\n');
    1.75 +  ++gFailCount;
    1.76 +}
    1.77 +
    1.78 +int NS_main(int argc, NS_tchar **argv)
    1.79 +{
    1.80 +  printf("Running TestAUSReadStrings tests\n");
    1.81 +
    1.82 +  int rv = 0;
    1.83 +  int retval;
    1.84 +  NS_tchar inifile[MAXPATHLEN];
    1.85 +  StringTable testStrings;
    1.86 +
    1.87 +  NS_tchar *slash = NS_tstrrchr(argv[0], PATH_SEPARATOR_CHAR);
    1.88 +#ifdef ALT_PATH_SEPARATOR_CHAR
    1.89 +  NS_tchar *altslash = NS_tstrrchr(argv[0], ALT_PATH_SEPARATOR_CHAR);
    1.90 +  slash = (slash > altslash) ? slash : altslash;
    1.91 +#endif // ALT_PATH_SEPARATOR_CHAR
    1.92 +
    1.93 +  if (!slash) {
    1.94 +    fail("%s | unable to find platform specific path separator (check 1)", TEST_NAME);
    1.95 +    return 20;
    1.96 +  }
    1.97 +
    1.98 +  *(++slash) = '\0';
    1.99 +  // Test success when the ini file exists with both Title and Info in the
   1.100 +  // Strings section and the values for Title and Info.
   1.101 +  NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings1.ini"), argv[0]);
   1.102 +  retval = ReadStrings(inifile, &testStrings);
   1.103 +  if (retval == OK) {
   1.104 +    if (strcmp(testStrings.title, "Title Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \
   1.105 +                                  "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \
   1.106 +                                  "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \
   1.107 +                                  "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \
   1.108 +                                  "\xE6\xB8\xAC\xE8\xA9\xA6 " \
   1.109 +                                  "\xE6\xB5\x8B\xE8\xAF\x95") != 0) {
   1.110 +      rv = 21;
   1.111 +      fail("%s | Title ini value incorrect (check 3)", TEST_NAME);
   1.112 +    }
   1.113 +
   1.114 +    if (strcmp(testStrings.info, "Info Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \
   1.115 +                                 "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \
   1.116 +                                 "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \
   1.117 +                                 "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \
   1.118 +                                 "\xE6\xB8\xAC\xE8\xA9\xA6 " \
   1.119 +                                 "\xE6\xB5\x8B\xE8\xAF\x95\xE2\x80\xA6") != 0) {
   1.120 +      rv = 22;
   1.121 +      fail("%s | Info ini value incorrect (check 4)", TEST_NAME);
   1.122 +    }
   1.123 +  }
   1.124 +  else {
   1.125 +    fail("%s | ReadStrings returned %i (check 2)", TEST_NAME, retval);
   1.126 +    rv = 23;
   1.127 +  }
   1.128 +
   1.129 +  // Test failure when the ini file exists without Title and with Info in the
   1.130 +  // Strings section.
   1.131 +  NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings2.ini"), argv[0]);
   1.132 +  retval = ReadStrings(inifile, &testStrings);
   1.133 +  if (retval != PARSE_ERROR) {
   1.134 +    rv = 24;
   1.135 +    fail("%s | ReadStrings returned %i (check 5)", TEST_NAME, retval);
   1.136 +  }
   1.137 +
   1.138 +  // Test failure when the ini file exists with Title and without Info in the
   1.139 +  // Strings section.
   1.140 +  NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]);
   1.141 +  retval = ReadStrings(inifile, &testStrings);
   1.142 +  if (retval != PARSE_ERROR) {
   1.143 +    rv = 25;
   1.144 +    fail("%s | ReadStrings returned %i (check 6)", TEST_NAME, retval);
   1.145 +  }
   1.146 +
   1.147 +  // Test failure when the ini file doesn't exist
   1.148 +  NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStringsBogus.ini"), argv[0]);
   1.149 +  retval = ReadStrings(inifile, &testStrings);
   1.150 +  if (retval != READ_ERROR) {
   1.151 +    rv = 26;
   1.152 +    fail("%s | ini file doesn't exist (check 7)", TEST_NAME);
   1.153 +  }
   1.154 +
   1.155 +  // Test reading a non-default section name
   1.156 +  NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]);
   1.157 +  retval = ReadStrings(inifile, "Title\0", 1, &testStrings.title, "BogusSection2");
   1.158 +  if (retval == OK) {
   1.159 +    if (strcmp(testStrings.title, "Bogus Title") != 0) {
   1.160 +      rv = 27;
   1.161 +      fail("%s | Title ini value incorrect (check 9)", TEST_NAME);
   1.162 +    }
   1.163 +  }
   1.164 +  else {
   1.165 +    fail("%s | ReadStrings returned %i (check 8)", TEST_NAME, retval);
   1.166 +    rv = 28;
   1.167 +  }
   1.168 +
   1.169 +
   1.170 +  if (rv == 0) {
   1.171 +    printf("TEST-PASS | %s | all checks passed\n", TEST_NAME);
   1.172 +  } else {
   1.173 +    fail("%s | %i out of 9 checks failed", TEST_NAME, gFailCount);
   1.174 +  }
   1.175 +
   1.176 +  return rv;
   1.177 +}

mercurial