2
0

SaveToLoadFromFileTests.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 22276: SaveToLoadFromFileTests.pas
  11. {
  12. { Rev 1.1 28/07/2003 19:30:02 CCostelloe
  13. { Added tests for base64, quoted-printable and default encoding. Added check 0
  14. { for message body text. Incorporated '.' tests to ensure that the
  15. { end-of-message marker CRLF.CRLF is not confused with inline '.'s.
  16. }
  17. {
  18. { Rev 1.0 26/07/2003 13:34:52 CCostelloe
  19. { Test to show bug with attachments being saved base64 encoded while Content
  20. { Transfer Encoding set to 7bit.
  21. }
  22. unit SaveToLoadFromFileTests;
  23. {This was written to demonstrate a bug in TIdMessage and hopefully check for it
  24. if it ever creeps in again.
  25. The attachment is written out with:
  26. Content-Transfer-Encoding: 7bit
  27. ...but the attachment is actually encoded as base64 - you can verify this by
  28. running the test, wait for the ShowMessage dialog, manually edit the file
  29. it displays the name of (change the "7bit" to "base64"), and it will pass
  30. check 6 but not check 7.
  31. The real bug is that SaveToFile should have left the attachment content
  32. unencoded.
  33. Ciaran Costelloe, 26th July 2003.
  34. CC2: Added tests for base64, quoted-printable and default encoding. Added
  35. check 0 for message body text. Incorporated '.' tests to ensure that the
  36. end-of-message marker CRLF.CRLF is not confused with inline '.'s.
  37. }
  38. interface
  39. uses
  40. Dialogs,
  41. Classes,
  42. IndyBox;
  43. type
  44. TSaveToLoadFromFileTests = class (TIndyBox)
  45. public
  46. procedure Test; override;
  47. procedure ATest(AContentTransferEncoding: string);
  48. end;
  49. implementation
  50. uses
  51. IdGlobal,
  52. IdMessage,
  53. SysUtils, IdAttachmentFile, IdAttachmentMemory, IdText;
  54. const
  55. ContentID: string = '<73829274893.90238490.hkjsdhfsk>';
  56. { TExtraHeadersBox }
  57. procedure TSaveToLoadFromFileTests.Test;
  58. begin
  59. ATest('base64');
  60. ATest(''); {"Default" encoding}
  61. ATest('quoted-printable');
  62. ATest('7bit');
  63. end;
  64. procedure TSaveToLoadFromFileTests.ATest(AContentTransferEncoding: string);
  65. var
  66. Msg: TIdMessage;
  67. Att: TIdAttachmentMemory;
  68. Att2: TIdAttachmentFile;
  69. LTempFilename: string;
  70. AttText: string;
  71. MsgText: string;
  72. sTemp: string;
  73. TheStrings: TStringList;
  74. begin
  75. sTemp := AContentTransferEncoding; if sTemp = '' then sTemp := 'default';
  76. ShowMessage('Starting test for '+sTemp+' encoding...');
  77. TheStrings := TStringList.Create;
  78. Msg := TIdMessage.Create(nil);
  79. MsgText := 'This is test message text.';
  80. Msg.Body.Add(MsgText);
  81. //Att := TIdAttachmentFile.Create(Msg.MessageParts, GetDataDir+'test.bmp');
  82. AttText := 'This is a test attachment. This is deliberately a long line to ensure that the generated encoded lines go beyond 80 characters so that their line-breaking is tested.'#13#10'.This starts with a period'#13#10'.'#13#10'Last line only had a period.';
  83. Att := TIdAttachmentMemory.Create(Msg.MessageParts, AttText);
  84. try
  85. Status('Creating message');
  86. Att.ContentID := ContentID;
  87. Att.FileName := 'test.txt';
  88. Att.ContentTransfer := AContentTransferEncoding;
  89. LTempFilename := MakeTempFilename;
  90. //Msg.SaveToFile(GetTempDir()+'test.msg');
  91. Msg.SaveToFile(LTempFilename);
  92. Status('Message saved to file '+LTempFilename);
  93. ShowMessage('Message saved to file '+LTempFilename+'. You may wish to view this to see if the intermediate file looks OK.');
  94. finally
  95. Msg.Free;
  96. end;
  97. Msg := TIdMessage.Create(nil);
  98. try
  99. Status('Loading message');
  100. //Msg.LoadFromFile(GetTempDir()+'test.msg');
  101. Msg.LoadFromFile(LTempFilename);
  102. sTemp := Msg.Body.Strings[0];
  103. Check(sTemp = MsgText, 'Check 0: Message body text >'+MsgText+'< changed to >'+sTemp+'<');
  104. Check(Msg.MessageParts.Count = 2, 'Check 1: Wrong messagepart count ('+IntToStr(Msg.MessageParts.Count)+')!');
  105. Check(Msg.MessageParts.Items[0] is TIdText, 'Check 2: Wrong type of attachment in #1');
  106. Check(Msg.MessageParts.Items[1] is TIdAttachmentFile, 'Check 3: Wrong type of attachment in #2');
  107. Att2 := TIdAttachmentFile(Msg.MessageParts.Items[1]);
  108. Check(Att2.FileName = 'test.txt', 'Check 4: Filename of Attachment lost');
  109. Check(Att2.ContentID = ContentID, 'Check 5: Content-ID lost/garbled!');
  110. TheStrings.LoadFromFile(Att2.StoredPathName);
  111. sTemp := TheStrings.Strings[0];
  112. Check(sTemp = 'This is a test attachment. This is deliberately a long line to ensure that the generated encoded lines go beyond 80 characters so that their line-breaking is tested.',
  113. 'Check 6a: Attachment text >'+'This is a test attachment. This is deliberately a long line to ensure that the generated encoded lines go beyond 80 characters so that their line-breaking is tested.'+'< changed to >'+sTemp+'<');
  114. sTemp := TheStrings.Strings[1];
  115. Check(sTemp = '.This starts with a period',
  116. 'Check 6b: Attachment text >'+'.This starts with a period'+'< changed to >'+sTemp+'<');
  117. sTemp := TheStrings.Strings[2];
  118. Check(sTemp = '.',
  119. 'Check 6c: Attachment text >'+'.'+'< changed to >'+sTemp+'<');
  120. sTemp := TheStrings.Strings[3];
  121. Check(sTemp = 'Last line only had a period.',
  122. 'Check 6d: Attachment text >'+'Last line only had a period.'+'< changed to >'+sTemp+'<');
  123. if AContentTransferEncoding <> '' then begin
  124. {Note: We don't check encoding type if AContentTransferEncoding is '' because
  125. we don't care what encoding SaveToFile chose. We do in the other cases, because
  126. we specifically requested a certain encoding type.}
  127. Check(Att2.ContentTransfer = AContentTransferEncoding, 'Check 7: Attachment Content Transfer Encoding changed from '+AContentTransferEncoding+' to '+Att2.ContentTransfer);
  128. end;
  129. finally
  130. Msg.Free;
  131. end;
  132. sTemp := AContentTransferEncoding; if sTemp = '' then sTemp := 'default';
  133. ShowMessage('Successfully completed test for '+sTemp+' encoding!');
  134. end;
  135. initialization
  136. TIndyBox.RegisterBox(TSaveToLoadFromFileTests, 'ExtraHeaders', 'Message');
  137. end.