domain 0.12.0

A DNS library for Rust.
Documentation
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! Record data from [RFC 8659]: CAA records.
//!
//! This RFC defines the CAA record type.
//!
//! [RFC 8659]: https://www.rfc-editor.org/info/rfc8659

use crate::base::{
    charstr::DisplayQuoted,
    name::FlattenInto,
    rdata::ComposeRecordData,
    scan::{Scan, Scanner, ScannerError},
    wire::{Compose, Parse, ParseError},
    zonefile_fmt::{self, Formatter, ZonefileFmt},
    CanonicalOrd, CharStr, ParseRecordData, RecordData, Rtype,
};
use core::{cmp::Ordering, fmt, hash};
#[cfg(feature = "serde")]
use octseq::{
    builder::{EmptyBuilder, FromBuilder},
    serde::DeserializeOctets,
    serde::SerializeOctets,
};
use octseq::{Octets, OctetsBuilder, OctetsFrom, OctetsInto, Parser};

//------------ Caa ---------------------------------------------------------

/// Caa record data.
///
/// The Certification Authority Authorization (CAA) DNS Resource Record allows
/// a DNS domain name holder to specify one or more Certification Authorities
/// (CAs) authorized to issue certificates for that domain name.
///
/// CAA Resource Records allow a public CA to implement additional controls to reduce the
/// risk of unintended certificate mis-issue.
///
/// The Caa record type is defined in [RFC 8659, section 4.1][1].
///
/// [1]: https://www.rfc-editor.org/rfc/rfc8659#section-4.1
#[derive(Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(bound(
        serialize = "
            Octs: SerializeOctets + AsRef<[u8]>
        ",
        deserialize = "
            Octs: FromBuilder + DeserializeOctets<'de>,
            <Octs as FromBuilder>::Builder:
                OctetsBuilder + EmptyBuilder
                + AsRef<[u8]>,
        ",
    ))
)]
pub struct Caa<Octs> {
    flags: CaaFlags,
    tag: CaaTag<Octs>,
    #[cfg_attr(
        feature = "serde",
        serde(
            serialize_with = "octseq::serde::SerializeOctets::serialize_octets",
            deserialize_with = "octseq::serde::DeserializeOctets::deserialize_octets",
            bound(
                serialize = "Octs: octseq::serde::SerializeOctets",
                deserialize = "Octs: octseq::serde::DeserializeOctets<'de>",
            )
        )
    )]
    value: Octs,
}

impl Caa<()> {
    /// The rtype of this record data type.
    pub const RTYPE: Rtype = Rtype::CAA;
}

impl<Octs> Caa<Octs> {
    /// Creates a new CAA record data from the flags, tag, and value.
    pub fn new(flags: CaaFlags, tag: CaaTag<Octs>, value: Octs) -> Self {
        Caa { flags, tag, value }
    }

    /// Returns the flags. If the value is set to "1", the Property is critical.
    /// A CA MUST NOT issue certificates for any FQDN if the
    /// Relevant RRset for that FQDN contains a CAA critical
    /// Property for an unknown or unsupported Property Tag.
    pub fn flags(&self) -> CaaFlags {
        self.flags
    }

    /// Returns the Property identifier
    pub fn tag(&self) -> &CaaTag<Octs> {
        &self.tag
    }

    /// Returns the Property Value
    pub fn value(&self) -> &Octs {
        &self.value
    }

    pub(in crate::rdata) fn convert_octets<TOcts: OctetsFrom<Octs>>(
        self,
    ) -> Result<Caa<TOcts>, TOcts::Error> {
        Ok(Caa::new(
            self.flags,
            self.tag.try_octets_into()?,
            self.value.try_octets_into()?,
        ))
    }

    pub(in crate::rdata) fn flatten<TOcts: OctetsFrom<Octs>>(
        self,
    ) -> Result<Caa<TOcts>, TOcts::Error> {
        self.convert_octets()
    }

    pub fn scan<S: Scanner<Octets = Octs>>(
        scanner: &mut S,
    ) -> Result<Self, S::Error>
    where
        Octs: AsRef<[u8]>,
    {
        Ok(Self::new(
            CaaFlags::scan(scanner)?,
            CaaTag::scan(scanner)?,
            scanner.scan_octets()?,
        ))
    }

