s_chunksaver_test.go 5.7 KB

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