Skip to content

Commit 1f1da97

Browse files
committed
verifier: Refactor output parsing code for ops that return u32s.
1 parent fb3dec3 commit 1f1da97

File tree

1 file changed

+40
-62
lines changed

1 file changed

+40
-62
lines changed

verifier/src/main.rs

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use env_logger::Builder;
88
use log::{debug, LevelFilter};
99
use std::{
1010
fmt::{self, Debug, Formatter},
11-
process::Command,
11+
process::{Command, Output},
1212
};
1313

1414
/// Execute HIF operations exposed by the RoT Attest task.
@@ -98,27 +98,41 @@ impl AttestHiffy {
9898
/// to be decimal. Currently this function ignores the interface and
9999
/// operation names from the string. Future work may check that these are
100100
/// consistent with the operation executed.
101-
fn u32_from_stdout(output: &[u8]) -> Result<u32> {
102-
// check interface & operation name?
103-
let output = String::from_utf8_lossy(output);
104-
let output: Vec<&str> = output.trim().split(' ').collect();
105-
let output = output[output.len() - 1];
106-
debug!("output: {}", output);
107-
108-
let (output, radix) = match output.strip_prefix("0x") {
109-
Some(s) => {
110-
debug!("prefix stripped: \"{}\"", s);
111-
(s, 16)
112-
}
113-
None => (output, 10),
114-
};
115-
let log_len = u32::from_str_radix(output, 16).with_context(|| {
116-
format!("Failed to parse \"{}\" as base {} u32", output, radix)
117-
})?;
118-
119-
debug!("output u32: {}", log_len);
120-
121-
Ok(log_len)
101+
fn u32_from_cmd_output(output: Output) -> Result<u32> {
102+
if output.status.success() {
103+
// check interface & operation name?
104+
let output = String::from_utf8_lossy(&output.stdout);
105+
let output: Vec<&str> = output.trim().split(' ').collect();
106+
let output = output[output.len() - 1];
107+
debug!("output: {}", output);
108+
109+
let (output, radix) = match output.strip_prefix("0x") {
110+
Some(s) => {
111+
debug!("prefix stripped: \"{}\"", s);
112+
(s, 16)
113+
}
114+
None => (output, 10),
115+
};
116+
117+
let log_len =
118+
u32::from_str_radix(output, 16).with_context(|| {
119+
format!(
120+
"Failed to parse \"{}\" as base {} u32",
121+
output, radix
122+
)
123+
})?;
124+
125+
debug!("output u32: {}", log_len);
126+
127+
Ok(log_len)
128+
} else {
129+
Err(anyhow!(
130+
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
131+
output.status,
132+
String::from_utf8_lossy(&output.stdout),
133+
String::from_utf8_lossy(&output.stderr)
134+
))
135+
}
122136
}
123137

124138
/// Get length of the certificate chain from the Attest task. This cert
@@ -135,16 +149,7 @@ impl AttestHiffy {
135149
debug!("executing command: {:?}", cmd);
136150

137151
let output = cmd.output()?;
138-
if output.status.success() {
139-
Self::u32_from_stdout(&output.stdout)
140-
} else {
141-
Err(anyhow!(
142-
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
143-
output.status,
144-
String::from_utf8_lossy(&output.stdout),
145-
String::from_utf8_lossy(&output.stderr)
146-
))
147-
}
152+
Self::u32_from_cmd_output(output)
148153
}
149154

150155
/// Get length of the certificate at the provided index in bytes.
@@ -159,16 +164,7 @@ impl AttestHiffy {
159164
debug!("executing command: {:?}", cmd);
160165

161166
let output = cmd.output()?;
162-
if output.status.success() {
163-
Self::u32_from_stdout(&output.stdout)
164-
} else {
165-
Err(anyhow!(
166-
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
167-
output.status,
168-
String::from_utf8_lossy(&output.stdout),
169-
String::from_utf8_lossy(&output.stderr)
170-
))
171-
}
167+
Self::u32_from_cmd_output(output)
172168
}
173169

174170
/// Get length of the measurement log in bytes.
@@ -181,16 +177,7 @@ impl AttestHiffy {
181177
debug!("executing command: {:?}", cmd);
182178

183179
let output = cmd.output()?;
184-
if output.status.success() {
185-
Self::u32_from_stdout(&output.stdout)
186-
} else {
187-
Err(anyhow!(
188-
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
189-
output.status,
190-
String::from_utf8_lossy(&output.stdout),
191-
String::from_utf8_lossy(&output.stderr)
192-
))
193-
}
180+
Self::u32_from_cmd_output(output)
194181
}
195182

196183
/// Get length of the measurement log in bytes.
@@ -203,16 +190,7 @@ impl AttestHiffy {
203190
debug!("executing command: {:?}", cmd);
204191

205192
let output = cmd.output()?;
206-
if output.status.success() {
207-
Self::u32_from_stdout(&output.stdout)
208-
} else {
209-
Err(anyhow!(
210-
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
211-
output.status,
212-
String::from_utf8_lossy(&output.stdout),
213-
String::from_utf8_lossy(&output.stderr)
214-
))
215-
}
193+
Self::u32_from_cmd_output(output)
216194
}
217195
}
218196

0 commit comments

Comments
 (0)