Skip to content

Commit 79e460f

Browse files
mwardMichael Ward
andauthored
[Rust] updated generator to correctly limit length of var data written (#1096)
* [Rust] updated how Cargo.toml package name is cleaned * [Rust] fixed RustGenerator::generateEncoderVarData() to cap slice len to primitive type size * [Rust] updated test imports * [Rust] updated issue1066.xml * [Rust] updated rust tests --------- Co-authored-by: Michael Ward <mward@drw.com>
1 parent 445395e commit 79e460f

15 files changed

Lines changed: 219 additions & 87 deletions

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,9 @@ tasks.register('generateRustTestCodecs', JavaExec) {
679679
'sbe-tool/src/test/resources/issue1028.xml',
680680
'sbe-tool/src/test/resources/issue1057.xml',
681681
'sbe-tool/src/test/resources/issue1066.xml',
682-
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
682+
'sbe-tool/src/test/resources/basic-variable-length-schema.xml',
683683
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
684+
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
684685
'sbe-tool/src/test/resources/nested-composite-name.xml',
685686
]
686687
}

rust/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "sbe_rust_example"
33
version = "0.1.0"
44
authors = []
5-
edition = "2018"
5+
edition = "2021"
66
publish = false
77

88
[dependencies]
@@ -19,12 +19,13 @@ issue_987 = { path = "../generated/rust/issue987" }
1919
issue_1028 = { path = "../generated/rust/issue1028" }
2020
issue_1057 = { path = "../generated/rust/issue1057" }
2121
issue_1066 = { path = "../generated/rust/issue1066" }
22-
baseline_bigendian = { path = "../generated/rust/baseline-bigendian" }
23-
nested_composite_name = { path = "../generated/rust/nested-composite-name" }
22+
baseline_bigendian = { path = "../generated/rust/baseline_bigendian" }
23+
nested_composite_name = { path = "../generated/rust/nested_composite_name" }
24+
sbe_tests = { path = "../generated/rust/sbe_tests" }
2425
fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" }
2526

2627
[dev-dependencies]
27-
criterion = "0.5"
28+
criterion = "0.7"
2829

2930
[[bench]]
3031
name = "car_benchmark"
Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
1+
use examples_baseline::boost_type::BoostType;
12
use BoostType::{NullVal, KERS, NITROUS, SUPERCHARGER, TURBO};
2-
use examples_baseline::{
3-
boost_type::BoostType,
4-
};
53

64
#[test]
75
fn test_boost_type_from_str() -> Result<(), ()> {
8-
assert_eq!("TURBO".parse::<BoostType>()?, TURBO, "Parse \"TURBO\" as BoostType");
9-
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType");
10-
assert_eq!("NITROUS".parse::<BoostType>()?, NITROUS, "Parse \"NITROUS\" as BoostType");
11-
assert_eq!("KERS".parse::<BoostType>()?, KERS, "Parse \"KERS\" as BoostType");
6+
assert_eq!(
7+
"TURBO".parse::<BoostType>()?,
8+
TURBO,
9+
"Parse \"TURBO\" as BoostType"
10+
);
11+
assert_eq!(
12+
"SUPERCHARGER".parse::<BoostType>()?,
13+
SUPERCHARGER,
14+
"Parse \"SUPERCHARGER\" as BoostType"
15+
);
16+
assert_eq!(
17+
"NITROUS".parse::<BoostType>()?,
18+
NITROUS,
19+
"Parse \"NITROUS\" as BoostType"
20+
);
21+
assert_eq!(
22+
"KERS".parse::<BoostType>()?,
23+
KERS,
24+
"Parse \"KERS\" as BoostType"
25+
);
1226

13-
assert_eq!("Turbo".parse::<BoostType>()?, NullVal, "Parse \"Turbo\" as BoostType");
14-
assert_eq!("Supercharger".parse::<BoostType>()?, NullVal, "Parse \"Supercharger\" as BoostType");
15-
assert_eq!("Nitrous".parse::<BoostType>()?, NullVal, "Parse \"Nitrous\" as BoostType");
16-
assert_eq!("Kers".parse::<BoostType>()?, NullVal, "Parse \"Kers\" as BoostType");
27+
assert_eq!(
28+
"Turbo".parse::<BoostType>()?,
29+
NullVal,
30+
"Parse \"Turbo\" as BoostType"
31+
);
32+
assert_eq!(
33+
"Supercharger".parse::<BoostType>()?,
34+
NullVal,
35+
"Parse \"Supercharger\" as BoostType"
36+
);
37+
assert_eq!(
38+
"Nitrous".parse::<BoostType>()?,
39+
NullVal,
40+
"Parse \"Nitrous\" as BoostType"
41+
);
42+
assert_eq!(
43+
"Kers".parse::<BoostType>()?,
44+
NullVal,
45+
"Parse \"Kers\" as BoostType"
46+
);
1747

18-
assert_eq!("AA".parse::<BoostType>()?, NullVal, "Parse \"AA\" as BoostType");
19-
assert_eq!("".parse::<BoostType>().unwrap(), NullVal, "Parse \"\" as BoostType");
48+
assert_eq!(
49+
"AA".parse::<BoostType>()?,
50+
NullVal,
51+
"Parse \"AA\" as BoostType"
52+
);
53+
assert_eq!(
54+
"".parse::<BoostType>().unwrap(),
55+
NullVal,
56+
"Parse \"\" as BoostType"
57+
);
2058

2159
Ok(())
2260
}

