Skip to content

Commit 5c7e5b3

Browse files
author
hydepwns
committed
fix: Update Elixir wrapper and tests for libsignal-protocol-nif
- Update Elixir wrapper modules and tests for compatibility - Improve test coverage and reliability - Sync with main repo changes
1 parent f18e1e8 commit 5c7e5b3

11 files changed

Lines changed: 279 additions & 138 deletions

test/elixir/libsignal_protocol_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ defmodule LibsignalProtocolTest do
3939
assert {:error, _} = LibsignalProtocol.decrypt_message(123, "test")
4040
end
4141
end
42-
end
42+
end

test/elixir/session_test.exs

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule LibsignalProtocol.SessionTest do
1717

1818
# Create session
1919
{:ok, session} = Session.create(local_identity_key, remote_identity_key)
20-
20+
2121
assert is_map(session)
2222
assert Map.has_key?(session, :id)
2323
assert Map.has_key?(session, :local_identity_key)
@@ -45,10 +45,12 @@ defmodule LibsignalProtocol.SessionTest do
4545

4646
test "handles invalid identity keys gracefully" do
4747
invalid_key = "invalid_key"
48-
48+
4949
case Session.create(invalid_key, invalid_key) do
50-
{:error, _} -> :ok
51-
{:ok, session} ->
50+
{:error, _} ->
51+
:ok
52+
53+
{:ok, session} ->
5254
assert is_map(session)
5355
assert Map.has_key?(session, :id)
5456
end
@@ -66,7 +68,8 @@ defmodule LibsignalProtocol.SessionTest do
6668

6769
# Generate pre-key bundle components
6870
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
69-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
71+
72+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
7073
:nif.generate_signed_pre_key(remote_identity_key, 67890)
7174

7275
# Create pre-key bundle
@@ -86,6 +89,7 @@ defmodule LibsignalProtocol.SessionTest do
8689
assert updated_session.signed_pre_key_id == signed_pre_key_id
8790
assert is_binary(updated_session.ephemeral_key)
8891
assert is_binary(updated_session.chain_key)
92+
8993
{:error, reason} ->
9094
# Some implementations might fail due to signature verification
9195
assert is_atom(reason) or is_binary(reason)
@@ -122,7 +126,8 @@ defmodule LibsignalProtocol.SessionTest do
122126

123127
# Generate pre-key bundle components
124128
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
125-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
129+
130+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
126131
:nif.generate_signed_pre_key(remote_identity_key, 67890)
127132

128133
# Create pre-key bundle
@@ -139,15 +144,20 @@ defmodule LibsignalProtocol.SessionTest do
139144
{:ok, established_session} ->
140145
# Test message encryption
141146
message = "Hello, Signal Protocol!"
147+
142148
case Session.encrypt_message(established_session, message) do
143149
{:ok, encrypted_message, updated_session} ->
144150
assert is_binary(encrypted_message)
145151
assert byte_size(encrypted_message) > byte_size(message)
146152
assert is_map(updated_session)
147153
assert updated_session.message_counter > established_session.message_counter
148-
{:error, _} -> :ok
154+
155+
{:error, _} ->
156+
:ok
149157
end
150-
{:error, _} -> :ok
158+
159+
{:error, _} ->
160+
:ok
151161
end
152162
end
153163

@@ -158,7 +168,8 @@ defmodule LibsignalProtocol.SessionTest do
158168

159169
# Generate pre-key bundle components
160170
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
161-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
171+
172+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
162173
:nif.generate_signed_pre_key(remote_identity_key, 67890)
163174

