package com.btr.proxy.selector.whitelist;

import static org.junit.Assert.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.junit.Test;

import com.btr.proxy.util.UriFilter;

/*****************************************************************************
 * Unit tests for DefaultWhiteListParser
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class DefaultWhiteListParserTest {

	private DefaultWhiteListParser parser = new DefaultWhiteListParser();
	
	/*************************************************************************
	 * Test method
	 * @throws URISyntaxException on error 
	 ************************************************************************/
	@Test
	public void shouldAllowAllPrefix() throws URISyntaxException {
		List<UriFilter> l = this.parser.parseWhiteList("*.mynet.com");
		UriFilter filter = l.get(0);
		assertTrue(filter.accept(new URI("http://rossi.mynet.com"))); 
		assertFalse(filter.accept(new URI("http://rossi.mynet.com.test")));
	}
	
	/*************************************************************************
	 * Test method
	 * @throws URISyntaxException on error 
	 ************************************************************************/
	@Test
	public void shouldAllowAllPostfix() throws URISyntaxException {
		List<UriFilter> l = this.parser.parseWhiteList("mynet.*");
		UriFilter filter = l.get(0);
		assertFalse(filter.accept(new URI("http://rossi.mynet.com"))); 
		assertTrue(filter.accept(new URI("http://mynet.junit.test")));
	}
	
	/*************************************************************************
	 * Test method
	 * @throws URISyntaxException on error 
	 ************************************************************************/
	@Test
	public void shouldSplitMultipleEntries() throws URISyntaxException {
		List<UriFilter> l = this.parser.parseWhiteList("*.mynet.com; *.rossi.invalid; junit*");
		assertEquals(3, l.size());
	}
	
	/*************************************************************************
	 * Test method
	 * @throws URISyntaxException on error 
	 ************************************************************************/
	@Test
	public void shouldAllowIpRange() throws URISyntaxException {
		List<UriFilter> l = this.parser.parseWhiteList("192.168.0.0/24");
		UriFilter filter = l.get(0);
		assertTrue(filter.accept(new URI("http://192.168.0.1")));
		assertTrue(filter.accept(new URI("http://192.168.0.11")));
		assertFalse(filter.accept(new URI("http://rossi.mynet.com"))); 
		assertFalse(filter.accept(new URI("http://145.5.5.1"))); 
	}
	
	
	/*************************************************************************
	 * Test method
	 * @throws URISyntaxException on error 
	 ************************************************************************/
	@Test
	public void shouldHandleInvalidWithoutException() throws URISyntaxException {
		List<UriFilter> l = this.parser.parseWhiteList("http://10.*.*.*");
		UriFilter filter = l.get(0);
		assertFalse(filter.accept(new URI("http://10.0.0.1")));
	}
	
	
//	
//	
//	.mynet.com  		- Filters all host names ending with .mynet.com
//	 *  *.mynet.com  		- Filters all host names ending with .mynet.com
//	 *  www.mynet.*  		- Filters all host names starting with www.mynet.
//	 *  123.12.32.1 		- Filters the IP 123.12.32.1
//	 *  123.12.32.1/255 	- Filters the IP range   
//	 *  http://www.mynet.com	
//	

}


@@ -3127,16 +3127,454 @@
 );%0A%09%7D%0A%09%0A
+%09/*************************************************************************%0A%09 * Test method%0A%09 * @throws URISyntaxException on error %0A%09 ************************************************************************/%0A%09@Test%0A%09public void shouldHandleLocalBypass() throws URISyntaxException %7B%0A%09%09List%3CUriFilter%3E l = this.parser.parseWhiteList(%22%3Clocal%3E%22);%0A%09%09UriFilter filter = l.get(0);%0A%09%09assertTrue(filter.accept(new URI(%22http://localhost%22)));%0A%09%7D%0A%09%0A
 %09%0A//%09%0A//

Fix for Issue 50 Mixed white list with "local bypass" enabled