1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use anyhow::{anyhow, Result};
use duration_str::parse;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;

use crate::{event::Event, helpers::Connection, Formalize};

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct Script {
    #[serde(default, alias = "Name", alias = "NAME")]
    pub name: Option<String>,
    #[serde(
        deserialize_with = "deserialize_string_to_u64",
        rename = "start-time",
        alias = "Start-time",
        alias = "START-TIME"
    )]
    pub start_time: u64,
    #[serde(
        deserialize_with = "deserialize_string_to_u64",
        rename = "end-time",
        alias = "End-time",
        alias = "END-TIME"
    )]
    pub end_time: u64,
    #[serde(alias = "Speed", alias = "SPEED")]
    pub speed: f32,
    #[serde(
        deserialize_with = "deserialize_events",
        alias = "Events",
        alias = "EVENTS"
    )]
    pub events: HashMap<String, u64>,
    #[serde(alias = "Description", alias = "DESCRIPTION")]
    pub description: Option<String>,
}

pub type Scripts = HashMap<String, Script>;

impl Formalize for Script {
    fn formalize(&mut self) -> Result<()> {
        if self.events.is_empty() {
            return Err(anyhow!("Script must have have at least one Event"));
        } else if self.start_time > self.end_time {
            return Err(anyhow!("Scripts end-time must be greater than start-time"));
        }

        for event in self.events.values() {
            if *event < self.start_time {
                return Err(anyhow!(
                    "Event time must be greater than or equal to script start time"
                ));
            } else if *event > self.end_time {
                return Err(anyhow!(
                    "Event time must be less than or equal to script end time"
                ));
            }
        }

        if self.speed.is_sign_negative() {
            return Err(anyhow!("Scripts speed must have a positive value"));
        }

        Ok(())
    }
}

impl Connection<Event> for (&String, &Script) {
    fn validate_connections(&self, potential_event_names: &Option<Vec<String>>) -> Result<()> {
        if potential_event_names.is_none() {
            return Err(anyhow!(
                "Script \"{script_name}\" requires at least one Event but none found under Scenario",
                script_name = self.0
            ));
        };

        if let Some(event_names) = potential_event_names {
            for event_name in self.1.events.keys() {
                if !event_names.contains(event_name) {
                    return Err(anyhow!(
                        "Event \"{event_name}\" not found under Scenario Events"
                    ));
                }
            }
        }

        Ok(())
    }
}

fn parse_time_string_to_u64_sec(mut string: String) -> Result<u64> {
    if string.eq("0") {
        string = String::from("0sec");
    }

    string.retain(|char| !char.is_whitespace() && char != '_');

    let duration = parse(&string)?;
    Ok(duration.as_secs())
}

fn deserialize_string_to_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: Deserializer<'de>,
{
    let string = String::deserialize(deserializer)?;
    let duration = parse_time_string_to_u64_sec(string)
        .map_err(|_| serde::de::Error::custom("failed to parse str to duration"))?;
    Ok(duration)
}

