icann_rdap_cli/rt/
results.rs

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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

/// Contains the results of test execution.
use chrono::{DateTime, Utc};
use icann_rdap_client::{
    md::{string::StringUtil, table::MultiPartTable, MdOptions},
    rdap::ResponseData,
    RdapClientError,
};
use icann_rdap_common::{
    check::{traverse_checks, Check, CheckClass, CheckItem, CheckParams, Checks, GetChecks},
    response::{types::ExtensionId, RdapResponse},
};
use reqwest::StatusCode;
use serde::Serialize;
use strum_macros::Display;

use super::exec::TestOptions;

#[derive(Debug, Serialize)]
pub struct TestResults {
    pub query_url: String,
    pub dns_data: DnsData,
    pub start_time: DateTime<Utc>,
    pub end_time: Option<DateTime<Utc>>,
    pub service_checks: Vec<CheckItem>,
    pub test_runs: Vec<TestRun>,
}

impl TestResults {
    pub fn new(query_url: String, dns_data: DnsData) -> Self {
        TestResults {
            query_url,
            dns_data,
            start_time: Utc::now(),
            end_time: None,
            service_checks: vec![],
            test_runs: vec![],
        }
    }

    pub fn end(&mut self, options: &TestOptions) {
        self.end_time = Some(Utc::now());

        //service checks
        if self.dns_data.v4_cname.is_some() && self.dns_data.v4_addrs.is_empty() {
            self.service_checks
                .push(Check::CnameWithoutARecords.check_item());
        }
        if self.dns_data.v6_cname.is_some() && self.dns_data.v6_addrs.is_empty() {
            self.service_checks
                .push(Check::CnameWithoutAAAARecords.check_item());
        }
        if self.dns_data.v4_addrs.is_empty() {
            self.service_checks.push(Check::NoARecords.check_item());
        }
        if self.dns_data.v6_addrs.is_empty() {
            self.service_checks.push(Check::NoAAAARecords.check_item());

            // see if required by ICANN
            let tig0 = ExtensionId::IcannRdapTechnicalImplementationGuide0.to_string();
            let tig1 = ExtensionId::IcannRdapTechnicalImplementationGuide1.to_string();
            let both_tigs = format!("{tig0}|{tig1}");
            if options.expect_extensions.contains(&tig0)
                || options.expect_extensions.contains(&tig1)
                || options.expect_extensions.contains(&both_tigs)
            {
                self.service_checks
                    .push(Check::Ipv6SupportRequiredByIcann.check_item())
            }
        }
    }

    pub fn add_test_run(&mut self, test_run: TestRun) {
        self.test_runs.push(test_run);
    }

    pub fn to_md(&self, options: &MdOptions, check_classes: &[CheckClass]) -> String {
        let mut md = String::new();

        // h1
        md.push_str(&format!(
            "\n{}\n",
            self.query_url.to_owned().to_header(1, options)
        ));

        // table
        let mut table = MultiPartTable::new();

        // test results summary
        table = table.multi(vec![
            "Start Time".to_inline(options),
            "End Time".to_inline(options),
            "Duration".to_inline(options),
            "Tested".to_inline(options),
        ]);
        let (end_time_s, duration_s) = if let Some(end_time) = self.end_time {
            (
                format_date_time(end_time),
                format!("{} s", (end_time - self.start_time).num_seconds()),
            )
        } else {
            ("FATAL".to_em(options), "N/A".to_string())
        };
        let tested = self
            .test_runs
            .iter()
            .filter(|r| matches!(r.outcome, RunOutcome::Tested))
            .count();
        table = table.multi(vec![
            format_date_time(self.start_time),
            end_time_s,
            duration_s,
            format!("{tested} of {}", self.test_runs.len()),
        ]);

        // dns data
        table = table.multi(vec![
            "DNS Query".to_inline(options),
            "DNS Answer".to_inline(options),
        ]);
        let v4_cname = if let Some(ref cname) = self.dns_data.v4_cname {
            cname.to_owned()
        } else {
            format!("{} A records", self.dns_data.v4_addrs.len())
        };
        table = table.multi(vec!["A (v4)".to_string(), v4_cname]);
        let v6_cname = if let Some(ref cname) = self.dns_data.v6_cname {
            cname.to_owned()
        } else {
            format!("{} AAAA records", self.dns_data.v6_addrs.len())
        };
        table = table.multi(vec!["AAAA (v6)".to_string(), v6_cname]);

        // summary of each run
        table = table.multi(vec![
            "Address".to_inline(options),
            "Attributes".to_inline(options),
            "Duration".to_inline(options),
            "Outcome".to_inline(options),
        ]);
        for test_run in &self.test_runs {
            table = test_run.add_summary(table, options);
        }
        md.push_str(&table.to_md_table(options));

        md.push('\n');

        // checks that are about the service and not a particular test run
        if !self.service_checks.is_empty() {
            md.push_str(&"Service Checks".to_string().to_header(1, options));
            let mut table = MultiPartTable::new();

            table = table.multi(vec!["Message".to_inline(options)]);
            for c in &self.service_checks {
                let message = check_item_md(c, options);
                table = table.multi(vec![message]);
            }
            md.push_str(&table.to_md_table(options));
            md.push('\n');
        }

        // each run in detail
        for run in &self.test_runs {
            md.push_str(&run.to_md(options, check_classes));
        }
        md
    }
}

