From 523aa36c71e2a4c1fbf41f9555221e81d1b41ea7 Mon Sep 17 00:00:00 2001 From: jgraham Date: Fri, 6 Jan 2017 17:22:11 +0000 Subject: [PATCH] webdriver: Fix check that key codes in actions are a single char. (#58) Previously this used String.len() which was incorrect since it counts bytes, not characters. Moving to a chars() iterator fixes the issue. Source-Repo: https://github.com/mozilla/webdriver-rust Source-Revision: 200afc084c8426b5dbc678a0c85dbb3c20c37777 committer: GitHub --HG-- extra : subtree_source : http%3A//tristan.corp.lon2.mozilla.com%3A8000 extra : subtree_revision : 3615c68aac49e5f819dc87969e8461f0abbf37c8 --- testing/webdriver/src/command.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/testing/webdriver/src/command.rs b/testing/webdriver/src/command.rs index 40b7e19576f5..7cfb2325b808 100644 --- a/testing/webdriver/src/command.rs +++ b/testing/webdriver/src/command.rs @@ -1398,6 +1398,23 @@ impl ToJson for KeyAction { } } +fn validate_key_value(value_str: &str) -> WebDriverResult { + let mut chars = value_str.chars(); + let value = if let Some(c) = chars.next() { + c + } else { + return Err(WebDriverError::new( + ErrorStatus::InvalidArgument, + "Parameter 'value' was an empty string")) + }; + if chars.next().is_some() { + return Err(WebDriverError::new( + ErrorStatus::InvalidArgument, + "Parameter 'value' contained multiple characters")) + }; + Ok(value) +} + #[derive(PartialEq)] pub struct KeyUpAction { pub value: char @@ -1411,13 +1428,8 @@ impl Parameters for KeyUpAction { "Missing value parameter").as_string(), ErrorStatus::InvalidArgument, "Parameter 'value' was not a string"); - if value_str.len() != 1 { - return Err(WebDriverError::new( - ErrorStatus::InvalidArgument, - "Key code was not a single char")) - } - let value = value_str.chars().next().unwrap(); + let value = try!(validate_key_value(value_str)); Ok(KeyUpAction { value: value }) @@ -1448,13 +1460,7 @@ impl Parameters for KeyDownAction { "Missing value parameter").as_string(), ErrorStatus::InvalidArgument, "Parameter 'value' was not a string"); - if value_str.len() != 1 { - return Err(WebDriverError::new( - ErrorStatus::InvalidArgument, - "Key code was not a single char")) - } - - let value = value_str.chars().next().unwrap(); + let value = try!(validate_key_value(value_str)); Ok(KeyDownAction { value: value })