Skip to content

Commit

Permalink
Remove union support code.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Sandu <sandreim@amazon.com>
  • Loading branch information
sandreim committed Sep 17, 2020
1 parent f89f27e commit be94f49
Showing 1 changed file with 0 additions and 346 deletions.
346 changes: 0 additions & 346 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,137 +295,6 @@ fn test_hardcoded_struct_deserialization() {
assert_eq!(restored_state, expected_state);
}

#[repr(C)]
#[derive(Versionize)]
union TestUnion {
a: i16,
b: i32,
#[version(start = 2, end = 3)]
c: [u32; 4usize],
#[version(start = 3)]
d: u64,
}

impl Default for TestUnion {
fn default() -> Self {
TestUnion { b: 64i32 }
}
}

impl Debug for TestUnion {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
unsafe {
write!(
f,
"{{ a: {:?}, b: {:?}, c: {:?}, d: {:?} }}",
self.a, self.b, self.c, self.d
)
}
}
}

#[test]
fn test_hardcoded_union_deserialization() {
// We are testing separately representation compatibility between versions for unions as it
// is pretty awkward to implement PartialEq for unions.

// The union instance size at a certain version will be equal with the max size of the available
// fields at that version.
#[rustfmt::skip]
let v1_hardcoded_snapshot: &[u8] = &[
// union value (4 bytes).
0x01, 0x02, 0x03, 0x04,
];

#[rustfmt::skip]
let v2_hardcoded_snapshot: &[u8] = &[
// 4 elements Vec of u32.
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
];

#[rustfmt::skip]
let v3_hardcoded_snapshot: &[u8] = &[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
];

#[rustfmt::skip]
let short_v3_hardcoded_snapshot: &[u8] = &[
0x01, 0x02, 0x03, 0x04,
];

#[rustfmt::skip]
let long_v3_hardcoded_snapshot: &[u8] = &[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
];

let mut vm = VersionMap::new();
vm.new_version()
.set_type_version(TestUnion::type_id(), 2)
.new_version()
.set_type_version(TestUnion::type_id(), 3);

let mut snapshot_blob = v1_hardcoded_snapshot;

let mut restored_state =
<TestUnion as Versionize>::deserialize(&mut snapshot_blob, &vm, 1).unwrap();
unsafe {
assert_eq!(restored_state.a, 0x0201i16);
assert_eq!(restored_state.b, 0x0403_0201i32);
}

snapshot_blob = v2_hardcoded_snapshot;

restored_state = <TestUnion as Versionize>::deserialize(&mut snapshot_blob, &vm, 2).unwrap();
unsafe {
assert_eq!(restored_state.a, 0x0201i16);
assert_eq!(restored_state.b, 0x0403_0201i32);
assert_eq!(
restored_state.c,
[
0x0403_0201u32,
0x0807_0605u32,
0x0C0B_0A09u32,
0x000F_0E0Du32
]
);
}

snapshot_blob = v3_hardcoded_snapshot;

restored_state = <TestUnion as Versionize>::deserialize(&mut snapshot_blob, &vm, 3).unwrap();
unsafe {
assert_eq!(restored_state.a, 0x0201i16);
assert_eq!(restored_state.b, 0x0403_0201i32);
assert_eq!(restored_state.d, 0x0807_0605_0403_0201u64);
}

// Let's try to deserialize a snapshot that is shorter than the expected one for version 3.
snapshot_blob = short_v3_hardcoded_snapshot;

// Reading a `TestUnion` value fails if we don't provide the expected number of bytes in the
// snapshot.
assert_eq!(
<TestUnion as Versionize>::deserialize(&mut snapshot_blob, &vm, 3).unwrap_err(),
VersionizeError::Deserialize(
"Io(Custom { kind: UnexpectedEof, error: \"failed to fill whole buffer\" })".to_owned()
)
);

// Now we will deserialize a longer snapshot than the expected one at version 3.
snapshot_blob = long_v3_hardcoded_snapshot;

// Reading a `TestUnion` value won't fail, but only the number of expected bytes for version 3
// (8 bytes) will be stored in the union variable.
restored_state = <TestUnion as Versionize>::deserialize(&mut snapshot_blob, &vm, 3).unwrap();
unsafe {
assert_eq!(restored_state.a, 0x0201i16);
assert_eq!(restored_state.b, 0x0403_0201i32);
assert_eq!(restored_state.d, 0x0807_0605_0403_0201u64);
}
}

#[test]
fn test_hardcoded_enum_deserialization() {
// We are testing separately also hardcoded snapshot deserialization for enums
Expand Down Expand Up @@ -645,41 +514,6 @@ fn test_versionize_struct_with_array() {
assert_eq!(restored_test_struct, test_struct);
}

#[test]
fn test_versionize_union_with_array() {
#[derive(Versionize)]
union TestUnion {
a: [u32; SIZE],
b: [u8; dummy_mod::SIZE],
}

impl Default for TestUnion {
fn default() -> Self {
TestUnion { a: [3; SIZE] }
}
}

impl Debug for TestUnion {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
unsafe { write!(f, "{{ a: {:?}, b: {:?} }}", self.a, self.b) }
}
}

let test_union = TestUnion { a: [1; SIZE] };

let mut mem = vec![0; 4096];
let version_map = VersionMap::new();

test_union
.serialize(&mut mem.as_mut_slice(), &version_map, 1)
.unwrap();
let restored_test_union = TestUnion::deserialize(&mut mem.as_slice(), &version_map, 1).unwrap();

unsafe {
assert_eq!(restored_test_union.a, test_union.a);
}
}