#[derive(Debug, Serialize, Clone, Default)]
pub struct DnsData {
    pub v4_cname: Option<String>,
    pub v6_cname: Option<String>,
    pub v4_addrs: Vec<Ipv4Addr>,
    pub v6_addrs: Vec<Ipv6Addr>,
}

#[derive(Debug, Serialize, Display)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
pub enum RunOutcome {
    Tested,
    NetworkError,
    HttpProtocolError,
    HttpConnectError,
    HttpRedirectError,
    HttpTimeoutError,
    HttpNon200Error,
    HttpTooManyRequestsError,
    HttpNotFoundError,
    HttpBadRequestError,
    HttpUnauthorizedError,
    HttpForbiddenError,
    JsonError,
    RdapDataError,
    InternalError,
    Skipped,
}

#[derive(Debug, Serialize, Display)]
#[strum(serialize_all = "snake_case")]
pub enum RunFeature {
    OriginHeader,
}

impl RunOutcome {
    pub fn to_md(&self, options: &MdOptions) -> String {
        match self {
            RunOutcome::Tested => self.to_bold(options),
            RunOutcome::Skipped => self.to_string(),
            _ => self.to_em(options),
        }
    }
}

#[derive(Debug, Serialize)]
pub struct TestRun {
    pub features: Vec<RunFeature>,
    pub socket_addr: SocketAddr,
    pub start_time: DateTime<Utc>,
    pub end_time: Option<DateTime<Utc>>,
    pub response_data: Option<ResponseData>,
    pub outcome: RunOutcome,
    pub checks: Option<Checks>,
}

impl TestRun {
    pub fn new_v4(features: Vec<RunFeature>, ipv4: Ipv4Addr, port: u16) -> Self {
        TestRun {
            features,
            start_time: Utc::now(),
            socket_addr: SocketAddr::new(IpAddr::V4(ipv4), port),
            end_time: None,
            response_data: None,
            outcome: RunOutcome::Skipped,
            checks: None,
        }
    }

    pub fn new_v6(features: Vec<RunFeature>, ipv6: Ipv6Addr, port: u16) -> Self {
        TestRun {
            features,
            start_time: Utc::now(),
            socket_addr: SocketAddr::new(IpAddr::V6(ipv6), port),
            end_time: None,
            response_data: None,
            outcome: RunOutcome::Skipped,
            checks: None,
        }
    }

    pub fn end(
        mut self,
        rdap_response: Result<ResponseData, RdapClientError>,
        options: &TestOptions,
    ) -> Self {
        if let Ok(response_data) = rdap_response {
            self.end_time = Some(Utc::now());
            self.outcome = RunOutcome::Tested;
            self.checks = Some(do_checks(&response_data, options));
            self.response_data = Some(response_data);
        } else {
            self.outcome = match rdap_response.err().unwrap() {
                RdapClientError::InvalidQueryValue
                | RdapClientError::AmbiquousQueryType
                | RdapClientError::Poison
                | RdapClientError::DomainNameError(_)
                | RdapClientError::BootstrapUnavailable
                | RdapClientError::BootstrapError(_)
                | RdapClientError::IanaResponse(_) => RunOutcome::InternalError,
                RdapClientError::Response(_) => RunOutcome::RdapDataError,
                RdapClientError::Json(_) | RdapClientError::ParsingError(_) => {
                    RunOutcome::JsonError
                }
                RdapClientError::IoError(_) => RunOutcome::NetworkError,
                RdapClientError::Client(e) => {
                    if e.is_redirect() {
                        RunOutcome::HttpRedirectError
                    } else if e.is_connect() {
                        RunOutcome::HttpConnectError
                    } else if e.is_timeout() {
                        RunOutcome::HttpTimeoutError
                    } else if e.is_status() {
                        match e.status().unwrap() {
                            StatusCode::TOO_MANY_REQUESTS => RunOutcome::HttpTooManyRequestsError,
                            StatusCode::NOT_FOUND => RunOutcome::HttpNotFoundError,
                            StatusCode::BAD_REQUEST => RunOutcome::HttpBadRequestError,
                            StatusCode::UNAUTHORIZED => RunOutcome::HttpUnauthorizedError,
                            StatusCode::FORBIDDEN => RunOutcome::HttpForbiddenError,
                            _ => RunOutcome::HttpNon200Error,
                        }
                    } else {
                        RunOutcome::HttpProtocolError
                    }
                }
            };
            self.end_time = Some(Utc::now());
        };
        self
    }

