summaryrefslogtreecommitdiffstats
path: root/vendor/pem-rfc7468/tests/decode.rs
blob: dc51528b3b16b8602b966b15d3a2217a54d6e916 (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
//! PEM decoding tests

#[test]
fn pkcs1_example() {
    let pem = include_bytes!("examples/pkcs1.pem");
    let mut buf = [0u8; 2048];
    let (label, decoded) = pem_rfc7468::decode(pem, &mut buf).unwrap();
    assert_eq!(label, "RSA PRIVATE KEY");
    assert_eq!(decoded, include_bytes!("examples/pkcs1.der"));
}

#[test]
fn binary_example() {
    let der = include_bytes!("examples/pkcs1.der");
    let mut buf = [0u8; 2048];
    match pem_rfc7468::decode(der, &mut buf) {
        Err(pem_rfc7468::Error::Preamble) => (),
        _ => panic!("Expected Preamble error"),
    }
}

#[test]
fn pkcs1_example_with_preceeding_junk() {
    let pem = include_bytes!("examples/pkcs1_with_preceeding_junk.pem");
    let mut buf = [0u8; 2048];
    let (label, decoded) = pem_rfc7468::decode(pem, &mut buf).unwrap();
    assert_eq!(label, "RSA PRIVATE KEY");
    assert_eq!(decoded, include_bytes!("examples/pkcs1.der"));
}

#[test]
fn pkcs1_enc_example() {
    let pem = include_bytes!("examples/ssh_rsa_pem_password.pem");
    let mut buf = [0u8; 2048];
    let result = pem_rfc7468::decode(pem, &mut buf);
    assert_eq!(result, Err(pem_rfc7468::Error::HeaderDisallowed));

    let label = pem_rfc7468::decode_label(pem).unwrap();
    assert_eq!(label, "RSA PRIVATE KEY");
}

#[test]
#[cfg(feature = "alloc")]
fn pkcs1_enc_example_with_vec() {
    let pem = include_bytes!("examples/ssh_rsa_pem_password.pem");
    let result = pem_rfc7468::decode_vec(pem);
    assert_eq!(result, Err(pem_rfc7468::Error::HeaderDisallowed));
}

#[test]
fn header_of_length_64() {
    let pem = include_bytes!("examples/chosen_header.pem");
    let mut buf = [0u8; 2048];
    let result = pem_rfc7468::decode(pem, &mut buf);
    assert_eq!(result, Err(pem_rfc7468::Error::HeaderDisallowed));

    let label = pem_rfc7468::decode_label(pem).unwrap();
    assert_eq!(label, "RSA PRIVATE KEY");
}

#[test]
#[cfg(feature = "alloc")]
fn header_of_length_64_with_vec() {
    let pem = include_bytes!("examples/chosen_header.pem");
    match pem_rfc7468::decode_vec(pem) {
        Err(pem_rfc7468::Error::HeaderDisallowed) => (),
        res => panic!("Expected HeaderDisallowed error; Found {:?}", res),
    }
}

#[test]
fn pkcs8_example() {
    let pem = include_bytes!("examples/pkcs8.pem");
    let mut buf = [0u8; 2048];
    let (label, decoded) = pem_rfc7468::decode(pem, &mut buf).unwrap();
    assert_eq!(label, "PRIVATE KEY");
    assert_eq!(decoded, include_bytes!("examples/pkcs8.der"));
}

#[test]
fn pkcs8_enc_example() {
    let pem = include_bytes!("examples/pkcs8-enc.pem");
    let mut buf = [0u8; 2048];
    let (label, decoded) = pem_rfc7468::decode(pem, &mut buf).unwrap();
    assert_eq!(label, "ENCRYPTED PRIVATE KEY");
    assert_eq!(decoded, include_bytes!("examples/pkcs8-enc.der"));
}

#[test]
#[cfg(feature = "alloc")]
fn pkcs1_example_with_vec() {
    let pem = include_bytes!("examples/pkcs1.pem");
    let (label, decoded) = pem_rfc7468::decode_vec(pem).unwrap();
    assert_eq!(label, "RSA PRIVATE KEY");
    assert_eq!(decoded, include_bytes!("examples/pkcs1.der"));
}

#[test]
#[cfg(feature = "alloc")]
fn pkcs8_enc_example_with_vec() {
    let pem = include_bytes!("examples/pkcs8-enc.pem");
    let (label, decoded) = pem_rfc7468::decode_vec(pem).unwrap();
    assert_eq!(label, "ENCRYPTED PRIVATE KEY");
    assert_eq!(decoded, include_bytes!("examples/pkcs8-enc.der"));
}

#[test]
fn ed25519_example() {
    let pem = include_bytes!("examples/ed25519_id.pem");
    let label = pem_rfc7468::decode_label(pem).unwrap();
    assert_eq!(label, "ED25519 CERT");
}