Commits

Hector Miuler Malpica Gallegos committed dad6c46

Cambios:
* El servidor MockSMTP ya responde a la orden basica helo/ehlo.
* Se agrego un logo, que puede usarse para la aplicación.
* Se agrego un BasicTests para algunas pruebas.

Comments (0)

Files changed (3)

src/main/resources/smtp_server.png

Added
New image

src/main/scala/com/miuler/mocksmtp/core/MockSMTP.scala

 import io.netty.channel.socket.nio.{NioServerSocketChannel, NioEventLoopGroup}
 import io.netty.channel._
 import io.netty.channel.socket.SocketChannel
-import io.netty.buffer.ByteBuf
-import io.netty.buffer.Unpooled.copiedBuffer
-import io.netty.util.CharsetUtil.UTF_8
+import io.netty.handler.codec.string.{StringEncoder, StringDecoder}
+import io.netty.handler.codec.{DelimiterBasedFrameDecoder, Delimiters}
 import java.net.InetAddress
-import java.util.Date
 
 
 class MockSMTP {
 
   def start = {
     log.debug("start server")
+
     server = new ServerBootstrap()
       .group(new NioEventLoopGroup())
       .channel(classOf[NioServerSocketChannel])
         new ChannelInitializer[SocketChannel]() {
           def initChannel(ch: SocketChannel) {
             ch.pipeline()
+              .addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()(0)))
+              .addLast("decoder", new StringDecoder())
+              .addLast("encoder", new StringEncoder())
               .addLast("handlerIn", new ChannelInboundHandlerBasic)
           }
         })
 }
 
 
-class ChannelInboundHandlerBasic extends ChannelInboundByteHandlerAdapter {
+class ChannelInboundHandlerBasic extends ChannelInboundMessageHandlerAdapter[String] {
 
   private val log = LoggerFactory.getLogger(classOf[MockSMTP])
 
   override def channelActive(ctx: ChannelHandlerContext) {
     log.debug("channelActive")
-    var msg = "220 ***************************************"
-    ctx.write(copiedBuffer(msg.toCharArray, UTF_8))
+    ctx.write("220 ***************************************\n")
     //ctx.fireChannelActive(); //TODO: I do not know if this is necessary
   }
 
-  override def inboundBufferUpdated(ctx: ChannelHandlerContext, in: ByteBuf) {
-    log.debug("mensage: \n{}", in.toString(UTF_8))
-    ctx.write(in)
-    in.clear()
+  override def messageReceived(ctx: ChannelHandlerContext, msg: String) {
+    log.debug("mensage: \n{}", msg)
 
+    val hello = "(HELLO)".r
+
+    msg match {
+      case msg:String if msg.toUpperCase matches "QUIT" =>
+        ctx.write("221 Bye\n").addListener(ChannelFutureListener.CLOSE)
+
+      case msg:String if msg.toUpperCase.trim matches "HELO|EHLO" =>
+        ctx.write("501 Syntax: " + msg.toUpperCase + " hostname\n")
+
+      case msg:String if msg.toUpperCase matches "(HELO|EHLO) .+" =>
+        ctx.write("250 " + InetAddress.getLocalHost.getHostName  + "\n")
+
+      case _ =>
+        ctx.write(msg + "\n")
+    }
+  }
+
+  override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
+    log.error("error", cause)
+    ctx.close()
   }
 }
 

src/test/scala/com/miuler/mocksmtp/BasicTests.scala

+package com.miuler.mocksmtp
+
+import org.junit.runner.RunWith
+import org.specs2.runner.JUnitRunner
+import org.specs2.Specification
+
+
+@RunWith(classOf[JUnitRunner])
+class BasicTests extends Specification {
+
+  def doBefore() { println("Prueba") }
+
+  def is =
+
+    " Pruebas Basicas "                           ^
+                                                  p^
+    " Prueba de expresiones regulaes con match "  ! pruebaRegExpMatch^
+                                                  end
+
+  def pruebaRegExpMatch = {
+    val texto = "Texto de Prueba"
+
+    texto match {
+      case text:String if text.toUpperCase matches "TEXTO.*"  => success
+      case _ => failure
+    }
+
+  }
+
+}