summaryrefslogtreecommitdiffstats
path: root/third_party/rust/neqo-common/src
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/neqo-common/src')
-rw-r--r--third_party/rust/neqo-common/src/datagram.rs2
-rw-r--r--third_party/rust/neqo-common/src/fuzz.rs43
-rw-r--r--third_party/rust/neqo-common/src/lib.rs4
-rw-r--r--third_party/rust/neqo-common/src/tos.rs27
4 files changed, 75 insertions, 1 deletions
diff --git a/third_party/rust/neqo-common/src/datagram.rs b/third_party/rust/neqo-common/src/datagram.rs
index cc2cb7d113..2f2bb53766 100644
--- a/third_party/rust/neqo-common/src/datagram.rs
+++ b/third_party/rust/neqo-common/src/datagram.rs
@@ -95,6 +95,6 @@ fn fmt_datagram() {
let d = datagram([0; 1].to_vec());
assert_eq!(
&format!("{d:?}"),
- "Datagram IpTos(Cs0, NotEct) TTL Some(128) [fe80::1]:443->[fe80::1]:443: [1]: 00"
+ "Datagram IpTos(Cs0, Ect0) TTL Some(128) [fe80::1]:443->[fe80::1]:443: [1]: 00"
);
}
diff --git a/third_party/rust/neqo-common/src/fuzz.rs b/third_party/rust/neqo-common/src/fuzz.rs
new file mode 100644
index 0000000000..d0a35a49ae
--- /dev/null
+++ b/third_party/rust/neqo-common/src/fuzz.rs
@@ -0,0 +1,43 @@
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::{
+ collections::hash_map::DefaultHasher,
+ fs::File,
+ hash::{Hash, Hasher},
+ io::Write,
+ path::Path,
+};
+
+/// Write a data item `data` for the fuzzing target `target` to the fuzzing corpus. The caller needs
+/// to make sure that `target` is the correct fuzzing target name for the data written.
+///
+/// # Panics
+///
+/// Panics if the corpus directory does not exist or if the corpus item cannot be written.
+pub fn write_item_to_fuzzing_corpus(target: &str, data: &[u8]) {
+ // This bakes in the assumption that we're executing in the root of the neqo workspace.
+ // Unfortunately, `cargo fuzz` doesn't provide a way to learn the location of the corpus
+ // directory.
+ let corpus = Path::new("../fuzz/corpus").join(target);
+ if !corpus.exists() {
+ std::fs::create_dir_all(&corpus).expect("failed to create corpus directory");
+ }
+
+ // Hash the data to get a unique name for the corpus item.
+ let mut hasher = DefaultHasher::new();
+ data.hash(&mut hasher);
+ let item_name = hex::encode(hasher.finish().to_be_bytes());
+ let item_path = corpus.join(item_name);
+ if item_path.exists() {
+ // Don't overwrite existing corpus items.
+ return;
+ }
+
+ // Write the data to the corpus item.
+ let mut file = File::create(item_path).expect("failed to create corpus item");
+ Write::write_all(&mut file, data).expect("failed to write to corpus item");
+}
diff --git a/third_party/rust/neqo-common/src/lib.rs b/third_party/rust/neqo-common/src/lib.rs
index e988c6071d..3a72425e44 100644
--- a/third_party/rust/neqo-common/src/lib.rs
+++ b/third_party/rust/neqo-common/src/lib.rs
@@ -9,6 +9,8 @@
mod codec;
mod datagram;
pub mod event;
+#[cfg(feature = "build-fuzzing-corpus")]
+mod fuzz;
pub mod header;
pub mod hrtime;
mod incrdecoder;
@@ -21,6 +23,8 @@ use std::fmt::Write;
use enum_map::Enum;
+#[cfg(feature = "build-fuzzing-corpus")]
+pub use self::fuzz::write_item_to_fuzzing_corpus;
pub use self::{
codec::{Decoder, Encoder},
datagram::Datagram,
diff --git a/third_party/rust/neqo-common/src/tos.rs b/third_party/rust/neqo-common/src/tos.rs
index 533c5447e2..c7e5228dee 100644
--- a/third_party/rust/neqo-common/src/tos.rs
+++ b/third_party/rust/neqo-common/src/tos.rs
@@ -52,6 +52,16 @@ impl From<IpTos> for IpTosEcn {
}
}
+impl IpTosEcn {
+ #[must_use]
+ pub fn is_ecn_marked(&self) -> bool {
+ match self {
+ IpTosEcn::Ect0 | IpTosEcn::Ect1 | IpTosEcn::Ce => true,
+ IpTosEcn::NotEct => false,
+ }
+ }
+}
+
/// Diffserv Codepoints, mapped to the upper six bits of the TOS field.
/// <https://www.iana.org/assignments/dscp-registry/dscp-registry.xhtml>
#[derive(Copy, Clone, PartialEq, Eq, Enum, Default, Debug)]
@@ -228,6 +238,11 @@ impl IpTos {
pub fn set_dscp(&mut self, dscp: IpTosDscp) {
self.0 = u8::from(IpTosEcn::from(*self)) | u8::from(dscp);
}
+
+ #[must_use]
+ pub fn is_ecn_marked(&self) -> bool {
+ IpTosEcn::from(*self).is_ecn_marked()
+ }
}
#[cfg(test)]
@@ -346,4 +361,16 @@ mod tests {
iptos.set_dscp(IpTosDscp::Le);
assert_eq!(u8::from(iptos), 0b0000_0101);
}
+
+ #[test]
+ fn iptos_is_ecn_marked() {
+ let iptos: IpTos = (IpTosDscp::Af41, IpTosEcn::Ce).into();
+ assert!(iptos.is_ecn_marked());
+ }
+
+ #[test]
+ fn iptosecn_is_ecn_marked() {
+ assert!(IpTosEcn::Ce.is_ecn_marked());
+ assert!(!IpTosEcn::NotEct.is_ecn_marked());
+ }
}