summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_longLines.js
blob: cd75e75d389e9fb72724a7cbe3306452ea971f32 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Test ensuring that messages with "long lines" are transmitted correctly.
 * Most of this test was copied from test_messageHeaders.js.
 */

const { MimeParser } = ChromeUtils.import("resource:///modules/mimeParser.jsm");

var CompFields = CC(
  "@mozilla.org/messengercompose/composefields;1",
  Ci.nsIMsgCompFields
);

// Copied from jsmime.js.
function stringToTypedArray(buffer) {
  var typedarray = new Uint8Array(buffer.length);
  for (var i = 0; i < buffer.length; i++) {
    typedarray[i] = buffer.charCodeAt(i);
  }
  return typedarray;
}

function checkDraftHeadersAndBody(
  expectedHeaders,
  expectedBody,
  charset = "UTF-8"
) {
  let msgData = mailTestUtils.loadMessageToString(
    gDraftFolder,
    mailTestUtils.firstMsgHdr(gDraftFolder)
  );
  checkMessageHeaders(msgData, expectedHeaders);

  // Get the message body, decode from base64 and check.
  let endOfHeaders = msgData.indexOf("\r\n\r\n");
  let body = msgData.slice(endOfHeaders + 4);
  let endOfBody = body.indexOf("\r\n\r\n");

  if (endOfBody > 0) {
    body = body.slice(0, endOfBody);
  } else {
    body = body.slice(0, body.length);
  }

  // Remove line breaks and decode from base64 if required.
  if (expectedHeaders["Content-Transfer-Encoding"] == "base64") {
    body = atob(body.replace(/\r\n/g, ""));
  }

  if (charset == "UTF-8") {
    let expectedBinary = String.fromCharCode.apply(
      undefined,
      new TextEncoder("UTF-8").encode(expectedBody)
    );
    Assert.equal(body, expectedBinary);
  } else {
    let strView = stringToTypedArray(body);
    let decodedBody = new TextDecoder(charset).decode(strView);
    Assert.equal(decodedBody, expectedBody);
  }
}

function checkMessageHeaders(msgData, expectedHeaders, partNum = "") {
  let seen = false;
  let handler = {
    startPart(part, headers) {
      if (part != partNum) {
        return;
      }
      seen = true;
      for (let header in expectedHeaders) {
        let expected = expectedHeaders[header];
        if (expected === undefined) {
          Assert.ok(!headers.has(header));
        } else {
          let value = headers.getRawHeader(header);
          Assert.equal(value.length, 1);
          value[0] = value[0].replace(/boundary=[^;]*(;|$)/, "boundary=.");
          Assert.equal(value[0], expected);
        }
      }
    },
  };
  MimeParser.parseSync(msgData, handler, {
    onerror(e) {
      throw e;
    },
  });
  Assert.ok(seen);
}

// Create a line with 600 letters 'a' with acute accent, encoded as
// two bytes c3a1 in UTF-8.
let longMultibyteLine = "\u00E1".repeat(600);

// And here a line with a Korean character, encoded as three bytes
// ec9588 in UTF-8.
let longMultibyteLineCJK = "안".repeat(400);

// And some Japanese.
let longMultibyteLineJapanese = "語".repeat(450);

async function testBodyWithLongLine() {
  // Lines in the message body are split by CRLF according to RFC 5322, should
  // be independent of the system.
  let newline = "\r\n";

  let fields = new CompFields();
  let identity = getSmtpIdentity(
    "from@tinderbox.invalid",
    getBasicSmtpServer()
  );
  identity.fullName = "Me";
  identity.organization = "World Destruction Committee";
  fields.from = "Nobody <nobody@tinderbox.invalid>";
  fields.to = "Nobody <nobody@tinderbox.invalid>";
  fields.subject = "Message with 1200 byte line in body";
  let htmlMessage =
    "<html><head>" +
    '<meta http-equiv="content-type" content="text/html; charset=utf-8">' +
    "</head><body>" +
    longMultibyteLine +
    "</body></html>\r\n\r\n";
  fields.body = htmlMessage;
  await richCreateMessage(fields, [], identity);
  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/html; charset=UTF-8",
      "Content-Transfer-Encoding": "base64",
    },
    htmlMessage
  );

  // Again, but this time as plain text.
  fields.body = htmlMessage;
  fields.forcePlainText = true;
  fields.useMultipartAlternative = false;
  await richCreateMessage(fields, [], identity);
  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/plain; charset=UTF-8; format=flowed",
      "Content-Transfer-Encoding": "base64",
    },
    longMultibyteLine + " " + newline + newline // Expected body: The message without the tags.
  );

  // Now CJK.
  fields.forcePlainText = false;
  htmlMessage =
    "<html><head>" +
    '<meta http-equiv="content-type" content="text/html; charset=utf-8">' +
    "</head><body>" +
    longMultibyteLineCJK +
    "</body></html>\r\n\r\n";
  fields.body = htmlMessage;
  await richCreateMessage(fields, [], identity);
  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/html; charset=UTF-8",
      "Content-Transfer-Encoding": "base64",
    },
    htmlMessage
  );

  // Again, but this time as plain text.
  fields.body = htmlMessage;
  fields.forcePlainText = true;
  fields.useMultipartAlternative = false;
  await richCreateMessage(fields, [], identity);
  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/plain; charset=UTF-8; format=flowed",
      "Content-Transfer-Encoding": "base64",
    },
    longMultibyteLineCJK + " " + newline + newline // Expected body: The message without the tags.
  );

  // Now a test for ISO-2022-JP.
  fields.forcePlainText = false;
  htmlMessage =
    "<html><head>" +
    '<meta http-equiv="content-type" content="text/html; charset=ISO-2022-JP">' +
    "</head><body>" +
    longMultibyteLineJapanese +
    "</body></html>\r\n\r\n";
  fields.body = htmlMessage;
  await richCreateMessage(fields, [], identity);
  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/html; charset=UTF-8",
      "Content-Transfer-Encoding": "base64",
    },
    htmlMessage
  );

  // Again, but this time as plain text.
  fields.body = htmlMessage;
  fields.forcePlainText = true;
  fields.useMultipartAlternative = false;
  await richCreateMessage(fields, [], identity);

  let expectedBody = longMultibyteLineJapanese + " " + newline + newline;

  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/plain; charset=UTF-8; format=flowed",
      "Content-Transfer-Encoding": "base64",
    },
    expectedBody
  );

  // Again, but this time not flowed.
  fields.body = htmlMessage;
  Services.prefs.setBoolPref("mailnews.send_plaintext_flowed", false);

  await richCreateMessage(fields, [], identity);
  checkDraftHeadersAndBody(
    {
      "Content-Type": "text/plain; charset=UTF-8",
      "Content-Transfer-Encoding": "base64",
    },
    expectedBody.replace(/ /g, "") // No spaces expected this time.
  );
}

var tests = [testBodyWithLongLine];

function run_test() {
  // Ensure we have at least one mail account
  localAccountUtils.loadLocalMailAccount();
  tests.forEach(x => add_task(x));
  run_next_test();
}