NfsExports { public AccessPrivilege getAccessPrivilege(InetAddress addr) { return getAccessPrivilege(addr.getHostAddress(), addr.getCanonicalHostName()); } NfsExports(int cacheSize, long expirationPeriodNano, String matchHosts); static synchronized NfsExports getInstance(Configuration conf); String[] getHostGroupList(); AccessPrivilege getAccessPrivilege(InetAddress addr); static final Logger LOG; }
@Test public void testWildcardRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "* rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); }
@Test public void testWildcardRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "* ro"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); }
@Test public void testExactAddressRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, address1 + " rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertFalse(AccessPrivilege.READ_WRITE == matcher .getAccessPrivilege(address2, hostname1)); }
@Test public void testExactAddressRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, address1); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testExactHostRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, hostname1 + " rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); }
@Test public void testExactHostRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, hostname1); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); }
@Test public void testCidrShortRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.0/22 rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testCidrShortRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.0/22"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testCidrLongRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.0/255.255.252.0 rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testCidrLongRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.0/255.255.252.0"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testRegexIPRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.[0-9]+ rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testRegexIPRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.[0-9]+"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.NONE, matcher.getAccessPrivilege(address2, hostname1)); }
@Test public void testRegexHostRW() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "[a-z]+.b.com rw"); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address1, hostname2)); }
@Test public void testRegexHostRO() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "[a-z]+.b.com"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname2)); }
@Test public void testRegexGrouping() { NfsExports matcher = new NfsExports(CacheSize, ExpirationPeriod, "192.168.0.(12|34)"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname2)); matcher = new NfsExports(CacheSize, ExpirationPeriod, "\\w*.a.b.com"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege("1.2.3.4", "web.a.b.com")); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege("1.2.3.4", "email.a.b.org")); }
@Test public void testMultiMatchers() throws Exception { long shortExpirationPeriod = 1 * 1000 * 1000 * 1000; NfsExports matcher = new NfsExports(CacheSize, shortExpirationPeriod, "192.168.0.[0-9]+;[a-z]+.b.com rw"); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname2)); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, address1)); Assert.assertEquals(AccessPrivilege.READ_ONLY, matcher.getAccessPrivilege(address1, hostname1)); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address2, hostname1)); Assert.assertEquals(AccessPrivilege.READ_WRITE, matcher.getAccessPrivilege(address2, hostname2)); Thread.sleep(1000); AccessPrivilege ap; long startNanos = System.nanoTime(); do { ap = matcher.getAccessPrivilege(address2, address2); if (ap == AccessPrivilege.NONE) { break; } Thread.sleep(500); } while ((System.nanoTime() - startNanos) / NanosPerMillis < 5000); Assert.assertEquals(AccessPrivilege.NONE, ap); }