fn deserialize_events<'de, D>(deserializer: D) -> Result<HashMap<String, u64>, D::Error>
where
    D: Deserializer<'de>,
{
    let mut event_map: HashMap<String, String> = HashMap::deserialize(deserializer)?;

    let output = event_map
        .drain()
        .map(|(key, string)| {
            let duration = parse_time_string_to_u64_sec(string)
                .map_err(|_| serde::de::Error::custom("failed to parse str to duration"))?;
            Ok((key, duration))
        })
        .collect::<Result<HashMap<String, u64>, D::Error>>()?;

    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse_sdl;

    #[test]
    fn parses_sdl_with_scripts() {
        let sdl = r#"
            name: test-scenario
            description: some description
            conditions:
                condition-1:
                    command: executable/path.sh
                    interval: 30
            scripts:
                my-cool-script:
                    start-time: 10min 2 sec
                    end-time: 1 week 1day 1h 10 ms
                    speed: 1.5
                    events:
                        my-cool-event: 30 min
            capabilities:
                capability-1:
                    description: "Can defend against Dirty Cow"
                    condition: condition-1
            injects:
                my-cool-inject:
                    source: inject-package
                    capabilities:
                        executive: capability-1
            events:
                my-cool-event:
                    conditions:
                        - condition-1
                    injects:
                        - my-cool-inject
        "#;
        let schema = parse_sdl(sdl).unwrap();

        insta::with_settings!({sort_maps => true}, {
                insta::assert_yaml_snapshot!(schema);
        });
    }

    #[test]
    fn parses_single_script() {
        let script = r#"
            start-time: 5h 10min 2sec
            end-time: 1 week 7d 3 hour 10 ms
            speed: 1
            events:
                my-cool-event: 6h 30min
      "#;
        serde_yaml::from_str::<Script>(script).unwrap();
    }

    #[test]
    #[should_panic(expected = "Scripts end-time must be greater than start-time")]
    fn fails_end_time_larger_than_start_time() {
        let script = r#"
            start-time: 1 year 5h 10min 2sec
            end-time: 1 week 7d 3 hour 10 ms
            speed: 1
            events:
                my-cool-event: 6h 30min
      "#;
        serde_yaml::from_str::<Script>(script)
            .unwrap()
            .formalize()
            .unwrap();
    }

    #[test]
    fn parses_zero_without_unit() {
        let script = r#"
            start-time: 0
            end-time: 1 week 7d 3 hour 10 ms
            speed: 1
            events:
                my-cool-event: 0
      "#;
        serde_yaml::from_str::<Script>(script)
            .unwrap()
            .formalize()
            .unwrap();
    }

    #[test]
    fn parses_underscore_formatted_numbers() {
        let script = r#"
            start-time: _1_0__0__ min
            end-time: 1_000_000s
            speed: 1
            events:
                my-cool-event: 1_2_0 min
      "#;

        let script = serde_yaml::from_str::<Script>(script).unwrap();

        assert_eq!(script.start_time, 6000);
        assert_eq!(script.end_time, 1000000);
        assert_eq!(script.events["my-cool-event"], 7200);
    }

    #[test]
    #[should_panic(expected = "Scripts speed must have a positive value")]
    fn fails_on_negative_speed_value() {
        let script = r#"
            start-time: 0
            end-time: 3 hour
            speed: -1.234
            events:
                my-cool-event: 2 hour
      "#;
        serde_yaml::from_str::<Script>(script)
            .unwrap()
            .formalize()
            .unwrap();
    }

    #[test]
    #[should_panic(
        expected = "Condition must have Command and Interval or Source defined, not both"
    )]
    fn fails_on_event_not_defined_for_script() {
        let sdl = r#"
                name: test-scenario
                description: some description
                conditions:
                    condition-1:
                        command: executable/path.sh
                        interval: 30
                        source: digital-library-package
                scripts:
                    my-cool-script:
                        start-time: 10min 2 sec
                        end-time: 1 week 1day 1h 10 ms
                        speed: 1.5
                        events:
                            my-cool-event: 20 min
            "#;
        parse_sdl(sdl).unwrap();
    }

    #[test]
    #[should_panic(expected = "Event \"my-cool-event\" not found under Scenario Events")]
    fn fails_on_missing_event_for_script() {
        let sdl = r#"
                name: test-scenario
                description: some description
                conditions:
                    condition-1:
                        command: executable/path.sh
                        interval: 30
                scripts:
                    my-cool-script:
                        start-time: 10min 2 sec
                        end-time: 1 week 1day 1h 10 ms
                        speed: 1.5
                        events:
                            my-cool-event: 20 min
                injects:
                    my-cool-inject:
                        source: inject-package
                        capabilities:
                            executive: capability-1
                capabilities:
                    capability-1:
                        description: "Can defend against Dirty Cow"
                        condition: condition-1
                events:
                    my-embarrassing-event:
                        time: 0.2345678
                        conditions:
                            - condition-1
                        injects:
                            - my-cool-inject
            "#;
        parse_sdl(sdl).unwrap();
    }
}