summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/db/gloda/test/unit/test_index_bad_messages.js
blob: 5920ac981e50e6a3a9df2bfccf1dba06e91a6038 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* 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/. */

/*
 * Test that we fail on bad messages by marking the messages as bad rather than
 *  exploding or something bad like that.
 */

var {
  assertExpectedMessagesIndexed,
  glodaTestHelperInitialize,
  waitForGlodaIndexer,
} = ChromeUtils.import("resource://testing-common/gloda/GlodaTestHelper.jsm");
var { configureGlodaIndexing } = ChromeUtils.import(
  "resource://testing-common/gloda/GlodaTestHelperFunctions.jsm"
);
var { Gloda } = ChromeUtils.import("resource:///modules/gloda/GlodaPublic.jsm");
var { GlodaMsgIndexer } = ChromeUtils.import(
  "resource:///modules/gloda/IndexMsg.jsm"
);
var { MessageGenerator } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageGenerator.jsm"
);
var { MessageInjection } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageInjection.jsm"
);

const GLODA_BAD_MESSAGE_ID = 2;

var illegalMessageTemplates = [
  // -- Authors
  {
    name: "no author",
    clobberHeaders: {
      From: "",
    },
  },
  {
    name: "too many authors (> 1)",
    clobberHeaders: {
      From: "Tweedle Dee <dee@example.com>, Tweedle Dum <dum@example.com>",
    },
  },
];

var messageInjection;

add_setup(function () {
  let msgGen = new MessageGenerator();
  messageInjection = new MessageInjection({ mode: "local" }, msgGen);
  glodaTestHelperInitialize(messageInjection);
});

add_task(async function test_illegal_message_no_author() {
  await illegal_message(illegalMessageTemplates[0]);
});
add_task(async function test_illegal_message_too_many_authors() {
  await illegal_message(illegalMessageTemplates[1]);
});

/**
 * A byzantine failure to stream should not sink us.  Fake a failure.
 */
add_task(async function test_streaming_failure() {
  configureGlodaIndexing({ injectFaultIn: "streaming" });

  // Inject the messages.
  let [msgSet] = await messageInjection.makeNewSetsInFolders(
    [messageInjection.getInboxFolder()],
    [{ count: 1 }]
  );

  // Indexing should complete without actually indexing the message.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], {
      recovered: 1,
      failedToRecover: 0,
      cleanedUp: 0,
      hadNoCleanUp: 0,
    })
  );

  // Make sure the header has the expected gloda bad message state.
  let msgHdr = msgSet.getMsgHdr(0);
  Assert.equal(msgHdr.getUint32Property("gloda-id"), GLODA_BAD_MESSAGE_ID);

  // Make sure gloda does not think the message is indexed
  Assert.equal(Gloda.isMessageIndexed(msgHdr), false);

  configureGlodaIndexing({});
});

/**
 * If we have one bad message followed by a good message, the good message
 *  should still get indexed.  Additionally, if we do a sweep on the folder,
 *  we should not attempt to index the message again.
 */
add_task(async function test_recovery_and_no_second_attempts() {
  let [, goodSet] = await messageInjection.makeNewSetsInFolders(
    [messageInjection.getInboxFolder()],
    [{ count: 1, clobberHeaders: { From: "" } }, { count: 1 }]
  );

  await waitForGlodaIndexer();
  Assert.ok(...assertExpectedMessagesIndexed([goodSet], { recovered: 1 }));

  // Index the folder; no messages should get indexed and there should be no
  //  failure things.
  GlodaMsgIndexer.indexFolder(messageInjection.getInboxFolder());
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], {
      recovered: 0,
      failedToRecover: 0,
      cleanedUp: 0,
      hadNoCleanUp: 0,
    })
  );
});

/**
 * Make sure that we attempt to reindex a dirty bad message and that when we
 *  fail that we clear the dirty bit.
 */
add_task(async function test_reindex_on_dirty_clear_dirty_on_fail() {
  // Inject a new illegal message
  let [msgSet] = await messageInjection.makeNewSetsInFolders(
    [messageInjection.getInboxFolder()],
    [
      {
        count: 1,
        clobberHeaders: illegalMessageTemplates[0].clobberHeaders,
      },
    ]
  );

  // Indexing should complete without actually indexing the message.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], {
      recovered: 1,
      failedToRecover: 0,
      cleanedUp: 0,
      hadNoCleanUp: 0,
    })
  );

  // Mark the message dirty, force the folder to be indexed.
  let msgHdr = msgSet.getMsgHdr(0);
  msgHdr.setUint32Property("gloda-dirty", 1);
  GlodaMsgIndexer.indexFolder(messageInjection.getInboxFolder());
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], {
      recovered: 1,
      failedToRecover: 0,
      cleanedUp: 0,
      hadNoCleanUp: 0,
    })
  );
  // Now the message should be clean.
  Assert.equal(msgHdr.getUint32Property("gloda-dirty"), 0);

  // Check again with filthy.
  msgHdr.setUint32Property("gloda-dirty", 2);
  GlodaMsgIndexer.indexFolder(messageInjection.getInboxFolder());
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], {
      recovered: 1,
      failedToRecover: 0,
      cleanedUp: 0,
      hadNoCleanUp: 0,
    })
  );
  // Now the message should be clean.
  Assert.equal(msgHdr.getUint32Property("gloda-dirty"), 0);
});

/**
 * Using exciting templates from |illegalMessageTemplates|, verify that gloda
 *  fails to index them and marks the messages bad.
 */
async function illegal_message(aInfo) {
  // Inject the messages.
  let [msgSet] = await messageInjection.makeNewSetsInFolders(
    [messageInjection.getInboxFolder()],
    [{ count: 1, clobberHeaders: aInfo.clobberHeaders }]
  );

  // Indexing should complete without actually indexing the message.
  await waitForGlodaIndexer();
  Assert.ok(
    ...assertExpectedMessagesIndexed([], {
      recovered: 1,
      failedToRecover: 0,
      cleanedUp: 0,
      hadNoCleanUp: 0,
    })
  );

  // Make sure the header has the expected gloda bad message state.
  let msgHdr = msgSet.getMsgHdr(0);
  Assert.equal(msgHdr.getUint32Property("gloda-id"), GLODA_BAD_MESSAGE_ID);

  // Make sure gloda does not think the message is indexed.
  Assert.equal(Gloda.isMessageIndexed(msgHdr), false);
}