LCOV - code coverage report
Current view: directory - toolkit/mozapps/update/test - TestAUSReadStrings.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 59 29 49.2 %
Date: 2012-06-02 Functions: 3 1 33.3 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is Mozilla Foundation
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *  Robert Strong <robert.bugzilla@gmail.com>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : /**
      39                 :  * This binary tests the updater's ReadStrings ini parser and should run in a
      40                 :  * directory with a Unicode character to test bug 473417.
      41                 :  */
      42                 : #ifdef XP_WIN
      43                 :   #include <windows.h>
      44                 :   #define NS_main wmain
      45                 :   #define NS_tstrrchr wcsrchr
      46                 :   #define NS_tsnprintf _snwprintf
      47                 :   #define NS_T(str) L ## str
      48                 :   #define PATH_SEPARATOR_CHAR L'\\'
      49                 : #else
      50                 :   #include <unistd.h>
      51                 :   #define NS_main main
      52                 :   #define NS_tstrrchr strrchr
      53                 :   #define NS_tsnprintf snprintf
      54                 :   #define NS_T(str) str
      55                 : #ifdef XP_OS2
      56                 :   #define PATH_SEPARATOR_CHAR '\\'
      57                 : #else
      58                 :   #define PATH_SEPARATOR_CHAR '/'
      59                 : #endif
      60                 : #endif
      61                 : 
      62                 : #include <stdio.h>
      63                 : #include <stdarg.h>
      64                 : #include <string.h>
      65                 : 
      66                 : #include "updater/resource.h"
      67                 : #include "updater/progressui.h"
      68                 : #include "common/readstrings.h"
      69                 : #include "common/errors.h"
      70                 : 
      71                 : #ifndef MAXPATHLEN
      72                 : # ifdef PATH_MAX
      73                 : #  define MAXPATHLEN PATH_MAX
      74                 : # elif defined(MAX_PATH)
      75                 : #  define MAXPATHLEN MAX_PATH
      76                 : # elif defined(_MAX_PATH)
      77                 : #  define MAXPATHLEN _MAX_PATH
      78                 : # elif defined(CCHMAXPATH)
      79                 : #  define MAXPATHLEN CCHMAXPATH
      80                 : # else
      81                 : #  define MAXPATHLEN 1024
      82                 : # endif
      83                 : #endif
      84                 : 
      85                 : #define TEST_NAME "Updater ReadStrings"
      86                 : 
      87                 : static int gFailCount = 0;
      88                 : 
      89                 : /**
      90                 :  * Prints the given failure message and arguments using printf, prepending
      91                 :  * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and
      92                 :  * appending "\n" to eliminate having to type it at each call site.
      93                 :  */
      94               0 : void fail(const char* msg, ...)
      95                 : {
      96                 :   va_list ap;
      97                 : 
      98               0 :   printf("TEST-UNEXPECTED-FAIL | ");
      99                 : 
     100               0 :   va_start(ap, msg);
     101               0 :   vprintf(msg, ap);
     102               0 :   va_end(ap);
     103                 : 
     104               0 :   putchar('\n');
     105               0 :   ++gFailCount;
     106               0 : }
     107                 : 
     108                 : /**
     109                 :  * Prints the given string prepending "TEST-PASS | " for the benefit of
     110                 :  * the test harness and with "\n" at the end, to be used at the end of a
     111                 :  * successful test function.
     112                 :  */
     113               0 : void passed(const char* test)
     114                 : {
     115               0 :   printf("TEST-PASS | %s\n", test);
     116               0 : }
     117                 : 
     118               1 : int NS_main(int argc, NS_tchar **argv)
     119                 : {
     120               1 :   printf("Running TestAUSReadStrings tests\n");
     121                 : 
     122               1 :   int rv = 0;
     123                 :   int retval;
     124                 :   NS_tchar inifile[MAXPATHLEN];
     125                 :   StringTable testStrings;
     126                 : 
     127               1 :   NS_tchar *slash = NS_tstrrchr(argv[0], PATH_SEPARATOR_CHAR);
     128               1 :   if (!slash) {
     129               0 :     fail("%s | unable to find platform specific path separator (check 1)", TEST_NAME);
     130               0 :     return 20;
     131                 :   }
     132                 : 
     133               1 :   *(++slash) = '\0';
     134                 :   // Test success when the ini file exists with both Title and Info in the
     135                 :   // Strings section and the values for Title and Info.
     136               1 :   NS_tsnprintf(inifile, sizeof(inifile), NS_T("%sTestAUSReadStrings1.ini"), argv[0]);
     137               1 :   retval = ReadStrings(inifile, &testStrings);
     138               1 :   if (retval == OK) {
     139               1 :     if (strcmp(testStrings.title, "Title Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \
     140                 :                                   "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \
     141                 :                                   "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \
     142                 :                                   "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \
     143                 :                                   "\xE6\xB8\xAC\xE8\xA9\xA6 " \
     144               1 :                                   "\xE6\xB5\x8B\xE8\xAF\x95") != 0) {
     145               0 :       rv = 21;
     146               0 :       fail("%s | Title ini value incorrect (check 3)", TEST_NAME);
     147                 :     }
     148                 : 
     149               1 :     if (strcmp(testStrings.info, "Info Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \
     150                 :                                  "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \
     151                 :                                  "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \
     152                 :                                  "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \
     153                 :                                  "\xE6\xB8\xAC\xE8\xA9\xA6 " \
     154               1 :                                  "\xE6\xB5\x8B\xE8\xAF\x95\xE2\x80\xA6") != 0) {
     155               0 :       rv = 22;
     156               0 :       fail("%s | Info ini value incorrect (check 4)", TEST_NAME);
     157                 :     }
     158                 :   }
     159                 :   else {
     160               0 :     fail("%s | ReadStrings returned %i (check 2)", TEST_NAME, retval);
     161               0 :     rv = 23;
     162                 :   }
     163                 : 
     164                 :   // Test failure when the ini file exists without Title and with Info in the
     165                 :   // Strings section.
     166               1 :   NS_tsnprintf(inifile, sizeof(inifile), NS_T("%sTestAUSReadStrings2.ini"), argv[0]);
     167               1 :   retval = ReadStrings(inifile, &testStrings);
     168               1 :   if (retval != PARSE_ERROR) {
     169               0 :     rv = 24;
     170               0 :     fail("%s | ReadStrings returned %i (check 5)", TEST_NAME, retval);
     171                 :   }
     172                 : 
     173                 :   // Test failure when the ini file exists with Title and without Info in the
     174                 :   // Strings section.
     175               1 :   NS_tsnprintf(inifile, sizeof(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]);
     176               1 :   retval = ReadStrings(inifile, &testStrings);
     177               1 :   if (retval != PARSE_ERROR) {
     178               0 :     rv = 25;
     179               0 :     fail("%s | ReadStrings returned %i (check 6)", TEST_NAME, retval);
     180                 :   }
     181                 : 
     182                 :   // Test failure when the ini file doesn't exist
     183               1 :   NS_tsnprintf(inifile, sizeof(inifile), NS_T("%sTestAUSReadStringsBogus.ini"), argv[0]);
     184               1 :   retval = ReadStrings(inifile, &testStrings);
     185               1 :   if (retval != READ_ERROR) {
     186               0 :     rv = 26;
     187               0 :     fail("%s | ini file doesn't exist (check 7)", TEST_NAME);
     188                 :   }
     189                 : 
     190                 :   // Test reading a non-default section name
     191               1 :   NS_tsnprintf(inifile, sizeof(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]);
     192               1 :   retval = ReadStrings(inifile, "Title\0", 1, &testStrings.title, "BogusSection2");
     193               1 :   if (retval == OK) {
     194               1 :     if (strcmp(testStrings.title, "Bogus Title") != 0) {
     195               0 :       rv = 27;
     196               0 :       fail("%s | Title ini value incorrect (check 9)", TEST_NAME);
     197                 :     }
     198                 :   }
     199                 :   else {
     200               0 :     fail("%s | ReadStrings returned %i (check 8)", TEST_NAME, retval);
     201               0 :     rv = 28;
     202                 :   }
     203                 : 
     204                 : 
     205               1 :   if (rv == 0) {
     206               1 :     printf("TEST-PASS | %s | all checks passed\n", TEST_NAME);
     207                 :   } else {
     208               0 :     fail("%s | %i out of 9 checks failed", TEST_NAME, gFailCount);
     209                 :   }
     210                 : 
     211               1 :   return rv;
     212                 : }

Generated by: LCOV version 1.7