Btree errors serialize (#62)

* keyStore spec

* Client and Server possibility to receive errors from the network from each other

* Client and Server possibility to send errors through the network to each other

* Docs fixes

* client sends 'clients error' to server and returns it as result type

* debug fail test

* debug fail test

* debug fail test

* disable log in test

* review
This commit is contained in:
Constantine Solovev 2018-02-22 21:32:18 +04:00 committed by GitHub
parent d88a5299e7
commit 554e1db8b1
2 changed files with 92 additions and 9 deletions

View File

@ -36,25 +36,32 @@ case class KeyStore(keyPair: KeyPair)
* }
*/
object KeyStore {
private val alphabet = Bases.Alphabets.Base64Url
implicit val encodeKeyStorage: Encoder[KeyStore] = new Encoder[KeyStore] {
final def apply(ks: KeyStore): Json = Json.obj(("keystore", Json.obj(
("secret", Json.fromString(ks.keyPair.secretKey.value.toBase64(alphabet))),
("public", Json.fromString(ks.keyPair.publicKey.value.toBase64(alphabet))))))
private object Field {
val Keystore = "keystore"
val Secret = "secret"
val Public = "public"
}
implicit val decodeKeyStorage: Decoder[Option[KeyStore]] = new Decoder[Option[KeyStore]] {
final def apply(c: HCursor): Decoder.Result[Option[KeyStore]] =
implicit val encodeKeyStorage: Encoder[KeyStore] = (ks: KeyStore) Json.obj(
(Field.Keystore, Json.obj(
(Field.Secret, Json.fromString(ks.keyPair.secretKey.value.toBase64(alphabet))),
(Field.Public, Json.fromString(ks.keyPair.publicKey.value.toBase64(alphabet))))
))
implicit val decodeKeyStorage: Decoder[Option[KeyStore]] =
(c: HCursor)
for {
secret c.downField("keystore").downField("secret").as[String]
public c.downField("keystore").downField("public").as[String]
secret c.downField(Field.Keystore).downField(Field.Secret).as[String]
public c.downField(Field.Keystore).downField(Field.Public).as[String]
} yield {
for {
secret ByteVector.fromBase64(secret, alphabet)
public ByteVector.fromBase64(public, alphabet)
} yield KeyStore(KeyPair.fromByteVectors(public, secret))
}
}
def fromBase64(base64: String): KeyStore = {
val jsonStr = ByteVector.fromBase64(base64, alphabet) match {

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2017 Fluence Labs Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fluence.crypto
import fluence.crypto.keypair.KeyPair
import org.scalatest.{ Matchers, WordSpec }
import scodec.bits.{ Bases, ByteVector }
class KeyStoreSpec extends WordSpec with Matchers {
private val alphabet = Bases.Alphabets.Base64Url
private val keyStore = KeyStore(KeyPair.fromBytes("pubKey".getBytes, "secKey".getBytes))
private val jsonString = """{"keystore":{"secret":"c2VjS2V5","public":"cHViS2V5"}}"""
"KeyStore.encodeKeyStorage" should {
"transform KeyStore to json" in {
val result = KeyStore.encodeKeyStorage(keyStore)
result.noSpaces shouldBe jsonString
}
}
"KeyStore.decodeKeyStorage" should {
"transform KeyStore to json" in {
import io.circe.parser._
val result = KeyStore.decodeKeyStorage.decodeJson(parse(jsonString).right.get)
result.right.get.get shouldBe keyStore
}
}
"KeyStore" should {
"transform KeyStore to json and back" in {
val result = KeyStore.decodeKeyStorage.decodeJson(KeyStore.encodeKeyStorage(keyStore))
result.right.get.get shouldBe keyStore
}
}
"fromBase64" should {
"throw an exception" when {
"invalid base64 format" in {
val invalidBase64 = "!@#$%"
the[IllegalArgumentException] thrownBy {
KeyStore.fromBase64(invalidBase64)
} should have message "'" + invalidBase64 + "' is not a valid base64."
}
"invalid decoded json" in {
val invalidJson = ByteVector("""{"keystore":{"public":"cHViS2V5"}}""".getBytes).toBase64(alphabet)
the[IllegalArgumentException] thrownBy {
KeyStore.fromBase64(invalidJson)
}
}
}
"fetch KeyStore from valid base64" in {
val invalidJson = ByteVector(jsonString.getBytes).toBase64(alphabet)
val result = KeyStore.fromBase64(invalidJson)
result shouldBe keyStore
}
}
}