summaryrefslogtreecommitdiffstats
path: root/sq/tests/sq-certify.rs
blob: 81a2fdfe121ca7da317c86c2b9f23deab2618c61 (plain)
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
use std::fs::File;
use std::time::Duration;

use assert_cli;
use assert_cli::Assert;
use tempfile;
use tempfile::TempDir;

use sequoia_openpgp as openpgp;
use openpgp::Result;
use openpgp::cert::prelude::*;
use openpgp::parse::Parse;
use openpgp::serialize::Serialize;
use openpgp::policy::StandardPolicy;

#[test]
fn sq_certify() -> Result<()> {
    let tmp_dir = TempDir::new().unwrap();
    let alice_pgp = tmp_dir.path().join("alice.pgp");
    let bob_pgp = tmp_dir.path().join("bob.pgp");

    let (alice, _) =
        CertBuilder::general_purpose(None, Some("alice@example.org"))
        .generate()?;
    let mut file = File::create(&alice_pgp)?;
    alice.as_tsk().serialize(&mut file)?;

    let (bob, _) =
        CertBuilder::general_purpose(None, Some("bob@example.org"))
        .generate()?;
    let mut file = File::create(&bob_pgp)?;
    bob.serialize(&mut file)?;


    // A simple certification.
    Assert::cargo_binary("sq")
        .with_args(
            &["certify",
              alice_pgp.to_str().unwrap(),
              bob_pgp.to_str().unwrap(),
              "bob@example.org",
            ])
        .stdout().satisfies(|output| {
            let p = &StandardPolicy::new();

            let cert = Cert::from_bytes(output).unwrap();
            let vc = cert.with_policy(p, None).unwrap();

            for ua in vc.userids() {
                if ua.userid().value() == b"bob@example.org" {
                    let certifications: Vec<_>
                        = ua.certifications().collect();
                    assert_eq!(certifications.len(), 1);
                    let c = certifications[0];

                    assert_eq!(c.trust_signature(), None);
                    assert_eq!(c.regular_expressions().count(), 0);
                    assert_eq!(c.revocable().unwrap_or(true), true);
                    assert_eq!(c.exportable_certification().unwrap_or(true), true);
                    // By default, we set a duration.
                    assert!(c.signature_validity_period().is_some());

                    return true;
                }
            }

            false
        },
                            "Bad certification")
        .unwrap();

    // No expiry.
    Assert::cargo_binary("sq")
        .with_args(
            &["certify",
              alice_pgp.to_str().unwrap(),
              bob_pgp.to_str().unwrap(),
              "bob@example.org",
              "--expires", "never"
            ])
        .stdout().satisfies(|output| {
            let p = &StandardPolicy::new();

            let cert = Cert::from_bytes(output).unwrap();
            let vc = cert.with_policy(p, None).unwrap();

            for ua in vc.userids() {
                if ua.userid().value() == b"bob@example.org" {
                    let certifications: Vec<_>
                        = ua.certifications().collect();
                    assert_eq!(certifications.len(), 1);
                    let c = certifications[0];

                    assert_eq!(c.trust_signature(), None);
                    assert_eq!(c.regular_expressions().count(), 0);
                    assert_eq!(c.revocable().unwrap_or(true), true);
                    assert_eq!(c.exportable_certification().unwrap_or(true), true);
                    assert!(c.signature_validity_period().is_none());

                    return true;
                }
            }

            false
        },
                            "Bad certification")
        .unwrap();

    // Have alice certify bob@example.org for 0xB0B.
    Assert::cargo_binary("sq")
        .with_args(
            &["certify",
              alice_pgp.to_str().unwrap(),
              bob_pgp.to_str().unwrap(),
              "bob@example.org",
              "--depth", "10",
              "--amount", "5",
              "--regex", "a",
              "--regex", "b",
              "--local",
              "--non-revocable",
              "--expires-in", "1d",
            ])
        .stdout().satisfies(|output| {
            let p = &mut StandardPolicy::new();
            // XXX: Compat with sequoia-openpgp 1.0.0:
            use openpgp::packet::signature::subpacket::SubpacketTag;
            p.accept_critical_subpacket(SubpacketTag::TrustSignature);
            p.accept_critical_subpacket(SubpacketTag::RegularExpression);

            let cert = Cert::from_bytes(output).unwrap();
            let vc = cert.with_policy(p, None).unwrap();

            for ua in vc.userids() {
                if ua.userid().value() == b"bob@example.org" {
                    let certifications: Vec<_>
                        = ua.certifications().collect();
                    assert_eq!(certifications.len(), 1);
                    let c = certifications[0];

                    assert_eq!(c.trust_signature(), Some((10, 5)));
                    assert_eq!(&c.regular_expressions().collect::<Vec<_>>()[..],
                               &[ b"a", b"b" ]);
                    assert_eq!(c.revocable(), Some(false));
                    assert_eq!(c.exportable_certification(), Some(false));
                    assert_eq!(c.signature_validity_period(),
                               Some(Duration::new(24 * 60 * 60, 0)));

                    return true;
                }
            }

            false
        },
                            "Bad certification")
        .unwrap();

    // It should fail if the User ID doesn't exist.
    Assert::cargo_binary("sq")
        .with_args(
            &["certify",
              alice_pgp.to_str().unwrap(),
              bob_pgp.to_str().unwrap(),
              "bob",
            ])
        .fails()
        .unwrap();

    Ok(())
}