storm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kabh...@apache.org
Subject [2/7] storm git commit: STORM-1235: port backtype.storm.security.auth.ReqContext-test to java
Date Thu, 07 Jul 2016 14:26:34 GMT
STORM-1235: port backtype.storm.security.auth.ReqContext-test to java


Project: http://git-wip-us.apache.org/repos/asf/storm/repo
Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/7d43b39e
Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/7d43b39e
Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/7d43b39e

Branch: refs/heads/master
Commit: 7d43b39e44735e9863ee376e32899ceae46bed0b
Parents: fe5d37a
Author: Abhishek Agarwal <abhishek.agarwal@inmobi.com>
Authored: Wed Mar 9 22:27:34 2016 +0530
Committer: Abhishek Agarwal <abhishek.agarwal@inmobi.com>
Committed: Thu Mar 10 18:08:20 2016 +0530

----------------------------------------------------------------------
 .../storm/security/auth/ReqContext_test.clj     | 73 ----------------
 .../storm/security/auth/ReqContextTest.java     | 87 ++++++++++++++++++++
 2 files changed, 87 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/storm/blob/7d43b39e/storm-core/test/clj/org/apache/storm/security/auth/ReqContext_test.clj
----------------------------------------------------------------------
diff --git a/storm-core/test/clj/org/apache/storm/security/auth/ReqContext_test.clj b/storm-core/test/clj/org/apache/storm/security/auth/ReqContext_test.clj
deleted file mode 100644
index dfce492..0000000
--- a/storm-core/test/clj/org/apache/storm/security/auth/ReqContext_test.clj
+++ /dev/null
@@ -1,73 +0,0 @@
-;; 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.
-(ns org.apache.storm.security.auth.ReqContext-test
-  (:import [org.apache.storm.security.auth ReqContext])
-  (:import [java.net InetAddress])
-  (:import [java.security AccessControlContext Principal])
-  (:import [javax.security.auth Subject])
-  (:use [clojure test])
-)
-
-(def test-subject
-  (let [rc (ReqContext/context)
-        expected (Subject.)]
-    (is (not (.isReadOnly expected)))
-    (.setSubject rc expected)
-    (is (= (.subject rc) expected))
-
-    ; Change the Subject by setting read-only.
-    (.setReadOnly expected)
-    (.setSubject rc expected)
-    (is (= (.subject rc) expected))
-  )
-)
-
-(deftest test-remote-address
-  (let [rc (ReqContext/context)
-        expected (InetAddress/getByAddress (.getBytes "ABCD"))]
-    (.setRemoteAddress rc expected)
-    (is (= (.remoteAddress rc) expected))
-  )
-)
-
-(deftest test-principal-returns-null-when-no-subject
-  (let [rc (ReqContext/context)]
-    (.setSubject rc (Subject.))
-    (is (nil? (.principal rc)))
-  )
-)
-
-(def principal-name "Test Principal")
-
-(defn TestPrincipal []
-  (reify Principal
-    (^String getName [this]
-      principal-name)
-  )
-)
-
-(deftest test-principal
-  (let [p (TestPrincipal)
-        principals (hash-set p)
-        creds (hash-set)
-        s (Subject. false principals creds, creds)
-        rc (ReqContext/context)]
-    (.setSubject rc s)
-    (is (not (nil? (.principal rc))))
-    (is (= (-> rc .principal .getName) principal-name))
-    (.setSubject rc nil)
-  )
-)

http://git-wip-us.apache.org/repos/asf/storm/blob/7d43b39e/storm-core/test/jvm/org/apache/storm/security/auth/ReqContextTest.java
----------------------------------------------------------------------
diff --git a/storm-core/test/jvm/org/apache/storm/security/auth/ReqContextTest.java b/storm-core/test/jvm/org/apache/storm/security/auth/ReqContextTest.java
new file mode 100644
index 0000000..ee93e2c
--- /dev/null
+++ b/storm-core/test/jvm/org/apache/storm/security/auth/ReqContextTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.storm.security.auth;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.security.Principal;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.security.auth.Subject;
+
+public class ReqContextTest {
+
+    private ReqContext rc;
+
+    @Before
+    public void setup() {
+        rc = ReqContext.context();
+    }
+
+    @Test
+    public void testSubject() {
+        Subject expected = new Subject();
+        Assert.assertFalse(expected.isReadOnly());
+        rc.setSubject(expected);
+        Assert.assertEquals(expected, rc.subject());
+
+        expected.setReadOnly();
+        rc.setSubject(expected);
+        Assert.assertEquals(expected, rc.subject());
+    }
+
+    @Test
+    public void testRemoteAddress() throws UnknownHostException {
+        InetAddress expected = InetAddress.getByAddress("ABCD".getBytes());
+        rc.setRemoteAddress(expected);
+        Assert.assertEquals(expected, rc.remoteAddress());
+    }
+
+    /**
+     * If subject has no principals, request context should return null principal
+     */
+    @Test
+    public void testPrincipalReturnsNullWhenNoSubject() {
+        rc.setSubject(new Subject());
+        Assert.assertNull(rc.principal());
+    }
+
+    @Test
+    public void testPrincipal() {
+        final String principalName = "Test Principal";
+        Principal testPrincipal = new Principal() {
+            @Override
+            public String getName() {
+                return principalName;
+            }
+        };
+        Set<Principal> principals = ImmutableSet.of(testPrincipal);
+        Subject subject = new Subject(false, principals, new HashSet<>(), new HashSet<>());
+        rc.setSubject(subject);
+        Assert.assertNotNull(rc.principal());
+        Assert.assertEquals(principalName, rc.principal().getName());
+        rc.setSubject(null);
+    }
+}


Mime
View raw message