    pub fn parse<'a, Src: Octets<Range<'a> = Octs> + ?Sized>(
        parser: &mut Parser<'a, Src>,
    ) -> Result<Self, ParseError>
    where
        Octs: AsRef<[u8]>,
    {
        Ok(Self::new(
            CaaFlags::parse(parser)?,
            CaaTag::parse(parser)?,
            parser.parse_octets(parser.remaining())?,
        ))
    }
}

//--- OctetsFrom

impl<Octs, SrcOcts> OctetsFrom<Caa<SrcOcts>> for Caa<Octs>
where
    Octs: OctetsFrom<SrcOcts>,
{
    type Error = Octs::Error;

    fn try_octets_from(source: Caa<SrcOcts>) -> Result<Self, Self::Error> {
        Ok(Caa {
            flags: source.flags,
            tag: CaaTag::try_octets_from(source.tag)?,
            value: Octs::try_octets_from(source.value)?,
        })
    }
}

//--- FlattenInto

impl<Octs, TOcts> FlattenInto<Caa<TOcts>> for Caa<Octs>
where
    TOcts: OctetsFrom<Octs>,
{
    type AppendError = TOcts::Error;

    fn try_flatten_into(self) -> Result<Caa<TOcts>, Self::AppendError> {
        self.flatten()
    }
}

//--- PartialEq and Eq

impl<Octs, OtherOcts> PartialEq<Caa<OtherOcts>> for Caa<Octs>
where
    Octs: AsRef<[u8]>,
    OtherOcts: AsRef<[u8]>,
{
    fn eq(&self, other: &Caa<OtherOcts>) -> bool {
        self.flags == other.flags
            && self.tag.eq(&other.tag)
            && self.value.as_ref().eq(other.value.as_ref())
    }
}

impl<O: AsRef<[u8]>> Eq for Caa<O> {}

//--- PartialOrd, Ord, and CanonicalOrd

impl<Octs, OtherOcts> PartialOrd<Caa<OtherOcts>> for Caa<Octs>
where
    Octs: AsRef<[u8]>,
    OtherOcts: AsRef<[u8]>,
{
    fn partial_cmp(&self, other: &Caa<OtherOcts>) -> Option<Ordering> {
        match self.flags.partial_cmp(&other.flags) {
            Some(Ordering::Equal) => (),
            other => return other,
        }
        match self.tag.partial_cmp(&other.tag) {
            Some(Ordering::Equal) => (),
            other => return other,
        }
        self.value.as_ref().partial_cmp(other.value.as_ref())
    }
}

impl<Octs, OtherOcts> CanonicalOrd<Caa<OtherOcts>> for Caa<Octs>
where
    Octs: AsRef<[u8]>,
    OtherOcts: AsRef<[u8]>,
{
    fn canonical_cmp(&self, other: &Caa<OtherOcts>) -> Ordering {
        match self.flags.cmp(&other.flags) {
            Ordering::Equal => (),
            ord => return ord,
        }
        match self.tag.canonical_cmp(&other.tag) {
            Ordering::Equal => (),
            ord => return ord,
        }
        self.value.as_ref().cmp(other.value.as_ref())
    }
}

impl<O: AsRef<[u8]>> Ord for Caa<O> {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.flags.cmp(&other.flags) {
            Ordering::Equal => (),
            ord => return ord,
        }
        match self.tag.cmp(&other.tag) {
            Ordering::Equal => (),
            ord => return ord,
        }
        self.value.as_ref().cmp(other.value.as_ref())
    }
}

//--- Hash

impl<O: AsRef<[u8]>> hash::Hash for Caa<O> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.flags.hash(state);
        self.tag.hash(state);
        self.value.as_ref().hash(state);
    }
}

//--- RecordData, ParseRecordData, ComposeRecordData

impl<Octs> RecordData for Caa<Octs> {
    fn rtype(&self) -> Rtype {
        Caa::RTYPE
    }
}

impl<'a, Octs: Octets + ?Sized> ParseRecordData<'a, Octs>
    for Caa<Octs::Range<'a>>
{
    fn parse_rdata(
        rtype: Rtype,
        parser: &mut octseq::Parser<'a, Octs>,
    ) -> Result<Option<Self>, crate::base::wire::ParseError> {
        if rtype == Caa::RTYPE {
            Self::parse(parser).map(Some)
        } else {
            Ok(None)
        }
    }
}