    fn add_summary(&self, mut table: MultiPartTable, options: &MdOptions) -> MultiPartTable {
        let duration_s = if let Some(end_time) = self.end_time {
            format!("{} ms", (end_time - self.start_time).num_milliseconds())
        } else {
            "n/a".to_string()
        };
        table = table.multi(vec![
            self.socket_addr.to_string(),
            self.attribute_set(),
            duration_s,
            self.outcome.to_md(options),
        ]);
        table
    }

    fn to_md(&self, options: &MdOptions, check_classes: &[CheckClass]) -> String {
        let mut md = String::new();

        // h1
        let header_value = format!("{} - {}", self.socket_addr, self.attribute_set());
        md.push_str(&format!("\n{}\n", header_value.to_header(1, options)));

        // if outcome is tested
        if matches!(self.outcome, RunOutcome::Tested) {
            // get check items according to class
            let mut check_v: Vec<(String, String)> = Vec::new();
            if let Some(ref checks) = self.checks {
                traverse_checks(checks, check_classes, None, &mut |struct_name, item| {
                    let message = check_item_md(item, options);
                    check_v.push((struct_name.to_string(), message))
                });
            };

            // table
            let mut table = MultiPartTable::new();

            if check_v.is_empty() {
                table = table.header_ref(&"No issues or errors.");
            } else {
                table = table.multi(vec![
                    "RDAP Structure".to_inline(options),
                    "Message".to_inline(options),
                ]);
                for c in check_v {
                    table = table.nv(&c.0, c.1);
                }
            }
            md.push_str(&table.to_md_table(options));
        } else {
            let mut table = MultiPartTable::new();
            table = table.multi(vec![self.outcome.to_md(options)]);
            md.push_str(&table.to_md_table(options));
        }

        md
    }

    fn attribute_set(&self) -> String {
        let socket_type = if self.socket_addr.is_ipv4() {
            "v4"
        } else {
            "v6"
        };
        if !self.features.is_empty() {
            format!(
                "{socket_type}, {}",
                self.features
                    .iter()
                    .map(|f| f.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        } else {
            socket_type.to_string()
        }
    }
}

fn check_item_md(item: &CheckItem, options: &MdOptions) -> String {
    if !matches!(item.check_class, CheckClass::Informational)
        && !matches!(item.check_class, CheckClass::SpecificationNote)
    {
        item.to_string().to_em(options)
    } else {
        item.to_string()
    }
}

fn format_date_time(date: DateTime<Utc>) -> String {
    date.format("%a, %v %X %Z").to_string()
}

fn do_checks(response: &ResponseData, options: &TestOptions) -> Checks {
    let check_params = CheckParams {
        do_subchecks: true,
        root: &response.rdap,
        parent_type: response.rdap.get_type(),
        allow_unreg_ext: options.allow_unregistered_extensions,
    };
    let mut checks = response.rdap.get_checks(check_params);

    // httpdata checks
    checks
        .items
        .append(&mut response.http_data.get_checks(check_params).items);

    // add expected extension checks
    for ext in &options.expect_extensions {
        if !rdap_has_expected_extension(&response.rdap, ext) {
            checks
                .items
                .push(Check::ExpectedExtensionNotFound.check_item());
        }
    }

    //return
    checks
}

fn rdap_has_expected_extension(rdap: &RdapResponse, ext: &str) -> bool {
    let count = ext.split('|').filter(|s| rdap.has_extension(s)).count();
    count > 0
}

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
    use icann_rdap_common::response::{
        domain::Domain,
        types::{Common, Extension},
        RdapResponse,
    };

    use super::rdap_has_expected_extension;

    #[test]
    fn GIVEN_expected_extension_WHEN_rdap_has_THEN_true() {
        // GIVEN
        let domain = Domain::basic().ldh_name("foo.example.com").build();
        let domain = Domain {
            common: Common::level0_with_options()
                .extension(Extension::from("foo0"))
                .build(),
            ..domain
        };
        let rdap = RdapResponse::Domain(domain);

        // WHEN
        let actual = rdap_has_expected_extension(&rdap, "foo0");

        // THEN
        assert!(actual);
    }

    #[test]
    fn GIVEN_expected_extension_WHEN_rdap_does_not_have_THEN_false() {
        // GIVEN
        let domain = Domain::basic().ldh_name("foo.example.com").build();
        let domain = Domain {
            common: Common::level0_with_options()
                .extension(Extension::from("foo0"))
                .build(),
            ..domain
        };
        let rdap = RdapResponse::Domain(domain);

        // WHEN
        let actual = rdap_has_expected_extension(&rdap, "foo1");

        // THEN
        assert!(!actual);
    }

    #[test]
    fn GIVEN_compound_expected_extension_WHEN_rdap_has_THEN_true() {
        // GIVEN
        let domain = Domain::basic().ldh_name("foo.example.com").build();
        let domain = Domain {
            common: Common::level0_with_options()
                .extension(Extension::from("foo0"))
                .build(),
            ..domain
        };
        let rdap = RdapResponse::Domain(domain);

        // WHEN
        let actual = rdap_has_expected_extension(&rdap, "foo0|foo1");

        // THEN
        assert!(actual);
    }
}