fuzz_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // +build gofuzz
  2. package guerrilla
  3. import (
  4. "testing"
  5. "io/ioutil"
  6. "os"
  7. )
  8. // writeCorpos writes data to corpus file name, if it doesn't exists
  9. func writeCorpos(name string, data []byte) {
  10. if _, err := os.Stat("./workdir/corpus"); err == nil {
  11. if _, err := os.Stat("./workdir/corpus/" + name); err != nil {
  12. ioutil.WriteFile("./workdir/corpus/"+name, data, 0644)
  13. }
  14. }
  15. }
  16. func TestGenerateCorpus(t *testing.T) {
  17. str := "EHLO test.com\r\n" +
  18. "MAIL FROM:<[email protected]>\r\n" +
  19. "RCPT TO: <[email protected]>" +
  20. "DATA\r\n" +
  21. "Subject: Testing Subject\r\n" +
  22. "\r\n" +
  23. "..Some body\r\n" +
  24. "..\r\n" +
  25. ".\r\n"
  26. writeCorpos("0", []byte(str))
  27. str = "HELO test.com\r\n" +
  28. "MAIL FROM:[email protected]\r\n" +
  29. "RCPT TO:<[email protected]>" +
  30. "DATA\r\n" +
  31. "Subject: =?ISO-2022-JP?B?GyRCMX5KZxsoQlVSTBskQiROGyhC?=\r\n" +
  32. "\t=?ISO-2022-JP?B?GyRCJCpDTiRpJDshQxsoQi0xOTYbJEIhbiU5JUglbSVzJTAlPCVtGyhC?=\r\n" +
  33. "\r\n" +
  34. "..Now you're just somebody that i used to know\r\n" +
  35. ".\r\n"
  36. writeCorpos("1", []byte(str))
  37. str = "HELO test.com\r\n" +
  38. "MAIL FROM:<[email protected]>\r\n" +
  39. "RCPT TO:<[email protected]>\r\n" +
  40. "RCPT TO:<[email protected]>\r\n" +
  41. "RCPT TO:<[email protected]>\r\n" +
  42. "DATA\r\n" +
  43. "Subject: =?ISO-2022-JP?B?GyRCMX5KZxsoQlVSTBskQiROGyhC?=\r\n" +
  44. "\t=?ISO-2022-JP?B?GyRCJCpDTiRpJDshQxsoQi0xOTYbJEIhbiU5JUglbSVzJTAlPCVtGyhC?=\r\n" +
  45. "\r\n" +
  46. "..Now you're just somebody that i used to know\r\n" +
  47. ".\r\n"
  48. writeCorpos("2", []byte(str))
  49. str = "HELO test.com\r\n" +
  50. "MAIL FROM:<[email protected]> BODY=8BITMIME\r\n" +
  51. "RCPT TO:<[email protected]>\r\n" +
  52. "RCPT TO:<[email protected]>\r\n" +
  53. "RCPT TO:<[email protected]>\r\n" +
  54. "DATA\r\n" +
  55. "Subject: =?ISO-2022-JP?B?GyRCMX5KZxsoQlVSTBskQiROGyhC?=\r\n" +
  56. "\t=?ISO-2022-JP?B?GyRCJCpDTiRpJDshQxsoQi0xOTYbJEIhbiU5JUglbSVzJTAlPCVtGyhC?=\r\n" +
  57. "\r\n" +
  58. "..Now you're just somebody that i used to know\r\n" +
  59. ".\r\n"
  60. writeCorpos("2", []byte(str))
  61. str = "HELO test.com\r\n" +
  62. "MAIL FROM:<[email protected]> BODY=8BITMIME\r\n" +
  63. "HELP\r\n" +
  64. "NOOP\r\n" +
  65. "RCPT TO:<[email protected]>\r\n" +
  66. "RCPT TO:<[email protected]>\r\n" +
  67. "DATA\r\n" +
  68. "No subject\r\n" +
  69. "..Now you're just somebody that i used to know\r\n" +
  70. ".\r\n"
  71. writeCorpos("3", []byte(str))
  72. str = "HELO test.com\r\n" +
  73. "MAIL FROM: <[email protected]> BODY=8BITMIME\r\n" +
  74. "RSET\r\n" +
  75. "NOOP\r\n" +
  76. "RCPT TO:<[email protected]>\r\n" +
  77. "RCPT TO:<[email protected]>\r\n" +
  78. "DATA\r\n" +
  79. "No subject\r\n" +
  80. "..Now you're just somebody that i used to know\r\n" +
  81. ".\r\n"
  82. writeCorpos("4", []byte(str))
  83. str = "HELO test.com\r\n" + "MAIL FROM:<>\r\n"
  84. writeCorpos("5", []byte(str))
  85. str = "MAIL from: <\r\n"
  86. writeCorpos("6", []byte(str))
  87. str = "HELO test.com\r\n" + "MAIL FrOm: <<>>\r\n"
  88. writeCorpos("8", []byte(str))
  89. str = "HELO test.com\r\n" + "MAIL FrOm:\r\n"
  90. writeCorpos("7", []byte(str))
  91. str = "HELO test.com\r\n" + "RCPT TO:\r\n"
  92. writeCorpos("9", []byte(str))
  93. str = "HELO test.com\r\n" + "RCPT TO:<>\r\n"
  94. writeCorpos("10", []byte(str))
  95. str = "HELO test.com\r\n" + "RCPT TO:<\r\n"
  96. writeCorpos("11", []byte(str))
  97. str = "HELO test.com\r\n" + "RCPT TO:<[email protected]> somethingstrange\r\n"
  98. writeCorpos("12", []byte(str))
  99. str = "HELO test.com\r\n" + "VRFY\r\n" + "QUIT\r\n"
  100. writeCorpos("13", []byte(str))
  101. str = "HELO test.com\r\n" + "VRFY:\r\n"
  102. writeCorpos("14", []byte(str))
  103. str = "HELO test.com\r\n" + "VRFY all cows eat grass\r\n"
  104. writeCorpos("15", []byte(str))
  105. str = "HELO test.com\r\n" + "RSET\r\n"
  106. writeCorpos("16", []byte(str))
  107. str = "HELO test.com\r\n" + "RSET:\r\n"
  108. writeCorpos("17", []byte(str))
  109. str = "HELO test.com\r\n" + "RSET all cows eat grass\r\n"
  110. writeCorpos("18", []byte(str))
  111. str = "HELO test.com\r\n" + "MAIL FROM: <[email protected]\r\n" +
  112. "MAIL FROM: <[email protected]\r\n"
  113. writeCorpos("19", []byte(str))
  114. str = "HELO test.com\r\n" + "MAIL FROM: <<[email protected]\r\n" +
  115. "MAIL FROM: <[email protected]\r\n"
  116. writeCorpos("20", []byte(str))
  117. str = "HELO test.com\r\n" + "DATA:\r\n"
  118. writeCorpos("21", []byte(str))
  119. str = "EHLO test.com\r\n" + "STARTTLS\r\n"
  120. writeCorpos("22", []byte(str))
  121. str = "HELO test.com\r\n" +
  122. "MAIL FROM: =?ISO-2022-JP?B?GyRCJEEkUCRSJG0bKEIgbWFnMiAwMDAxMDIzMTIw?= <[email protected]>\r\n" +
  123. "RCPT TO:<[email protected]>\r\n" +
  124. "DATA\r\n" +
  125. "Subject: =?ISO-2022-JP?B?GyRCIVokQSRQJFIkbSFbGyhCMy8xMhskQktcRnw9Kk47ISobKEI=?=\r\n" +
  126. " =?ISO-2022-JP?B?UFBDLUUbJEIhViVeJTklPyE8JTMlcyVGJXMlRCFXGyhC?=\r\n" +
  127. "\r\n" +
  128. "..Now you're just somebody that i used to know\r\n" +
  129. ".\r\n"
  130. writeCorpos("23", []byte(str))
  131. str = "HELO test.com\r\n" +
  132. "MAIL FROM:<> BODY=8BITMIME\r\n" +
  133. "RCPT TO:<[email protected]>\r\n" +
  134. "RCPT TO:<[email protected]>\r\n" +
  135. "DATA\r\n" +
  136. "No subject\r\n" +
  137. "..Now you're just somebody that i used to know\r\n" +
  138. ".\r\n" +
  139. "QUIT"
  140. writeCorpos("24", []byte(str))
  141. str = "HELO test.com\r\n" + "XCLIENT ADDR=127.0.0.1\r\n" + "MAIL FROM:<[email protected]>\r\n"
  142. writeCorpos("25", []byte(str))
  143. }
  144. // Tests the Fuzz function.
  145. func TestFuzz(t *testing.T) {
  146. result := Fuzz([]byte("EHLO me\r\nMail From:[email protected]\r\nRcpt to:test@test" +
  147. ".com\r\nDATA\r\ntest\r\n.\r\n"))
  148. if result != 1 {
  149. t.Error("Fuzz test did not return 1")
  150. }
  151. /*
  152. isFuzzDebug = true
  153. result = Fuzz([]byte("MAIL from: <\r"))
  154. if result != 1 {
  155. t.Error("Fuzz test did not return 1")
  156. }
  157. result = Fuzz([]byte("MAIL from: <\r\nHELP\r\n"))
  158. if result != 1 {
  159. t.Error("Fuzz test did not return 1")
  160. }
  161. */
  162. }
  163. func TestFuzz2(t *testing.T) {
  164. isFuzzDebug = true
  165. result := Fuzz([]byte("MAIL from: <\r\nHELP\r\n"))
  166. if result != 1 {
  167. t.Error("Fuzz test did not return 1")
  168. }
  169. }
  170. func TestFuzz3(t *testing.T) {
  171. isFuzzDebug = true
  172. result := Fuzz([]byte("DATA\r\n"))
  173. if result != 0 {
  174. t.Error("Fuzz test did not return 1")
  175. }
  176. }
  177. func TestFuzz4(t *testing.T) {
  178. isFuzzDebug = true
  179. result := Fuzz([]byte("MAIL from: <\r"))
  180. if result != 0 {
  181. t.Error("Fuzz test did not return 1")
  182. }
  183. }