001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap.sasl;
019:
020: import java.util.Hashtable;
021:
022: import javax.naming.AuthenticationNotSupportedException;
023: import javax.naming.Context;
024:
025: import junit.framework.TestCase;
026:
027: public class SaslBindTest extends TestCase {
028: private final String DIGEST_MD5 = "DIGEST-MD5";
029:
030: private final String CRAM_MD5 = "CRAM-MD5";
031:
032: private final String GSSAPI = "GSSAPI";
033:
034: private final String EXTERNAL = "EXTERNAL";
035:
036: private SaslBind sbind = new SaslBind();
037:
038: private Hashtable<String, Object> env = new Hashtable<String, Object>();
039:
040: public void setUp() throws Exception {
041: super .setUp();
042:
043: env.clear();
044: }
045:
046: public void tearDown() throws Exception {
047: super .tearDown();
048: }
049:
050: public void test_valueAuthMech_none()
051: throws AuthenticationNotSupportedException {
052: sbind.valueAuthMech(env);
053: assertEquals(SaslBind.AuthMech.None, sbind.getAuthMech());
054:
055: env.put(Context.SECURITY_CREDENTIALS, "test_credentials");
056: sbind.valueAuthMech(env);
057: assertEquals(SaslBind.AuthMech.None, sbind.getAuthMech());
058:
059: env.clear();
060: env.put(Context.SECURITY_AUTHENTICATION, "none");
061: env.put(Context.SECURITY_PRINCIPAL, "test_principal");
062: env.put(Context.SECURITY_CREDENTIALS, "test_credentials");
063: sbind.valueAuthMech(env);
064: assertEquals(SaslBind.AuthMech.None, sbind.getAuthMech());
065:
066: env.put(Context.SECURITY_AUTHENTICATION, "NoNe");
067: assertEquals(SaslBind.AuthMech.None, sbind.getAuthMech());
068: }
069:
070: public void test_valueAuthMech_simple()
071: throws AuthenticationNotSupportedException {
072: env.put(Context.SECURITY_PRINCIPAL, "test_principal");
073: sbind.valueAuthMech(env);
074: assertEquals(SaslBind.AuthMech.Simple, sbind.getAuthMech());
075:
076: env.put(Context.SECURITY_AUTHENTICATION, "simple");
077: sbind.valueAuthMech(env);
078: assertEquals(SaslBind.AuthMech.Simple, sbind.getAuthMech());
079:
080: env.put(Context.SECURITY_AUTHENTICATION, "SiMpLe");
081: sbind.valueAuthMech(env);
082: assertEquals(SaslBind.AuthMech.Simple, sbind.getAuthMech());
083: }
084:
085: public void test_valueAuthMech_SASL()
086: throws AuthenticationNotSupportedException {
087: env.put(Context.SECURITY_AUTHENTICATION, DIGEST_MD5);
088: sbind.valueAuthMech(env);
089: assertEquals(SaslBind.AuthMech.SASL, sbind.getAuthMech());
090:
091: env.put(Context.SECURITY_AUTHENTICATION, CRAM_MD5);
092: sbind.valueAuthMech(env);
093: assertEquals(SaslBind.AuthMech.SASL, sbind.getAuthMech());
094:
095: env.put(Context.SECURITY_AUTHENTICATION, GSSAPI);
096: sbind.valueAuthMech(env);
097: assertEquals(SaslBind.AuthMech.SASL, sbind.getAuthMech());
098:
099: env.put(Context.SECURITY_AUTHENTICATION, EXTERNAL);
100: sbind.valueAuthMech(env);
101: assertEquals(SaslBind.AuthMech.SASL, sbind.getAuthMech());
102:
103: env.put(Context.SECURITY_AUTHENTICATION,
104: " test test2 EXTERNAL test3 GSSAPI");
105: sbind.valueAuthMech(env);
106: assertEquals(SaslBind.AuthMech.SASL, sbind.getAuthMech());
107: }
108:
109: public void test_valueAuthMech_notSupportException() {
110: env.put(Context.SECURITY_AUTHENTICATION, "test");
111: try {
112: sbind.valueAuthMech(env);
113: fail("AuthenticationNotSupportedException expected");
114: } catch (AuthenticationNotSupportedException e) {
115: // expected
116: }
117:
118: env.put(Context.SECURITY_AUTHENTICATION, " test test2 test3");
119: try {
120: sbind.valueAuthMech(env);
121: fail("AuthenticationNotSupportedException expected");
122: } catch (AuthenticationNotSupportedException e) {
123: // expected
124: }
125: }
126: }
|