<commit_before>/*
 * Copyright (c) 2016 <NAME>' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   <NAME> 
 *      - Initial concept and implementation
 */
package coyote.commons.network;

//import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


/**
 * 
 */
public class IpAclTest {

  /**
   * @throws java.lang.Exception
   */
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {}




  /**
   * @throws java.lang.Exception
   */
  @AfterClass
  public static void tearDownAfterClass() throws Exception {}




  @Test
  public void testConstrucor() {
    try {
      IpAcl acl = new IpAcl();
      acl.add( "192.168/16", true );
      acl.add( "10/8", false );
    } catch ( Exception ex ) {
      fail( "Could not construct: " + ex.getMessage() );
    }
  }




  @Test
  public void testAllows() {
    try {
      IpAcl acl = new IpAcl( IpAcl.DENY );
      acl.add( "192.168/16", true );
      acl.add( "10/8", false );

      String arg = "192.168.1.100";
      assertTrue( "Should allow '" + arg + "'", acl.allows( arg ) );

      arg = "10.8.107.12";

      assertTrue( "Should NOT allow '" + arg + "'", !acl.allows( arg ) );

      // if( acl.allows( arg ) )
      // {
      // System.out.println( "Error: ACL allows '" + arg + "'" );
      // }
      // else
      // {
      // System.out.println( "ACL denies '" + arg + "'" );
      // }
    } catch ( Exception ex ) {
      fail( "Could not construct: " + ex.getMessage() );
    }

    try {
      IpAcl acl = new IpAcl( IpAcl.DENY );

      // Only allow this one IP address
      acl.add( "192.168.1.100/32", IpAcl.ALLOW );

      // This should pass
      String arg = "192.168.1.100";
      assertTrue( "Should allow '" + arg + "'", acl.allows( arg ) );

      // These should not pass
      arg = "10.8.107.12";

      assertTrue( "Should NOT allow '" + arg + "'", !acl.allows( arg ) );

      arg = "192.168.1.101";

      assertTrue( "Should NOT allow '" + arg + "'", !acl.allows( arg ) );
    } catch ( Exception ex ) {
      fail( "Could not construct: " + ex.getMessage() );
    }

    // Test the ordering, 192.168.100 subnet is denied, but the rest of 192.168 
    // is allowed
    try {
      IpAcl acl = new IpAcl( IpAcl.DENY );
      acl.add( "192.168.100/24", false );
      acl.add( "192.168/16", true );

      String arg = "192.168.100.23";
      assertFalse( "Should NOT allow '" + arg + "'", acl.allows( arg ) );

      arg = "192.168.23.100";
      assertTrue( "Should allow '" + arg + "'", acl.allows( arg ) );

      arg = "10.8.107.12";
      assertFalse( "Should NOT allow '" + arg + "'", acl.allows( arg ) );

    } catch ( Exception ex ) {
      fail( "Could not construct: " + ex.getMessage() );
    }

  }
<commit_msg>Test for allow all with CIDR of 0/0<commit_after>/*
 * Copyright (c) 2016 <NAME>' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   <NAME> 
 *      - Initial concept and implementation
 */
package coyote.commons.network;

//import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


/**
 * 
 */
public class IpAclTest {

  /**
   * @throws java.lang.Exception
   */
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {}




  /**
   * @throws java.lang.Exception
   */
  @AfterClass
  public static void tearDownAfterClass() throws Exception {}




  @Test
  public void testConstrucor() {
    try {
      IpAcl acl = new IpAcl();
      acl.add("192.168/16", true);
      acl.add("10/8", false);
    } catch (Exception ex) {
      fail("Could not construct: " + ex.getMessage());
    }
  }




  @Test
  public void testAllows() {
    try {
      IpAcl acl = new IpAcl(IpAcl.DENY);
      acl.add("192.168/16", true);
      acl.add("10/8", false);

      String arg = "192.168.1.100";
      assertTrue("Should allow '" + arg + "'", acl.allows(arg));

      arg = "10.8.107.12";

      assertTrue("Should NOT allow '" + arg + "'", !acl.allows(arg));

      // if( acl.allows( arg ) )
      // {
      // System.out.println( "Error: ACL allows '" + arg + "'" );
      // }
      // else
      // {
      // System.out.println( "ACL denies '" + arg + "'" );
      // }
    } catch (Exception ex) {
      fail("Could not construct: " + ex.getMessage());
    }

    try {
      IpAcl acl = new IpAcl(IpAcl.DENY);

      // Only allow this one IP address
      acl.add("192.168.1.100/32", IpAcl.ALLOW);

      // This should pass
      String arg = "192.168.1.100";
      assertTrue("Should allow '" + arg + "'", acl.allows(arg));

      // These should not pass
      arg = "10.8.107.12";

      assertTrue("Should NOT allow '" + arg + "'", !acl.allows(arg));

      arg = "192.168.1.101";

      assertTrue("Should NOT allow '" + arg + "'", !acl.allows(arg));
    } catch (Exception ex) {
      fail("Could not construct: " + ex.getMessage());
    }

    // Test the ordering, 192.168.100 subnet is denied, but the rest of 192.168 
    // is allowed
    try {
      IpAcl acl = new IpAcl(IpAcl.DENY);
      acl.add("192.168.100/24", false);
      acl.add("192.168/16", true);

      String arg = "192.168.100.23";
      assertFalse("Should NOT allow '" + arg + "'", acl.allows(arg));

      arg = "192.168.23.100";
      assertTrue("Should allow '" + arg + "'", acl.allows(arg));

      arg = "10.8.107.12";
      assertFalse("Should NOT allow '" + arg + "'", acl.allows(arg));

    } catch (Exception ex) {
      fail("Could not construct: " + ex.getMessage());
    }

  }




  @Test
  public void testAllowAll() {
    try {
      IpAcl acl = new IpAcl(IpAcl.DENY);
      acl.add("255.255.255.255/0", true);
      String arg = "192.168.1.100";
      assertTrue("Should allow '" + arg + "'", acl.allows(arg));
    } catch (Exception ex) {
      fail("Could not construct: " + ex.getMessage());
    }
    try {
      IpAcl acl = new IpAcl(IpAcl.DENY);
      acl.add("0/0", true);
      String arg = "192.168.1.100";
      assertTrue("Should allow '" + arg + "'", acl.allows(arg));
    } catch (Exception ex) {
      fail("Could not construct: " + ex.getMessage());
    }
  }

{'fraction_non_alphanumeric': 0.1645383825040364, 'fraction_numerical': 0.0488771466314399, 'mean_word_length': 2.2634099616858236, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 21, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '249979', 'n_tokens_mistral': 2573, 'n_tokens_neox': 2255, 'n_words': 575}