diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-09-11 16:12:21 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-09-18 13:08:08 +0200 |
commit | 446dfdbcb63af00daa3a777958419e1dfb6fc737 (patch) | |
tree | a5bbe4244083d9a467ab8d86be762fb67bd0054e | |
parent | 130eed7cda9f77502bbb181bfabb45934af46a8d (diff) |
openpgp: Make Parse::from_bytes polymorphic over AsRef<[u8]>.
- A drawback of this change is that currently AsRef<[u8]> is not
implemented for [u8; _], only for specific lengths. This is a
compiler limitation that may be lifted in the future. This
limitation required fixing some tests, notably those using
include_bytes!.
- Fixes #296.
-rw-r--r-- | examples/guide-exploring-openpgp.rs | 2 | ||||
-rw-r--r-- | examples/guide-the-keystore.rs | 2 | ||||
-rw-r--r-- | openpgp/src/message/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/packet/skesk.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/user_attribute.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet_pile.rs | 4 | ||||
-rw-r--r-- | openpgp/src/parse/packet_parser_builder.rs | 4 | ||||
-rw-r--r-- | openpgp/src/parse/packet_pile_parser.rs | 4 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 7 | ||||
-rw-r--r-- | openpgp/src/parse/sexp/mod.rs | 9 | ||||
-rw-r--r-- | openpgp/src/serialize/stream.rs | 10 | ||||
-rw-r--r-- | openpgp/src/serialize/tpk_armored.rs | 4 | ||||
-rw-r--r-- | openpgp/src/tpk/mod.rs | 6 | ||||
-rw-r--r-- | openpgp/src/tpk/parser/mod.rs | 2 | ||||
-rw-r--r-- | store/src/lib.rs | 28 |
15 files changed, 46 insertions, 44 deletions
diff --git a/examples/guide-exploring-openpgp.rs b/examples/guide-exploring-openpgp.rs index c5aba0dc..bb7f4572 100644 --- a/examples/guide-exploring-openpgp.rs +++ b/examples/guide-exploring-openpgp.rs @@ -5,7 +5,7 @@ use crate::openpgp::parse::Parse; fn main() { let tpk = - b"-----BEGIN PGP PUBLIC KEY BLOCK----- + "-----BEGIN PGP PUBLIC KEY BLOCK----- mQENBFpxtsABCADZcBa1Q3ZLZnju18o0+t8LoQuIIeyeUQ0H45y6xUqyrD5HSkVM VGQs6IHLq70mAizBJ4VznUVqVOh/NhOlapXi6/TKpjHvttdg45o6Pgqa0Kx64luT diff --git a/examples/guide-the-keystore.rs b/examples/guide-the-keystore.rs index f818137a..51fcb857 100644 --- a/examples/guide-the-keystore.rs +++ b/examples/guide-the-keystore.rs @@ -7,7 +7,7 @@ use crate::openpgp::parse::Parse; fn main() { let tpk = - b"-----BEGIN PGP PUBLIC KEY BLOCK----- + "-----BEGIN PGP PUBLIC KEY BLOCK----- mQENBFpxtsABCADZcBa1Q3ZLZnju18o0+t8LoQuIIeyeUQ0H45y6xUqyrD5HSkVM VGQs6IHLq70mAizBJ4VznUVqVOh/NhOlapXi6/TKpjHvttdg45o6Pgqa0Kx64luT diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index 517c859f..cdf3d1d9 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -333,8 +333,8 @@ impl<'a> Parse<'a, Message> for Message { /// See [`Message::from_packet_pile`] for more details. /// /// [`Message::from_packet_pile`]: #method.from_packet_pile - fn from_bytes(buf: &'a [u8]) -> Result<Self> { - Self::from_packet_pile(PacketPile::from_bytes(buf)?) + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<Self> { + Self::from_packet_pile(PacketPile::from_bytes(data)?) } } diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs index 0f62e948..55064fbd 100644 --- a/openpgp/src/packet/skesk.rs +++ b/openpgp/src/packet/skesk.rs @@ -437,7 +437,7 @@ mod test { 0xbd, 0x45, 0x6d, 0x17, 0x38, 0xc6, 0x3c, 0x36, ]; let packets: Vec<Packet> = - PacketPile::from_bytes(&raw).unwrap().into_children().collect(); + PacketPile::from_bytes(&raw[..]).unwrap().into_children().collect(); assert_eq!(packets.len(), 1); if let Packet::SKESK(SKESK::V5(ref s)) = packets[0] { assert_eq!(&s.s2k().derive_key( diff --git a/openpgp/src/packet/user_attribute.rs b/openpgp/src/packet/user_attribute.rs index 4e50e1dc..3b00ae01 100644 --- a/openpgp/src/packet/user_attribute.rs +++ b/openpgp/src/packet/user_attribute.rs @@ -277,7 +277,7 @@ mod tests { #[test] fn image() { use crate::Packet; - let p = Packet::from_bytes(b" + let p = Packet::from_bytes(" -----BEGIN PGP ARMORED FILE----- 0cFuwWwBEAABAQAAAAAAAAAAAAAAAP/Y/+AAEEpGSUYAAQEBASwBLAAA//4AE0Ny diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index d5958b7c..2db68ef3 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -57,9 +57,9 @@ impl<'a> Parse<'a, PacketPile> for PacketPile { /// Deserializes the OpenPGP message stored in the provided buffer. /// /// See `from_reader` for more details and caveats. - fn from_bytes(data: &'a [u8]) -> Result<PacketPile> { + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<PacketPile> { let bio = buffered_reader::Memory::with_cookie( - data, Cookie::default()); + data.as_ref(), Cookie::default()); PacketPile::from_buffered_reader(Box::new(bio)) } } diff --git a/openpgp/src/parse/packet_parser_builder.rs b/openpgp/src/parse/packet_parser_builder.rs index 6fe1e518..5719ddc4 100644 --- a/openpgp/src/parse/packet_parser_builder.rs +++ b/openpgp/src/parse/packet_parser_builder.rs @@ -59,10 +59,10 @@ impl<'a> Parse<'a, PacketParserBuilder<'a>> for PacketParserBuilder<'a> { /// Creates a `PacketParserBuilder` for an OpenPGP message stored /// in the specified buffer. - fn from_bytes(bytes: &'a [u8]) -> Result<PacketParserBuilder> { + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<PacketParserBuilder<'a>> { PacketParserBuilder::from_buffered_reader( Box::new(buffered_reader::Memory::with_cookie( - bytes, Cookie::default()))) + data.as_ref(), Cookie::default()))) } } diff --git a/openpgp/src/parse/packet_pile_parser.rs b/openpgp/src/parse/packet_pile_parser.rs index 36247e2e..815636df 100644 --- a/openpgp/src/parse/packet_pile_parser.rs +++ b/openpgp/src/parse/packet_pile_parser.rs @@ -107,10 +107,10 @@ impl<'a> Parse<'a, PacketPileParser<'a>> for PacketPileParser<'a> { /// Creates a `PacketPileParser` to parse the OpenPGP message stored /// in the provided buffer. - fn from_bytes(data: &'a [u8]) + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<PacketPileParser<'a>> { let bio = Box::new(buffered_reader::Memory::with_cookie( - data, Cookie::default())); + data.as_ref(), Cookie::default())); PacketPileParser::from_buffered_reader(bio) } } diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 896b4cf9..dc8cd7b2 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -90,8 +90,7 @@ pub trait Parse<'a, T> { /// implementations can provide their own specialized version. /// /// [`from_reader(..)`]: #tymethod.from_reader - fn from_bytes(data: &'a [u8]) -> Result<T> - { + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<T> { Self::from_reader(io::Cursor::new(data)) } } @@ -2582,9 +2581,9 @@ impl<'a> Parse<'a, PacketParserResult<'a>> for PacketParser<'a> { /// /// This function returns a `PacketParser` for the first packet in /// the stream. - fn from_bytes(bytes: &'a [u8]) + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<PacketParserResult<'a>> { - PacketParserBuilder::from_bytes(bytes)?.finalize() + PacketParserBuilder::from_bytes(data)?.finalize() } } diff --git a/openpgp/src/parse/sexp/mod.rs b/openpgp/src/parse/sexp/mod.rs index f5795725..5272ab35 100644 --- a/openpgp/src/parse/sexp/mod.rs +++ b/openpgp/src/parse/sexp/mod.rs @@ -34,8 +34,13 @@ impl<'a> Parse<'a, Sexp> for Sexp { buffered_reader::File::open(path)?.data_eof()?) } - fn from_bytes(data: &'a [u8]) -> Result<Sexp> - { + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<Sexp> { + Self::from_bytes_private(data.as_ref()) + } +} + +impl Sexp { + fn from_bytes_private(data: &[u8]) -> Result<Sexp> { match self::grammar::SexprParser::new().parse(Lexer::new(data)) { Ok(r) => Ok(r), Err(err) => { diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs index 8869eb9a..f33ce1af 100644 --- a/openpgp/src/serialize/stream.rs +++ b/openpgp/src/serialize/stream.rs @@ -231,8 +231,8 @@ impl<'a, R> Signer<'a, R> /// # use openpgp::crypto::KeyPair; /// # use openpgp::parse::Parse; /// # use openpgp::parse::stream::*; - /// # let tsk = TPK::from_bytes(include_bytes!( - /// # "../../tests/data/keys/testy-new-private.pgp")) + /// # let tsk = TPK::from_bytes(&include_bytes!( + /// # "../../tests/data/keys/testy-new-private.pgp")[..]) /// # .unwrap(); /// # let keypair = tsk.keys_valid().signing_capable().nth(0).unwrap().2 /// # .clone().mark_parts_secret().into_keypair().unwrap(); @@ -317,8 +317,8 @@ impl<'a, R> Signer<'a, R> /// # use openpgp::crypto::KeyPair; /// # use openpgp::parse::Parse; /// # use openpgp::parse::stream::*; - /// # let tsk = TPK::from_bytes(include_bytes!( - /// # "../../tests/data/keys/testy-new-private.pgp")) + /// # let tsk = TPK::from_bytes(&include_bytes!( + /// # "../../tests/data/keys/testy-new-private.pgp")[..]) /// # .unwrap(); /// # let keypair = tsk.keys_valid().signing_capable().nth(0).unwrap().2 /// # .clone().mark_parts_secret().into_keypair().unwrap(); @@ -931,7 +931,7 @@ impl<'a> Encryptor<'a> { /// # fn f() -> Result<()> { /// let tpk = openpgp::TPK::from_bytes( /// # // We do some acrobatics here to abbreviate the TPK. - /// b"-----BEGIN PGP PUBLIC KEY BLOCK----- + /// "-----BEGIN PGP PUBLIC KEY BLOCK----- /// /// mQENBFpxtsABCADZcBa1Q3ZLZnju18o0+t8LoQuIIeyeUQ0H45y6xUqyrD5HSkVM /// # VGQs6IHLq70mAizBJ4VznUVqVOh/NhOlapXi6/TKpjHvttdg45o6Pgqa0Kx64luT diff --git a/openpgp/src/serialize/tpk_armored.rs b/openpgp/src/serialize/tpk_armored.rs index 3c602965..7ab2405b 100644 --- a/openpgp/src/serialize/tpk_armored.rs +++ b/openpgp/src/serialize/tpk_armored.rs @@ -199,9 +199,7 @@ mod tests { #[test] fn serialize_succeed() { - let tpk = TPK::from_bytes(include_bytes!( - "../../tests/data/keys/neal.pgp")) - .unwrap(); + let tpk = TPK::from_bytes(crate::tests::key("neal.pgp")).unwrap(); // Enarmor the TPK. let mut buffer = Vec::new(); diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs index fbc141a4..6396cb6f 100644 --- a/openpgp/src/tpk/mod.rs +++ b/openpgp/src/tpk/mod.rs @@ -1003,8 +1003,8 @@ impl<'a> Parse<'a, TPK> for TPK { /// Returns the first TPK found in `buf`. /// /// `buf` must be an OpenPGP-encoded message. - fn from_bytes(buf: &[u8]) -> Result<Self> { - TPK::from_packet_parser(PacketParser::from_bytes(buf)?) + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<Self> { + TPK::from_packet_parser(PacketParser::from_bytes(data)?) } } @@ -2963,7 +2963,7 @@ mod test { #[test] fn issue_120() { - let tpk = b" + let tpk = " -----BEGIN PGP ARMORED FILE----- xcBNBFoVcvoBCACykTKOJddF8SSUAfCDHk86cNTaYnjCoy72rMgWJsrMLnz/V16B diff --git a/openpgp/src/tpk/parser/mod.rs b/openpgp/src/tpk/parser/mod.rs index a4d118b8..ac7d8162 100644 --- a/openpgp/src/tpk/parser/mod.rs +++ b/openpgp/src/tpk/parser/mod.rs @@ -462,7 +462,7 @@ impl<'a> Parse<'a, TPKParser<'a, vec::IntoIter<Packet>>> } /// Initializes a `TPKParser` from a byte string. - fn from_bytes(data: &'a [u8]) -> Result<Self> { + fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<Self> { Ok(Self::from_packet_parser(PacketParser::from_bytes(data)?)) } } diff --git a/store/src/lib.rs b/store/src/lib.rs index 9e710a9b..59e73063 100644 --- a/store/src/lib.rs +++ b/store/src/lib.rs @@ -157,7 +157,7 @@ impl Store { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let tpk = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// let key = Store::import(&ctx, &tpk)?; /// assert_eq!(key.tpk()?.fingerprint(), tpk.fingerprint()); /// # Ok(()) @@ -193,7 +193,7 @@ impl Store { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let tpk = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// Store::import(&ctx, &tpk)?; /// let key = Store::lookup(&ctx, &tpk.fingerprint())?; /// assert_eq!(key.tpk()?.fingerprint(), tpk.fingerprint()); @@ -228,7 +228,7 @@ impl Store { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let tpk = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// Store::import(&ctx, &tpk)?; /// let key = Store::lookup_by_keyid(&ctx, &tpk.fingerprint().to_keyid())?; /// assert_eq!(key.tpk()?.fingerprint(), tpk.fingerprint()); @@ -264,7 +264,7 @@ impl Store { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let tpk = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/neal.pgp")) + /// # &include_bytes!("../../openpgp/tests/data/keys/neal.pgp")[..]) /// # .unwrap(); /// Store::import(&ctx, &tpk)?; /// @@ -412,7 +412,7 @@ impl Mapping { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let tpk = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; /// mapping.import("Testy McTestface", &tpk)?; /// # Ok(()) @@ -483,7 +483,7 @@ impl Mapping { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let tpk = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/emmelie-dorothea-dina-samantha-awina-ed25519.pgp")) + /// # &include_bytes!("../../openpgp/tests/data/keys/emmelie-dorothea-dina-samantha-awina-ed25519.pgp")[..]) /// # .unwrap(); /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; /// mapping.import("Emmelie", &tpk)?; @@ -683,9 +683,9 @@ impl Binding { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let old = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// # let new = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap(); /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; /// mapping.import("Testy McTestface", &old)?; /// // later... @@ -738,9 +738,9 @@ impl Binding { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let old = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// # let new = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap(); /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; /// mapping.import("Testy McTestface", &old)?; /// // later... @@ -890,9 +890,9 @@ impl Key { /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let old = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// # let new = TPK::from_bytes( - /// # include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")).unwrap(); + /// # &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap(); /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; /// let fp = Fingerprint::from_hex("3E8877C877274692975189F5D03F6F865226FE8B").unwrap(); /// let binding = mapping.add("Testy McTestface", &fp)?; @@ -1267,7 +1267,7 @@ mod test { .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); - let tpk = TPK::from_bytes(bytes!("testy.pgp")).unwrap(); + let tpk = TPK::from_bytes(&bytes!("testy.pgp")[..]).unwrap(); mapping.import("Mr. McTestface", &tpk).unwrap(); let binding = mapping.lookup("Mr. McTestface").unwrap(); let tpk_retrieved = binding.tpk().unwrap(); @@ -1295,7 +1295,7 @@ mod test { .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); - let tpk = TPK::from_bytes(bytes!("testy.pgp")).unwrap(); + let tpk = TPK::from_bytes(&bytes!("testy.pgp")[..]).unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); let binding = mapping.add("Mister B.", &fp).unwrap(); let r = binding.import(&tpk); |