s_chunksaver_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "github.com/flashmob/go-guerrilla/mail/mime"
  5. "testing"
  6. )
  7. func TestChunkedBytesBuffer(t *testing.T) {
  8. var in string
  9. var buf chunkedBytesBuffer
  10. buf.capTo(64)
  11. // the data to write is over-aligned
  12. in = `123456789012345678901234567890123456789012345678901234567890abcde12345678901234567890123456789012345678901234567890123456789abcdef` // len == 130
  13. i, _ := buf.Write([]byte(in[:]))
  14. if i != len(in) {
  15. t.Error("did not write", len(in), "bytes")
  16. }
  17. // the data to write is aligned
  18. var buf2 chunkedBytesBuffer
  19. buf2.capTo(64)
  20. in = `123456789012345678901234567890123456789012345678901234567890abcde12345678901234567890123456789012345678901234567890123456789abcd` // len == 128
  21. i, _ = buf2.Write([]byte(in[:]))
  22. if i != len(in) {
  23. t.Error("did not write", len(in), "bytes")
  24. }
  25. // the data to write is under-aligned
  26. var buf3 chunkedBytesBuffer
  27. buf3.capTo(64)
  28. in = `123456789012345678901234567890123456789012345678901234567890abcde12345678901234567890123456789012345678901234567890123456789ab` // len == 126
  29. i, _ = buf3.Write([]byte(in[:]))
  30. if i != len(in) {
  31. t.Error("did not write", len(in), "bytes")
  32. }
  33. // the data to write is smaller than the buffer
  34. var buf4 chunkedBytesBuffer
  35. buf4.capTo(64)
  36. in = `1234567890` // len == 10
  37. i, _ = buf4.Write([]byte(in[:]))
  38. if i != len(in) {
  39. t.Error("did not write", len(in), "bytes")
  40. }
  41. // what if the buffer already contains stuff before Write is called
  42. // and the buffer len is smaller than the len of the slice of bytes we pass it?
  43. var buf5 chunkedBytesBuffer
  44. buf5.capTo(5)
  45. buf5.buf = append(buf5.buf, []byte{'a', 'b', 'c'}...)
  46. in = `1234567890` // len == 10
  47. i, _ = buf5.Write([]byte(in[:]))
  48. if i != len(in) {
  49. t.Error("did not write", len(in), "bytes")
  50. }
  51. }
  52. var email = `From: Al Gore <[email protected]>
  53. To: White House Transportation Coordinator <[email protected]>
  54. Subject: [Fwd: Map of Argentina with Description]
  55. MIME-Version: 1.0
  56. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; s=ncr424; d=reliancegeneral.co.in;
  57. h=List-Unsubscribe:MIME-Version:From:To:Reply-To:Date:Subject:Content-Type:Content-Transfer-Encoding:Message-ID; [email protected];
  58. bh=F4UQPGEkpmh54C7v3DL8mm2db1QhZU4gRHR1jDqffG8=;
  59. b=MVltcq6/I9b218a370fuNFLNinR9zQcdBSmzttFkZ7TvV2mOsGrzrwORT8PKYq4KNJNOLBahswXf
  60. GwaMjDKT/5TXzegdX/L3f/X4bMAEO1einn+nUkVGLK4zVQus+KGqm4oP7uVXjqp70PWXScyWWkbT
  61. 1PGUwRfPd/HTJG5IUqs=
  62. Content-Type: multipart/mixed;
  63. boundary="D7F------------D7FD5A0B8AB9C65CCDBFA872"
  64. This is a multi-part message in MIME format.
  65. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  66. Content-Type: text/plain; charset=us-ascii
  67. Content-Transfer-Encoding: 7bit
  68. Fred,
  69. Fire up Air Force One! We're going South!
  70. Thanks,
  71. Al
  72. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  73. Content-Type: message/rfc822
  74. Content-Transfer-Encoding: 7bit
  75. Content-Disposition: inline
  76. Return-Path: <[email protected]>
  77. Received: from mailhost.whitehouse.gov ([192.168.51.200])
  78. by heartbeat.whitehouse.gov (8.8.8/8.8.8) with ESMTP id SAA22453
  79. for <[email protected]>;
  80. Mon, 13 Aug 1998 l8:14:23 +1000
  81. Received: from the_big_box.whitehouse.gov ([192.168.51.50])
  82. by mailhost.whitehouse.gov (8.8.8/8.8.7) with ESMTP id RAA20366
  83. for [email protected]; Mon, 13 Aug 1998 17:42:41 +1000
  84. Date: Mon, 13 Aug 1998 17:42:41 +1000
  85. Message-Id: <[email protected]>
  86. From: Bill Clinton <[email protected]>
  87. To: A1 (The Enforcer) Gore <[email protected]>
  88. Subject: Map of Argentina with Description
  89. MIME-Version: 1.0
  90. Content-Type: multipart/mixed;
  91. boundary="DC8------------DC8638F443D87A7F0726DEF7"
  92. This is a multi-part message in MIME format.
  93. --DC8------------DC8638F443D87A7F0726DEF7
  94. Content-Type: text/plain; charset=us-ascii
  95. Content-Transfer-Encoding: 7bit
  96. Hi A1,
  97. I finally figured out this MIME thing. Pretty cool. I'll send you
  98. some sax music in .au files next week!
  99. Anyway, the attached image is really too small to get a good look at
  100. Argentina. Try this for a much better map:
  101. http://www.1one1yp1anet.com/dest/sam/graphics/map-arg.htm
  102. Then again, shouldn't the CIA have something like that?
  103. Bill
  104. --DC8------------DC8638F443D87A7F0726DEF7
  105. Content-Type: image/gif; name="map_of_Argentina.gif"
  106. Content-Transfer-Encoding: base64
  107. Content-Disposition: attachment; filename="map_of_Argentina.gif"
  108. R01GOD1hJQA1AKIAAP/////78P/omn19fQAAAAAAAAAAAAAAACwAAAAAJQA1AAAD7Qi63P5w
  109. wEmjBCLrnQnhYCgM1wh+pkgqqeC9XrutmBm7hAK3tP31gFcAiFKVQrGFR6kscnonTe7FAAad
  110. GugmRu3CmiBt57fsVq3Y0VFKnpYdxPC6M7Ze4crnnHum4oN6LFJ1bn5NXTN7OF5fQkN5WYow
  111. BEN2dkGQGWJtSzqGTICJgnQuTJN/WJsojad9qXMuhIWdjXKjY4tenjo6tjVssk2gaWq3uGNX
  112. U6ZGxseyk8SasGw3J9GRzdTQky1iHNvcPNNI4TLeKdfMvy0vMqLrItvuxfDW8ubjueDtJufz
  113. 7itICBxISKDBgwgTKjyYAAA7
  114. --DC8------------DC8638F443D87A7F0726DEF7--
  115. --D7F------------D7FD5A0B8AB9C65CCDBFA872--
  116. `
  117. func TestChunkSaverWrite(t *testing.T) {
  118. // parse an email
  119. parser := mime.NewMimeParser()
  120. // place the parse result in an envelope
  121. e := mail.NewEnvelope("127.0.0.1", 1)
  122. e.Values["MimeParts"] = &parser.Parts
  123. // instantiate the chunk saver
  124. chunksaver := streamers["chunksaver"]()
  125. // add the default processor as the underlying processor for chunksaver
  126. // this will also set our Open, Close and Initialize functions
  127. stream := chunksaver.p(DefaultStreamProcessor{})
  128. // configure the buffer cap
  129. bc := BackendConfig{}
  130. bc["chunksaver_chunk_size"] = 64
  131. _ = Svc.initialize(bc)
  132. // give it the envelope with the parse results
  133. _ = chunksaver.Open(e)
  134. // let's test it
  135. writeIt(parser, t, stream, 128)
  136. _ = chunksaver.Close()
  137. //writeIt(parser, t, stream, 128000)
  138. }
  139. func writeIt(parser *mime.Parser, t *testing.T, stream StreamProcessor, size int) {
  140. if size > len(email) {
  141. size = len(email)
  142. }
  143. total := 0
  144. // break up the email in to chunks of size. Feed them through the mime parser
  145. for msgPos := 0; msgPos < len(email); msgPos += size {
  146. err := parser.Parse([]byte(email)[msgPos : msgPos+size])
  147. if err != nil {
  148. t.Error(err)
  149. t.Fail()
  150. }
  151. // todo: close parser on last chunk! (and finalize save)
  152. cut := msgPos + size
  153. if cut > len(email) {
  154. // the last chunk make be shorter than size
  155. cut -= cut - len(email)
  156. }
  157. i, _ := stream.Write([]byte(email)[msgPos:cut])
  158. total += i
  159. }
  160. if total != len(email) {
  161. t.Error("short write, total is", total, "but len(email) is", len(email))
  162. }
  163. }