#[derive(Clone, Debug, PartialEq, Versionize)]
pub enum DeviceStatus {
Inactive,
Expand Down Expand Up @@ -1045,186 +879,6 @@ fn test_versionize_enum() {
assert_eq!(restored_state, State::One(false));
}

#[test]
fn test_versionize_union() {
let mut vm = VersionMap::new();
vm.new_version()
.set_type_version(TestUnion::type_id(), 2)
.new_version()
.set_type_version(TestUnion::type_id(), 3);

let state = TestUnion {
c: [
0x0403_0201u32,
0x0807_0605u32,
0x0000_0000u32,
0x2222_1111u32,
],
};

let mut snapshot_mem = vec![0u8; 1024];

// Serialize as v1.
state
.serialize(&mut snapshot_mem.as_mut_slice(), &vm, 1)
.unwrap();
let mut restored_state =
<TestUnion as Versionize>::deserialize(&mut snapshot_mem.as_slice(), &vm, 1).unwrap();

// At v1, `c` field is unavailable, so when we serialize the union, the memory occupied
// by it will be = the max size of the fields that exist at v1 (`b` -> 4 bytes). So, when
// we deserialize this union, `c` field will no longer be equal with its original value
// (only the least significant 4 bytes will be preserved).
unsafe {
assert_eq!(restored_state.c[0], 0x0403_0201u32);
assert_ne!(
restored_state.c,
[
0x0403_0201u32,
0x0807_0605u32,
0x0000_0000u32,
0x2222_1111u32
]
);
}

// Serialize as v2.
state
.serialize(&mut snapshot_mem.as_mut_slice(), &vm, 2)
.unwrap();
restored_state =
<TestUnion as Versionize>::deserialize(&mut snapshot_mem.as_slice(), &vm, 2).unwrap();

// At v2, `c` field is available. So, when we deserialize the union, we expect that `c` field
// will be equal with its original value.
unsafe {
assert_eq!(
restored_state.c,
[
0x0403_0201u32,
0x0807_0605u32,
0x0000_0000u32,
0x2222_1111u32
]
);
}

// Serialize as v3.
state
.serialize(&mut snapshot_mem.as_mut_slice(), &vm, 3)
.unwrap();
restored_state =
<TestUnion as Versionize>::deserialize(&mut snapshot_mem.as_slice(), &vm, 3).unwrap();

// At v3, `d` field is available and `c` field not, so the memory occupied by the union, when
// serializing it, will be = `d` field size (8 bytes).
unsafe {
assert_eq!(restored_state.c[0], 0x0403_0201u32);
assert_eq!(restored_state.c[1], 0x0807_0605u32);
}
}

#[allow(non_upper_case_globals)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[test]
fn test_versionize_union_with_struct() {
#[derive(Clone, Copy, Versionize)]
struct kvm_run__bindgen_ty_1 {
pub code_1: u64,
pub code_2: u32,
}

#[repr(C)]
#[derive(Clone, Copy, Versionize)]
union kvm_irq_level__bindgen_ty_1 {
irq: ::std::os::raw::c_uint,
status: ::std::os::raw::c_int,
other_status: ::std::os::raw::c_longlong,

#[version(start = 1, end = 1)]
bindgen_union_align: [u64; 2usize],

#[version(start = 2)]
extended_status: ::std::os::raw::c_longlong,

#[version(start = 2)]
kvm_run_field: kvm_run__bindgen_ty_1,

#[version(start = 3)]
bindgen_union_align_2: [u64; 2usize],
}

impl Default for kvm_irq_level__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}

let mut vm = VersionMap::new();
vm.new_version()
.set_type_version(kvm_irq_level__bindgen_ty_1::type_id(), 2)
.new_version()
.set_type_version(kvm_irq_level__bindgen_ty_1::type_id(), 3);

let state = kvm_irq_level__bindgen_ty_1 {
bindgen_union_align_2: [0x1234_5678_8765_4321u64, 0x1122_3344_5555_6666u64],
};

let mut snapshot_mem = vec![0u8; 256];

// Serialize as v1.
state
.serialize(&mut snapshot_mem.as_mut_slice(), &vm, 1)
.unwrap();
let mut restored_state = <kvm_irq_level__bindgen_ty_1 as Versionize>::deserialize(
&mut snapshot_mem.as_slice(),
&vm,
1,
)
.unwrap();
unsafe {
assert_ne!(
restored_state.bindgen_union_align_2,
[0x1234_5678_8765_4321u64, 0x1122_3344_5555_6666u64]
);
}

// Serialize as v2.
state
.serialize(&mut snapshot_mem.as_mut_slice(), &vm, 2)
.unwrap();
restored_state = <kvm_irq_level__bindgen_ty_1 as Versionize>::deserialize(
&mut snapshot_mem.as_slice(),
&vm,
2,
)
.unwrap();
unsafe {
assert_ne!(
restored_state.bindgen_union_align_2,
[0x1234_5678_8765_4321u64, 0x1122_3344_5555_6666u64]
);
}

// Serialize as v3.
state
.serialize(&mut snapshot_mem.as_mut_slice(), &vm, 3)
.unwrap();
restored_state = <kvm_irq_level__bindgen_ty_1 as Versionize>::deserialize(
&mut snapshot_mem.as_slice(),
&vm,
3,
)
.unwrap();
unsafe {
assert_eq!(
restored_state.bindgen_union_align_2,
[0x1234_5678_8765_4321u64, 0x1122_3344_5555_6666u64]
);
}
}

#[derive(Clone, Debug, PartialEq, Versionize)]
pub struct S {
a: f64,
Expand Down

0 comments on commit be94f49

Please sign in to comment.