1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::internal::hci::{
16 packets::{Event, EventBuilder, EventCode, Sco},
17 parse_with_expected_packet_type, prepend_packet_type, Error, Packet, PacketType,
18 PacketTypeParseError, WithPacketType,
19 };
20 use bytes::Bytes;
21
22 #[test]
prepends_packet_type()23 fn prepends_packet_type() {
24 let packet_type = PacketType::Event;
25 let actual = prepend_packet_type(packet_type, FakePacket { bytes: vec![0xFF] });
26 assert_eq!(vec![0x04, 0xFF], actual);
27 }
28
29 #[test]
parse_empty_slice_should_error()30 fn parse_empty_slice_should_error() {
31 let actual = parse_with_expected_packet_type(FakePacket::parse, PacketType::Event, &[]);
32 assert_eq!(Err(PacketTypeParseError::EmptySlice), actual);
33 }
34
35 #[test]
parse_invalid_packet_type_should_error()36 fn parse_invalid_packet_type_should_error() {
37 let actual = parse_with_expected_packet_type(FakePacket::parse, PacketType::Event, &[0xFF]);
38 assert_eq!(
39 Err(PacketTypeParseError::InvalidPacketType { value: 0xFF }),
40 actual
41 );
42 }
43
44 #[test]
parse_mismatched_packet_type_should_error()45 fn parse_mismatched_packet_type_should_error() {
46 let actual = parse_with_expected_packet_type(FakePacket::parse, PacketType::Acl, &[0x01]);
47 assert_eq!(
48 Err(PacketTypeParseError::PacketTypeMismatch {
49 expected: PacketType::Acl,
50 actual: PacketType::Command
51 }),
52 actual
53 );
54 }
55
56 #[test]
parse_invalid_packet_should_error()57 fn parse_invalid_packet_should_error() {
58 let actual = parse_with_expected_packet_type(Sco::parse, PacketType::Sco, &[0x03]);
59 assert!(actual.is_err());
60 }
61
62 #[test]
test_packet_roundtrip_with_type()63 fn test_packet_roundtrip_with_type() {
64 let event_packet = EventBuilder {
65 event_code: EventCode::InquiryComplete,
66 payload: None,
67 }
68 .build();
69 let event_packet_bytes = event_packet.clone().to_vec_with_packet_type();
70 let actual =
71 parse_with_expected_packet_type(Event::parse, PacketType::Event, &event_packet_bytes)
72 .unwrap();
73 assert_eq!(event_packet, actual);
74 }
75
76 #[derive(Debug, PartialEq)]
77 struct FakePacket {
78 bytes: Vec<u8>,
79 }
80
81 impl FakePacket {
parse(bytes: &[u8]) -> Result<Self, Error>82 fn parse(bytes: &[u8]) -> Result<Self, Error> {
83 Ok(Self {
84 bytes: bytes.to_vec(),
85 })
86 }
87 }
88
89 impl Packet for FakePacket {
to_bytes(self) -> Bytes90 fn to_bytes(self) -> Bytes {
91 Bytes::new()
92 }
93
to_vec(self) -> Vec<u8>94 fn to_vec(self) -> Vec<u8> {
95 self.bytes
96 }
97 }
98