ibc_testkit/fixtures/core/channel/
mod.rs

1mod acknowledgement;
2mod chan_close_confirm;
3mod chan_close_init;
4mod chan_open_ack;
5mod chan_open_confirm;
6mod chan_open_init;
7mod chan_open_try;
8mod packet;
9mod recv_packet;
10mod timeout;
11mod timeout_on_close;
12
13use ibc::core::channel::types::proto::v1::{
14    Channel as RawChannel, Counterparty as RawCounterparty,
15};
16use ibc::core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
17use ibc::primitives::prelude::*;
18
19pub use self::acknowledgement::*;
20pub use self::chan_close_confirm::*;
21pub use self::chan_close_init::*;
22pub use self::chan_open_ack::*;
23pub use self::chan_open_confirm::*;
24pub use self::chan_open_init::*;
25pub use self::chan_open_try::*;
26pub use self::packet::*;
27pub use self::recv_packet::*;
28pub use self::timeout::*;
29pub use self::timeout_on_close::*;
30
31/// Returns a dummy `RawCounterparty`, for testing purposes only!
32/// Can be optionally parameterized with a specific channel identifier.
33pub fn dummy_raw_counterparty_chan(channel_id: String) -> RawCounterparty {
34    RawCounterparty {
35        port_id: PortId::transfer().to_string(),
36        channel_id,
37    }
38}
39
40/// Returns a dummy `RawChannel`, for testing purposes only!
41pub fn dummy_raw_channel_end(state: i32, channel_id: Option<u64>) -> RawChannel {
42    let channel_id = match channel_id {
43        Some(id) => ChannelId::new(id).to_string(),
44        None => "".to_string(),
45    };
46    RawChannel {
47        state,
48        ordering: 2,
49        counterparty: Some(dummy_raw_counterparty_chan(channel_id)),
50        connection_hops: vec![ConnectionId::zero().to_string()],
51        version: "".to_string(), // The version is not validated.
52        upgrade_sequence: 0,
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use core::str::FromStr;
59
60    use ibc::core::channel::types::channel::ChannelEnd;
61
62    use super::*;
63    #[test]
64    fn channel_end_try_from_raw() {
65        let raw_channel_end = dummy_raw_channel_end(2, Some(0));
66
67        let empty_raw_channel_end = RawChannel {
68            counterparty: None,
69            ..raw_channel_end.clone()
70        };
71
72        struct Test {
73            name: String,
74            params: RawChannel,
75            want_pass: bool,
76        }
77
78        let tests: Vec<Test> = vec![
79            Test {
80                name: "Raw channel end with missing counterparty".to_string(),
81                params: empty_raw_channel_end,
82                want_pass: false,
83            },
84            Test {
85                name: "Raw channel end with incorrect state".to_string(),
86                params: RawChannel {
87                    state: -1,
88                    ..raw_channel_end.clone()
89                },
90                want_pass: false,
91            },
92            Test {
93                name: "Raw channel end with incorrect ordering".to_string(),
94                params: RawChannel {
95                    ordering: -1,
96                    ..raw_channel_end.clone()
97                },
98                want_pass: false,
99            },
100            Test {
101                name: "Raw channel end with incorrect connection id in connection hops".to_string(),
102                params: RawChannel {
103                    connection_hops: vec!["connection*".to_string()].into_iter().collect(),
104                    ..raw_channel_end.clone()
105                },
106                want_pass: false,
107            },
108            Test {
109                name: "Raw channel end with incorrect connection id (has blank space)".to_string(),
110                params: RawChannel {
111                    connection_hops: vec!["con nection".to_string()].into_iter().collect(),
112                    ..raw_channel_end.clone()
113                },
114                want_pass: false,
115            },
116            Test {
117                name: "Raw channel end with two correct connection ids in connection hops"
118                    .to_string(),
119                params: RawChannel {
120                    connection_hops: vec!["connection-1".to_string(), "connection-2".to_string()]
121                        .into_iter()
122                        .collect(),
123                    ..raw_channel_end.clone()
124                },
125                want_pass: true,
126            },
127            Test {
128                name: "Raw channel end with correct params".to_string(),
129                params: raw_channel_end,
130                want_pass: true,
131            },
132        ]
133        .into_iter()
134        .collect();
135
136        for test in tests {
137            let p = test.params.clone();
138
139            let ce_result = ChannelEnd::try_from(p);
140
141            assert_eq!(
142                test.want_pass,
143                ce_result.is_ok(),
144                "ChannelEnd::try_from() failed for test {}, \nmsg{:?} with error {:?}",
145                test.name,
146                test.params.clone(),
147                ce_result.err(),
148            );
149        }
150    }
151
152    #[test]
153    fn parse_channel_ordering_type() {
154        use ibc::core::channel::types::channel::Order;
155
156        struct Test {
157            ordering: &'static str,
158            want_res: Order,
159            want_err: bool,
160        }
161        let tests: Vec<Test> = vec![
162            Test {
163                ordering: "UNINITIALIZED",
164                want_res: Order::None,
165                want_err: false,
166            },
167            Test {
168                ordering: "UNORDERED",
169                want_res: Order::Unordered,
170                want_err: false,
171            },
172            Test {
173                ordering: "ORDERED",
174                want_res: Order::Ordered,
175                want_err: false,
176            },
177            Test {
178                ordering: "UNKNOWN_ORDER",
179                want_res: Order::None,
180                want_err: true,
181            },
182        ]
183        .into_iter()
184        .collect();
185
186        for test in tests {
187            match Order::from_str(test.ordering) {
188                Ok(res) => {
189                    assert!(!test.want_err);
190                    assert_eq!(test.want_res, res);
191                }
192                Err(_) => assert!(test.want_err, "parse failed"),
193            }
194        }
195    }
196}