summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/TestMsgStripRE.cpp
blob: eae217dcb2db16a88b9ab5be8a7a1b71cc59e220 (plain)
1
2
3
4
5
6
7
8
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include "mozilla/ArrayUtils.h"
#include "nsCOMPtr.h"
#include "nsMsgUtils.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsISupportsPrimitives.h"
#include "nsString.h"

#define STRING_SIZE 255
struct testInfo {
  char encodedInput[STRING_SIZE];
  char expectedOutput[STRING_SIZE];
  bool expectedDidModify;
};

int testStripRe(const char* encodedInput, char* expectedOutput,
                bool expectedDidModify) {
  // call NS_StripRE with the appropriate args
  nsCString modifiedSubject;
  bool didModify;
  didModify = NS_MsgStripRE(nsDependentCString(encodedInput), modifiedSubject);

  // make sure we got the right results
  if (didModify != expectedDidModify) return 2;

  if (didModify) {
    if (strcmp(expectedOutput, modifiedSubject.get())) {
      return 3;
    }
  } else if (strcmp(expectedOutput, encodedInput)) {
    return 4;
  }

  // test passed
  return 0;
}

// int main(int argc, char** argv)
TEST(TestMsgStripRE, TestMsgStripREMain)
{
  nsresult rv;
  nsCOMPtr<nsIPrefBranch> prefBranch(
      do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
  EXPECT_TRUE(NS_SUCCEEDED(rv));

  // set localizedRe pref, value "SV,ÆØÅ",
  // \xC3\x86, \xC3\x98 and \xC3\x85 are the UTF-8 encodings of Æ, Ø and Å.
  rv = prefBranch->SetStringPref("mailnews.localizedRe",
                                 "SV,\xC3\x86\xC3\x98\xC3\x85"_ns);
  EXPECT_TRUE(NS_SUCCEEDED(rv));

  // run our tests
  struct testInfo testInfoStructs[] = {
      // Note that re-encoding always happens in UTF-8.
      {"SV: =?ISO-8859-1?Q?=C6blegr=F8d?=", "=?UTF-8?B?w4ZibGVncsO4ZA==?=",
       true},
      {"=?ISO-8859-1?Q?SV=3A=C6blegr=F8d?=", "=?UTF-8?B?w4ZibGVncsO4ZA==?=",
       true},

      // Note that in the next two tests, the only ISO-8859-1 chars are in the
      // localizedRe piece, so once they've been stripped, the re-encoding
      // process simply writes out ASCII rather than an ISO-8859-1 encoded
      // string with no actual ISO-8859-1 special characters, which seems
      // reasonable.
      {"=?ISO-8859-1?Q?=C6=D8=C5=3A_Foo_bar?=", "Foo bar", true},
      {"=?ISO-8859-1?Q?=C6=D8=C5=3AFoo_bar?=", "Foo bar", true}};

  bool allTestsPassed = true;
  int result;
  for (unsigned int i = 0; i < MOZ_ARRAY_LENGTH(testInfoStructs); i++) {
    result = testStripRe(testInfoStructs[i].encodedInput,
                         testInfoStructs[i].expectedOutput,
                         testInfoStructs[i].expectedDidModify);
    if (result) {
      printf("Failed: %s, i=%d | result=%d\n", __FILE__, i, result);
      allTestsPassed = false;
    }
    EXPECT_TRUE(result == 0);
  }

  if (allTestsPassed) {
    printf("all tests passed\n");
  }
  EXPECT_TRUE(allTestsPassed);
}