Skip to content

Commit

Permalink
test(payload): add tests to check for raw to utf8 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kaans committed Jan 10, 2024
1 parent f5e1bb7 commit fdfbaf2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 64 deletions.
36 changes: 18 additions & 18 deletions src/payload/json.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::fmt::{Display, Formatter};

use crate::config::{PayloadJson, PayloadOptionRawFormat};
use base64::engine::general_purpose;
use base64::Engine;
use derive_getters::Getters;
use serde_json::{from_slice, Value};

use crate::payload::{PayloadFormat, PayloadFormatError};
use crate::config::PayloadJson;
use crate::payload::{convert_raw_type, PayloadFormat, PayloadFormatError};

/// This payload format contains a JSON payload. Its value is encoded as
/// `serde_json::Value`.
Expand All @@ -27,14 +25,6 @@ impl PayloadFormatJson {
fn encode_to_json(value: Vec<u8>) -> serde_json::Result<Value> {
from_slice(value.as_slice())
}

fn convert_raw_type(options: &PayloadJson, value: Vec<u8>) -> String {
match options.raw_as_type() {
PayloadOptionRawFormat::Hex => hex::encode(value),
PayloadOptionRawFormat::Base64 => general_purpose::STANDARD.encode(value),
PayloadOptionRawFormat::Utf8 => String::from_utf8_lossy(value.as_slice()).to_string(),
}
}
}

