Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some trait implementation under the test module #182

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions src/olm/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,11 @@ impl From<MessageType> for usize {
}
}

#[cfg(test)]
use olm_rs::session::OlmMessage as LibolmMessage;

#[cfg(test)]
impl From<LibolmMessage> for OlmMessage {
fn from(other: LibolmMessage) -> Self {
let (message_type, ciphertext) = other.to_tuple();
let ciphertext_bytes = base64_decode(ciphertext).expect("Can't decode base64");

Self::from_parts(message_type.into(), ciphertext_bytes.as_slice())
.expect("Can't decode a libolm message")
}
}

#[cfg(test)]
impl From<OlmMessage> for LibolmMessage {
fn from(value: OlmMessage) -> LibolmMessage {
match value {
OlmMessage::Normal(m) => LibolmMessage::from_type_and_ciphertext(1, m.to_base64())
.expect("Can't create a valid libolm message"),
OlmMessage::PreKey(m) => LibolmMessage::from_type_and_ciphertext(0, m.to_base64())
.expect("Can't create a valid libolm pre-key message"),
}
}
}

#[cfg(test)]
mod tests {
use anyhow::Result;
use assert_matches::assert_matches;
use olm_rs::session::OlmMessage as LibolmMessage;
use serde_json::json;

use super::*;
Expand All @@ -201,6 +176,27 @@ mod tests {
const MESSAGE_CIPHERTEXT: [u8; 16] =
[249, 125, 179, 111, 185, 4, 95, 253, 201, 190, 130, 236, 165, 195, 65, 112];

impl From<OlmMessage> for LibolmMessage {
fn from(value: OlmMessage) -> LibolmMessage {
match value {
OlmMessage::Normal(m) => LibolmMessage::from_type_and_ciphertext(1, m.to_base64())
.expect("Can't create a valid libolm message"),
OlmMessage::PreKey(m) => LibolmMessage::from_type_and_ciphertext(0, m.to_base64())
.expect("Can't create a valid libolm pre-key message"),
}
}
}

impl From<LibolmMessage> for OlmMessage {
fn from(other: LibolmMessage) -> Self {
let (message_type, ciphertext) = other.to_tuple();
let ciphertext_bytes = base64_decode(ciphertext).expect("Can't decode base64");

Self::from_parts(message_type.into(), ciphertext_bytes.as_slice())
.expect("Can't decode a libolm message")
}
}

#[test]
fn message_type_from_usize() {
assert_eq!(
Expand Down