Skip to content

Commit a758177

Browse files
authored
chore: Guard testing functions properly (#2264)
* chore: Guard testing functions properly We have a bunch of functions that in their doc comments say that they are for testing purposes only. This commit adds guards to those functions to make sure they are not used in production code. * More
1 parent 9143d73 commit a758177

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

neqo-common/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ regex = { workspace = true }
3737
bench = ["test-fixture/bench"]
3838
build-fuzzing-corpus = ["hex"]
3939
ci = []
40+
test-fixture = []
4041

4142
[lib]
4243
# See https://github.com/bheisler/criterion.rs/blob/master/book/src/faq.md#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options

neqo-common/src/codec.rs

+5
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,23 @@ impl<'a> Decoder<'a> {
4444
}
4545

4646
/// Skip helper that panics if `n` is `None` or not able to fit in `usize`.
47+
/// Only use this for tests because we panic rather than reporting a result.
48+
#[cfg(any(test, feature = "test-fixture"))]
4749
fn skip_inner(&mut self, n: Option<u64>) {
4850
self.skip(usize::try_from(n.expect("invalid length")).unwrap());
4951
}
5052

5153
/// Skip a vector. Panics if there isn't enough space.
5254
/// Only use this for tests because we panic rather than reporting a result.
55+
#[cfg(any(test, feature = "test-fixture"))]
5356
pub fn skip_vec(&mut self, n: usize) {
5457
let len = self.decode_uint(n);
5558
self.skip_inner(len);
5659
}
5760

5861
/// Skip a variable length vector. Panics if there isn't enough space.
5962
/// Only use this for tests because we panic rather than reporting a result.
63+
#[cfg(any(test, feature = "test-fixture"))]
6064
pub fn skip_vvec(&mut self) {
6165
let len = self.decode_varint();
6266
self.skip_inner(len);
@@ -272,6 +276,7 @@ impl Encoder {
272276
/// # Panics
273277
///
274278
/// When `s` contains non-hex values or an odd number of values.
279+
#[cfg(any(test, feature = "test-fixture"))]
275280
#[must_use]
276281
pub fn from_hex(s: impl AsRef<str>) -> Self {
277282
let s = s.as_ref();

neqo-qpack/src/encoder_instructions.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,33 @@ use crate::{
1818
Res,
1919
};
2020

21-
// The encoder only uses InsertWithNameLiteral, therefore clippy is complaining about dead_code.
22-
// We may decide to use othe instruction in the future.
23-
// All instructions are used for testing, therefore they are defined.
24-
#[allow(dead_code)]
21+
// The encoder only uses InsertWithNameLiteral.
22+
// All instructions are used for testing, therefore they are guarded with `#[cfg(test)]`.
2523
#[derive(Debug, PartialEq, Eq)]
2624
pub enum EncoderInstruction<'a> {
27-
Capacity { value: u64 },
28-
InsertWithNameRefStatic { index: u64, value: &'a [u8] },
29-
InsertWithNameRefDynamic { index: u64, value: &'a [u8] },
30-
InsertWithNameLiteral { name: &'a [u8], value: &'a [u8] },
31-
Duplicate { index: u64 },
25+
Capacity {
26+
value: u64,
27+
},
28+
#[cfg(test)]
29+
InsertWithNameRefStatic {
30+
index: u64,
31+
value: &'a [u8],
32+
},
33+
#[cfg(test)]
34+
InsertWithNameRefDynamic {
35+
index: u64,
36+
value: &'a [u8],
37+
},
38+
InsertWithNameLiteral {
39+
name: &'a [u8],
40+
value: &'a [u8],
41+
},
42+
#[cfg(test)]
43+
Duplicate {
44+
index: u64,
45+
},
46+
#[cfg(test)]
47+
#[allow(dead_code)]
3248
NoInstruction,
3349
}
3450

@@ -38,10 +54,12 @@ impl EncoderInstruction<'_> {
3854
Self::Capacity { value } => {
3955
enc.encode_prefixed_encoded_int(ENCODER_CAPACITY, *value);
4056
}
57+
#[cfg(test)]
4158
Self::InsertWithNameRefStatic { index, value } => {
4259
enc.encode_prefixed_encoded_int(ENCODER_INSERT_WITH_NAME_REF_STATIC, *index);
4360
enc.encode_literal(use_huffman, NO_PREFIX, value);
4461
}
62+
#[cfg(test)]
4563
Self::InsertWithNameRefDynamic { index, value } => {
4664
enc.encode_prefixed_encoded_int(ENCODER_INSERT_WITH_NAME_REF_DYNAMIC, *index);
4765
enc.encode_literal(use_huffman, NO_PREFIX, value);
@@ -50,9 +68,11 @@ impl EncoderInstruction<'_> {
5068
enc.encode_literal(use_huffman, ENCODER_INSERT_WITH_NAME_LITERAL, name);
5169
enc.encode_literal(use_huffman, NO_PREFIX, value);
5270
}
71+
#[cfg(test)]
5372
Self::Duplicate { index } => {
5473
enc.encode_prefixed_encoded_int(ENCODER_DUPLICATE, *index);
5574
}
75+
#[cfg(test)]
5676
Self::NoInstruction => {}
5777
}
5878
}
@@ -81,12 +101,14 @@ impl<'a> From<&'a EncoderInstruction<'a>> for DecodedEncoderInstruction {
81101
fn from(inst: &'a EncoderInstruction) -> Self {
82102
match inst {
83103
EncoderInstruction::Capacity { value } => Self::Capacity { value: *value },
104+
#[cfg(test)]
84105
EncoderInstruction::InsertWithNameRefStatic { index, value } => {
85106
Self::InsertWithNameRefStatic {
86107
index: *index,
87108
value: value.to_vec(),
88109
}
89110
}
111+
#[cfg(test)]
90112
EncoderInstruction::InsertWithNameRefDynamic { index, value } => {
91113
Self::InsertWithNameRefDynamic {
92114
index: *index,
@@ -99,7 +121,9 @@ impl<'a> From<&'a EncoderInstruction<'a>> for DecodedEncoderInstruction {
99121
value: value.to_vec(),
100122
}
101123
}
124+
#[cfg(test)]
102125
EncoderInstruction::Duplicate { index } => Self::Duplicate { index: *index },
126+
#[cfg(test)]
103127
EncoderInstruction::NoInstruction => Self::NoInstruction,
104128
}
105129
}

test-fixture/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ workspace = true
1818
[dependencies]
1919
# Checked against https://searchfox.org/mozilla-central/source/Cargo.lock 2024-11-11
2020
log = { workspace = true }
21-
neqo-common = { path = "../neqo-common" }
21+
neqo-common = { path = "../neqo-common", features = ["test-fixture"] }
2222
neqo-crypto = { path = "../neqo-crypto" }
2323
neqo-http3 = { path = "../neqo-http3" }
2424
neqo-transport = { path = "../neqo-transport" }

0 commit comments

Comments
 (0)