forked from nathanmarz/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for code from security changes
Add tests for SerializationFactory, utils, SaslTransportPlugin ThriftClient, ThriftServer, AuthUtils and ReqContext. Removes error log message in SerializationFactory before throwing RuntimeException. Introduces dev dependency on Mockito for certain tests.
- Loading branch information
Derek Dagit
committed
Mar 27, 2013
1 parent
a6f409e
commit c75dbf3
Showing
9 changed files
with
257 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(ns backtype.storm.security.auth.AuthUtils-test | ||
(:import [backtype.storm.security.auth AuthUtils]) | ||
(:import [java.io IOException]) | ||
(:import [javax.security.auth.login AppConfigurationEntry Configuration]) | ||
(:import [org.mockito Mockito]) | ||
(:use [clojure test]) | ||
) | ||
|
||
(deftest test-throws-on-missing-section | ||
(is (thrown? IOException | ||
(AuthUtils/get (Mockito/mock Configuration) "bogus-section" ""))) | ||
) | ||
|
||
(defn- mk-mock-app-config-entry [] | ||
(let [toRet (Mockito/mock AppConfigurationEntry)] | ||
(. (Mockito/when (.getOptions toRet)) thenReturn (hash-map)) | ||
toRet | ||
) | ||
) | ||
|
||
(deftest test-returns-null-if-no-such-section | ||
(let [entry (mk-mock-app-config-entry) | ||
entries (into-array (.getClass entry) [entry]) | ||
section "bogus-section" | ||
conf (Mockito/mock Configuration)] | ||
(. (Mockito/when (. conf getAppConfigurationEntry section )) | ||
thenReturn entries) | ||
(is (nil? (AuthUtils/get conf section "nonexistent-key"))) | ||
) | ||
) | ||
|
||
(deftest test-returns-first-value-for-valid-key | ||
(let [k "the-key" | ||
expected "good-value" | ||
empty-entry (mk-mock-app-config-entry) | ||
bad-entry (Mockito/mock AppConfigurationEntry) | ||
good-entry (Mockito/mock AppConfigurationEntry) | ||
conf (Mockito/mock Configuration)] | ||
(. (Mockito/when (.getOptions bad-entry)) thenReturn {k "bad-value"}) | ||
(. (Mockito/when (.getOptions good-entry)) thenReturn {k expected}) | ||
(let [entries (into-array (.getClass empty-entry) | ||
[empty-entry good-entry bad-entry]) | ||
section "bogus-section"] | ||
(. (Mockito/when (. conf getAppConfigurationEntry section)) | ||
thenReturn entries) | ||
(is (not (nil? (AuthUtils/get conf section k)))) | ||
(is (= (AuthUtils/get conf section k) expected)) | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
(ns backtype.storm.security.auth.ReqContext-test | ||
(:import [backtype.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)) | ||
) | ||
) |
28 changes: 28 additions & 0 deletions
28
test/clj/backtype/storm/security/auth/SaslTransportPlugin_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns backtype.storm.security.auth.SaslTransportPlugin-test | ||
(:use [clojure test]) | ||
(import [backtype.storm.security.auth SaslTransportPlugin$User]) | ||
) | ||
|
||
(deftest test-User-name | ||
(let [nam "Andy" | ||
user (SaslTransportPlugin$User. nam)] | ||
(are [a b] (= a b) | ||
nam (.toString user) | ||
(.getName user) (.toString user) | ||
(.hashCode nam) (.hashCode user) | ||
) | ||
) | ||
) | ||
|
||
(deftest test-User-equals | ||
(let [nam "Andy" | ||
user1 (SaslTransportPlugin$User. nam) | ||
user2 (SaslTransportPlugin$User. nam) | ||
user3 (SaslTransportPlugin$User. "Bobby")] | ||
(is (-> user1 (.equals user1))) | ||
(is (-> user1 (.equals user2))) | ||
(is (not (-> user1 (.equals nil)))) | ||
(is (not (-> user1 (.equals "Potato")))) | ||
(is (not (-> user1 (.equals user3)))) | ||
) | ||
) |
26 changes: 26 additions & 0 deletions
26
test/clj/backtype/storm/security/auth/ThriftClient_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns backtype.storm.security.auth.ThriftClient-test | ||
(:use [backtype.storm config]) | ||
(:use [clojure test]) | ||
(:import [backtype.storm.security.auth ThriftClient]) | ||
(:import [org.apache.thrift7.transport TTransportException]) | ||
) | ||
|
||
(deftest test-ctor-throws-if-port-invalid | ||
(let [conf (read-default-config) | ||
timeout (Integer. 30)] | ||
(is (thrown? java.lang.IllegalArgumentException | ||
(ThriftClient. conf "bogushost" -1 timeout))) | ||
(is (thrown? java.lang.IllegalArgumentException | ||
(ThriftClient. conf "bogushost" 0 timeout))) | ||
) | ||
) | ||
|
||
(deftest test-ctor-throws-if-host-not-set | ||
(let [conf (read-default-config) | ||
timeout (Integer. 60)] | ||
(is (thrown? TTransportException | ||
(ThriftClient. conf "" 4242 timeout))) | ||
(is (thrown? IllegalArgumentException | ||
(ThriftClient. conf nil 4242 timeout))) | ||
) | ||
) |
14 changes: 14 additions & 0 deletions
14
test/clj/backtype/storm/security/auth/ThriftServer_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(ns backtype.storm.security.auth.ThriftServer-test | ||
(:use [backtype.storm config]) | ||
(:use [clojure test]) | ||
(:import [backtype.storm.security.auth ThriftServer]) | ||
(:import [org.apache.thrift7.transport TTransportException]) | ||
) | ||
|
||
(deftest test-stop-checks-for-null | ||
(let [server (ThriftServer. (read-default-config) nil 12345)] | ||
(.stop server))) | ||
|
||
(deftest test-isServing-checks-for-null | ||
(let [server (ThriftServer. (read-default-config) nil 12345)] | ||
(is (not (.isServing server))))) |
39 changes: 39 additions & 0 deletions
39
test/clj/backtype/storm/serialization/SerializationFactory_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(ns backtype.storm.serialization.SerializationFactory-test | ||
(:import [backtype.storm Config]) | ||
(:import [backtype.storm.security.serialization BlowfishTupleSerializer]) | ||
(:import [backtype.storm.serialization SerializationFactory]) | ||
(:import [backtype.storm.utils ListDelegate]) | ||
(:use [backtype.storm config]) | ||
(:use [clojure test]) | ||
) | ||
|
||
|
||
(deftest test-registers-default-when-not-in-conf | ||
(let [conf (read-default-config) | ||
klass-name (get conf Config/TOPOLOGY_TUPLE_SERIALIZER) | ||
configured-class (Class/forName klass-name) | ||
kryo (SerializationFactory/getKryo conf)] | ||
(is (= configured-class (.getClass (.getSerializer kryo ListDelegate)))) | ||
) | ||
) | ||
|
||
(deftest test-throws-runtimeexception-when-no-such-class | ||
(let [conf (merge (read-default-config) | ||
{Config/TOPOLOGY_TUPLE_SERIALIZER "null.this.class.does.not.exist"})] | ||
(is (thrown? RuntimeException | ||
(SerializationFactory/getKryo conf))) | ||
) | ||
) | ||
|
||
(deftest test-registeres-when-valid-class-name | ||
(let [arbitrary-class-name | ||
(String. "backtype.storm.security.serialization.BlowfishTupleSerializer") | ||
serializer-class (Class/forName arbitrary-class-name) | ||
arbitrary-key "0123456789abcdef" | ||
conf (merge (read-default-config) | ||
{Config/TOPOLOGY_TUPLE_SERIALIZER arbitrary-class-name | ||
BlowfishTupleSerializer/SECRET_KEY arbitrary-key}) | ||
kryo (SerializationFactory/getKryo conf)] | ||
(is (= serializer-class (.getClass (.getSerializer kryo ListDelegate)))) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters