|
| 1 | +// Copyright 2019 The Exonum Team |
| 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 | + |
| 15 | +use std::{collections::HashSet, fs::File, io::Read, path::Path}; |
| 16 | + |
| 17 | +use toml; |
| 18 | + |
| 19 | +pub const PATH_TO_SERVICES_TO_ENABLE: &str = "ejb_app_services.toml"; |
| 20 | +pub const CONFIGURATION_SERVICE: &str = "configuration"; |
| 21 | +pub const BTC_ANCHORING_SERVICE: &str = "btc-anchoring"; |
| 22 | +pub const TIME_SERVICE: &str = "time"; |
| 23 | +pub const EJB_SERVICE: &str = "ejb-service"; |
| 24 | + |
| 25 | +#[derive(Serialize, Deserialize)] |
| 26 | +struct ServicesToEnable { |
| 27 | + services: HashSet<String>, |
| 28 | +} |
| 29 | + |
| 30 | +type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
| 31 | + |
| 32 | +/// Loads service names from a specific TOML configuration file |
| 33 | +pub fn load_enabled_services<P: AsRef<Path>>(path: P) -> Result<HashSet<String>> { |
| 34 | + let mut file = File::open(path)?; |
| 35 | + let mut toml = String::new(); |
| 36 | + file.read_to_string(&mut toml)?; |
| 37 | + let ServicesToEnable { services } = |
| 38 | + toml::from_str(&toml).expect("Invalid list of services to enable"); |
| 39 | + Ok(services) |
| 40 | +} |
| 41 | + |
| 42 | +/// Determines whether particular service name is defined in the specific TOML configuration file. |
| 43 | +pub fn is_service_enabled_in_config_file<P: AsRef<Path>>(service_name: &str, path: P) -> bool { |
| 44 | + load_enabled_services(path) |
| 45 | + .map(|services| services.contains(service_name)) |
| 46 | + .unwrap_or(false) |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + use std::io::Write; |
| 53 | + use tempfile::{Builder, TempPath}; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn no_config() { |
| 57 | + let res = load_enabled_services("nonexistent"); |
| 58 | + assert!(res.is_err()); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn all_services() { |
| 63 | + let cfg = create_config( |
| 64 | + "all_services_test.toml", |
| 65 | + "services = [\"ejb-service\", \"configuration\", \"btc-anchoring\", \"time\"]", |
| 66 | + ); |
| 67 | + |
| 68 | + let res = load_enabled_services(cfg); |
| 69 | + assert!(res.is_ok()); |
| 70 | + let services_to_enable = res.unwrap(); |
| 71 | + assert_eq!(services_to_enable.len(), 4); |
| 72 | + assert!(services_to_enable.contains(EJB_SERVICE)); |
| 73 | + assert!(services_to_enable.contains(CONFIGURATION_SERVICE)); |
| 74 | + assert!(services_to_enable.contains(BTC_ANCHORING_SERVICE)); |
| 75 | + assert!(services_to_enable.contains(TIME_SERVICE)); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn empty_list() { |
| 80 | + let cfg = create_config("empty_list.toml", "services = []"); |
| 81 | + let res = load_enabled_services(cfg); |
| 82 | + assert!(res.is_ok()); |
| 83 | + let services_to_enable = res.unwrap(); |
| 84 | + assert_eq!(services_to_enable.len(), 0); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn check_service_enabled() { |
| 89 | + let cfg = create_config( |
| 90 | + "service_enabled_test.toml", |
| 91 | + "services = [\"ejb-service\", \"time\"]", |
| 92 | + ); |
| 93 | + |
| 94 | + assert!(is_service_enabled_in_config_file(EJB_SERVICE, &cfg)); |
| 95 | + assert!(!is_service_enabled_in_config_file( |
| 96 | + CONFIGURATION_SERVICE, |
| 97 | + &cfg |
| 98 | + )); |
| 99 | + assert!(!is_service_enabled_in_config_file( |
| 100 | + BTC_ANCHORING_SERVICE, |
| 101 | + &cfg |
| 102 | + )); |
| 103 | + assert!(is_service_enabled_in_config_file(TIME_SERVICE, &cfg)); |
| 104 | + } |
| 105 | + |
| 106 | + fn create_config(filename: &str, cfg: &str) -> TempPath { |
| 107 | + let mut cfg_file = Builder::new().prefix(filename).tempfile().unwrap(); |
| 108 | + writeln!(cfg_file, "{}", cfg).unwrap(); |
| 109 | + cfg_file.into_temp_path() |
| 110 | + } |
| 111 | +} |
0 commit comments