Skip to content

Commit

Permalink
Merge pull request #2103 from vdebergue/fix/case-number-serialization
Browse files Browse the repository at this point in the history
Fix serialization for case number messages
  • Loading branch information
To-om authored Jun 30, 2021
2 parents 555c65a + 0009f47 commit d5d7555
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
14 changes: 4 additions & 10 deletions thehive/app/org/thp/thehive/services/CaseNumber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.thp.thehive.GuiceAkkaExtension
import org.thp.thehive.services.CaseOps._

import java.io.NotSerializableException
import java.nio.ByteBuffer
import javax.inject.{Inject, Provider, Singleton}

object CaseNumberActor {
Expand Down Expand Up @@ -55,22 +56,15 @@ class CaseNumberSerializer(system: ExtendedActorSystem) extends Serializer {
override def toBinary(o: AnyRef): Array[Byte] =
o match {
case GetNextNumber(replyTo) => 0.toByte +: actorRefResolver.toSerializationFormat(replyTo).getBytes
case NextNumber(number) =>
Array(1.toByte, ((number >> 24) % 0xff).toByte, ((number >> 16) % 0xff).toByte, ((number >> 8) % 0xff).toByte, (number % 0xff).toByte)
case _ => throw new NotSerializableException
case NextNumber(number) => ByteBuffer.allocate(5).put(1.toByte).putInt(number).array()
case _ => throw new NotSerializableException
}

override def includeManifest: Boolean = false

override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
bytes(0) match {
case 0 => GetNextNumber(actorRefResolver.resolveActorRef(new String(bytes.tail)))
case 1 =>
NextNumber(
(bytes(2) << 24) +
(bytes(3) << 16) +
(bytes(4) << 8) +
bytes(5)
)
case 1 => NextNumber(ByteBuffer.wrap(bytes).getInt(1))
}
}
34 changes: 34 additions & 0 deletions thehive/test/org/thp/thehive/services/CaseNumberTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.thp.thehive.services

import akka.actor.ExtendedActorSystem
import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import org.thp.thehive.TestAppBuilder
import play.api.test.PlaySpecification

class CaseNumberTest extends PlaySpecification with TestAppBuilder {

"case number actor" should {
"serialize and deserialize messages" in withActorSystem { system =>
val ref = system.deadLetters[CaseNumberActor.Response]
val sut = new CaseNumberSerializer(system.classicSystem.asInstanceOf[ExtendedActorSystem])

val messages = Seq(
CaseNumberActor.GetNextNumber(ref),
CaseNumberActor.NextNumber(42),
CaseNumberActor.NextNumber(Int.MaxValue)
)
val out = messages.map(message => sut.toBinary(message))

val result = out.map(bin => sut.fromBinary(bin))

result must beEqualTo(messages)
}
}

private def withActorSystem[T](body: ActorSystem[Nothing] => T) = {
val system = ActorSystem(Behaviors.empty, "test")
try body(system)
finally system.terminate()
}
}

0 comments on commit d5d7555

Please sign in to comment.