grin/p2p/tests/ser_deser.rs
Antioch Peverell ab30f714fc
Capabilities now deserialize safely (#1946)
* deserialize capabilities dropping unknown flag bits

* add tests for capabilities deserialization

* rustfmt

* FULL_NODE needs to be handled safely for legacy nodes
2018-11-07 11:15:37 +00:00

78 lines
2.1 KiB
Rust

// Copyright 2018 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate grin_p2p as p2p;
extern crate enum_primitive;
extern crate num;
use num::FromPrimitive;
// Test that Healthy == 0.
#[test]
fn test_store_state_enum() {
assert_eq!(p2p::State::from_i32(0), Some(p2p::State::Healthy));
}
#[test]
fn test_direction_enum() {
assert_eq!(p2p::Direction::from_i32(0), Some(p2p::Direction::Inbound));
}
#[test]
fn test_reason_for_ban_enum() {
assert_eq!(
p2p::types::ReasonForBan::from_i32(0),
Some(p2p::types::ReasonForBan::None)
);
}
#[test]
fn test_type_enum() {
assert_eq!(p2p::msg::Type::from_i32(0), Some(p2p::msg::Type::Error));
}
#[test]
fn test_capabilities() {
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b00000000 as u32),
p2p::types::Capabilities::UNKNOWN
);
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b10000000 as u32),
p2p::types::Capabilities::UNKNOWN
);
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b0111 as u32),
p2p::types::Capabilities::FULL_NODE
);
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b00000111 as u32),
p2p::types::Capabilities::FULL_NODE
);
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b11110111 as u32),
p2p::types::Capabilities::FULL_NODE
);
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b00100111 as u32),
p2p::types::Capabilities::FULL_NODE
);
assert_eq!(
p2p::types::Capabilities::from_bits_truncate(0b00101111 as u32),
p2p::types::Capabilities::FULL_NODE | p2p::types::Capabilities::TX_KERNEL_HASH
);
}