/*
 * Licensed to the University of California, Berkeley 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 tachyon.security.login;

import java.security.Principal;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;

import org.junit.Assert;
import org.junit.Test;

import tachyon.security.User;

/**
 * Unit test for the login modules defined in {@link tachyon.security.login.TachyonLoginModule} and
 * used in {@link tachyon.security.login.TachyonJaasConfiguration}.
 */
public class LoginModuleTest {

  /**
   * This test verify whether the simple login works in JAAS framework.
   * Simple mode login get the OS user and convert to Tachyon user.
   *
   * @throws Exception thrown when failing to set the context appropriately
   */
  @Test
  public void simpleLoginTest() throws Exception {
    String clazzName = TachyonJaasProperties.OS_PRINCIPAL_CLASS_NAME;
    @SuppressWarnings("unchecked")
    Class<? extends Principal> clazz = (Class<? extends Principal>) ClassLoader
        .getSystemClassLoader().loadClass(clazzName);
    Subject subject = new Subject();

    // login, add OS user into subject, and add corresponding Tachyon user into subject
    LoginContext loginContext = new LoginContext("simple", subject, null,
        new TachyonJaasConfiguration());
    loginContext.login();

    // verify whether OS user and Tachyon user is added.
    Assert.assertFalse(subject.getPrincipals(clazz).isEmpty());
    Assert.assertFalse(subject.getPrincipals(User.class).isEmpty());

    // logout and verify the user is removed
    loginContext.logout();
    Assert.assertTrue(subject.getPrincipals(User.class).isEmpty());
  }

  // TODO(dong): Kerberos login test
}

@@ -939,55 +939,169 @@
 xt;%0A
-%0Aimport org.junit.Assert;%0Aimport org.junit.Test
+import javax.security.auth.login.LoginException;%0A%0Aimport org.junit.Assert;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0A%0Ai
@@ -1338,16 +1338,142 @@
 Test %7B%0A%0A
+  /**%0A   * The exception expected to be thrown.%0A   */%0A  @Rule%0A  public ExpectedException mThrown = ExpectedException.none();%0A%0A
   /**%0A  
@@ -2593,16 +2593,1336 @@
 mpty());
+%0A%0A    // logout twice should be no-op.%0A    loginContext.logout();%0A    Assert.assertTrue(subject.getPrincipals(User.class).isEmpty());%0A  %7D%0A%0A   /**%0A   * This test verify logging out a read only subject should fail.%0A   */%0A  @Test%0A  public void logoutReadOnlySubject() throws Exception %7B%0A    String clazzName = TachyonJaasProperties.OS_PRINCIPAL_CLASS_NAME;%0A    @SuppressWarnings(%22unchecked%22)%0A    Class%3C? extends Principal%3E clazz = (Class%3C? extends Principal%3E) ClassLoader%0A        .getSystemClassLoader().loadClass(clazzName);%0A    Subject subject = new Subject();%0A%0A    // login, add OS user into subject, and add corresponding Tachyon user into subject%0A    LoginContext loginContext = new LoginContext(%22simple%22, subject, null,%0A        new TachyonJaasConfiguration());%0A    loginContext.login();%0A%0A    // verify whether OS user and Tachyon user is added.%0A    Assert.assertFalse(subject.getPrincipals(clazz).isEmpty());%0A    Assert.assertFalse(subject.getPrincipals(User.class).isEmpty());%0A%0A    // logout read only subject should fail.%0A    subject.setReadOnly();%0A    mThrown.expect(LoginException.class);%0A    mThrown.expectMessage(%22logout Failed: Subject is Readonly%22);%0A    loginContext.logout();%0A    Assert.assertFalse(subject.getPrincipals(clazz).isEmpty());%0A    Assert.assertFalse(subject.getPrincipals(User.class).isEmpty());
 %0A  %7D%0A%0A  

Add unit test for logout with ready only subject in LoginModuleTest.