Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame^] | 1 | // Licensed to the Apache Software Foundation (ASF) under one |
| 2 | // or more contributor license agreements. See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership. The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt}; |
| 19 | use std::cell::RefCell; |
| 20 | use std::convert::From; |
| 21 | use std::io::{Read, Write}; |
| 22 | use std::rc::Rc; |
| 23 | use try_from::TryFrom; |
| 24 | |
| 25 | use ::{ProtocolError, ProtocolErrorKind}; |
| 26 | use ::transport::TTransport; |
| 27 | use super::{TFieldIdentifier, TInputProtocol, TInputProtocolFactory, TListIdentifier, |
| 28 | TMapIdentifier, TMessageIdentifier, TMessageType}; |
| 29 | use super::{TOutputProtocol, TOutputProtocolFactory, TSetIdentifier, TStructIdentifier, TType}; |
| 30 | |
| 31 | const BINARY_PROTOCOL_VERSION_1: u32 = 0x80010000; |
| 32 | |
| 33 | /// Read messages encoded in the Thrift simple binary encoding. |
| 34 | /// |
| 35 | /// There are two available modes: `strict` and `non-strict`, where the |
| 36 | /// `non-strict` version does not check for the protocol version in the |
| 37 | /// received message header. |
| 38 | /// |
| 39 | /// # Examples |
| 40 | /// |
| 41 | /// Create and use a `TBinaryInputProtocol`. |
| 42 | /// |
| 43 | /// ```no_run |
| 44 | /// use std::cell::RefCell; |
| 45 | /// use std::rc::Rc; |
| 46 | /// use thrift::protocol::{TBinaryInputProtocol, TInputProtocol}; |
| 47 | /// use thrift::transport::{TTcpTransport, TTransport}; |
| 48 | /// |
| 49 | /// let mut transport = TTcpTransport::new(); |
| 50 | /// transport.open("localhost:9090").unwrap(); |
| 51 | /// let transport = Rc::new(RefCell::new(Box::new(transport) as Box<TTransport>)); |
| 52 | /// |
| 53 | /// let mut i_prot = TBinaryInputProtocol::new(transport, true); |
| 54 | /// |
| 55 | /// let recvd_bool = i_prot.read_bool().unwrap(); |
| 56 | /// let recvd_string = i_prot.read_string().unwrap(); |
| 57 | /// ``` |
| 58 | pub struct TBinaryInputProtocol { |
| 59 | strict: bool, |
| 60 | transport: Rc<RefCell<Box<TTransport>>>, |
| 61 | } |
| 62 | |
| 63 | impl TBinaryInputProtocol { |
| 64 | /// Create a `TBinaryInputProtocol` that reads bytes from `transport`. |
| 65 | /// |
| 66 | /// Set `strict` to `true` if all incoming messages contain the protocol |
| 67 | /// version number in the protocol header. |
| 68 | pub fn new(transport: Rc<RefCell<Box<TTransport>>>, strict: bool) -> TBinaryInputProtocol { |
| 69 | TBinaryInputProtocol { |
| 70 | strict: strict, |
| 71 | transport: transport, |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl TInputProtocol for TBinaryInputProtocol { |
| 77 | #[cfg_attr(feature = "cargo-clippy", allow(collapsible_if))] |
| 78 | fn read_message_begin(&mut self) -> ::Result<TMessageIdentifier> { |
| 79 | let mut first_bytes = vec![0; 4]; |
| 80 | self.transport.borrow_mut().read_exact(&mut first_bytes[..])?; |
| 81 | |
| 82 | // the thrift version header is intentionally negative |
| 83 | // so the first check we'll do is see if the sign bit is set |
| 84 | // and if so - assume it's the protocol-version header |
| 85 | if first_bytes[0] >= 8 { |
| 86 | // apparently we got a protocol-version header - check |
| 87 | // it, and if it matches, read the rest of the fields |
| 88 | if first_bytes[0..2] != [0x80, 0x01] { |
| 89 | Err(::Error::Protocol(ProtocolError { |
| 90 | kind: ProtocolErrorKind::BadVersion, |
| 91 | message: format!("received bad version: {:?}", &first_bytes[0..2]), |
| 92 | })) |
| 93 | } else { |
| 94 | let message_type: TMessageType = TryFrom::try_from(first_bytes[3])?; |
| 95 | let name = self.read_string()?; |
| 96 | let sequence_number = self.read_i32()?; |
| 97 | Ok(TMessageIdentifier::new(name, message_type, sequence_number)) |
| 98 | } |
| 99 | } else { |
| 100 | // apparently we didn't get a protocol-version header, |
| 101 | // which happens if the sender is not using the strict protocol |
| 102 | if self.strict { |
| 103 | // we're in strict mode however, and that always |
| 104 | // requires the protocol-version header to be written first |
| 105 | Err(::Error::Protocol(ProtocolError { |
| 106 | kind: ProtocolErrorKind::BadVersion, |
| 107 | message: format!("received bad version: {:?}", &first_bytes[0..2]), |
| 108 | })) |
| 109 | } else { |
| 110 | // in the non-strict version the first message field |
| 111 | // is the message name. strings (byte arrays) are length-prefixed, |
| 112 | // so we've just read the length in the first 4 bytes |
| 113 | let name_size = BigEndian::read_i32(&first_bytes) as usize; |
| 114 | let mut name_buf: Vec<u8> = Vec::with_capacity(name_size); |
| 115 | self.transport.borrow_mut().read_exact(&mut name_buf)?; |
| 116 | let name = String::from_utf8(name_buf)?; |
| 117 | |
| 118 | // read the rest of the fields |
| 119 | let message_type: TMessageType = self.read_byte().and_then(TryFrom::try_from)?; |
| 120 | let sequence_number = self.read_i32()?; |
| 121 | Ok(TMessageIdentifier::new(name, message_type, sequence_number)) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | fn read_message_end(&mut self) -> ::Result<()> { |
| 127 | Ok(()) |
| 128 | } |
| 129 | |
| 130 | fn read_struct_begin(&mut self) -> ::Result<Option<TStructIdentifier>> { |
| 131 | Ok(None) |
| 132 | } |
| 133 | |
| 134 | fn read_struct_end(&mut self) -> ::Result<()> { |
| 135 | Ok(()) |
| 136 | } |
| 137 | |
| 138 | fn read_field_begin(&mut self) -> ::Result<TFieldIdentifier> { |
| 139 | let field_type_byte = self.read_byte()?; |
| 140 | let field_type = field_type_from_u8(field_type_byte)?; |
| 141 | let id = match field_type { |
| 142 | TType::Stop => Ok(0), |
| 143 | _ => self.read_i16(), |
| 144 | }?; |
| 145 | Ok(TFieldIdentifier::new::<Option<String>, String, i16>(None, field_type, id)) |
| 146 | } |
| 147 | |
| 148 | fn read_field_end(&mut self) -> ::Result<()> { |
| 149 | Ok(()) |
| 150 | } |
| 151 | |
| 152 | fn read_bytes(&mut self) -> ::Result<Vec<u8>> { |
| 153 | let num_bytes = self.transport.borrow_mut().read_i32::<BigEndian>()? as usize; |
| 154 | let mut buf = vec![0u8; num_bytes]; |
| 155 | self.transport.borrow_mut().read_exact(&mut buf).map(|_| buf).map_err(From::from) |
| 156 | } |
| 157 | |
| 158 | fn read_bool(&mut self) -> ::Result<bool> { |
| 159 | let b = self.read_i8()?; |
| 160 | match b { |
| 161 | 0 => Ok(false), |
| 162 | _ => Ok(true), |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | fn read_i8(&mut self) -> ::Result<i8> { |
| 167 | self.transport.borrow_mut().read_i8().map_err(From::from) |
| 168 | } |
| 169 | |
| 170 | fn read_i16(&mut self) -> ::Result<i16> { |
| 171 | self.transport.borrow_mut().read_i16::<BigEndian>().map_err(From::from) |
| 172 | } |
| 173 | |
| 174 | fn read_i32(&mut self) -> ::Result<i32> { |
| 175 | self.transport.borrow_mut().read_i32::<BigEndian>().map_err(From::from) |
| 176 | } |
| 177 | |
| 178 | fn read_i64(&mut self) -> ::Result<i64> { |
| 179 | self.transport.borrow_mut().read_i64::<BigEndian>().map_err(From::from) |
| 180 | } |
| 181 | |
| 182 | fn read_double(&mut self) -> ::Result<f64> { |
| 183 | self.transport.borrow_mut().read_f64::<BigEndian>().map_err(From::from) |
| 184 | } |
| 185 | |
| 186 | fn read_string(&mut self) -> ::Result<String> { |
| 187 | let bytes = self.read_bytes()?; |
| 188 | String::from_utf8(bytes).map_err(From::from) |
| 189 | } |
| 190 | |
| 191 | fn read_list_begin(&mut self) -> ::Result<TListIdentifier> { |
| 192 | let element_type: TType = self.read_byte().and_then(field_type_from_u8)?; |
| 193 | let size = self.read_i32()?; |
| 194 | Ok(TListIdentifier::new(element_type, size)) |
| 195 | } |
| 196 | |
| 197 | fn read_list_end(&mut self) -> ::Result<()> { |
| 198 | Ok(()) |
| 199 | } |
| 200 | |
| 201 | fn read_set_begin(&mut self) -> ::Result<TSetIdentifier> { |
| 202 | let element_type: TType = self.read_byte().and_then(field_type_from_u8)?; |
| 203 | let size = self.read_i32()?; |
| 204 | Ok(TSetIdentifier::new(element_type, size)) |
| 205 | } |
| 206 | |
| 207 | fn read_set_end(&mut self) -> ::Result<()> { |
| 208 | Ok(()) |
| 209 | } |
| 210 | |
| 211 | fn read_map_begin(&mut self) -> ::Result<TMapIdentifier> { |
| 212 | let key_type: TType = self.read_byte().and_then(field_type_from_u8)?; |
| 213 | let value_type: TType = self.read_byte().and_then(field_type_from_u8)?; |
| 214 | let size = self.read_i32()?; |
| 215 | Ok(TMapIdentifier::new(key_type, value_type, size)) |
| 216 | } |
| 217 | |
| 218 | fn read_map_end(&mut self) -> ::Result<()> { |
| 219 | Ok(()) |
| 220 | } |
| 221 | |
| 222 | // utility |
| 223 | // |
| 224 | |
| 225 | fn read_byte(&mut self) -> ::Result<u8> { |
| 226 | self.transport.borrow_mut().read_u8().map_err(From::from) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// Factory for creating instances of `TBinaryInputProtocol`. |
| 231 | #[derive(Default)] |
| 232 | pub struct TBinaryInputProtocolFactory; |
| 233 | |
| 234 | impl TBinaryInputProtocolFactory { |
| 235 | /// Create a `TBinaryInputProtocolFactory`. |
| 236 | pub fn new() -> TBinaryInputProtocolFactory { |
| 237 | TBinaryInputProtocolFactory {} |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | impl TInputProtocolFactory for TBinaryInputProtocolFactory { |
| 242 | fn create(&mut self, transport: Rc<RefCell<Box<TTransport>>>) -> Box<TInputProtocol> { |
| 243 | Box::new(TBinaryInputProtocol::new(transport, true)) as Box<TInputProtocol> |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /// Write messages using the Thrift simple binary encoding. |
| 248 | /// |
| 249 | /// There are two available modes: `strict` and `non-strict`, where the |
| 250 | /// `strict` version writes the protocol version number in the outgoing message |
| 251 | /// header and the `non-strict` version does not. |
| 252 | /// |
| 253 | /// # Examples |
| 254 | /// |
| 255 | /// Create and use a `TBinaryOutputProtocol`. |
| 256 | /// |
| 257 | /// ```no_run |
| 258 | /// use std::cell::RefCell; |
| 259 | /// use std::rc::Rc; |
| 260 | /// use thrift::protocol::{TBinaryOutputProtocol, TOutputProtocol}; |
| 261 | /// use thrift::transport::{TTcpTransport, TTransport}; |
| 262 | /// |
| 263 | /// let mut transport = TTcpTransport::new(); |
| 264 | /// transport.open("localhost:9090").unwrap(); |
| 265 | /// let transport = Rc::new(RefCell::new(Box::new(transport) as Box<TTransport>)); |
| 266 | /// |
| 267 | /// let mut o_prot = TBinaryOutputProtocol::new(transport, true); |
| 268 | /// |
| 269 | /// o_prot.write_bool(true).unwrap(); |
| 270 | /// o_prot.write_string("test_string").unwrap(); |
| 271 | /// ``` |
| 272 | pub struct TBinaryOutputProtocol { |
| 273 | strict: bool, |
| 274 | transport: Rc<RefCell<Box<TTransport>>>, |
| 275 | } |
| 276 | |
| 277 | impl TBinaryOutputProtocol { |
| 278 | /// Create a `TBinaryOutputProtocol` that writes bytes to `transport`. |
| 279 | /// |
| 280 | /// Set `strict` to `true` if all outgoing messages should contain the |
| 281 | /// protocol version number in the protocol header. |
| 282 | pub fn new(transport: Rc<RefCell<Box<TTransport>>>, strict: bool) -> TBinaryOutputProtocol { |
| 283 | TBinaryOutputProtocol { |
| 284 | strict: strict, |
| 285 | transport: transport, |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn write_transport(&mut self, buf: &[u8]) -> ::Result<()> { |
| 290 | self.transport.borrow_mut().write(buf).map(|_| ()).map_err(From::from) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | impl TOutputProtocol for TBinaryOutputProtocol { |
| 295 | fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> ::Result<()> { |
| 296 | if self.strict { |
| 297 | let message_type: u8 = identifier.message_type.into(); |
| 298 | let header = BINARY_PROTOCOL_VERSION_1 | (message_type as u32); |
| 299 | self.transport.borrow_mut().write_u32::<BigEndian>(header)?; |
| 300 | self.write_string(&identifier.name)?; |
| 301 | self.write_i32(identifier.sequence_number) |
| 302 | } else { |
| 303 | self.write_string(&identifier.name)?; |
| 304 | self.write_byte(identifier.message_type.into())?; |
| 305 | self.write_i32(identifier.sequence_number) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | fn write_message_end(&mut self) -> ::Result<()> { |
| 310 | Ok(()) |
| 311 | } |
| 312 | |
| 313 | fn write_struct_begin(&mut self, _: &TStructIdentifier) -> ::Result<()> { |
| 314 | Ok(()) |
| 315 | } |
| 316 | |
| 317 | fn write_struct_end(&mut self) -> ::Result<()> { |
| 318 | Ok(()) |
| 319 | } |
| 320 | |
| 321 | fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> ::Result<()> { |
| 322 | if identifier.id.is_none() && identifier.field_type != TType::Stop { |
| 323 | return Err(::Error::Protocol(ProtocolError { |
| 324 | kind: ProtocolErrorKind::Unknown, |
| 325 | message: format!("cannot write identifier {:?} without sequence number", |
| 326 | &identifier), |
| 327 | })); |
| 328 | } |
| 329 | |
| 330 | self.write_byte(field_type_to_u8(identifier.field_type))?; |
| 331 | if let Some(id) = identifier.id { |
| 332 | self.write_i16(id) |
| 333 | } else { |
| 334 | Ok(()) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | fn write_field_end(&mut self) -> ::Result<()> { |
| 339 | Ok(()) |
| 340 | } |
| 341 | |
| 342 | fn write_field_stop(&mut self) -> ::Result<()> { |
| 343 | self.write_byte(field_type_to_u8(TType::Stop)) |
| 344 | } |
| 345 | |
| 346 | fn write_bytes(&mut self, b: &[u8]) -> ::Result<()> { |
| 347 | self.write_i32(b.len() as i32)?; |
| 348 | self.write_transport(b) |
| 349 | } |
| 350 | |
| 351 | fn write_bool(&mut self, b: bool) -> ::Result<()> { |
| 352 | if b { |
| 353 | self.write_i8(1) |
| 354 | } else { |
| 355 | self.write_i8(0) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | fn write_i8(&mut self, i: i8) -> ::Result<()> { |
| 360 | self.transport.borrow_mut().write_i8(i).map_err(From::from) |
| 361 | } |
| 362 | |
| 363 | fn write_i16(&mut self, i: i16) -> ::Result<()> { |
| 364 | self.transport.borrow_mut().write_i16::<BigEndian>(i).map_err(From::from) |
| 365 | } |
| 366 | |
| 367 | fn write_i32(&mut self, i: i32) -> ::Result<()> { |
| 368 | self.transport.borrow_mut().write_i32::<BigEndian>(i).map_err(From::from) |
| 369 | } |
| 370 | |
| 371 | fn write_i64(&mut self, i: i64) -> ::Result<()> { |
| 372 | self.transport.borrow_mut().write_i64::<BigEndian>(i).map_err(From::from) |
| 373 | } |
| 374 | |
| 375 | fn write_double(&mut self, d: f64) -> ::Result<()> { |
| 376 | self.transport.borrow_mut().write_f64::<BigEndian>(d).map_err(From::from) |
| 377 | } |
| 378 | |
| 379 | fn write_string(&mut self, s: &str) -> ::Result<()> { |
| 380 | self.write_bytes(s.as_bytes()) |
| 381 | } |
| 382 | |
| 383 | fn write_list_begin(&mut self, identifier: &TListIdentifier) -> ::Result<()> { |
| 384 | self.write_byte(field_type_to_u8(identifier.element_type))?; |
| 385 | self.write_i32(identifier.size) |
| 386 | } |
| 387 | |
| 388 | fn write_list_end(&mut self) -> ::Result<()> { |
| 389 | Ok(()) |
| 390 | } |
| 391 | |
| 392 | fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> ::Result<()> { |
| 393 | self.write_byte(field_type_to_u8(identifier.element_type))?; |
| 394 | self.write_i32(identifier.size) |
| 395 | } |
| 396 | |
| 397 | fn write_set_end(&mut self) -> ::Result<()> { |
| 398 | Ok(()) |
| 399 | } |
| 400 | |
| 401 | fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> ::Result<()> { |
| 402 | let key_type = identifier.key_type |
| 403 | .expect("map identifier to write should contain key type"); |
| 404 | self.write_byte(field_type_to_u8(key_type))?; |
| 405 | let val_type = identifier.value_type |
| 406 | .expect("map identifier to write should contain value type"); |
| 407 | self.write_byte(field_type_to_u8(val_type))?; |
| 408 | self.write_i32(identifier.size) |
| 409 | } |
| 410 | |
| 411 | fn write_map_end(&mut self) -> ::Result<()> { |
| 412 | Ok(()) |
| 413 | } |
| 414 | |
| 415 | fn flush(&mut self) -> ::Result<()> { |
| 416 | self.transport.borrow_mut().flush().map_err(From::from) |
| 417 | } |
| 418 | |
| 419 | // utility |
| 420 | // |
| 421 | |
| 422 | fn write_byte(&mut self, b: u8) -> ::Result<()> { |
| 423 | self.transport.borrow_mut().write_u8(b).map_err(From::from) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /// Factory for creating instances of `TBinaryOutputProtocol`. |
| 428 | #[derive(Default)] |
| 429 | pub struct TBinaryOutputProtocolFactory; |
| 430 | |
| 431 | impl TBinaryOutputProtocolFactory { |
| 432 | /// Create a `TBinaryOutputProtocolFactory`. |
| 433 | pub fn new() -> TBinaryOutputProtocolFactory { |
| 434 | TBinaryOutputProtocolFactory {} |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | impl TOutputProtocolFactory for TBinaryOutputProtocolFactory { |
| 439 | fn create(&mut self, transport: Rc<RefCell<Box<TTransport>>>) -> Box<TOutputProtocol> { |
| 440 | Box::new(TBinaryOutputProtocol::new(transport, true)) as Box<TOutputProtocol> |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | fn field_type_to_u8(field_type: TType) -> u8 { |
| 445 | match field_type { |
| 446 | TType::Stop => 0x00, |
| 447 | TType::Void => 0x01, |
| 448 | TType::Bool => 0x02, |
| 449 | TType::I08 => 0x03, // equivalent to TType::Byte |
| 450 | TType::Double => 0x04, |
| 451 | TType::I16 => 0x06, |
| 452 | TType::I32 => 0x08, |
| 453 | TType::I64 => 0x0A, |
| 454 | TType::String | TType::Utf7 => 0x0B, |
| 455 | TType::Struct => 0x0C, |
| 456 | TType::Map => 0x0D, |
| 457 | TType::Set => 0x0E, |
| 458 | TType::List => 0x0F, |
| 459 | TType::Utf8 => 0x10, |
| 460 | TType::Utf16 => 0x11, |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | fn field_type_from_u8(b: u8) -> ::Result<TType> { |
| 465 | match b { |
| 466 | 0x00 => Ok(TType::Stop), |
| 467 | 0x01 => Ok(TType::Void), |
| 468 | 0x02 => Ok(TType::Bool), |
| 469 | 0x03 => Ok(TType::I08), // Equivalent to TType::Byte |
| 470 | 0x04 => Ok(TType::Double), |
| 471 | 0x06 => Ok(TType::I16), |
| 472 | 0x08 => Ok(TType::I32), |
| 473 | 0x0A => Ok(TType::I64), |
| 474 | 0x0B => Ok(TType::String), // technically, also a UTF7, but we'll treat it as string |
| 475 | 0x0C => Ok(TType::Struct), |
| 476 | 0x0D => Ok(TType::Map), |
| 477 | 0x0E => Ok(TType::Set), |
| 478 | 0x0F => Ok(TType::List), |
| 479 | 0x10 => Ok(TType::Utf8), |
| 480 | 0x11 => Ok(TType::Utf16), |
| 481 | unkn => { |
| 482 | Err(::Error::Protocol(ProtocolError { |
| 483 | kind: ProtocolErrorKind::InvalidData, |
| 484 | message: format!("cannot convert {} to TType", unkn), |
| 485 | })) |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | #[cfg(test)] |
| 491 | mod tests { |
| 492 | |
| 493 | use std::rc::Rc; |
| 494 | use std::cell::RefCell; |
| 495 | |
| 496 | use ::protocol::{TFieldIdentifier, TMessageIdentifier, TMessageType, TInputProtocol, |
| 497 | TListIdentifier, TMapIdentifier, TOutputProtocol, TSetIdentifier, |
| 498 | TStructIdentifier, TType}; |
| 499 | use ::transport::{TPassThruTransport, TTransport}; |
| 500 | use ::transport::mem::TBufferTransport; |
| 501 | |
| 502 | use super::*; |
| 503 | |
| 504 | #[test] |
| 505 | fn must_write_message_call_begin() { |
| 506 | let (trans, _, mut o_prot) = test_objects(); |
| 507 | |
| 508 | let ident = TMessageIdentifier::new("test", TMessageType::Call, 1); |
| 509 | assert!(o_prot.write_message_begin(&ident).is_ok()); |
| 510 | |
| 511 | let buf = trans.borrow().write_buffer_to_vec(); |
| 512 | |
| 513 | let expected: [u8; 16] = [0x80, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x74, 0x65, |
| 514 | 0x73, 0x74, 0x00, 0x00, 0x00, 0x01]; |
| 515 | |
| 516 | assert_eq!(&expected, buf.as_slice()); |
| 517 | } |
| 518 | |
| 519 | |
| 520 | #[test] |
| 521 | fn must_write_message_reply_begin() { |
| 522 | let (trans, _, mut o_prot) = test_objects(); |
| 523 | |
| 524 | let ident = TMessageIdentifier::new("test", TMessageType::Reply, 10); |
| 525 | assert!(o_prot.write_message_begin(&ident).is_ok()); |
| 526 | |
| 527 | let buf = trans.borrow().write_buffer_to_vec(); |
| 528 | |
| 529 | let expected: [u8; 16] = [0x80, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x74, 0x65, |
| 530 | 0x73, 0x74, 0x00, 0x00, 0x00, 0x0A]; |
| 531 | |
| 532 | assert_eq!(&expected, buf.as_slice()); |
| 533 | } |
| 534 | |
| 535 | #[test] |
| 536 | fn must_round_trip_strict_message_begin() { |
| 537 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 538 | |
| 539 | let sent_ident = TMessageIdentifier::new("test", TMessageType::Call, 1); |
| 540 | assert!(o_prot.write_message_begin(&sent_ident).is_ok()); |
| 541 | |
| 542 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 543 | |
| 544 | let received_ident = assert_success!(i_prot.read_message_begin()); |
| 545 | assert_eq!(&received_ident, &sent_ident); |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn must_write_message_end() { |
| 550 | assert_no_write(|o| o.write_message_end()); |
| 551 | } |
| 552 | |
| 553 | #[test] |
| 554 | fn must_write_struct_begin() { |
| 555 | assert_no_write(|o| o.write_struct_begin(&TStructIdentifier::new("foo"))); |
| 556 | } |
| 557 | |
| 558 | #[test] |
| 559 | fn must_write_struct_end() { |
| 560 | assert_no_write(|o| o.write_struct_end()); |
| 561 | } |
| 562 | |
| 563 | #[test] |
| 564 | fn must_write_field_begin() { |
| 565 | let (trans, _, mut o_prot) = test_objects(); |
| 566 | |
| 567 | assert!(o_prot.write_field_begin(&TFieldIdentifier::new("some_field", TType::String, 22)) |
| 568 | .is_ok()); |
| 569 | |
| 570 | let expected: [u8; 3] = [0x0B, 0x00, 0x16]; |
| 571 | let buf = trans.borrow().write_buffer_to_vec(); |
| 572 | assert_eq!(&expected, buf.as_slice()); |
| 573 | } |
| 574 | |
| 575 | #[test] |
| 576 | fn must_round_trip_field_begin() { |
| 577 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 578 | |
| 579 | let sent_field_ident = TFieldIdentifier::new("foo", TType::I64, 20); |
| 580 | assert!(o_prot.write_field_begin(&sent_field_ident).is_ok()); |
| 581 | |
| 582 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 583 | |
| 584 | let expected_ident = TFieldIdentifier { |
| 585 | name: None, |
| 586 | field_type: TType::I64, |
| 587 | id: Some(20), |
| 588 | }; // no name |
| 589 | let received_ident = assert_success!(i_prot.read_field_begin()); |
| 590 | assert_eq!(&received_ident, &expected_ident); |
| 591 | } |
| 592 | |
| 593 | #[test] |
| 594 | fn must_write_stop_field() { |
| 595 | let (trans, _, mut o_prot) = test_objects(); |
| 596 | |
| 597 | assert!(o_prot.write_field_stop().is_ok()); |
| 598 | |
| 599 | let expected: [u8; 1] = [0x00]; |
| 600 | let buf = trans.borrow().write_buffer_to_vec(); |
| 601 | assert_eq!(&expected, buf.as_slice()); |
| 602 | } |
| 603 | |
| 604 | #[test] |
| 605 | fn must_round_trip_field_stop() { |
| 606 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 607 | |
| 608 | assert!(o_prot.write_field_stop().is_ok()); |
| 609 | |
| 610 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 611 | |
| 612 | let expected_ident = TFieldIdentifier { |
| 613 | name: None, |
| 614 | field_type: TType::Stop, |
| 615 | id: Some(0), |
| 616 | }; // we get id 0 |
| 617 | |
| 618 | let received_ident = assert_success!(i_prot.read_field_begin()); |
| 619 | assert_eq!(&received_ident, &expected_ident); |
| 620 | } |
| 621 | |
| 622 | #[test] |
| 623 | fn must_write_field_end() { |
| 624 | assert_no_write(|o| o.write_field_end()); |
| 625 | } |
| 626 | |
| 627 | #[test] |
| 628 | fn must_write_list_begin() { |
| 629 | let (trans, _, mut o_prot) = test_objects(); |
| 630 | |
| 631 | assert!(o_prot.write_list_begin(&TListIdentifier::new(TType::Bool, 5)).is_ok()); |
| 632 | |
| 633 | let expected: [u8; 5] = [0x02, 0x00, 0x00, 0x00, 0x05]; |
| 634 | let buf = trans.borrow().write_buffer_to_vec(); |
| 635 | assert_eq!(&expected, buf.as_slice()); |
| 636 | } |
| 637 | |
| 638 | #[test] |
| 639 | fn must_round_trip_list_begin() { |
| 640 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 641 | |
| 642 | let ident = TListIdentifier::new(TType::List, 900); |
| 643 | assert!(o_prot.write_list_begin(&ident).is_ok()); |
| 644 | |
| 645 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 646 | |
| 647 | let received_ident = assert_success!(i_prot.read_list_begin()); |
| 648 | assert_eq!(&received_ident, &ident); |
| 649 | } |
| 650 | |
| 651 | #[test] |
| 652 | fn must_write_list_end() { |
| 653 | assert_no_write(|o| o.write_list_end()); |
| 654 | } |
| 655 | |
| 656 | #[test] |
| 657 | fn must_write_set_begin() { |
| 658 | let (trans, _, mut o_prot) = test_objects(); |
| 659 | |
| 660 | assert!(o_prot.write_set_begin(&TSetIdentifier::new(TType::I16, 7)).is_ok()); |
| 661 | |
| 662 | let expected: [u8; 5] = [0x06, 0x00, 0x00, 0x00, 0x07]; |
| 663 | let buf = trans.borrow().write_buffer_to_vec(); |
| 664 | assert_eq!(&expected, buf.as_slice()); |
| 665 | } |
| 666 | |
| 667 | #[test] |
| 668 | fn must_round_trip_set_begin() { |
| 669 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 670 | |
| 671 | let ident = TSetIdentifier::new(TType::I64, 2000); |
| 672 | assert!(o_prot.write_set_begin(&ident).is_ok()); |
| 673 | |
| 674 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 675 | |
| 676 | let received_ident_result = i_prot.read_set_begin(); |
| 677 | assert!(received_ident_result.is_ok()); |
| 678 | assert_eq!(&received_ident_result.unwrap(), &ident); |
| 679 | } |
| 680 | |
| 681 | #[test] |
| 682 | fn must_write_set_end() { |
| 683 | assert_no_write(|o| o.write_set_end()); |
| 684 | } |
| 685 | |
| 686 | #[test] |
| 687 | fn must_write_map_begin() { |
| 688 | let (trans, _, mut o_prot) = test_objects(); |
| 689 | |
| 690 | assert!(o_prot.write_map_begin(&TMapIdentifier::new(TType::I64, TType::Struct, 32)) |
| 691 | .is_ok()); |
| 692 | |
| 693 | let expected: [u8; 6] = [0x0A, 0x0C, 0x00, 0x00, 0x00, 0x20]; |
| 694 | let buf = trans.borrow().write_buffer_to_vec(); |
| 695 | assert_eq!(&expected, buf.as_slice()); |
| 696 | } |
| 697 | |
| 698 | #[test] |
| 699 | fn must_round_trip_map_begin() { |
| 700 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 701 | |
| 702 | let ident = TMapIdentifier::new(TType::Map, TType::Set, 100); |
| 703 | assert!(o_prot.write_map_begin(&ident).is_ok()); |
| 704 | |
| 705 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 706 | |
| 707 | let received_ident = assert_success!(i_prot.read_map_begin()); |
| 708 | assert_eq!(&received_ident, &ident); |
| 709 | } |
| 710 | |
| 711 | #[test] |
| 712 | fn must_write_map_end() { |
| 713 | assert_no_write(|o| o.write_map_end()); |
| 714 | } |
| 715 | |
| 716 | #[test] |
| 717 | fn must_write_bool_true() { |
| 718 | let (trans, _, mut o_prot) = test_objects(); |
| 719 | |
| 720 | assert!(o_prot.write_bool(true).is_ok()); |
| 721 | |
| 722 | let expected: [u8; 1] = [0x01]; |
| 723 | let buf = trans.borrow().write_buffer_to_vec(); |
| 724 | assert_eq!(&expected, buf.as_slice()); |
| 725 | } |
| 726 | |
| 727 | #[test] |
| 728 | fn must_write_bool_false() { |
| 729 | let (trans, _, mut o_prot) = test_objects(); |
| 730 | |
| 731 | assert!(o_prot.write_bool(false).is_ok()); |
| 732 | |
| 733 | let expected: [u8; 1] = [0x00]; |
| 734 | let buf = trans.borrow().write_buffer_to_vec(); |
| 735 | assert_eq!(&expected, buf.as_slice()); |
| 736 | } |
| 737 | |
| 738 | #[test] |
| 739 | fn must_read_bool_true() { |
| 740 | let (trans, mut i_prot, _) = test_objects(); |
| 741 | |
| 742 | trans.borrow_mut().set_readable_bytes(&[0x01]); |
| 743 | |
| 744 | let read_bool = assert_success!(i_prot.read_bool()); |
| 745 | assert_eq!(read_bool, true); |
| 746 | } |
| 747 | |
| 748 | #[test] |
| 749 | fn must_read_bool_false() { |
| 750 | let (trans, mut i_prot, _) = test_objects(); |
| 751 | |
| 752 | trans.borrow_mut().set_readable_bytes(&[0x00]); |
| 753 | |
| 754 | let read_bool = assert_success!(i_prot.read_bool()); |
| 755 | assert_eq!(read_bool, false); |
| 756 | } |
| 757 | |
| 758 | #[test] |
| 759 | fn must_allow_any_non_zero_value_to_be_interpreted_as_bool_true() { |
| 760 | let (trans, mut i_prot, _) = test_objects(); |
| 761 | |
| 762 | trans.borrow_mut().set_readable_bytes(&[0xAC]); |
| 763 | |
| 764 | let read_bool = assert_success!(i_prot.read_bool()); |
| 765 | assert_eq!(read_bool, true); |
| 766 | } |
| 767 | |
| 768 | #[test] |
| 769 | fn must_write_bytes() { |
| 770 | let (trans, _, mut o_prot) = test_objects(); |
| 771 | |
| 772 | let bytes: [u8; 10] = [0x0A, 0xCC, 0xD1, 0x84, 0x99, 0x12, 0xAB, 0xBB, 0x45, 0xDF]; |
| 773 | |
| 774 | assert!(o_prot.write_bytes(&bytes).is_ok()); |
| 775 | |
| 776 | let buf = trans.borrow().write_buffer_to_vec(); |
| 777 | assert_eq!(&buf[0..4], [0x00, 0x00, 0x00, 0x0A]); // length |
| 778 | assert_eq!(&buf[4..], bytes); // actual bytes |
| 779 | } |
| 780 | |
| 781 | #[test] |
| 782 | fn must_round_trip_bytes() { |
| 783 | let (trans, mut i_prot, mut o_prot) = test_objects(); |
| 784 | |
| 785 | let bytes: [u8; 25] = [0x20, 0xFD, 0x18, 0x84, 0x99, 0x12, 0xAB, 0xBB, 0x45, 0xDF, 0x34, |
| 786 | 0xDC, 0x98, 0xA4, 0x6D, 0xF3, 0x99, 0xB4, 0xB7, 0xD4, 0x9C, 0xA5, |
| 787 | 0xB3, 0xC9, 0x88]; |
| 788 | |
| 789 | assert!(o_prot.write_bytes(&bytes).is_ok()); |
| 790 | |
| 791 | trans.borrow_mut().copy_write_buffer_to_read_buffer(); |
| 792 | |
| 793 | let received_bytes = assert_success!(i_prot.read_bytes()); |
| 794 | assert_eq!(&received_bytes, &bytes); |
| 795 | } |
| 796 | |
| 797 | fn test_objects |
| 798 | () |
| 799 | -> (Rc<RefCell<Box<TBufferTransport>>>, TBinaryInputProtocol, TBinaryOutputProtocol) |
| 800 | { |
| 801 | let mem = Rc::new(RefCell::new(Box::new(TBufferTransport::with_capacity(40, 40)))); |
| 802 | |
| 803 | let inner: Box<TTransport> = Box::new(TPassThruTransport { inner: mem.clone() }); |
| 804 | let inner = Rc::new(RefCell::new(inner)); |
| 805 | |
| 806 | let i_prot = TBinaryInputProtocol::new(inner.clone(), true); |
| 807 | let o_prot = TBinaryOutputProtocol::new(inner.clone(), true); |
| 808 | |
| 809 | (mem, i_prot, o_prot) |
| 810 | } |
| 811 | |
| 812 | fn assert_no_write<F: FnMut(&mut TBinaryOutputProtocol) -> ::Result<()>>(mut write_fn: F) { |
| 813 | let (trans, _, mut o_prot) = test_objects(); |
| 814 | assert!(write_fn(&mut o_prot).is_ok()); |
| 815 | assert_eq!(trans.borrow().write_buffer_as_ref().len(), 0); |
| 816 | } |
| 817 | } |