tests.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. use crate::iface::*;
  2. use crate::wire::*;
  3. pub(crate) fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, TestingDevice) {
  4. let mut device = TestingDevice::new(medium);
  5. let config = Config::new(match medium {
  6. #[cfg(feature = "medium-ethernet")]
  7. Medium::Ethernet => {
  8. HardwareAddress::Ethernet(EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]))
  9. }
  10. #[cfg(feature = "medium-ip")]
  11. Medium::Ip => HardwareAddress::Ip,
  12. #[cfg(feature = "medium-ieee802154")]
  13. Medium::Ieee802154 => HardwareAddress::Ieee802154(Ieee802154Address::Extended([
  14. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  15. ])),
  16. });
  17. let mut iface = Interface::new(config, &mut device, Instant::ZERO);
  18. #[cfg(feature = "proto-ipv4")]
  19. {
  20. iface.update_ip_addrs(|ip_addrs| {
  21. ip_addrs
  22. .push(IpCidr::new(IpAddress::v4(192, 168, 1, 1), 24))
  23. .unwrap();
  24. ip_addrs
  25. .push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
  26. .unwrap();
  27. });
  28. }
  29. #[cfg(feature = "proto-ipv6")]
  30. {
  31. iface.update_ip_addrs(|ip_addrs| {
  32. ip_addrs
  33. .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
  34. .unwrap();
  35. ip_addrs
  36. .push(IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128))
  37. .unwrap();
  38. ip_addrs
  39. .push(IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64))
  40. .unwrap();
  41. });
  42. }
  43. (iface, SocketSet::new(vec![]), device)
  44. }
  45. use heapless::Deque;
  46. use heapless::Vec;
  47. use crate::phy::{self, Device, DeviceCapabilities, Medium};
  48. use crate::time::Instant;
  49. /// A testing device.
  50. #[derive(Debug)]
  51. pub struct TestingDevice {
  52. pub(crate) queue: Deque<Vec<u8, 1514>, 4>,
  53. max_transmission_unit: usize,
  54. medium: Medium,
  55. }
  56. #[allow(clippy::new_without_default)]
  57. impl TestingDevice {
  58. /// Creates a testing device.
  59. ///
  60. /// Every packet transmitted through this device will be received through it
  61. /// in FIFO order.
  62. pub fn new(medium: Medium) -> Self {
  63. TestingDevice {
  64. queue: Deque::new(),
  65. max_transmission_unit: match medium {
  66. #[cfg(feature = "medium-ethernet")]
  67. Medium::Ethernet => 1514,
  68. #[cfg(feature = "medium-ip")]
  69. Medium::Ip => 1500,
  70. #[cfg(feature = "medium-ieee802154")]
  71. Medium::Ieee802154 => 1500,
  72. },
  73. medium,
  74. }
  75. }
  76. }
  77. impl Device for TestingDevice {
  78. type RxToken<'a> = RxToken;
  79. type TxToken<'a> = TxToken<'a>;
  80. fn capabilities(&self) -> DeviceCapabilities {
  81. DeviceCapabilities {
  82. medium: self.medium,
  83. max_transmission_unit: self.max_transmission_unit,
  84. ..DeviceCapabilities::default()
  85. }
  86. }
  87. fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
  88. self.queue.pop_front().map(move |buffer| {
  89. let rx = RxToken { buffer };
  90. let tx = TxToken {
  91. queue: &mut self.queue,
  92. };
  93. (rx, tx)
  94. })
  95. }
  96. fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
  97. Some(TxToken {
  98. queue: &mut self.queue,
  99. })
  100. }
  101. }
  102. #[doc(hidden)]
  103. pub struct RxToken {
  104. buffer: Vec<u8, 1514>,
  105. }
  106. impl phy::RxToken for RxToken {
  107. fn consume<R, F>(mut self, f: F) -> R
  108. where
  109. F: FnOnce(&mut [u8]) -> R,
  110. {
  111. f(&mut self.buffer)
  112. }
  113. }
  114. #[doc(hidden)]
  115. #[derive(Debug)]
  116. pub struct TxToken<'a> {
  117. queue: &'a mut Deque<Vec<u8, 1514>, 4>,
  118. }
  119. impl<'a> phy::TxToken for TxToken<'a> {
  120. fn consume<R, F>(self, len: usize, f: F) -> R
  121. where
  122. F: FnOnce(&mut [u8]) -> R,
  123. {
  124. let mut buffer = Vec::new();
  125. buffer.resize(len, 0).unwrap();
  126. let result = f(&mut buffer);
  127. self.queue.push_back(buffer).unwrap();
  128. result
  129. }
  130. }