vmm: api: Use 'BTreeMap' for 'HttpRoutes'

In this way, we get the values sorted by its key by default, which is
useful for the 'http_api' fuzzer.

Signed-off-by: Bo Chen <chen.bo@intel.com>
This commit is contained in:
Bo Chen 2022-07-29 12:52:45 -07:00 committed by Rob Bradford
parent e5155bab62
commit 1125fd2667
2 changed files with 5 additions and 10 deletions

View File

@ -14,13 +14,8 @@ use vmm::{EpollContext, EpollDispatch};
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
// Need to be ordered for test case reproducibility // Need to be ordered for test case reproducibility
static ROUTES: Lazy<Vec<&Box<dyn EndpointHandler + Sync + Send>>> = Lazy::new(|| { static ROUTES: Lazy<Vec<&Box<dyn EndpointHandler + Sync + Send>>> =
let mut keys: Vec<&String> = HTTP_ROUTES.routes.keys().collect(); Lazy::new(|| HTTP_ROUTES.routes.values().collect());
keys.sort();
keys.iter()
.map(|k| HTTP_ROUTES.routes.get(*k).unwrap())
.collect()
});
fuzz_target!(|bytes| { fuzz_target!(|bytes| {
if bytes.len() < 2 { if bytes.len() < 2 {

View File

@ -12,7 +12,7 @@ use micro_http::{Body, HttpServer, MediaType, Method, Request, Response, StatusC
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use seccompiler::{apply_filter, SeccompAction}; use seccompiler::{apply_filter, SeccompAction};
use serde_json::Error as SerdeError; use serde_json::Error as SerdeError;
use std::collections::HashMap; use std::collections::BTreeMap;
use std::fs::File; use std::fs::File;
use std::os::unix::io::{IntoRawFd, RawFd}; use std::os::unix::io::{IntoRawFd, RawFd};
use std::os::unix::net::UnixListener; use std::os::unix::net::UnixListener;
@ -122,7 +122,7 @@ pub trait EndpointHandler {
/// An HTTP routes structure. /// An HTTP routes structure.
pub struct HttpRoutes { pub struct HttpRoutes {
/// routes is a hash table mapping endpoint URIs to their endpoint handlers. /// routes is a hash table mapping endpoint URIs to their endpoint handlers.
pub routes: HashMap<String, Box<dyn EndpointHandler + Sync + Send>>, pub routes: BTreeMap<String, Box<dyn EndpointHandler + Sync + Send>>,
} }
macro_rules! endpoint { macro_rules! endpoint {
@ -134,7 +134,7 @@ macro_rules! endpoint {
/// HTTP_ROUTES contain all the cloud-hypervisor HTTP routes. /// HTTP_ROUTES contain all the cloud-hypervisor HTTP routes.
pub static HTTP_ROUTES: Lazy<HttpRoutes> = Lazy::new(|| { pub static HTTP_ROUTES: Lazy<HttpRoutes> = Lazy::new(|| {
let mut r = HttpRoutes { let mut r = HttpRoutes {
routes: HashMap::new(), routes: BTreeMap::new(),
}; };
r.routes.insert( r.routes.insert(