impl<Octs: AsRef<[u8]>> ComposeRecordData for Caa<Octs> {
    fn rdlen(&self, _compress: bool) -> Option<u16> {
        Some(
            u8::COMPOSE_LEN
                .checked_add(self.tag.compose_len())
                .expect("long tag")
                .checked_add(
                    u16::try_from(self.value.as_ref().len())
                        .expect("long value"),
                )
                .expect("long value"),
        )
    }

    fn compose_rdata<Target: crate::base::wire::Composer + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        self.flags.compose(target)?;
        self.tag.compose(target)?;
        target.append_slice(self.value.as_ref())
    }

    fn compose_canonical_rdata<
        Target: crate::base::wire::Composer + ?Sized,
    >(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        self.flags.compose(target)?;
        self.tag.compose(target)?;
        target.append_slice(self.value.as_ref())
    }
}

//--- Display

impl<O: AsRef<[u8]>> fmt::Display for Caa<O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {} {}",
            self.flags,
            self.tag,
            DisplayQuoted::from_slice(self.value.as_ref()),
        )
    }
}

//--- Debug

impl<O: AsRef<[u8]>> fmt::Debug for Caa<O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Caa")
            .field("flags", &self.flags)
            .field("tag", &self.tag)
            .field("value", &DisplayQuoted::from_slice(self.value.as_ref()))
            .finish()
    }
}

//--- ZonefileFmt

impl<O: AsRef<[u8]>> ZonefileFmt for Caa<O> {
    fn fmt(&self, p: &mut impl Formatter) -> zonefile_fmt::Result {
        p.block(|p| {
            p.write_token(self.flags)?;
            p.write_comment("flags")?;
            p.write_token(&self.tag)?;
            p.write_comment("tag")?;
            p.write_token(DisplayQuoted::from_slice(self.value.as_ref()))?;
            p.write_comment("value")
        })
    }
}

/// A CAA property tag as defined in [RFC 8659 section 4.1].
///
/// A CAA tag identifies the property name that an issuer must honor when
/// evaluating a certificate issuance request. RFC 8659 restricts the tag to
/// printable ASCII alphabetic characters and digits with a maximal length of
/// 255 octets, and the wire format is a length-prefixed string of those
/// characters.
///
/// [RFC 8659 section 4.1]: https://www.rfc-editor.org/rfc/rfc8659#section-4.1
#[derive(Clone)]
#[repr(transparent)]
pub struct CaaTag<Octs: ?Sized>(CharStr<Octs>);

impl<Octs> CaaTag<Octs> {
    /// Constructs a CAA tag from a `CharStr`, validating that it only contains
    /// ASCII letters/digits and is at most 255 octets long.
    pub fn new(charstr: CharStr<Octs>) -> Result<Self, ParseError>
    where
        Octs: AsRef<[u8]>,
    {
        CaaTag::check_slice(charstr.as_slice())?;
        Ok(CaaTag(charstr))
    }

    /// Parses a CAA tag from an octets sequence while enforcing the same
    /// validation as [`CaaTag::new`].
    pub fn from_octets(octets: Octs) -> Result<Self, ParseError>
    where
        Octs: AsRef<[u8]>,
    {
        CaaTag::check_slice(octets.as_ref())?;
        Ok(unsafe { Self::from_octets_unchecked(octets) })
    }

    /// Creates a CAA tag from octets without validation.
    ///
    /// # Safety
    ///
    /// The caller must ensure `octets` consists only of ASCII alphanumeric
    /// characters and is no longer than 255 octets, as required by RFC 8659.
    pub unsafe fn from_octets_unchecked(octets: Octs) -> Self {
        CaaTag(CharStr::from_octets_unchecked(octets))
    }
}

impl CaaTag<[u8]> {
    /// Parses a CAA tag from a slice, validating it against the same rules as
    /// [`CaaTag::new`].
    pub fn from_slice(slice: &[u8]) -> Result<&Self, ParseError> {
        Self::check_slice(slice)?;
        Ok(unsafe { Self::from_slice_unchecked(slice) })
    }

    /// Creates a new value from a slice without checking.
    ///
    /// # Safety
    ///
    /// The caller needs to make sure that the slice only contains ascii
    /// alphanumeric characters and is not longer than 255 bytes.
    pub unsafe fn from_slice_unchecked(slice: &[u8]) -> &Self {
        // SAFETY: CaaTag has repr(transparent)
        &*(CharStr::from_slice_unchecked(slice) as *const CharStr<[u8]>
            as *const Self)
    }

