|
| 1 | +defmodule Astarte.Pairing.TO0UtilTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Astarte.Pairing.TO0Util |
| 5 | + |
| 6 | + describe "get_nonce_from_hello_ack/1" do |
| 7 | + test "returns nonce for actual FDO HelloAck CBOR payload (binary nonce)" do |
| 8 | + valid_nonce = <<32, 54, 127, 243, 66, 48, 228, 115, 59, 186, 230, 246, 198, 179, 113, 78>> |
| 9 | + hello_ack_cbor = CBOR.encode([valid_nonce]) |
| 10 | + assert {:ok, ^valid_nonce} = TO0Util.get_nonce_from_hello_ack(hello_ack_cbor) |
| 11 | + end |
| 12 | + |
| 13 | + test "fails with non-CBOR binary" do |
| 14 | + invalid_binary_nonce = <<1, 2, 3, 4, 5>> |
| 15 | + hello_ack_cbor = CBOR.encode([invalid_binary_nonce]) |
| 16 | + assert {:error, {:unexpected_binary, _}} = TO0Util.get_nonce_from_hello_ack(hello_ack_cbor) |
| 17 | + end |
| 18 | + |
| 19 | + test "fails with CBOR not formatted as FDO expects" do |
| 20 | + wrong_struct1 = %{"nonce" => "map instead of list"} |
| 21 | + wrong_cbor1 = CBOR.encode(wrong_struct1) |
| 22 | + |
| 23 | + assert {:error, {:unexpected_cbor_format, _}} = |
| 24 | + TO0Util.get_nonce_from_hello_ack(wrong_cbor1) |
| 25 | + |
| 26 | + wrong_cbor2 = |
| 27 | + CBOR.encode([ |
| 28 | + <<32, 54, 127, 243, 66, 48, 228, 115, 59, 186, 230, 246, 198, 179, 113, 78>>, |
| 29 | + <<32, 54, 127, 243, 66, 48, 228, 115, 59, 186, 230, 246, 198, 179, 113, 78>> |
| 30 | + ]) |
| 31 | + |
| 32 | + assert {:error, {:unexpected_cbor_format, _}} = |
| 33 | + TO0Util.get_nonce_from_hello_ack(wrong_cbor2) |
| 34 | + end |
| 35 | + |
| 36 | + test "fails with CBOR single binary but wrong length" do |
| 37 | + invalid_nonce = <<1, 2, 3, 4, 5, 6, 7, 8>> |
| 38 | + hello_ack_cbor = CBOR.encode([invalid_nonce]) |
| 39 | + |
| 40 | + assert {:error, {:unexpected_binary, ^invalid_nonce}} = |
| 41 | + TO0Util.get_nonce_from_hello_ack(hello_ack_cbor) |
| 42 | + end |
| 43 | + end |
| 44 | +end |
0 commit comments