1717#
1818
1919defmodule Astarte.Pairing.FDO.ServiceInfoTest do
20- use ExUnit.Case
20+ use Astarte.Cases.Data , async: true
21+ use Astarte.Cases.Device
2122 doctest Astarte.Pairing.FDO.ServiceInfo
2223
2324 alias Astarte.Pairing.FDO.ServiceInfo
2425 alias Astarte.Pairing.FDO.OwnerOnboarding.DeviceServiceInfoReady
26+ alias Astarte.Pairing.FDO.OwnerOnboarding.HelloDevice
27+ alias Astarte.Pairing.FDO.OwnerOnboarding.SessionKey
2528 alias Astarte.Pairing.FDO.OwnershipVoucher
2629 alias COSE.Messages.Encrypt0
30+ alias Astarte.Pairing.FDO.OwnerOnboarding.Session
2731 alias COSE.Keys
32+ import Astarte.Helpers.FDO
2833
2934 @ owner_max_service_info 4096
3035 @ aes_256_gcm :aes_256_gcm
3136
37+ setup_all do
38+ hello_device = HelloDevice . generate ( )
39+ ownership_voucher = sample_ownership_voucher ( )
40+ owner_key = sample_extracted_private_key ( )
41+ device_key = COSE.Keys.ECC . generate ( :es256 )
42+ { :ok , device_random , xb } = SessionKey . new ( hello_device . kex_name , device_key )
43+
44+ % {
45+ hello_device: hello_device ,
46+ ownership_voucher: ownership_voucher ,
47+ owner_key: owner_key ,
48+ device_key: device_key ,
49+ device_random: device_random ,
50+ xb: xb
51+ }
52+ end
53+
3254 setup do
3355 header_list = [
3456 # prot_ver
@@ -60,13 +82,22 @@ defmodule Astarte.Pairing.FDO.ServiceInfoTest do
6082
6183 describe "handle_msg_66/4" do
6284 test "successfully processes Msg 66, creates new voucher, and returns Msg 67" , % {
85+ realm: realm_name ,
86+ hello_device: hello_device ,
87+ owner_key: owner_key ,
88+ ownership_voucher: ownership_voucher ,
6389 old_voucher: old_voucher
6490 } do
91+ { :ok , session } =
92+ Session . new ( realm_name , hello_device , ownership_voucher , owner_key )
93+
6594 new_hmac = :crypto . strong_rand_bytes ( 32 )
6695 device_max_size = 2048
6796
6897 assert { :ok , result_msg_67 } =
6998 ServiceInfo . handle_msg_66 (
99+ realm_name ,
100+ session . key ,
70101 % DeviceServiceInfoReady {
71102 replacement_hmac: new_hmac ,
72103 max_owner_service_info_sz: device_max_size
@@ -78,10 +109,19 @@ defmodule Astarte.Pairing.FDO.ServiceInfoTest do
78109 end
79110
80111 test "handles Credential Reuse (nil HMAC) correctly" , % {
112+ realm: realm_name ,
113+ hello_device: hello_device ,
114+ owner_key: owner_key ,
115+ ownership_voucher: ownership_voucher ,
81116 old_voucher: old_voucher
82117 } do
118+ { :ok , session } =
119+ Session . new ( realm_name , hello_device , ownership_voucher , owner_key )
120+
83121 assert { :ok , _result } =
84122 ServiceInfo . handle_msg_66 (
123+ realm_name ,
124+ session . key ,
85125 % DeviceServiceInfoReady {
86126 replacement_hmac: nil ,
87127 max_owner_service_info_sz: 2048
@@ -91,20 +131,37 @@ defmodule Astarte.Pairing.FDO.ServiceInfoTest do
91131 end
92132
93133 test "returns error if inner CBOR payload is malformed" , % {
134+ realm: realm_name ,
135+ hello_device: hello_device ,
136+ owner_key: owner_key ,
137+ ownership_voucher: ownership_voucher ,
94138 old_voucher: old_voucher
95139 } do
140+ { :ok , session } =
141+ Session . new ( realm_name , hello_device , ownership_voucher , owner_key )
142+
96143 malformed_payload = "not_a_valid_payload"
97144
98- result = ServiceInfo . handle_msg_66 ( malformed_payload , old_voucher )
145+ result = ServiceInfo . handle_msg_66 ( realm_name , session . key , malformed_payload , old_voucher )
99146
100147 assert { :error , :invalid_payload } = result
101148 end
102149
103- test "returns error if old voucher is invalid" do
150+ test "returns error if old voucher is invalid" , % {
151+ realm: realm_name ,
152+ hello_device: hello_device ,
153+ owner_key: owner_key ,
154+ ownership_voucher: ownership_voucher
155+ } do
104156 invalid_voucher = CBOR . encode ( "not a voucher" )
105157
158+ { :ok , session } =
159+ Session . new ( realm_name , hello_device , ownership_voucher , owner_key )
160+
106161 result =
107162 ServiceInfo . handle_msg_66 (
163+ realm_name ,
164+ session . key ,
108165 % DeviceServiceInfoReady {
109166 replacement_hmac: :crypto . strong_rand_bytes ( 32 ) ,
110167 max_owner_service_info_sz: 1024
0 commit comments