Skip to content

Commit af6f472

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

File tree

2 files changed

+117
-0
lines changed

2 files changed

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

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)