Skip to content

Commit 335e28c

Browse files
committed
add test case for ip4config2 and http protocols.
Signed-off-by: Gerd Hoffmann <[email protected]>
1 parent 8af70c7 commit 335e28c

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
extern crate alloc;
4+
use alloc::vec::Vec;
5+
6+
use uefi::proto::device_path::text::{AllowShortcuts, DisplayOnly};
7+
use uefi::proto::device_path::DevicePath;
8+
use uefi::proto::network::http::{HttpBinding, HttpHelper};
9+
use uefi::proto::network::ip4config2::Ip4Config2;
10+
use uefi::{boot, Handle};
11+
12+
use uefi_raw::protocol::network::http::HttpStatusCode;
13+
14+
pub fn print_handle_devpath(prefix: &str, handle: &Handle) {
15+
let Ok(dp) = boot::open_protocol_exclusive::<DevicePath>(*handle) else {
16+
info!("{}no device path for handle", prefix);
17+
return;
18+
};
19+
if let Ok(string) = dp.to_string(DisplayOnly(true), AllowShortcuts(true)) {
20+
info!("{}{}", prefix, string);
21+
}
22+
}
23+
24+
fn fetch_http(handle: Handle, url: &str) -> Option<Vec<u8>> {
25+
info!("http: fetching {} ...", url);
26+
27+
let http_res = HttpHelper::new(handle);
28+
if let Err(e) = http_res {
29+
error!("http new: {}", e);
30+
return None;
31+
}
32+
let mut http = http_res.unwrap();
33+
34+
let res = http.configure();
35+
if let Err(e) = res {
36+
error!("http configure: {}", e);
37+
return None;
38+
}
39+
40+
let res = http.request_get(url);
41+
if let Err(e) = res {
42+
error!("http request: {}", e);
43+
return None;
44+
}
45+
46+
let res = http.response_first(true);
47+
if let Err(e) = res {
48+
error!("http response: {}", e);
49+
return None;
50+
}
51+
52+
let rsp = res.unwrap();
53+
if rsp.status != HttpStatusCode::STATUS_200_OK {
54+
error!("http server error: {:?}", rsp.status);
55+
return None;
56+
}
57+
let Some(cl_hdr) = rsp.headers.iter().find(|h| h.0 == "content-length") else {
58+
error!("no content length");
59+
return None;
60+
};
61+
let Ok(cl) = cl_hdr.1.parse::<usize>() else {
62+
error!("parse content length ({})", cl_hdr.1);
63+
return None;
64+
};
65+
info!("http: size is {} bytes", cl);
66+
67+
let mut data = rsp.body;
68+
loop {
69+
if data.len() >= cl {
70+
break;
71+
}
72+
73+
let res = http.response_more();
74+
if let Err(e) = res {
75+
error!("read response: {}", e);
76+
return None;
77+
}
78+
79+
let mut buf = res.unwrap();
80+
data.append(&mut buf);
81+
}
82+
83+
Some(data)
84+
}
85+
86+
pub fn test() {
87+
info!("Testing ip4 config2 + http protocols");
88+
89+
let Ok(handles) = boot::locate_handle_buffer(boot::SearchType::from_proto::<HttpBinding>())
90+
else {
91+
info!("No NICs found.");
92+
return;
93+
};
94+
95+
for h in handles.as_ref() {
96+
print_handle_devpath("nic: ", h);
97+
98+
info!("Bring up interface (ip4 config2 protocol)");
99+
let mut ip4 = Ip4Config2::new(*h).expect("open ip4 config2 protocol");
100+
ip4.ifup(true).expect("acquire ipv4 address");
101+
102+
// hard to find web sites which still allow plain http these days ...
103+
info!("Testing HTTP");
104+
let Some(_) = fetch_http(*h, "http://boot.netboot.xyz/robots.txt") else {
105+
// network can be flaky, so not assert
106+
info!("FAILED");
107+
return;
108+
};
109+
110+
info!("Testing HTTPS");
111+
let Some(_) = fetch_http(*h, "https://boot.netboot.xyz/robots.txt") else {
112+
// network can be flaky, so not assert
113+
info!("FAILED");
114+
return;
115+
};
116+
117+
info!("PASSED");
118+
}
119+
}

uefi-test-runner/src/proto/network/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
pub fn test() {
44
info!("Testing Network protocols");
55

6+
http::test();
67
pxe::test();
78
snp::test();
89
}
910

11+
mod http;
1012
mod pxe;
1113
mod snp;

0 commit comments

Comments
 (0)