rust/tests/baseline_enum_into.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
use examples_baseline::{
2-
boost_type::BoostType,
3-
};
1+
use examples_baseline::boost_type::BoostType;
42

53
#[test]
64
fn test_boost_type_from_str() -> Result<(), ()> {
7-
assert_eq!(<BoostType as Into<u8>>::into(BoostType::TURBO), 84_u8, "BoostType::TURBO into value");
8-
assert_eq!(<BoostType as Into<u8>>::into(BoostType::SUPERCHARGER), 83_u8, "BoostType::SUPERCHARGER into value");
9-
assert_eq!(<BoostType as Into<u8>>::into(BoostType::NITROUS), 78_u8, "BoostType::NITROUS into value");
10-
assert_eq!(<BoostType as Into<u8>>::into(BoostType::KERS), 75_u8, "BoostType::KERS into value");
11-
assert_eq!(<BoostType as Into<u8>>::into(BoostType::NullVal), 0_u8, "BoostType::NullVal into value");
5+
assert_eq!(
6+
<BoostType as Into<u8>>::into(BoostType::TURBO),
7+
84_u8,
8+
"BoostType::TURBO into value"
9+
);
10+
assert_eq!(
11+
<BoostType as Into<u8>>::into(BoostType::SUPERCHARGER),
12+
83_u8,
13+
"BoostType::SUPERCHARGER into value"
14+
);
15+
assert_eq!(
16+
<BoostType as Into<u8>>::into(BoostType::NITROUS),
17+
78_u8,
18+
"BoostType::NITROUS into value"
19+
);
20+
assert_eq!(
21+
<BoostType as Into<u8>>::into(BoostType::KERS),
22+
75_u8,
23+
"BoostType::KERS into value"
24+
);
25+
assert_eq!(
26+
<BoostType as Into<u8>>::into(BoostType::NullVal),
27+
0_u8,
28+
"BoostType::NullVal into value"
29+
);
1230

1331
Ok(())
1432
}

rust/tests/baseline_enum_to_str.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
use examples_baseline::{
2-
boost_type::BoostType,
3-
};
1+
use examples_baseline::boost_type::BoostType;
42

53
#[test]
64
fn test_boost_type_from_str() -> Result<(), ()> {
7-
assert_eq!(format!("{}", BoostType::TURBO), "TURBO", "Display \"TURBO\"");
8-
assert_eq!(format!("{}", BoostType::SUPERCHARGER), "SUPERCHARGER", "Display \"SUPERCHARGER\"");
9-
assert_eq!(format!("{}", BoostType::NITROUS), "NITROUS", "Display \"NITROUS\"");
5+
assert_eq!(
6+
format!("{}", BoostType::TURBO),
7+
"TURBO",
8+
"Display \"TURBO\""
9+
);
10+
assert_eq!(
11+
format!("{}", BoostType::SUPERCHARGER),
12+
"SUPERCHARGER",
13+
"Display \"SUPERCHARGER\""
14+
);
15+
assert_eq!(
16+
format!("{}", BoostType::NITROUS),
17+
"NITROUS",
18+
"Display \"NITROUS\""
19+
);
1020
assert_eq!(format!("{}", BoostType::KERS), "KERS", "Display \"KERS\"");
11-
assert_eq!(format!("{}", BoostType::NullVal), "NullVal", "Display \"NullVal\"");
21+
assert_eq!(
22+
format!("{}", BoostType::NullVal),
23+
"NullVal",
24+
"Display \"NullVal\""
25+
);
1226

1327
Ok(())
1428
}

