Skip to content

Commit 36f9071

Browse files
committed
fix(p2p): fix broken doc tests in webrtc module
Fix 11 doc tests that never compiled: wrong crate name (mina:: -> mina_p2p::), missing imports, undefined variables, and missing error handling wrappers. All doc tests are now self-contained and pass.
1 parent 162c63d commit 36f9071

File tree

3 files changed

+135
-27
lines changed

3 files changed

+135
-27
lines changed

crates/p2p/src/webrtc/connection_auth.rs

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,29 @@ use super::{Answer, Offer};
8888
/// ## Usage
8989
///
9090
/// ```rust
91-
/// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer};
91+
/// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
92+
/// use mina_p2p::identity::SecretKey;
93+
/// use rand::thread_rng;
94+
///
95+
/// let sk_a = SecretKey::rand();
96+
/// let sk_b = SecretKey::rand();
97+
/// let offer = Offer {
98+
/// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
99+
/// chain_id: mina_core::DEVNET_CHAIN_ID,
100+
/// identity_pub_key: sk_a.public_key(),
101+
/// target_peer_id: sk_b.public_key().peer_id(),
102+
/// host: Host::Domain("localhost".into()),
103+
/// listen_port: Some(8080),
104+
/// };
105+
/// let answer = Answer {
106+
/// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
107+
/// identity_pub_key: sk_b.public_key(),
108+
/// target_peer_id: sk_a.public_key().peer_id(),
109+
/// };
92110
///
93111
/// let connection_auth = ConnectionAuth::new(&offer, &answer);
94-
/// let encrypted_auth = connection_auth.encrypt(&my_secret_key, &peer_public_key, rng)?;
112+
/// let encrypted_auth = connection_auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng());
113+
/// assert!(encrypted_auth.is_some());
95114
/// ```
96115
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
97116
pub struct ConnectionAuth(Vec<u8>);
@@ -124,9 +143,32 @@ pub struct ConnectionAuth(Vec<u8>);
124143
/// ## Example
125144
///
126145
/// ```rust
146+
/// use mina_p2p::webrtc::{ConnectionAuth, ConnectionAuthEncrypted, Offer, Answer, Host};
147+
/// use mina_p2p::identity::SecretKey;
148+
/// use rand::thread_rng;
149+
///
150+
/// let sk_a = SecretKey::rand();
151+
/// let sk_b = SecretKey::rand();
152+
/// let offer = Offer {
153+
/// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
154+
/// chain_id: mina_core::DEVNET_CHAIN_ID,
155+
/// identity_pub_key: sk_a.public_key(),
156+
/// target_peer_id: sk_b.public_key().peer_id(),
157+
/// host: Host::Domain("localhost".into()),
158+
/// listen_port: Some(8080),
159+
/// };
160+
/// let answer = Answer {
161+
/// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
162+
/// identity_pub_key: sk_b.public_key(),
163+
/// target_peer_id: sk_a.public_key().peer_id(),
164+
/// };
165+
///
166+
/// let auth = ConnectionAuth::new(&offer, &answer);
167+
/// let encrypted_auth = auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng()).unwrap();
168+
///
127169
/// // After receiving encrypted authentication data
128-
/// let decrypted_auth = encrypted_auth.decrypt(&my_secret_key, &peer_public_key)?;
129-
/// // Verify that the decrypted data matches expected values
170+
/// let decrypted_auth = encrypted_auth.decrypt(&sk_b, &sk_a.public_key());
171+
/// assert!(decrypted_auth.is_some());
130172
/// ```
131173
#[derive(Debug, Clone)]
132174
pub struct ConnectionAuthEncrypted(Box<[u8; 92]>);
@@ -158,7 +200,24 @@ impl ConnectionAuth {
158200
/// # Example
159201
///
160202
/// ```rust
161-
/// use mina_p2p::webrtc::ConnectionAuth;
203+
/// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
204+
/// use mina_p2p::identity::SecretKey;
205+
///
206+
/// let sk_a = SecretKey::rand();
207+
/// let sk_b = SecretKey::rand();
208+
/// let offer = Offer {
209+
/// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
210+
/// chain_id: mina_core::DEVNET_CHAIN_ID,
211+
/// identity_pub_key: sk_a.public_key(),
212+
/// target_peer_id: sk_b.public_key().peer_id(),
213+
/// host: Host::Domain("localhost".into()),
214+
/// listen_port: Some(8080),
215+
/// };
216+
/// let answer = Answer {
217+
/// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
218+
/// identity_pub_key: sk_b.public_key(),
219+
/// target_peer_id: sk_a.public_key().peer_id(),
220+
/// };
162221
///
163222
/// let auth = ConnectionAuth::new(&offer, &answer);
164223
/// // Use auth for connection verification
@@ -196,10 +255,28 @@ impl ConnectionAuth {
196255
/// # Example
197256
///
198257
/// ```rust
258+
/// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
259+
/// use mina_p2p::identity::SecretKey;
199260
/// use rand::thread_rng;
200261
///
201-
/// let mut rng = thread_rng();
202-
/// let encrypted_auth = connection_auth.encrypt(&my_secret_key, &peer_public_key, &mut rng);
262+
/// let sk_a = SecretKey::rand();
263+
/// let sk_b = SecretKey::rand();
264+
/// let offer = Offer {
265+
/// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
266+
/// chain_id: mina_core::DEVNET_CHAIN_ID,
267+
/// identity_pub_key: sk_a.public_key(),
268+
/// target_peer_id: sk_b.public_key().peer_id(),
269+
/// host: Host::Domain("localhost".into()),
270+
/// listen_port: Some(8080),
271+
/// };
272+
/// let answer = Answer {
273+
/// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
274+
/// identity_pub_key: sk_b.public_key(),
275+
/// target_peer_id: sk_a.public_key().peer_id(),
276+
/// };
277+
///
278+
/// let connection_auth = ConnectionAuth::new(&offer, &answer);
279+
/// let encrypted_auth = connection_auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng());
203280
///
204281
/// if let Some(encrypted) = encrypted_auth {
205282
/// // Send encrypted authentication data to peer
@@ -254,8 +331,31 @@ impl ConnectionAuthEncrypted {
254331
/// # Example
255332
///
256333
/// ```rust
334+
/// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
335+
/// use mina_p2p::identity::SecretKey;
336+
/// use rand::thread_rng;
337+
///
338+
/// let sk_a = SecretKey::rand();
339+
/// let sk_b = SecretKey::rand();
340+
/// let offer = Offer {
341+
/// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
342+
/// chain_id: mina_core::DEVNET_CHAIN_ID,
343+
/// identity_pub_key: sk_a.public_key(),
344+
/// target_peer_id: sk_b.public_key().peer_id(),
345+
/// host: Host::Domain("localhost".into()),
346+
/// listen_port: Some(8080),
347+
/// };
348+
/// let answer = Answer {
349+
/// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
350+
/// identity_pub_key: sk_b.public_key(),
351+
/// target_peer_id: sk_a.public_key().peer_id(),
352+
/// };
353+
///
354+
/// let auth = ConnectionAuth::new(&offer, &answer);
355+
/// let encrypted_auth = auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng()).unwrap();
356+
///
257357
/// // After receiving encrypted authentication data from peer
258-
/// if let Some(decrypted_auth) = encrypted_auth.decrypt(&my_secret_key, &peer_public_key) {
358+
/// if let Some(decrypted_auth) = encrypted_auth.decrypt(&sk_b, &sk_a.public_key()) {
259359
/// // Authentication successful, proceed with connection
260360
/// println!("Peer authentication verified");
261361
/// } else {

crates/p2p/src/webrtc/signaling_method/http.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ use super::SignalingMethodParseError;
5858
///
5959
/// # Examples
6060
///
61-
/// ```
62-
/// use mina::webrtc::Host;
63-
/// use mina::signaling_method::HttpSignalingInfo;
61+
/// ```rust
62+
/// use mina_p2p::webrtc::{Host, HttpSignalingInfo};
6463
///
6564
/// // IPv4 signaling server
6665
/// let info = HttpSignalingInfo {
67-
/// host: Host::Ipv4("192.168.1.100".parse()?),
66+
/// host: Host::Ipv4("192.168.1.100".parse().unwrap()),
6867
/// port: 8080,
6968
/// };
7069
///
@@ -118,7 +117,9 @@ impl From<([u8; 4], u16)> for HttpSignalingInfo {
118117
///
119118
/// # Example
120119
///
121-
/// ```
120+
/// ```rust
121+
/// use mina_p2p::webrtc::HttpSignalingInfo;
122+
///
122123
/// let info = HttpSignalingInfo::from(([192, 168, 1, 100], 8080));
123124
/// assert_eq!(info.port, 8080);
124125
/// ```
@@ -147,17 +148,17 @@ impl FromStr for HttpSignalingInfo {
147148
///
148149
/// # Examples
149150
///
150-
/// ```
151-
/// use mina::signaling_method::HttpSignalingInfo;
151+
/// ```rust
152+
/// use mina_p2p::webrtc::HttpSignalingInfo;
152153
///
153154
/// // Domain and port
154-
/// let info: HttpSignalingInfo = "signal.example.com/443".parse()?;
155+
/// let info: HttpSignalingInfo = "signal.example.com/443".parse().unwrap();
155156
///
156157
/// // IPv4 and port
157-
/// let info: HttpSignalingInfo = "192.168.1.100/8080".parse()?;
158+
/// let info: HttpSignalingInfo = "192.168.1.100/8080".parse().unwrap();
158159
///
159160
/// // With leading slash
160-
/// let info: HttpSignalingInfo = "/localhost/8080".parse()?;
161+
/// let info: HttpSignalingInfo = "/localhost/8080".parse().unwrap();
161162
/// ```
162163
///
163164
/// # Errors

crates/p2p/src/webrtc/signaling_method/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ impl BinProtWrite for PathPrefix {
172172
///
173173
/// # Example
174174
///
175-
/// ```
175+
/// ```rust
176+
/// use mina_p2p::webrtc::SignalingMethod;
177+
/// use mina_p2p::identity::SecretKey;
178+
///
176179
/// // Direct HTTPS signaling
177-
/// let method = "/https/signal.example.com/443".parse::<SignalingMethod>()?;
180+
/// let method: SignalingMethod = "/https/signal.example.com/443".parse().unwrap();
178181
///
179182
/// // P2P relay through an existing peer
180-
/// let method = SignalingMethod::P2p { relay_peer_id: peer_id };
183+
/// let relay_peer_id = SecretKey::rand().public_key().peer_id();
184+
/// let method = SignalingMethod::P2p { relay_peer_id };
181185
/// ```
182186
#[derive(BinProtWrite, BinProtRead, Eq, PartialEq, Ord, PartialOrd, Debug, Clone)]
183187
pub enum SignalingMethod {
@@ -261,7 +265,10 @@ impl SignalingMethod {
261265
///
262266
/// # Example
263267
///
264-
/// ```
268+
/// ```rust
269+
/// use mina_p2p::webrtc::{SignalingMethod, HttpSignalingInfo, Host};
270+
///
271+
/// let info = HttpSignalingInfo { host: Host::Domain("signal.example.com".into()), port: 443 };
265272
/// let method = SignalingMethod::Https(info);
266273
/// let url = method.http_url(); // Some("https://signal.example.com:443/mina/webrtc/signal")
267274
/// ```
@@ -431,17 +438,17 @@ impl FromStr for SignalingMethod {
431438
///
432439
/// # Examples
433440
///
434-
/// ```
435-
/// use mina::signaling_method::SignalingMethod;
441+
/// ```rust
442+
/// use mina_p2p::webrtc::SignalingMethod;
436443
///
437444
/// // HTTP signaling
438-
/// let method: SignalingMethod = "/http/localhost/8080".parse()?;
445+
/// let method: SignalingMethod = "/http/localhost/8080".parse().unwrap();
439446
///
440447
/// // HTTPS signaling
441-
/// let method: SignalingMethod = "/https/signal.example.com/443".parse()?;
448+
/// let method: SignalingMethod = "/https/signal.example.com/443".parse().unwrap();
442449
///
443450
/// // HTTPS proxy with cluster ID
444-
/// let method: SignalingMethod = "/https_proxy/123/proxy.example.com/443".parse()?;
451+
/// let method: SignalingMethod = "/https_proxy/123/proxy.example.com/443".parse().unwrap();
445452
/// ```
446453
///
447454
/// # Errors

0 commit comments

Comments
 (0)