Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions hdfs-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.ranger.authorization.hadoop.exceptions;

import org.apache.hadoop.security.AccessControlException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* @generated by Cursor
* @description : Unit Test cases for RangerAccessControlException
*/

@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerAccessControlException {
@Test
public void test01_constructWithMessage_setsMessage() {
String message = "Permission denied: user=test, access=READ, inode=/path";
RangerAccessControlException ex = new RangerAccessControlException(message);

Assertions.assertEquals(message, ex.getMessage());
}

@Test
public void test02_isInstanceOfAccessControlException() {
RangerAccessControlException ex = new RangerAccessControlException("msg");

Assertions.assertInstanceOf(AccessControlException.class, ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.ranger.authorization.hadoop.RangerHdfsAuthorizer;
import org.junit.Assert;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.PrivilegedExceptionAction;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Here we plug the Ranger AccessControlEnforcer into HDFS.
* <p>
Expand All @@ -51,6 +59,8 @@
* with the tag called "TmpdirTag". A "hdfs_path" entity was created in Apache Atlas + then associated with the "TmpdirTag". This was
* then imported into Ranger using the TagSyncService. The policies were then downloaded locally and saved for testing off-line.
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class HDFSRangerTest {
private static final File baseDir = new File("./target/hdfs/").getAbsoluteFile();

Expand Down Expand Up @@ -138,10 +148,10 @@ public void writeTest() throws Exception {
try {
fs.append(file);

Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand Down Expand Up @@ -181,7 +191,7 @@ public void executeTest() throws Exception {
try (FileSystem fs = FileSystem.get(conf)) {
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(file.getParent(), false);

Assert.assertTrue(iter.hasNext());
assertTrue(iter.hasNext());
}

return null;
Expand All @@ -197,7 +207,7 @@ public void executeTest() throws Exception {
try (FileSystem fs = FileSystem.get(conf)) {
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(file.getParent(), false);

Assert.assertTrue(iter.hasNext());
assertTrue(iter.hasNext());
}

return null;
Expand All @@ -215,11 +225,11 @@ public void executeTest() throws Exception {
try {
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(file.getParent(), false);

Assert.assertTrue(iter.hasNext());
Assert.fail("Failure expected on an incorrect permission");
assertTrue(iter.hasNext());
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand Down Expand Up @@ -259,9 +269,9 @@ public void readTestUsingTagPolicy() throws Exception {

IOUtils.copy(in, output);

String content = new String(output.toByteArray());
String content = output.toString();

Assert.assertTrue(content.startsWith("data0"));
assertTrue(content.startsWith("data0"));
}

return null;
Expand All @@ -282,9 +292,9 @@ public void readTestUsingTagPolicy() throws Exception {

IOUtils.copy(in, output);

String content = new String(output.toByteArray());
String content = output.toString();

Assert.assertTrue(content.startsWith("data0"));
assertTrue(content.startsWith("data0"));
}

return null;
Expand All @@ -303,10 +313,10 @@ public void readTestUsingTagPolicy() throws Exception {
try {
fs.open(file);

Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand All @@ -325,10 +335,10 @@ public void readTestUsingTagPolicy() throws Exception {
// Read the file
try {
fs.open(file);
Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand Down Expand Up @@ -386,9 +396,9 @@ void hdfsReadTest(String fileName) throws Exception {

IOUtils.copy(in, output);

String content = new String(output.toByteArray());
String content = output.toString();

Assert.assertTrue(content.startsWith("data0"));
assertTrue(content.startsWith("data0"));
}

return null;
Expand All @@ -409,9 +419,9 @@ void hdfsReadTest(String fileName) throws Exception {

IOUtils.copy(in, output);

String content = new String(output.toByteArray());
String content = output.toString();

Assert.assertTrue(content.startsWith("data0"));
assertTrue(content.startsWith("data0"));
}

return null;
Expand All @@ -429,10 +439,10 @@ void hdfsReadTest(String fileName) throws Exception {
try {
fs.open(file);

Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand Down Expand Up @@ -469,10 +479,10 @@ void hdfsReadFailTest(String fileName) throws Exception {
try {
fs.open(file);

Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand All @@ -492,10 +502,10 @@ void hdfsReadFailTest(String fileName) throws Exception {
try {
fs.open(file);

Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand All @@ -514,10 +524,10 @@ void hdfsReadFailTest(String fileName) throws Exception {
// Read the file
try {
fs.open(file);
Assert.fail("Failure expected on an incorrect permission");
fail("Failure expected on an incorrect permission");
} catch (AccessControlException ex) {
// expected
Assert.assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
assertEquals(AccessControlException.class.getName(), ex.getClass().getName());
}
}

Expand Down Expand Up @@ -547,7 +557,7 @@ void hdfsGetContentSummary(final String dirName) throws Exception {

Assert.assertEquals("Found unexpected number of directories; expected-count=3, actual-count=" + directoryCount, 3, directoryCount);
} catch (Exception e) {
Assert.fail("Failed to getContentSummary, exception=" + e);
fail("Failed to getContentSummary, exception=" + e);
}
}

Expand Down
Loading
Loading