rust/tests/baseline_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,4 @@ fn test_issue_1018() {
272272
assert_eq!(1, examples_baseline::SBE_SCHEMA_ID);
273273
assert_eq!(0, examples_baseline::SBE_SCHEMA_VERSION);
274274
assert_eq!("5.2", examples_baseline::SBE_SEMANTIC_VERSION);
275-
}
275+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use sbe_tests::{
2+
message_header_codec::MessageHeaderDecoder,
3+
test_message_1_codec::{
4+
TestMessage1Decoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_TEMPLATE_ID,
5+
},
6+
};
7+
use sbe_tests::{
8+
WriteBuf,
9+
{message_header_codec, test_message_1_codec::TestMessage1Encoder, Encoder, ReadBuf},
10+
};
11+
12+
#[test]
13+
fn should_limit_var_data_length() {
14+
// encode...
15+
let mut buffer = vec![0u8; 1024];
16+
let mut encoder = TestMessage1Encoder::default();
17+
encoder = encoder.wrap(
18+
WriteBuf::new(buffer.as_mut_slice()),
19+
message_header_codec::ENCODED_LENGTH,
20+
);
21+
encoder = encoder.header(0).parent().unwrap();
22+
23+
let password: String = (0..1024).map(|_| 'x' as char).collect();
24+
encoder.encrypted_new_password(password.as_bytes());
25+
assert_eq!(263, encoder.get_limit());
26+
27+
// decode...
28+
let buf = ReadBuf::new(buffer.as_slice());
29+
let header = MessageHeaderDecoder::default().wrap(buf, 0);
30+
assert_eq!(SBE_BLOCK_LENGTH, header.block_length());
31+
assert_eq!(SBE_SCHEMA_VERSION, header.version());
32+
assert_eq!(SBE_TEMPLATE_ID, header.template_id());
33+
assert_eq!(SBE_SCHEMA_ID, header.schema_id());
34+
35+
let mut decoder = TestMessage1Decoder::default().header(header, 0);
36+
let coord = decoder.encrypted_new_password_decoder();
37+
let password = String::from_utf8_lossy(decoder.encrypted_new_password_slice(coord));
38+
assert_eq!(254, password.len());
39+
password
40+
.as_bytes()
41+
.iter()
42+
.for_each(|x| assert_eq!(b'x', *x));
43+
}

rust/tests/big_endian_test.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
use baseline_bigendian::car_codec::{CarDecoder, CarEncoder, SBE_TEMPLATE_ID};
12
use baseline_bigendian::{
23
boolean_type::BooleanType,
34
boost_type::BoostType,
4-
car_codec::{
5-
encoder::{AccelerationEncoder, FuelFiguresEncoder, PerformanceFiguresEncoder},
6-
*,
7-
},
5+
car_codec::encoder::{AccelerationEncoder, FuelFiguresEncoder, PerformanceFiguresEncoder},
86
message_header_codec,
97
message_header_codec::MessageHeaderDecoder,
108
model::Model,

rust/tests/fixed_sized_primitive_array.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,28 +337,32 @@ fn test_encode_then_decode_non_u8_signed_primitive_slice() {
337337
DemoDecoder::fixed_16_i8,
338338
DemoEncoder::fixed_16_i8_end,
339339
DemoDecoder::fixed_16_i8_end,
340-
i8, i8::from_le_bytes([uninit])
340+
i8,
341+
i8::from_le_bytes([uninit])
341342
);
342343
run_encode_then_decode_for_array_of_signed_len_16!(
343344
DemoEncoder::fixed_16_i16_from_iter,
344345
DemoDecoder::fixed_16_i16,
345346
DemoEncoder::fixed_16_i16_end,
346347
DemoDecoder::fixed_16_i16_end,
347-
i16, i16::from_le_bytes([uninit, uninit])
348+
i16,
349+
i16::from_le_bytes([uninit, uninit])
348350
);
349351
run_encode_then_decode_for_array_of_signed_len_16!(
350352
DemoEncoder::fixed_16_i32_from_iter,
351353
DemoDecoder::fixed_16_i32,
352354
DemoEncoder::fixed_16_i32_end,
353355
DemoDecoder::fixed_16_i32_end,
354-
i32, i32::from_le_bytes([uninit, uninit, uninit, uninit])
356+
i32,
357+
i32::from_le_bytes([uninit, uninit, uninit, uninit])
355358
);
356359
run_encode_then_decode_for_array_of_signed_len_16!(
357360
DemoEncoder::fixed_16_i64_from_iter,
358361
DemoDecoder::fixed_16_i64,
359362
DemoEncoder::fixed_16_i64_end,
360363
DemoDecoder::fixed_16_i64_end,
361-
i64, i64::from_le_bytes([uninit, uninit, uninit, uninit, uninit, uninit, uninit, uninit])
364+
i64,
365+
i64::from_le_bytes([uninit, uninit, uninit, uninit, uninit, uninit, uninit, uninit])
362366
);
363367
}
364368

rust/tests/issue_1066_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use issue_1066::{
22
issue_1066_codec,
33
issue_1066_codec::{Issue1066Decoder, Issue1066Encoder},
4-
*,
4+
message_header_codec, ReadBuf, WriteBuf,
55
};
66

77
#[test]

0 commit comments

Comments
 (0)