• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #![forbid(unsafe_code)]
15 #![deny(missing_docs)]
16 
17 //! Helper crate for common functions used in testing
18 
19 use std::fs;
20 use std::io::Read;
21 
22 /// Returns data file path for specific build system. Input is the path to the file relative to the
23 /// workspace root dir
get_data_file(file: &str) -> std::path::PathBuf24 pub fn get_data_file(file: &str) -> std::path::PathBuf {
25     let mut full_path = std::path::PathBuf::from(env!("WORKSPACE_DIR"));
26     full_path.push(file);
27     full_path
28 }
29 
30 /// Opens a file at the specified path (relative to the workspace root)
31 /// and yields its contents as a string
load_data_file_contents_as_string(file: &str) -> String32 pub fn load_data_file_contents_as_string(file: &str) -> String {
33     let full_path = get_data_file(file);
34     let mut file = fs::File::open(full_path).expect("Should be able to open data file");
35     let mut data = String::new();
36     file.read_to_string(&mut data)
37         .expect("should be able to read data file");
38     data
39 }
40 
41 /// Opens a json file at the specified path and parses it into a value
parse_json_data_file(file: &str) -> serde_json::Value42 pub fn parse_json_data_file(file: &str) -> serde_json::Value {
43     let data = load_data_file_contents_as_string(file);
44     serde_json::de::from_str(data.as_str()).expect("should be able to parse json date file")
45 }
46 
47 /// extract a string from a jsonvalue
extract_key_str<'a>(value: &'a serde_json::Value, key: &str) -> &'a str48 pub fn extract_key_str<'a>(value: &'a serde_json::Value, key: &str) -> &'a str {
49     value[key].as_str().unwrap()
50 }
51 
52 /// Decode a hex-encoded vec at `key`
extract_key_vec(value: &serde_json::Value, key: &str) -> Vec<u8>53 pub fn extract_key_vec(value: &serde_json::Value, key: &str) -> Vec<u8> {
54     hex::decode(value[key].as_str().unwrap()).unwrap()
55 }
56 
57 /// Decode a hex-encoded array at `key`
extract_key_array<const N: usize>(value: &serde_json::Value, key: &str) -> [u8; N]58 pub fn extract_key_array<const N: usize>(value: &serde_json::Value, key: &str) -> [u8; N] {
59     extract_key_vec(value, key).try_into().unwrap()
60 }
61 
62 /// Convert a hex string to a Vec of the hex bytes
string_to_hex(str: &str) -> Vec<u8>63 pub fn string_to_hex(str: &str) -> Vec<u8> {
64     hex::decode(str).unwrap()
65 }
66