s_chunksaver_test.go 6.1 KB

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