summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/TestImapHdrXferInfo.cpp
blob: 442ca0356758f495589743cd7017ed42d60f3f9c (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
92
93
94
95
96
97
98
99
100
101
/* -*- 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 <stdio.h>
#include "TestHarness.h"
#include "nsCOMPtr.h"
#include "msgCore.h"
#include "nsImapProtocol.h"

// What to check
enum EHdrArrayCheck { eAddNoCheck, eCheckSame };

int MainChecks(nsMsgImapHdrXferInfo* hdrInfo, nsIImapHeaderInfo** hdrArray,
               EHdrArrayCheck hdrArrayCheck) {
  nsCOMPtr<nsIImapHeaderInfo> hdr;
  int32_t numHdrs = -1;

  // Check the number of headers initially is zero
  if (NS_FAILED(hdrInfo->GetNumHeaders(&numHdrs))) return 1;

  if (numHdrs != 0) return 2;

  // Get a header that doesn't exist
  if (hdrInfo->GetHeader(1, getter_AddRefs(hdr)) != NS_ERROR_NULL_POINTER)
    return 3;

  int32_t i;
  for (i = 0; i < kNumHdrsToXfer; ++i) {
    // Now kick off a new one.
    hdr = hdrInfo->StartNewHdr();
    if (!hdr) return 4;

    // Check pointers are different or not depending on which cycle we are in
    switch (hdrArrayCheck) {
      case eAddNoCheck:
        hdrArray[i] = hdr;
        break;
      case eCheckSame:
        if (hdrArray[i] != hdr) return 5;
        break;
      default:
        return 1;
    }

    if (NS_FAILED(hdrInfo->GetNumHeaders(&numHdrs))) return 1;

    if (numHdrs != i + 1) return 7;
  }

  // Now try and get one more (this should return null)
  if (hdrInfo->StartNewHdr()) return 8;

  // Now check the number of headers
  if (NS_FAILED(hdrInfo->GetNumHeaders(&numHdrs))) return 1;

  if (numHdrs != kNumHdrsToXfer) return 9;

  // Now check our pointers align with those from GetHeader
  if (hdrArrayCheck != 2) {
    for (i = 0; i < kNumHdrsToXfer; ++i) {
      if (NS_FAILED(hdrInfo->GetHeader(i, getter_AddRefs(hdr)))) return 1;

      if (hdr != hdrArray[i]) return 10;
    }
  }
  return 0;
}

// General note about return values:
// return 1 for a setup or xpcom type failure, return 2 for a real test failure
int main(int argc, char** argv) {
  ScopedXPCOM xpcom("TestImapHdrXferInfo.cpp");
  if (xpcom.failed()) return 1;

  RefPtr<nsMsgImapHdrXferInfo> hdrInfo = new nsMsgImapHdrXferInfo();
  // Purposely not reference counted to ensure we get the same pointers the
  // second time round MainChecks.
  nsIImapHeaderInfo* hdrArray[kNumHdrsToXfer] = {nullptr};

  int result = MainChecks(hdrInfo, hdrArray, eAddNoCheck);
  if (result) {
    printf("TEST-UNEXPECTED-FAIL | %s | %d\n", __FILE__, result);
    return result;
  }

  // Now reset all
  hdrInfo->ResetAll();

  // and repeat
  result = MainChecks(hdrInfo, hdrArray, eCheckSame);
  if (result) {
    // add 100 to differentiate results
    result += 100;
    printf("TEST-UNEXPECTED-FAIL | %s | %d\n", __FILE__, result);
    return result;
  }

  printf("TEST-PASS | %s | all tests passed\n", __FILE__);
  return result;
}