/**
 * 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.hadoop.hdfs.server.namenode;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.ipc.RemoteException;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;

import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_AUDIT_LOGGERS_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Tests for the {@link AuditLogger} custom audit logging interface.
 */
public class TestAuditLogger {

  /**
   * Tests that AuditLogger works as expected.
   */
  @Test
  public void testAuditLogger() throws IOException {
    Configuration conf = new HdfsConfiguration();
    conf.set(DFS_NAMENODE_AUDIT_LOGGERS_KEY, DummyAuditLogger.class.getName());
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();

    try {
      cluster.waitClusterUp();
      assertTrue(DummyAuditLogger.initialized);

      FileSystem fs = cluster.getFileSystem();
      long time = System.currentTimeMillis();
      fs.setTimes(new Path("/"), time, time);
      assertEquals(1, DummyAuditLogger.logCount);
    } finally {
      cluster.shutdown();
    }
  }

  /**
   * Tests that a broken audit logger causes requests to fail.
   */
  @Test
  public void testBrokenLogger() throws IOException {
    Configuration conf = new HdfsConfiguration();
    conf.set(DFS_NAMENODE_AUDIT_LOGGERS_KEY, BrokenAuditLogger.class.getName());
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();

    try {
      cluster.waitClusterUp();

      FileSystem fs = cluster.getFileSystem();
      long time = System.currentTimeMillis();
      fs.setTimes(new Path("/"), time, time);
      fail("Expected exception due to broken audit logger.");
    } catch (RemoteException re) {
      // Expected.
    } finally {
      cluster.shutdown();
    }
  }

  public static class DummyAuditLogger implements AuditLogger {

    static boolean initialized;
    static int logCount;

    public void initialize(Configuration conf) {
      initialized = true;
    }

    public void logAuditEvent(boolean succeeded, String userName,
        InetAddress addr, String cmd, String src, String dst, FileStatus stat) {
      logCount++;
    }

  }

  public static class BrokenAuditLogger implements AuditLogger {

    public void initialize(Configuration conf) {
      // No op.
    }

    public void logAuditEvent(boolean succeeded, String userName,
        InetAddress addr, String cmd, String src, String dst, FileStatus stat) {
      throw new RuntimeException("uh oh");
    }

  }

}

@@ -1230,16 +1230,69 @@
 Address;
+%0Aimport org.apache.hadoop.fs.permission.FsPermission;
 %0A%0Aimport
@@ -1603,16 +1603,80 @@
 gger %7B%0A%0A
+  private static final short TEST_PERMISSION = (short) 0654;%0A  %0A
   /**%0A  
@@ -2321,16 +2321,1069 @@
  %7D%0A  %7D%0A%0A
+  /**%0A   * Minor test related to HADOOP-9155. Verify that during a%0A   * FileSystem.setPermission() operation, the stat passed in during the%0A   * logAuditEvent() call returns the new permission rather than the old%0A   * permission.%0A   */%0A  @Test%0A  public void testAuditLoggerWithSetPermission() throws IOException %7B%0A    Configuration conf = new HdfsConfiguration();%0A    conf.set(DFS_NAMENODE_AUDIT_LOGGERS_KEY,%0A        DummyAuditLogger.class.getName());%0A    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();%0A    try %7B%0A      cluster.waitClusterUp();%0A      assertTrue(DummyAuditLogger.initialized);%0A      DummyAuditLogger.resetLogCount();%0A      FileSystem fs = cluster.getFileSystem();%0A      long time = System.currentTimeMillis();%0A      final Path p = new Path(%22/%22);%0A      fs.setTimes(p, time, time);%0A      fs.setPermission(p, new FsPermission(TEST_PERMISSION));%0A      assertEquals(TEST_PERMISSION, DummyAuditLogger.foundPermission);%0A      assertEquals(2, DummyAuditLogger.logCount);%0A    %7D finally %7B%0A      cluster.shutdown();%0A    %7D%0A  %7D%0A  %0A
   /**%0A  
@@ -4186,17 +4186,55 @@
 gCount;%0A
+    static short foundPermission;%0A    
 %0A
-
     publ
@@ -4299,32 +4299,108 @@
  = true;%0A    %7D%0A%0A
+    public static void resetLogCount() %7B%0A      logCount = 0;%0A    %7D%0A        %0A
     public void 
@@ -4526,24 +4526,24 @@
 tus stat) %7B%0A
-
       logCou
@@ -4548,16 +4548,108 @@
 ount++;%0A
+      if (stat != null) %7B%0A        foundPermission = stat.getPermission().toShort();%0A      %7D%0A
     %7D%0A%0A 

apply HDFS-6224