    fn check_slice(octets: &[u8]) -> Result<(), ParseError> {
        if octets.iter().any(|e| !e.is_ascii_alphanumeric()) {
            return Err(ParseError::form_error(
                "CAA tag contains invalid character",
            ));
        }
        Ok(())
    }
}

impl<Octs: AsRef<[u8]>> CaaTag<Octs> {
    /// Returns the length of the wire-format tag, which mirrors the length of
    /// the underlying `CharStr`.
    pub fn compose_len(&self) -> u16 {
        self.0.compose_len()
    }

    /// Writes the tag into `target` using the standard length-prefixed wire
    /// format built from the ASCII characters of the tag.
    pub fn compose<Target: OctetsBuilder + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        self.0.compose(target)
    }

    /// Scans a CAA tag from the scanner, enforcing the ASCII rules used by
    /// the CAA property tag.
    pub fn scan<S: Scanner<Octets = Octs>>(
        scanner: &mut S,
    ) -> Result<Self, S::Error> {
        let octets = CharStr::scan(scanner)?;
        CaaTag::check_slice(octets.as_slice()).map_err(|_| {
            S::Error::custom("CAA tag contains invalid character")
        })?;
        Ok(CaaTag(octets))
    }

    /// Parses a CAA tag from the parser while validating it for ASCII letters
    /// and digits with a valid length.
    pub fn parse<'a, Src: Octets<Range<'a> = Octs> + ?Sized>(
        parser: &mut Parser<'a, Src>,
    ) -> Result<Self, ParseError> {
        Self::new(CharStr::parse(parser)?)
    }
}

impl<Octs, SrcOcts> OctetsFrom<CaaTag<SrcOcts>> for CaaTag<Octs>
where
    Octs: OctetsFrom<SrcOcts>,
{
    type Error = Octs::Error;

    fn try_octets_from(source: CaaTag<SrcOcts>) -> Result<Self, Self::Error> {
        Ok(CaaTag(CharStr::try_octets_from(source.0)?))
    }
}

//--- PartialEq and Eq

impl<Octs, OtherOcts> PartialEq<CaaTag<OtherOcts>> for CaaTag<Octs>
where
    Octs: AsRef<[u8]>,
    OtherOcts: AsRef<[u8]>,
{
    fn eq(&self, other: &CaaTag<OtherOcts>) -> bool {
        self.0.eq(&other.0)
    }
}

impl<O: AsRef<[u8]>> Eq for CaaTag<O> {}

//--- PartialOrd, Ord, and CanonicalOrd

impl<Octs, OtherOcts> PartialOrd<CaaTag<OtherOcts>> for CaaTag<Octs>
where
    Octs: AsRef<[u8]>,
    OtherOcts: AsRef<[u8]>,
{
    fn partial_cmp(&self, other: &CaaTag<OtherOcts>) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}
impl<Octs, OtherOcts> CanonicalOrd<CaaTag<OtherOcts>> for CaaTag<Octs>
where
    Octs: AsRef<[u8]>,
    OtherOcts: AsRef<[u8]>,
{
    fn canonical_cmp(&self, other: &CaaTag<OtherOcts>) -> Ordering {
        self.0.canonical_cmp(&other.0)
    }
}

impl<O: AsRef<[u8]>> Ord for CaaTag<O> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.cmp(&other.0)
    }
}

//--- Hash
impl<O: AsRef<[u8]>> hash::Hash for CaaTag<O> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

//--- Display and Debug
impl<O: AsRef<[u8]>> fmt::Display for CaaTag<O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<O: AsRef<[u8]>> fmt::Debug for CaaTag<O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("CaaTag").field(&self.0).finish()
    }
}

//--- Serialize and Deserialize

#[cfg(feature = "serde")]
impl<Octs> serde::Serialize for CaaTag<Octs>
where
    Octs: AsRef<[u8]> + octseq::serde::SerializeOctets,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, Octs> serde::Deserialize<'de> for CaaTag<Octs>
