You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ar...@apache.org on 2020/01/20 08:08:17 UTC

[zookeeper] branch branch-3.6 updated: ZOOKEEPER-3613: ZKConfig fails to return proper value on getBoolean()when user accidentally includes spaces at the end of the value

This is an automated email from the ASF dual-hosted git repository.

arshad pushed a commit to branch branch-3.6
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/branch-3.6 by this push:
     new 1066de7  ZOOKEEPER-3613: ZKConfig fails to return proper value on getBoolean()when user accidentally includes spaces at the end of the value
1066de7 is described below

commit 1066de70f21e31f394be57b970d6d4b079a04e4d
Author: Sujith Simon <su...@huawei.com>
AuthorDate: Mon Jan 20 13:36:59 2020 +0530

    ZOOKEEPER-3613: ZKConfig fails to return proper value on getBoolean()when user accidentally includes spaces at the end of the value
    
    Author: sujithsimon22 <su...@huawei.com>
    
    Reviewers: Allan Lyu <fa...@apache.org>,Justin Mao Ling <ma...@sina.com>,Mohammad Arshad <ar...@apache.org>
    
    Closes #1190 from sujithsimon22/3613
    
    (cherry picked from commit dcef1a6f33d8120a93f011da1870a503aafb0312)
    Signed-off-by: Mohammad Arshad <ar...@apache.org>
---
 .../java/org/apache/zookeeper/common/ZKConfig.java |  2 +-
 .../org/apache/zookeeper/common/ZKConfigTest.java  | 96 ++++++++++++++++++++++
 2 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java
index 11cdc06..6bbe698 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java
@@ -253,7 +253,7 @@ public class ZKConfig {
         if (propertyValue == null) {
             return defaultValue;
         } else {
-            return Boolean.parseBoolean(propertyValue);
+            return Boolean.parseBoolean(propertyValue.trim());
         }
     }
 
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKConfigTest.java
new file mode 100644
index 0000000..eff1bfb
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKConfigTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.common;
+
+
+import static org.junit.Assert.assertEquals;
+import java.util.concurrent.TimeUnit;
+import org.junit.After;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.Timeout;
+
+
+
+public class ZKConfigTest {
+
+    X509Util x509Util = new ClientX509Util();
+
+    @Rule
+    public Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
+
+    @After
+    public void tearDown() throws Exception {
+        System.clearProperty(x509Util.getSslProtocolProperty());
+    }
+
+    // property is not set we should get the default value
+    @Test
+    public void testBooleanRetrievalFromPropertyDefault() {
+        ZKConfig conf = new ZKConfig();
+        String prop = "UnSetProperty" + System.currentTimeMillis();
+        boolean defaultValue = false;
+        boolean result = conf.getBoolean(prop, defaultValue);
+        assertEquals(defaultValue, result);
+    }
+
+    // property is set to an valid boolean, we should get the set value
+    @Test
+    public void testBooleanRetrievalFromProperty() {
+        boolean value = true;
+        boolean defaultValue = false;
+        System.setProperty(x509Util.getSslProtocolProperty(), Boolean.toString(value));
+        ZKConfig conf = new ZKConfig();
+        boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue);
+        assertEquals(value, result);
+    }
+
+    // property is set but with white spaces in the beginning
+    @Test
+    public void testBooleanRetrievalFromPropertyWithWhitespacesInBeginning() {
+        boolean value = true;
+        boolean defaultValue = false;
+        System.setProperty(x509Util.getSslProtocolProperty(), " " + value);
+        ZKConfig conf = new ZKConfig();
+        boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue);
+        assertEquals(value, result);
+    }
+
+    // property is set but with white spaces at the end
+    @Test
+    public void testBooleanRetrievalFromPropertyWithWhitespacesAtEnd() {
+        boolean value = true;
+        boolean defaultValue = false;
+        System.setProperty(x509Util.getSslProtocolProperty(), value + " ");
+        ZKConfig conf = new ZKConfig();
+        boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue);
+        assertEquals(value, result);
+    }
+
+    // property is set but with white spaces at the beginning and the end
+    @Test
+    public void testBooleanRetrievalFromPropertyWithWhitespacesAtBeginningAndEnd() {
+        boolean value = true;
+        boolean defaultValue = false;
+        System.setProperty(x509Util.getSslProtocolProperty(), " " + value + " ");
+        ZKConfig conf = new ZKConfig();
+        boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue);
+        assertEquals(value, result);
+    }
+}