module FreePalace.Messages.PalaceProtocol.ObfuscateSpec (spec) where

import qualified Data.ByteString.Lazy as LazyByteString
import Test.Hspec

import FreePalace.Messages.PalaceProtocol.Obfuscate

spec :: Spec
spec = do
  describe "obfuscationKeys" $ do
    it "contains the right bytes" $ do
      let expectedBytes = [
            0x37, 0xC5, 0x60, 0x72, 0xCD, 0xA5, 0x0B, 0x06, 0xD1, 0xF9, 0xCB, 0x3C, 0x57, 0x0C,
            0x0D, 0x1B, 0x79, 0x1E, 0x2D, 0x72, 0xB4, 0x04, 0xB3, 0x60, 0xB2, 0x80, 0xDD, 0x89,
            0xA0, 0xED, 0xF2, 0x84, 0x7C, 0xFA, 0xC4, 0x99, 0x9F, 0x27, 0xED, 0xDE, 0x6C, 0xDE,
            0x1E, 0xB8, 0x48, 0x34, 0x64, 0x29, 0x76, 0xA5, 0x01, 0x86, 0x21, 0x0B, 0xBF, 0xB0,
            0x99, 0xED, 0x20, 0x2D, 0x75, 0x42, 0x4C, 0x6A, 0xC0, 0x75, 0x7E, 0x89, 0x33, 0x00,
            0x65, 0x82, 0x03, 0x04, 0x93, 0x3D, 0x57, 0x4A, 0xAA, 0x2F, 0xA5, 0xD5, 0x74, 0xBD,
            0xD7, 0x31, 0xDA, 0x83, 0x96, 0xE2, 0x18, 0x59, 0xEE, 0x20, 0x53, 0x24, 0x3D, 0x88,
            0xAB, 0x77, 0x4A, 0x99, 0xB9, 0x04, 0xE1, 0xD3, 0x9D, 0xD0, 0x89, 0xBF, 0x89, 0x44,
            0x32, 0x64, 0xC7, 0x2E, 0x93, 0x33, 0xD5, 0x6B, 0x29, 0x95, 0xBB, 0x3B, 0x82, 0x96,
            0x3D, 0x60, 0x26, 0xD0, 0x84, 0x5B, 0x45, 0x5A, 0x8B, 0x4C, 0x5A, 0x3C, 0x5B, 0x8A,
            0x58, 0x94, 0xB8, 0x9B, 0x13, 0xDB, 0x6B, 0xBE, 0x1A, 0x0A, 0x60, 0x73, 0x74, 0x55,
            0xE7, 0x59, 0xB0, 0xBA, 0x44, 0x76, 0xAC, 0xE2, 0x85, 0x16, 0xC4, 0x10, 0x53, 0x97,
            0x38, 0xC0, 0xEE, 0xA8, 0x06, 0x42, 0xA9, 0x1B, 0xBD, 0xD2, 0x97, 0xE2, 0xDD, 0x25,
            0xDA, 0xA5, 0x44, 0xC7, 0x20, 0x51, 0x3A, 0xFB, 0x92, 0x94, 0x17, 0x71, 0x2F, 0x02,
            0x55, 0x60, 0x96, 0xB5, 0x26, 0x4C, 0x57, 0xF7, 0x8B, 0x46, 0x78, 0x19, 0xE2, 0xB6,
            0x60, 0x8F, 0xD0, 0xF7, 0x94, 0x69, 0x04, 0x2D, 0xC5, 0x41, 0xD2, 0x40, 0x83, 0x89,
            0x67, 0xC7, 0xEE, 0xF5, 0x5D, 0xA7, 0x89, 0x31, 0xD7, 0x92, 0x52, 0x57, 0xD8, 0xA7,
            0x10, 0xF6, 0xD3, 0x14, 0x8A, 0x12, 0x76, 0x7F, 0x1A, 0xE2, 0x17, 0xD5, 0xB1, 0xFF,
            0x7E, 0x82, 0xD8, 0x32, 0xB3, 0x89, 0x1D, 0xB2, 0x44, 0x6A, 0x78, 0xAF, 0x0F, 0x23,
            0xF9, 0x64, 0x7A, 0x3B, 0x7A, 0x13, 0x21, 0x50, 0x86, 0x80, 0x07, 0x47, 0x98, 0xBC,
            0x84, 0x24, 0x6E, 0xE0, 0xAB, 0x84, 0xDE, 0xDC, 0x01, 0x20, 0xED, 0x03, 0x02, 0x99,
            0xA3, 0x2F, 0x9E, 0x89, 0x67, 0x03, 0x69, 0xC5, 0xA5, 0x2E, 0x12, 0xB1, 0x54, 0x5F,
            0xD3, 0x36, 0xF9, 0x80, 0xDC, 0x37, 0x5F, 0x23, 0x69, 0xB2, 0xCB, 0x39, 0x99, 0x7B,
            0xE9, 0x59, 0x63, 0x28, 0x09, 0x05, 0xB6, 0x92, 0xAA, 0x45, 0x79, 0x54, 0xD4, 0xE3,
            0x2D, 0x6B, 0x6C, 0x5A, 0xA7, 0x29, 0xE7, 0x6C, 0x66, 0x95, 0x40, 0xC9, 0x2D, 0xCE,
            0x33, 0x39, 0x55, 0x66, 0xCA, 0x7E, 0xD1, 0x7A, 0xC0, 0x20, 0x40, 0x09, 0x4E, 0x29,
            0xB3, 0xDE, 0x09, 0xCD, 0x99, 0xAE, 0xFB, 0x18, 0xAD, 0x73, 0xD5, 0x7E, 0xBA, 0x02,
            0x5B, 0x8F, 0x5C, 0xA3, 0xF9, 0xA9, 0x2B, 0xF2, 0x63, 0xDE, 0x98, 0x2F, 0x26, 0x99,
            0x28, 0xCA, 0xBE, 0xEF, 0x77, 0x72, 0xAE, 0xC8, 0x5D, 0x29, 0x72, 0x6C, 0x2A, 0x18,
            0x3B, 0x8B, 0xA1, 0x15, 0xAC, 0x1E, 0x5D, 0x09, 0x48, 0x7D, 0xDD, 0x2C, 0xAB, 0x8A,
            0x07, 0xE4, 0x4A, 0xA4, 0x6B, 0x96, 0x11, 0x88, 0xCD, 0x9C, 0x30, 0x15, 0xCF, 0x55,
            0x12, 0xF1, 0x82, 0x4C, 0x04, 0x24, 0xEC, 0xA8, 0xAE, 0xD2, 0x1C, 0x2E, 0x1C, 0x12,
            0x42, 0x9A, 0xE8, 0x20, 0x64, 0x5C, 0xBF, 0xDE, 0xE8, 0xF4, 0xE5, 0x4C, 0x77, 0xE4,
            0x51, 0xB0, 0x26, 0xDA, 0xA7, 0x86, 0xF3, 0x10, 0xB4, 0x86, 0xE8, 0x0E, 0x79, 0x91,
            0x4E, 0xDE, 0x75, 0x7B, 0xD5, 0xD1, 0xD7, 0xAA, 0x16, 0xBD, 0x5B, 0x03, 0xA5, 0x31,
            0x38, 0x93, 0x33, 0x66, 0xF2, 0x0E, 0x6F, 0x10, 0xA8, 0xD8, 0x35, 0x95, 0x50, 0x2D,
            0xA1, 0x3F, 0xFD, 0x06, 0x09, 0x90, 0x86, 0xCC
            ]
      obfuscationKeys `shouldBe` expectedBytes

  describe "illuminate" $ do
    it "produces the right plain text for a string of length 1" $ do
      let encodedText = [ 0x6F ]
          plaintext = "X"
      illuminate encodedText `shouldBe` plaintext

    it "produces the right plain text for a string of length 2" $ do
      let encodedText = [ 0x90, 0x04 ]
          plaintext = "13"
      illuminate encodedText `shouldBe` plaintext
      
    it "produces the right plain text for a longer string" $ do
      let encodedText = [
            0x59, 0x18, 0x9B, 0x94, 0x15, 0xEE, 0x7B, 0x78, 0x8C, 0x66, 0x23, 0xFB, 0x82, 0x47,
            0x3E, 0x08, 0xE5, 0xAA, 0x71, 0xAE, 0x2A, 0x1F, 0x3A, 0xA7, 0x17, 0xC0, 0x6E, 0x48,
            0x19, 0x97, 0xED, 0xB6, 0xCB, 0x6D, 0xB2, 0x11, 0x02, 0x0C, 0x6C, 0x7D, 0x36, 0xC1,
            0x0B, 0xDB, 0x19
            ]
          plaintext = "The quick brown fox jumped over the lazy dog."
      illuminate encodedText `shouldBe` plaintext

  describe "illuminateRecursive" $ do
    it "produces the right bytes for a string of length 2" $ do
      let encodedText = [ 0x90, 0x04 ]
          reversed = reverse encodedText
          initialPartiallyObfuscatedByte = 0
          illuminated = illuminateRecursive obfuscationKeys initialPartiallyObfuscatedByte [] reversed
      illuminated `shouldBe` [ 0x31, 0x33 ]
      

  describe "obfuscate" $ do
    it "produces the right obfuscated bytes for a string of length 1" $ do
      let plaintext = "X"
          encodedText =  LazyByteString.pack [0x6F]
      obfuscate plaintext `shouldBe` encodedText

    it "produces the right obfuscated bytes for a string of length 2" $ do
      let plaintext = "13"
          encodedText =  LazyByteString.pack [ 0x90, 0x04 ]
      obfuscate plaintext `shouldBe` encodedText

    it "produces the right obfuscated bytes for a longer string" $ do
      let plaintext = "The quick brown fox jumped over the lazy dog."
          encodedText = LazyByteString.pack [
            0x59, 0x18, 0x9B, 0x94, 0x15, 0xEE, 0x7B, 0x78, 0x8C, 0x66, 0x23, 0xFB, 0x82, 0x47,
            0x3E, 0x08, 0xE5, 0xAA, 0x71, 0xAE, 0x2A, 0x1F, 0x3A, 0xA7, 0x17, 0xC0, 0x6E, 0x48,
            0x19, 0x97, 0xED, 0xB6, 0xCB, 0x6D, 0xB2, 0x11, 0x02, 0x0C, 0x6C, 0x7D, 0x36, 0xC1,
            0x0B, 0xDB, 0x19
            ]
      obfuscate plaintext `shouldBe` encodedText