where
    Octs: FromBuilder + octseq::serde::DeserializeOctets<'de>,
    <Octs as FromBuilder>::Builder: AsRef<[u8]> + EmptyBuilder,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Self::new(CharStr::deserialize(deserializer)?)
            .map_err(serde::de::Error::custom)
    }
}

/// CAA flags as defined in [RFC 8659 section 4.1].
///
/// The only defined flag is the critical flag (bit 7).
/// You can create a plain CAA flags instance with [CaaFlags::default()]
/// or critical CAA flags with [CaaFlags::critical()].
///
/// The [CaaFlags::new()] method allows creating a CAA flags instance
/// from any underlying byte, but be aware that only bit 7 is defined.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CaaFlags(u8);

impl CaaFlags {
    /// Creates a new CAA flags instance from the underlying byte.
    pub fn new(bits: u8) -> Self {
        CaaFlags(bits)
    }

    /// Creates a CAA flags instance with the critical flag set.
    pub fn critical() -> Self {
        CaaFlags(0x80)
    }

    /// Returns the underlying flags byte.
    pub fn bits(&self) -> u8 {
        self.0
    }
}

impl fmt::Display for CaaFlags {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Compose for CaaFlags {
    fn compose<Target: octseq::OctetsBuilder + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        self.0.compose(target)
    }
}

impl<S: Scanner> Scan<S> for CaaFlags {
    fn scan(scanner: &mut S) -> Result<Self, S::Error> {
        Ok(CaaFlags(u8::scan(scanner)?))
    }
}

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parse<'a, Octs> for CaaFlags {
    fn parse(parser: &mut Parser<'a, Octs>) -> Result<Self, ParseError> {
        Ok(CaaFlags(u8::parse(parser)?))
    }
}

#[cfg(test)]
#[cfg(all(feature = "std", feature = "bytes"))]
mod test {
    use super::*;
    use crate::std::string::ToString;
    use octseq::array::Array;

    #[test]
    fn caa_eq() {
        let caa1 = Caa::new(
            CaaFlags::default(),
            CaaTag::from_octets("ISSUE".as_bytes()).unwrap(),
            "ca.example.net".as_bytes(),
        );
        let caa2 = Caa::new(
            CaaFlags::default(),
            CaaTag::from_octets("issue".as_bytes()).unwrap(),
            "ca.example.net".as_bytes(),
        );
        assert_eq!(caa1, caa2);
    }

    #[test]
    fn caa_octets_info() {
        let caa = Caa::new(
            CaaFlags::default(),
            CaaTag::from_octets("issue".as_bytes()).unwrap(),
            "ca.example.net".as_bytes(),
        );
        let caa_bytes: Caa<bytes::Bytes> = caa.clone().octets_into();
        assert_eq!(caa.flags, caa_bytes.flags);
        assert_eq!(caa.tag, caa_bytes.tag);
        assert_eq!(caa.value, caa_bytes.value);
    }

    #[test]
    fn caa_display() {
        let caa = Caa::new(
            CaaFlags::default(),
            CaaTag::from_octets("issue".as_bytes()).unwrap(),
            "ca.example.net".as_bytes(),
        );

        assert_eq!(caa.to_string(), r#"0 issue "ca.example.net""#);
    }

    #[test]
    fn caa_tag_creation_and_validation() {
        assert!(CaaTag::from_octets("issue".as_bytes()).is_ok());
        assert!(CaaTag::from_octets("bad tag".as_bytes()).is_err());
    }

    #[test]
    fn caa_tag_display_and_debug() {
        let tag = CaaTag::from_octets("ISSUE".as_bytes()).unwrap();
        assert_eq!(tag.to_string(), "ISSUE");
        assert_eq!(format!("{:?}", tag), "CaaTag(CharStr(ISSUE))");
    }

    #[test]
    fn caa_tag_compose_canonical_lowercases() {
        let tag = CaaTag::from_octets("Issue".as_bytes()).unwrap();
        let mut buf = Array::<8>::new();
        tag.compose(&mut buf).unwrap();
        assert_eq!(buf.as_ref(), &[5, b'I', b's', b's', b'u', b'e']);
    }

    #[test]
    fn caa_flags_display() {
        let flags = CaaFlags::default();
        assert_eq!(flags.bits(), 0);
    }

    #[test]
    fn caa_flags_critical() {
        let flags = CaaFlags::critical();
        assert_eq!(flags.bits(), 0x80);
    }
}