فهرست منبع

ZOOKEEPER-374. Uninitialized struct variable in C causes warning which is treated as an error (phunt via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@765746 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 16 سال پیش
والد
کامیت
b82c45bd81
3فایلهای تغییر یافته به همراه56 افزوده شده و 2 حذف شده
  1. 3 0
      CHANGES.txt
  2. 4 1
      src/c/src/zookeeper.c
  3. 49 1
      src/c/tests/TestClient.cc

+ 3 - 0
CHANGES.txt

@@ -48,6 +48,9 @@ BUGFIXES:
   ZOOKEEPER-355. make validatePath non public in Zookeeper client api. (phunt
 via mahadev)
 
+  ZOOKEEPER-374. Uninitialized struct variable in C causes warning which 
+is treated as an error (phunt via mahadev)
+
 IMPROVEMENTS:
   ZOOKEEPER-308. improve the atomic broadcast performance 3x.
   (breed via mahadev)

+ 4 - 1
src/c/src/zookeeper.c

@@ -2390,13 +2390,16 @@ int zoo_add_auth(zhandle_t *zh,const char* scheme,const char* cert,
         }
         memcpy(auth.buff,cert,certLen);
         auth.len=certLen;
+    } else {
+        auth.buff = 0;
+        auth.len = 0;
     }
 
     zoo_lock_auth(zh);
 
     free_auth_info(&zh->auth);
     zh->auth.scheme=strdup(scheme);
-    if(cert!=NULL && certLen!=0)
+    if(auth.buff)
         zh->auth.auth=auth;
     zh->auth.completion=completion;
     zh->auth.data=data;

+ 49 - 1
src/c/tests/TestClient.cc

@@ -160,6 +160,7 @@ class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture
     CPPUNIT_TEST(testPathValidation);
     CPPUNIT_TEST(testPing);
     CPPUNIT_TEST(testAcl);
+    CPPUNIT_TEST(testAuth);
     CPPUNIT_TEST(testWatcherAutoResetWithGlobal);
     CPPUNIT_TEST(testWatcherAutoResetWithLocal);
 #endif
@@ -286,6 +287,10 @@ public:
         }
     }
 
+    static void voidCompletion(int rc, const void *data) {
+        CPPUNIT_ASSERT_EQUAL((int)data, rc);
+    }
+
     static void verifyCreateFails(const char *path, zhandle_t *zk) {
       CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk,
           path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0));
@@ -332,7 +337,6 @@ public:
 
     void testAcl() {
         int rc;
-        struct String_vector strings;
         struct ACL_vector aclvec;
         struct Stat stat;
         watchctx_t ctx;
@@ -353,6 +357,50 @@ public:
     }
 
 
+    void testAuth() {
+        int rc;
+
+        watchctx_t ctx1;
+        zhandle_t *zk = createClient(&ctx1);
+
+        rc = zoo_add_auth(0, "", 0, 0, voidCompletion, (void*)-1);
+        CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
+
+        rc = zoo_add_auth(zk, 0, 0, 0, voidCompletion, (void*)-1);
+        CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
+
+        // auth as pat, create /tauth1, close session
+        rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion,
+                          (void*)ZOK);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+
+        rc = zoo_create(zk, "/tauth1", "", 0, &ZOO_CREATOR_ALL_ACL, 0, 0, 0);
+        CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
+
+        // auth as pat w/bad pass, access /tauth1, verify failure
+        watchctx_t ctx2;
+        zk = createClient(&ctx2);
+
+        rc = zoo_add_auth(zk, "digest", "pat:passwd2", 11, voidCompletion,
+                          (void*)ZOK);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+
+        char buf[1024];
+        int blen = sizeof(buf);
+        struct Stat stat;
+        rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
+        CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc);
+
+        // add auth pat w/correct pass verify success
+        rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion,
+                          (void*)ZOK);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+
+        rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
+        CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
+    }
+
+
     void testPathValidation() {
         watchctx_t ctx;
         zhandle_t *zk = createClient(&ctx);