fuzz_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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:=?ISO-2022-JP?B?GyRCJEEkUCRSJG0bKEIgbWFnMiAwMDAxMDIzMTIw?= <[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:=?utf-8?B?2KfZhNit2YjYp9isINmE2YTYstmK2YjYqiDYp9mE2LfYqNmK2LnZitip?=<[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 = "MAIL FROM:<>\r\n"
  84. writeCorpos("5", []byte(str))
  85. str = "MAIL from: <\r\n"
  86. writeCorpos("6", []byte(str))
  87. str = "MAIL FrOm: <<>>\r\n"
  88. writeCorpos("8", []byte(str))
  89. str = "MAIL FrOm:\r\n"
  90. writeCorpos("7", []byte(str))
  91. str = "RCPT TO:\r\n"
  92. writeCorpos("9", []byte(str))
  93. str = "RCPT TO:<>\r\n"
  94. writeCorpos("10", []byte(str))
  95. str = "RCPT TO:<\r\n"
  96. writeCorpos("11", []byte(str))
  97. str = "RCPT TO:<[email protected]> somethingstrange\r\n"
  98. writeCorpos("12", []byte(str))
  99. str = "VRFY\r\n"
  100. writeCorpos("13", []byte(str))
  101. str = "VRFY:\r\n"
  102. writeCorpos("14", []byte(str))
  103. str = "VRFY all cows eat grass\r\n"
  104. writeCorpos("15", []byte(str))
  105. str = "RSET\r\n"
  106. writeCorpos("16", []byte(str))
  107. str = "RSET:\r\n"
  108. writeCorpos("17", []byte(str))
  109. str = "RSET all cows eat grass\r\n"
  110. writeCorpos("18", []byte(str))
  111. str = "MAIL FROM: <[email protected]\r\n" +
  112. "MAIL FROM: <[email protected]\r\n"
  113. writeCorpos("19", []byte(str))
  114. str = "MAIL FROM: <<[email protected]\r\n" +
  115. "MAIL FROM: <[email protected]\r\n"
  116. writeCorpos("20", []byte(str))
  117. str = "DATA:\r\n"
  118. writeCorpos("21", []byte(str))
  119. str = "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. }
  132. // Tests the Fuzz function.
  133. func TestFuzz(t *testing.T) {
  134. result := Fuzz([]byte("EHLO me\r\nMail From:[email protected]\r\nRcpt to:test@test" +
  135. ".com\r\nDATA\r\ntest\r\n.\r\n"))
  136. if result != 1 {
  137. t.Error("Fuzz test did not return 1")
  138. }
  139. /*
  140. isFuzzDebug = true
  141. result = Fuzz([]byte("MAIL from: <\r"))
  142. if result != 1 {
  143. t.Error("Fuzz test did not return 1")
  144. }
  145. result = Fuzz([]byte("MAIL from: <\r\nHELP\r\n"))
  146. if result != 1 {
  147. t.Error("Fuzz test did not return 1")
  148. }
  149. */
  150. }
  151. func TestFuzz2(t *testing.T) {
  152. isFuzzDebug = true
  153. result := Fuzz([]byte("MAIL from: <\r\nHELP\r\n"))
  154. if result != 1 {
  155. t.Error("Fuzz test did not return 1")
  156. }
  157. }
  158. func TestFuzz3(t *testing.T) {
  159. isFuzzDebug = true
  160. result := Fuzz([]byte("DATA\r\n"))
  161. if result != 0 {
  162. t.Error("Fuzz test did not return 1")
  163. }
  164. }
  165. func TestFuzz4(t *testing.T) {
  166. isFuzzDebug = true
  167. result := Fuzz([]byte("MAIL from: <\r"))
  168. if result != 0 {
  169. t.Error("Fuzz test did not return 1")
  170. }
  171. }