Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization for case number messages #2103

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}