164175
bundle = %{
@@ -173,14 +184,18 @@ defmodule LibsignalProtocol.SessionTest do
173184
{:ok, established_session} ->
174185
message1 = "First message"
175186
message2 = "Second message"
176-
187+
177188
case {Session.encrypt_message(established_session, message1),
178189
Session.encrypt_message(established_session, message2)} do
179190
{{:ok, encrypted1, _}, {:ok, encrypted2, _}} ->
180191
assert encrypted1 != encrypted2
181-
_ -> :ok
192+
193+
_ ->
194+
:ok
182195
end
183-
{:error, _} -> :ok
196+
197+
{:error, _} ->
198+
:ok
184199
end
185200
end
186201

@@ -191,7 +206,8 @@ defmodule LibsignalProtocol.SessionTest do
191206

192207
# Generate pre-key bundle components
193208
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
194-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
209+
210+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
195211
:nif.generate_signed_pre_key(remote_identity_key, 67890)
196212

197213
bundle = %{
@@ -208,17 +224,24 @@ defmodule LibsignalProtocol.SessionTest do
208224
case Session.encrypt_message(established_session, "") do
209225
{:ok, encrypted_empty, _} ->
210226
assert is_binary(encrypted_empty)
211-
{:error, _} -> :ok
227+
228+
{:error, _} ->
229+
:ok
212230
end
213231

214232
# Test large message
215233
large_message = String.duplicate("A", 1000)
234+
216235
case Session.encrypt_message(established_session, large_message) do
217236
{:ok, encrypted_large, _} ->
218237
assert is_binary(encrypted_large)
219-
{:error, _} -> :ok
238+
239+
{:error, _} ->
240+
:ok
220241
end
221-
{:error, _} -> :ok
242+
243+
{:error, _} ->
244+
:ok
222245
end
223246
end
224247
end
@@ -231,7 +254,8 @@ defmodule LibsignalProtocol.SessionTest do
231254

232255
# Generate pre-key bundle components
233256
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
234-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
257+
258+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
235259
:nif.generate_signed_pre_key(remote_identity_key, 67890)
236260

237261
bundle = %{
@@ -245,17 +269,23 @@ defmodule LibsignalProtocol.SessionTest do
245269
case Session.process_pre_key_bundle(session, bundle) do
246270
{:ok, established_session} ->
247271
original_message = "Test message for decryption"
248-
272+
249273
case Session.encrypt_message(established_session, original_message) do
250274
{:ok, encrypted_message, updated_session} ->
251275
case Session.decrypt_message(updated_session, encrypted_message) do
252276
{:ok, decrypted_message, _} ->
253277
assert decrypted_message == original_message
254-
{:error, _} -> :ok
278+
279+
{:error, _} ->
280+
:ok
255281
end
256-
{:error, _} -> :ok
282+
283+
{:error, _} ->
284+
:ok
257285
end
258-
{:error, _} -> :ok
286+
287+
{:error, _} ->
288+
:ok
259289
end
260290
end
261291

@@ -265,7 +295,7 @@ defmodule LibsignalProtocol.SessionTest do
265295
{:ok, session} = Session.create(local_identity_key, remote_identity_key)
266296

267297
invalid_encrypted = "invalid_encrypted_message"
268-
298+
269299
case Session.decrypt_message(session, invalid_encrypted) do
270300
{:error, _} -> :ok
271301
{:ok, _, _} -> :ok
@@ -280,7 +310,8 @@ defmodule LibsignalProtocol.SessionTest do
280310

281311
# Generate pre-key bundle components
282312
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
283-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
313+
314+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
284315
:nif.generate_signed_pre_key(remote_identity_key, 67890)
285316

286317
bundle = %{
@@ -297,7 +328,9 @@ defmodule LibsignalProtocol.SessionTest do
297328
assert Map.has_key?(session, :id)
298329
assert Map.has_key?(session, :pre_key_id)
299330
assert Map.has_key?(session, :signed_pre_key_id)
300-
{:error, _} -> :ok
331+
332+
{:error, _} ->
333+
:ok
301334
end
302335
end
303336
end
@@ -310,7 +343,8 @@ defmodule LibsignalProtocol.SessionTest do
310343

311344
# Generate pre-key bundle components
312345
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
313-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
346+
347+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
314348
:nif.generate_signed_pre_key(remote_identity_key, 67890)
315349

316350
bundle = %{
@@ -324,14 +358,18 @@ defmodule LibsignalProtocol.SessionTest do
324358
case Session.process_pre_key_bundle(session, bundle) do
325359
{:ok, established_session} ->
326360
message = "Hello from sender!"
327-
361+
328362
case Session.send_message(established_session, message) do
329363
{:ok, encrypted_message, updated_session} ->
330364
assert is_binary(encrypted_message)
331365
assert is_map(updated_session)
332-
{:error, _} -> :ok
366+
367+
{:error, _} ->
368+
:ok
333369
end
334-
{:error, _} -> :ok
370+
371+
{:error, _} ->
372+
:ok
335373
end
336374
end
337375
end
@@ -344,7 +382,8 @@ defmodule LibsignalProtocol.SessionTest do
344382

345383
# Generate pre-key bundle components
346384
{:ok, {pre_key_id, pre_key_public}} = :nif.generate_pre_key(12345)
347-
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
385+
386+
{:ok, {signed_pre_key_id, signed_pre_key_public, signature}} =
348387
:nif.generate_signed_pre_key(remote_identity_key, 67890)
349388

350389
bundle = %{
@@ -358,18 +397,24 @@ defmodule LibsignalProtocol.SessionTest do
358397
case Session.process_pre_key_bundle(session, bundle) do
359398
{:ok, established_session} ->
360399
original_message = "Hello from sender!"
361-
400+
362401
case Session.encrypt_message(established_session, original_message) do
363402
{:ok, encrypted_message, updated_session} ->
364403
case Session.receive_message(updated_session, encrypted_message) do
365404
{:ok, decrypted_message, _} ->
366405
assert decrypted_message == original_message
367-
{:error, _} -> :ok
406+
407+
{:error, _} ->
408+
:ok
368409
end
369-
{:error, _} -> :ok
410+
411+
{:error, _} ->
412+
:ok
370413
end
371-
{:error, _} -> :ok
414+
415+
{:error, _} ->
416+
:ok
372417
end
373418
end
374419
end
375-
end
420+
end

test/elixir/signal_protocol_test.exs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ defmodule SignalProtocolTest do
3030
test "generates valid signed pre-key" do
3131
{:ok, {identity_key, _}} = SignalProtocol.generate_identity_key_pair()
3232
key_id = :rand.uniform(1000)
33-
assert {:ok, {^key_id, public_key, signature}} =
34-
SignalProtocol.generate_signed_pre_key(identity_key, key_id)
33+
34+
assert {:ok, {^key_id, public_key, signature}} =
35+
SignalProtocol.generate_signed_pre_key(identity_key, key_id)
36+
3537
assert is_binary(public_key)
3638
assert is_binary(signature)
3739
assert byte_size(public_key) == 32
@@ -43,7 +45,10 @@ defmodule SignalProtocolTest do
4345
test "creates valid session" do
4446
{:ok, {local_identity_key, _}} = SignalProtocol.generate_identity_key_pair()
4547
{:ok, {remote_identity_key, _}} = SignalProtocol.generate_identity_key_pair()
46-
assert {:ok, session_id} = SignalProtocol.create_session(local_identity_key, remote_identity_key)
48+
49+
assert {:ok, session_id} =
50+
SignalProtocol.create_session(local_identity_key, remote_identity_key)
51+
4752
assert is_binary(session_id)
4853
end
4954
end
@@ -53,11 +58,11 @@ defmodule SignalProtocolTest do
5358
{:ok, {local_identity_key, _}} = SignalProtocol.generate_identity_key_pair()
5459
{:ok, {remote_identity_key, _}} = SignalProtocol.generate_identity_key_pair()
5560
{:ok, session_id} = SignalProtocol.create_session(local_identity_key, remote_identity_key)
56-
61+
5762
message = "Hello, Signal Protocol!"
5863
assert {:ok, ciphertext} = SignalProtocol.encrypt_message(session_id, message)
5964
assert is_binary(ciphertext)
60-
65+
6166
assert {:ok, decrypted} = SignalProtocol.decrypt_message(session_id, ciphertext)
6267
assert decrypted == message
6368
end
@@ -67,37 +72,37 @@ defmodule SignalProtocolTest do
6772
test "signs and verifies data" do
6873
{:ok, {public_key, private_key}} = SignalProtocol.generate_identity_key_pair()
6974
data = "Hello, World!"
70-
75+
7176
assert {:ok, signature} = SignalProtocol.sign_data(private_key, data)
7277
assert is_binary(signature)
73-
78+
7479
assert {:ok, true} = SignalProtocol.verify_signature(public_key, data, signature)
7580
end
7681

7782
test "encrypts and decrypts with key and IV" do
7883
key = :crypto.strong_rand_bytes(32)
7984
iv = :crypto.strong_rand_bytes(12)
8085
data = "Hello, World!"
81-
86+
8287
assert {:ok, ciphertext} = SignalProtocol.encrypt_message(key, iv, data)
8388
assert is_binary(ciphertext)
84-
89+
8590
assert {:ok, decrypted} = SignalProtocol.decrypt_message(key, iv, ciphertext)
8691
assert decrypted == data
8792
end
8893

8994
test "generates HMAC" do
9095
key = :crypto.strong_rand_bytes(32)
9196
data = "Hello, World!"
92-
97+
9398
assert {:ok, mac} = SignalProtocol.hmac_sha256(key, data)
9499
assert is_binary(mac)
95100
assert byte_size(mac) == 32
96101
end
97102

98103
test "generates SHA-256 hash" do
99104
data = "Hello, World!"
100-
105+
101106
assert {:ok, hash} = SignalProtocol.sha256(data)
102107
assert is_binary(hash)
103108
assert byte_size(hash) == 32
@@ -110,4 +115,4 @@ defmodule SignalProtocolTest do
110115
assert byte_size(bytes) == n
111116
end
112117
end
113-
end
118+
end

0 commit comments

Comments
 (0)