/// Displays the hex encoded content.
Expand Down Expand Up @@ -189,7 +179,7 @@ impl TryFrom<(PayloadFormat, &PayloadJson)> for PayloadFormatJson {
match value {
PayloadFormat::Text(value) => encode_to_json_with_string_content(value.into()),
PayloadFormat::Raw(value) => encode_to_json_with_string_content(
PayloadFormatJson::convert_raw_type(options, value.into()),
convert_raw_type(options.raw_as_type(), value.into()),
),
PayloadFormat::Protobuf(value) => Ok(Self {
content: protobuf::get_message_value(
Expand All @@ -209,13 +199,12 @@ impl TryFrom<(PayloadFormat, &PayloadJson)> for PayloadFormatJson {
}

mod protobuf {
use crate::config::PayloadJson;
use protofish::context::Context;
use protofish::decode::{FieldValue, MessageValue, Value};
use serde_json::json;

use crate::payload::json::PayloadFormatJson;
use crate::payload::PayloadFormatError;
use crate::config::PayloadJson;
use crate::payload::{convert_raw_type, PayloadFormatError};

pub(super) fn get_message_value(
context: &Context,
Expand Down Expand Up @@ -288,7 +277,7 @@ mod protobuf {
}
Value::Message(value) => get_message_value(context, value, options)?,
Value::Bytes(value) => {
json!(PayloadFormatJson::convert_raw_type(options, value.to_vec()))
json!(convert_raw_type(options.raw_as_type(), value.to_vec()))
}
Value::Enum(value) => {
let enum_ref = value.enum_ref;
Expand All @@ -315,6 +304,7 @@ mod protobuf {
#[cfg(test)]
mod tests {
use serde_json::from_str;
use crate::config::PayloadOptionRawFormat;

use crate::payload::base64::PayloadFormatBase64;
use crate::payload::hex::PayloadFormatHex;
Expand Down Expand Up @@ -428,6 +418,16 @@ mod tests {
assert_eq!(get_json_value(INPUT_STRING_BASE64), result.content);
}

#[test]
fn from_raw_as_utf8() {
let input = PayloadFormatRaw::try_from(Vec::from(INPUT_STRING)).unwrap();
let options = PayloadJson::new(PayloadOptionRawFormat::Utf8);
let result = PayloadFormatJson::try_from((PayloadFormat::Raw(input), &options)).unwrap();

assert_eq!(get_json_value(INPUT_STRING), result.content);
}


#[test]
fn from_hex() {
let input = PayloadFormatHex::try_from(INPUT_STRING_HEX.to_owned()).unwrap();
Expand Down Expand Up @@ -472,7 +472,7 @@ mod tests {
"size: 54.3\nname: \"{}\"",
INPUT_STRING
)))
.unwrap();
.unwrap();
let result = PayloadFormatJson::try_from(PayloadFormat::Yaml(input)).unwrap();

assert_eq!(54.3, result.content.get("size").unwrap().as_f64().unwrap());
Expand Down
13 changes: 11 additions & 2 deletions src/payload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::io::Read;
use std::path::PathBuf;
use std::string::FromUtf8Error;

use ::base64::DecodeError;
use ::base64::{DecodeError, Engine};
use ::base64::engine::general_purpose;
use ::hex::FromHexError;
use log::error;
use protofish::context::ParseError;
use thiserror::Error;

use crate::config::{PayloadType, PublishInputType, PublishInputTypeContentPath};
use crate::config::{PayloadOptionRawFormat, PayloadType, PublishInputType, PublishInputTypeContentPath};
use crate::payload::base64::PayloadFormatBase64;
use crate::payload::hex::PayloadFormatHex;
use crate::payload::json::PayloadFormatJson;
Expand Down Expand Up @@ -306,3 +307,11 @@ fn read_from_path(path: &PathBuf) -> Result<Vec<u8>, PayloadFormatError> {
};
Ok(buf)
}

fn convert_raw_type(option: &PayloadOptionRawFormat, value: Vec<u8>) -> String {
match option {
PayloadOptionRawFormat::Hex => ::hex::encode(value),
PayloadOptionRawFormat::Base64 => general_purpose::STANDARD.encode(value),
PayloadOptionRawFormat::Utf8 => String::from_utf8_lossy(value.as_slice()).to_string(),
}
}
86 changes: 62 additions & 24 deletions src/payload/text.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::fmt::{Display, Formatter};
use std::string::FromUtf8Error;

use base64::engine::general_purpose;
use base64::Engine;

use crate::config::{PayloadOptionRawFormat, PayloadText};
use crate::payload::{PayloadFormat, PayloadFormatError};
use crate::config::PayloadText;
use crate::payload::{convert_raw_type, PayloadFormat, PayloadFormatError};

#[derive(Clone, Debug)]
pub struct PayloadFormatText {
Expand All @@ -20,14 +17,6 @@ impl PayloadFormatText {
fn encode_to_utf8(value: Vec<u8>) -> Result<String, FromUtf8Error> {
String::from_utf8(value)
}

fn convert_raw_type(options: &PayloadText, value: Vec<u8>) -> String {
match options.raw_as_type() {
PayloadOptionRawFormat::Hex => hex::encode(value),
PayloadOptionRawFormat::Base64 => general_purpose::STANDARD.encode(value),
PayloadOptionRawFormat::Utf8 => String::from_utf8_lossy(value.as_slice()).to_string(),
}
}
}

/// Displays the UTF-8 encoded content.
Expand Down Expand Up @@ -93,14 +82,22 @@ impl TryFrom<PayloadFormat> for PayloadFormatText {
}
}

impl TryFrom<(PayloadFormat, PayloadText)> for PayloadFormatText {
type Error = PayloadFormatError;

fn try_from((value, options): (PayloadFormat, PayloadText)) -> Result<Self, Self::Error> {
Self::try_from((value, &options))
}
}

impl TryFrom<(PayloadFormat, &PayloadText)> for PayloadFormatText {
type Error = PayloadFormatError;

fn try_from((value, options): (PayloadFormat, &PayloadText)) -> Result<Self, Self::Error> {
match value {
PayloadFormat::Text(value) => Ok(value),
PayloadFormat::Raw(value) => Ok(Self {
content: Self::convert_raw_type(options, value.into()),
content: convert_raw_type(options.raw_as_type(), value.into()),
}),
PayloadFormat::Protobuf(value) => Ok(Self {
content: protobuf::get_message_value(
Expand All @@ -114,13 +111,13 @@ impl TryFrom<(PayloadFormat, &PayloadText)> for PayloadFormatText {
PayloadFormat::Hex(value) => {
let a: Vec<u8> = value.try_into()?;
Ok(Self {
content: Self::convert_raw_type(options, a),
content: convert_raw_type(options.raw_as_type(), a),
})
}
PayloadFormat::Base64(value) => {
let a: Vec<u8> = value.try_into()?;
Ok(Self {
content: Self::convert_raw_type(options, a),
content: convert_raw_type(options.raw_as_type(), a),
})
}
PayloadFormat::Json(value) => {
Expand Down Expand Up @@ -158,12 +155,11 @@ impl TryFrom<(PayloadFormat, &PayloadText)> for PayloadFormatText {
}

mod protobuf {
use crate::config::PayloadText;
use protofish::context::{Context, EnumField, MessageInfo};
use protofish::decode::{FieldValue, MessageValue, Value};

use crate::payload::text::PayloadFormatText;
use crate::payload::PayloadFormatError;
use crate::config::PayloadText;
use crate::payload::{convert_raw_type, PayloadFormatError};

pub(super) fn get_message_value(
context: &Context,
Expand Down Expand Up @@ -304,7 +300,7 @@ mod protobuf {
format!(
"{indent_spaces}[{}] {type_name} = {:?} (Bytes)\n",
field.number,
PayloadFormatText::convert_raw_type(options, value.to_vec())
convert_raw_type(options.raw_as_type(), value.to_vec())
)
}
Value::Message(value) => get_message_value(
Expand Down Expand Up @@ -355,6 +351,7 @@ mod protobuf {

#[cfg(test)]
mod tests {
use crate::config::PayloadOptionRawFormat;
use crate::payload::base64::PayloadFormatBase64;
use crate::payload::hex::PayloadFormatHex;
use crate::payload::json::PayloadFormatJson;
Expand Down Expand Up @@ -480,18 +477,59 @@ mod tests {
}

#[test]
fn from_hex() {
fn from_raw_as_utf8() {
let input = PayloadFormatRaw::try_from(Vec::from(INPUT_STRING)).unwrap();
let options = PayloadText::new(PayloadOptionRawFormat::Utf8);
let result = PayloadFormatText::try_from((PayloadFormat::Raw(input), &options)).unwrap();

assert_eq!(INPUT_STRING.to_string(), result.content);
}

#[test]
fn from_hex_as_hex() {
let input = PayloadFormatHex::try_from(INPUT_STRING_HEX.to_owned()).unwrap();
let result = PayloadFormatText::try_from(PayloadFormat::Hex(input)).unwrap();

assert_eq!(INPUT_STRING.to_owned(), result.content);
assert_eq!(INPUT_STRING_HEX.to_owned(), result.content);
}

#[test]
fn from_base64() {
fn from_base64_as_hex() {
let input = PayloadFormatBase64::try_from(INPUT_STRING_BASE64.to_owned()).unwrap();
let result = PayloadFormatText::try_from(PayloadFormat::Base64(input)).unwrap();

assert_eq!(INPUT_STRING_HEX.to_owned(), result.content);
}

#[test]
fn from_hex_as_base64() {
let input = PayloadFormatHex::try_from(INPUT_STRING_HEX.to_owned()).unwrap();
let result = PayloadFormatText::try_from((PayloadFormat::Hex(input), PayloadText::new(PayloadOptionRawFormat::Base64))).unwrap();

assert_eq!(INPUT_STRING_BASE64.to_owned(), result.content);
}

#[test]
fn from_base64_as_base64() {
let input = PayloadFormatBase64::try_from(INPUT_STRING_BASE64.to_owned()).unwrap();
let result = PayloadFormatText::try_from((PayloadFormat::Base64(input), PayloadText::new(PayloadOptionRawFormat::Base64))).unwrap();

assert_eq!(INPUT_STRING_BASE64.to_owned(), result.content);
}

#[test]
fn from_hex_as_text() {
let input = PayloadFormatHex::try_from(INPUT_STRING_HEX.to_owned()).unwrap();
let result = PayloadFormatText::try_from((PayloadFormat::Hex(input), PayloadText::new(PayloadOptionRawFormat::Utf8))).unwrap();

assert_eq!(INPUT_STRING.to_owned(), result.content);
}

#[test]
fn from_base64_as_text() {
let input = PayloadFormatBase64::try_from(INPUT_STRING_BASE64.to_owned()).unwrap();
let result = PayloadFormatText::try_from((PayloadFormat::Base64(input), PayloadText::new(PayloadOptionRawFormat::Utf8))).unwrap();

assert_eq!(INPUT_STRING.to_owned(), result.content);
}

Expand All @@ -501,7 +539,7 @@ mod tests {
"{{\"content\": \"{}\"}}",
INPUT_STRING
)))
.unwrap();
.unwrap();
let result = PayloadFormatText::try_from(PayloadFormat::Json(input)).unwrap();

assert_eq!(INPUT_STRING.to_owned(), result.content);
Expand Down
Loading

0 comments on commit fdfbaf2

Please sign in to comment.