[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] rust/protover: parse empty string into empty *ProtoEntry



commit 44facb83d5b8801ec53135fab8a1f610d468fcd1
Author: cypherpunks <cypherpunks@xxxxxxxxxxxxxx>
Date:   Sun Feb 9 01:15:18 2020 +0000

    rust/protover: parse empty string into empty *ProtoEntry
    
    Contrary to what 15e59a1fedf47e7e689e06e5649849552d8b8c0d
    said, the C implementation has always accepted an empty string
    without complaint. Meanwhile the rust implementation has always
    given an error. Make the rust implementation match C.
    
    Also eliminate some more redundant tests.
    
    Fix on 0.3.3.1-alpha.
---
 src/rust/protover/protover.rs       | 12 +++++++++++-
 src/rust/protover/tests/protover.rs | 13 -------------
 2 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/src/rust/protover/protover.rs b/src/rust/protover/protover.rs
index 383a781fdc..14a70cd42b 100644
--- a/src/rust/protover/protover.rs
+++ b/src/rust/protover/protover.rs
@@ -245,6 +245,11 @@ impl FromStr for ProtoEntry {
     /// Otherwise, the `Err` value of this `Result` is a `ProtoverError`.
     fn from_str(protocol_entry: &str) -> Result<ProtoEntry, ProtoverError> {
         let mut proto_entry: ProtoEntry = ProtoEntry::default();
+
+        if protocol_entry.is_empty() {
+            return Ok(proto_entry);
+        }
+
         let entries = protocol_entry.split(' ');
 
         for entry in entries {
@@ -493,6 +498,10 @@ impl UnvalidatedProtoEntry {
     ) -> Result<Vec<(&'a str, &'a str)>, ProtoverError> {
         let mut protovers: Vec<(&str, &str)> = Vec::new();
 
+        if protocol_string.is_empty() {
+            return Ok(protovers);
+        }
+
         for subproto in protocol_string.split(' ') {
             let mut parts = subproto.splitn(2, '=');
 
@@ -851,7 +860,8 @@ mod test {
 
     #[test]
     fn test_protoentry_from_str_empty() {
-        assert_protoentry_is_unparseable!("");
+        assert_protoentry_is_parseable!("");
+        assert!(UnvalidatedProtoEntry::from_str("").is_ok());
     }
 
     #[test]
diff --git a/src/rust/protover/tests/protover.rs b/src/rust/protover/tests/protover.rs
index cf0fb5bd3a..c97810a6f2 100644
--- a/src/rust/protover/tests/protover.rs
+++ b/src/rust/protover/tests/protover.rs
@@ -69,18 +69,6 @@ fn protocol_all_supported_with_one_value() {
     assert_eq!(true, unsupported.is_none());
 }
 
-#[test]
-#[should_panic]
-fn parse_protocol_unvalidated_with_empty() {
-    let _: UnvalidatedProtoEntry = "".parse().unwrap();
-}
-
-#[test]
-#[should_panic]
-fn parse_protocol_validated_with_empty() {
-    let _: ProtoEntry = "".parse().unwrap();
-}
-
 #[test]
 fn protocol_all_supported_with_three_values() {
     let protocols: UnvalidatedProtoEntry = "LinkAuth=1 Microdesc=1-2 Relay=2".parse().unwrap();
@@ -156,7 +144,6 @@ fn parse_protocol_with_unexpected_characters() {
 }
 
 #[test]
-#[should_panic]
 fn protover_compute_vote_returns_empty_for_empty_string() {
     let protocols: &[UnvalidatedProtoEntry] = &["".parse().unwrap()];
     let listed = ProtoverVote::compute(protocols, &1);



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits