diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopBackendService.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopBackendService.scala index d08f53aca..1ef0813ed 100644 --- a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopBackendService.scala +++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopBackendService.scala @@ -17,8 +17,16 @@ package org.apache.kyuubi.service +import org.apache.kyuubi.KyuubiException import org.apache.kyuubi.session.{NoopSessionManager, SessionManager} class NoopBackendService extends AbstractBackendService("noop") { override val sessionManager: SessionManager = new NoopSessionManager() + + override def start(): Unit = { + if (conf.getOption("kyuubi.test.backend.should.fail").exists(_.toBoolean)) { + throw new KyuubiException("should fail backend") + } + super.start() + } } diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopServer.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopServer.scala index 72ecfa1ff..890923f81 100644 --- a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopServer.scala +++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/NoopServer.scala @@ -24,7 +24,7 @@ class NoopServer extends Serverable("noop") { override def start(): Unit = { super.start() - if (getConf.getOption("kyuubi.test.should.fail").exists(_.toBoolean)) { + if (getConf.getOption("kyuubi.test.server.should.fail").exists(_.toBoolean)) { throw new IllegalArgumentException("should fail") } } diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/ServerableSuite.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/ServerableSuite.scala index f5fdba363..86941e89a 100644 --- a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/ServerableSuite.scala +++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/ServerableSuite.scala @@ -61,10 +61,19 @@ class ServerableSuite extends KyuubiFunSuite { test("error start child services") { val conf = KyuubiConf() .set(KyuubiConf.FRONTEND_BIND_PORT, 0) - .set("kyuubi.test.should.fail", "true") + .set("kyuubi.test.server.should.fail", "true") val server = new NoopServer() server.initialize(conf) val e = intercept[IllegalArgumentException](server.start()) assert(e.getMessage === "should fail") + + conf + .set("kyuubi.test.server.should.fail", "false") + .set("kyuubi.test.backend.should.fail", "true") + val server1 = new NoopServer() + server1.initialize(conf) + val e1 = intercept[KyuubiException](server1.start()) + assert(e1.getMessage === "Failed to Start noop") + assert(e1.getCause.getMessage === "should fail backend") } } diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/PlainSASLServerSuite.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/PlainSASLServerSuite.scala new file mode 100644 index 000000000..e46be31e0 --- /dev/null +++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/PlainSASLServerSuite.scala @@ -0,0 +1,85 @@ +/* + * 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.kyuubi.service.authentication + +import java.util.Collections +import javax.security.auth.callback.{Callback, CallbackHandler} +import javax.security.sasl.{AuthorizeCallback, SaslException} + +import org.apache.kyuubi.KyuubiFunSuite +import org.apache.kyuubi.service.authentication.PlainSASLServer.SaslPlainServerFactory + +class PlainSASLServerSuite extends KyuubiFunSuite { + + test("SaslPlainServerFactory") { + val plainServerFactory = new SaslPlainServerFactory() + val map = Collections.emptyMap[String, String]() + assert(plainServerFactory.getMechanismNames(map) === + Array(PlainSASLServer.PLAIN_METHOD)) + val ch = new CallbackHandler { + override def handle(callbacks: Array[Callback]): Unit = callbacks.foreach { + case ac: AuthorizeCallback => ac.setAuthorized(true) + case _ => + } + } + + val s1 = plainServerFactory.createSaslServer("INVALID", "", "", map, ch) + assert(s1 === null) + val s2 = plainServerFactory.createSaslServer(PlainSASLServer.PLAIN_METHOD, "", "", map, ch) + assert(s2 === null) + + val server = plainServerFactory.createSaslServer( + PlainSASLServer.PLAIN_METHOD, AuthMethods.NONE.toString, "KYUUBI", map, ch) + assert(server.getMechanismName === PlainSASLServer.PLAIN_METHOD) + assert(!server.isComplete) + + val e1 = intercept[SaslException](server.evaluateResponse(Array())) + assert(e1.getMessage === "Error validating the login") + assert(e1.getCause.getMessage === "Invalid message format") + val e2 = intercept[SaslException](server.evaluateResponse(Array(0))) + assert(e2.getMessage === "Error validating the login") + assert(e2.getCause.getMessage === "No user name provided") + val e3 = intercept[SaslException](server.evaluateResponse(Array(1, 0))) + assert(e3.getMessage === "Error validating the login") + assert(e3.getCause.getMessage === "No password name provided") + val res4 = Array(97, 0, 99) + server.evaluateResponse(Array(97, 0, 99)) + assert(server.isComplete) + assert(server.getAuthorizationID === "a") + intercept[UnsupportedOperationException](server.wrap(res4.map(_.toByte), 0, 3)) + intercept[UnsupportedOperationException](server.unwrap(res4.map(_.toByte), 0, 3)) + assert(server.getNegotiatedProperty("name") === null) + val res5 = Array(1, 0, 1, 0, 1, 0) + val e5 = intercept[SaslException](server.evaluateResponse(Array(1, 0, 1, 0, 1, 0))) + assert(e5.getMessage === "Error validating the login") + assert(e5.getCause.getMessage === "Invalid message format") + assert(server.dispose().isInstanceOf[Unit]) + + val server2 = plainServerFactory.createSaslServer( + "PLAIN", + "NONE", + "KYUUBI", + map, new CallbackHandler { + override def handle(callbacks: Array[Callback]): Unit = {} + } + ) + val e6 = intercept[SaslException](server2.evaluateResponse(res4.map(_.toByte))) + assert(e6.getMessage === "Error validating the login") + assert(e6.getCause.getMessage === "Authentication failed") + } +} diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/TSetIpAddressProcessorSuite.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/TSetIpAddressProcessorSuite.scala new file mode 100644 index 000000000..7943fefea --- /dev/null +++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/TSetIpAddressProcessorSuite.scala @@ -0,0 +1,29 @@ +/* + * 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.kyuubi.service.authentication + +import org.apache.kyuubi.KyuubiFunSuite + +class TSetIpAddressProcessorSuite extends KyuubiFunSuite { + + test("initial value of user information") { + assert(TSetIpAddressProcessor.getUserIpAddress === null) + assert(TSetIpAddressProcessor.getUserName === null) + } + +} diff --git a/kyuubi-server/src/test/scala/yaooqinn/kyuubi/auth/PlainSaslServerSuite.scala b/kyuubi-server/src/test/scala/yaooqinn/kyuubi/auth/PlainSaslServerSuite.scala deleted file mode 100644 index 409a3d4e6..000000000 --- a/kyuubi-server/src/test/scala/yaooqinn/kyuubi/auth/PlainSaslServerSuite.scala +++ /dev/null @@ -1,118 +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. - */ - -package yaooqinn.kyuubi.auth - -import javax.security.auth.callback.{Callback, CallbackHandler} -import javax.security.sasl.{AuthorizeCallback, SaslException} - -import scala.collection.JavaConverters._ - -import org.apache.spark.SparkFunSuite - -import yaooqinn.kyuubi.auth - -class PlainSaslServerSuite extends SparkFunSuite { - - test("PLAIN_METHOD Const") { - assert(PlainSaslServer.PLAIN_METHOD === "PLAIN") - } - - test("Sasl Plain Server Factory ") { - val saslPlainServerFactory = new auth.PlainSaslServer.SaslPlainServerFactory - val emp = Map.empty[String, String].asJava - assert(saslPlainServerFactory.getMechanismNames(emp).head === - PlainSaslServer.PLAIN_METHOD) - val server = saslPlainServerFactory.createSaslServer( - "PLAIN", - "NONE", - "KYUUBI", - emp, new CallbackHandler { - override def handle(callbacks: Array[Callback]): Unit = callbacks.foreach { - case ac: AuthorizeCallback => ac.setAuthorized(true) - case _ => - } - } - ) - assert(server.isInstanceOf[PlainSaslServer]) - assert(server.getMechanismName === PlainSaslServer.PLAIN_METHOD) - assert(!server.isComplete) - - val res1 = Array.empty[Int] - val e1 = intercept[SaslException](server.evaluateResponse(res1.map(_.toByte))) - assert(e1.getMessage === "Error validating the login") - assert(e1.getCause.getMessage === "Invalid message format") - val res2 = Array(0) - val e2 = intercept[SaslException](server.evaluateResponse(res2.map(_.toByte))) - assert(e2.getMessage === "Error validating the login") - assert(e2.getCause.getMessage === "No user name provided") - val res3 = Array(1, 0) - val e3 = intercept[SaslException](server.evaluateResponse(res3.map(_.toByte))) - assert(e3.getMessage === "Error validating the login") - assert(e3.getCause.getMessage === "No password name provided") - val res4 = Array(97, 0, 99) - server.evaluateResponse(res4.map(_.toByte)) - assert(server.isComplete) - assert(server.getAuthorizationID === "a") - intercept[UnsupportedOperationException](server.wrap(res4.map(_.toByte), 0, 3)) - intercept[UnsupportedOperationException](server.unwrap(res4.map(_.toByte), 0, 3)) - assert(server.getNegotiatedProperty("name") === null) - val res5 = Array(1, 0, 1, 0, 1, 0) - val e5 = intercept[SaslException](server.evaluateResponse(res5.map(_.toByte))) - assert(e5.getMessage === "Error validating the login") - assert(e5.getCause.getMessage === "Invalid message format") - assert(server.dispose().isInstanceOf[Unit]) - - assert(saslPlainServerFactory.createSaslServer( - "ELSE", - "NONE", - "KYUUBI", - emp, new CallbackHandler { - override def handle(callbacks: Array[Callback]): Unit = callbacks.foreach { - case ac: AuthorizeCallback => ac.setAuthorized(true) - case _ => - } - } - ) === null) - - assert(saslPlainServerFactory.createSaslServer( - "PLAIN", - "ELSE", - "KYUUBI", - emp, new CallbackHandler { - override def handle(callbacks: Array[Callback]): Unit = callbacks.foreach { - case ac: AuthorizeCallback => ac.setAuthorized(true) - case _ => - } - } - ) === null) - - val server2 = saslPlainServerFactory.createSaslServer( - "PLAIN", - "NONE", - "KYUUBI", - emp, new CallbackHandler { - override def handle(callbacks: Array[Callback]): Unit = {} - } - ) - val e6 = intercept[SaslException](server2.evaluateResponse(res4.map(_.toByte))) - assert(e6.getMessage === "Error validating the login") - assert(e6.getCause.getMessage === "Authentication failed") - - } - -} diff --git a/kyuubi-server/src/test/scala/yaooqinn/kyuubi/auth/SaslQOPSuite.scala b/kyuubi-server/src/test/scala/yaooqinn/kyuubi/auth/SaslQOPSuite.scala deleted file mode 100644 index c3182ba4d..000000000 --- a/kyuubi-server/src/test/scala/yaooqinn/kyuubi/auth/SaslQOPSuite.scala +++ /dev/null @@ -1,42 +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. - */ - -package yaooqinn.kyuubi.auth - -import org.apache.spark.SparkFunSuite - -class SaslQOPSuite extends SparkFunSuite { - - test("SaslQOP toString") { - assert(new SaslQOP {}.toString === "auth, auth-int, auth-conf") - assert(SaslQOP.AUTH.toString === "auth") - assert(SaslQOP.AUTH_INT.toString === "auth-int") - assert(SaslQOP.AUTH_CONF.toString === "auth-conf") - } - - test("SaslQOP fromString") { - assert(SaslQOP.fromString("auth") === SaslQOP.AUTH) - assert(SaslQOP.fromString(SaslQOP.AUTH.toString) === SaslQOP.AUTH) - assert(SaslQOP.fromString("auth-int") === SaslQOP.AUTH_INT) - assert(SaslQOP.fromString(SaslQOP.AUTH_INT.toString) === SaslQOP.AUTH_INT) - assert(SaslQOP.fromString("auth-conf") === SaslQOP.AUTH_CONF) - assert(SaslQOP.fromString(SaslQOP.AUTH_CONF.toString) === SaslQOP.AUTH_CONF) - val e = intercept[IllegalArgumentException](SaslQOP.fromString("xxx")) - assert(e.getMessage.contains("xxx")) - } - -}