package org.torproject.jtor.circuits.path;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.torproject.jtor.data.IPv4Address;

public class ConfigNodeFilterTest {

	@Test
	public void testIsAddressString() {
		final List<String> validStrings = Arrays.asList(
				"1.2.3.4/16",
				"0.0.0.0/1",
				"255.0.255.0/16");
		
		final List<String> invalidStrings = Arrays.asList(
				"1.2.3.256/16",
				"1.2.3.4/61",
				"1.2.3.4/0",
				"1.2.3.4/22x",
				"1.2.3.4/",
				"1.2.3.4");

		for(String s: validStrings) {
			assertTrue(s, ConfigNodeFilter.isAddressString(s));
		}
		for(String s: invalidStrings) {
			assertFalse(s, ConfigNodeFilter.isAddressString(s));
		}

	}
	
	@Test
	public void testIsCountryCode() {
		final List<String> validStrings = Arrays.asList("{CC}", "{xx}");
		final List<String> invalidStrings = Arrays.asList("US", "{xxx}");
		for(String s: validStrings) { assertTrue(s, ConfigNodeFilter.isCountryCodeString(s)); }
		for(String s: invalidStrings) { assertFalse(s, ConfigNodeFilter.isCountryCodeString(s)); }
	}
	
	@Test
	public void testMaskFilter() {
		RouterMock r1 = new RouterMock();
		RouterMock r2 = new RouterMock();
		r1.address = IPv4Address.createFromString("1.2.3.4");
		r2.address = IPv4Address.createFromString("1.3.3.4");
		
		RouterFilter f = ConfigNodeFilter.createFilterFor("1.2.3.0/16");
		assertTrue(f.filter(r1));
		assertFalse(f.filter(r2));
	}
	
	
}

@@ -37,16 +37,196 @@
 .path;%0A%0A
+import static org.easymock.EasyMock.createMock;%0Aimport static org.easymock.EasyMock.expect;%0Aimport static org.easymock.EasyMock.replay;%0Aimport static org.easymock.EasyMock.verify;%0A
 import s
@@ -421,16 +421,61 @@
 Address;
+%0Aimport org.torproject.jtor.directory.Router;
 %0A%0Apublic
@@ -1382,222 +1382,433 @@
 %0A%09%0A%09
-@Test%0A%09public void testMaskFilter() %7B%0A%09%09RouterMock r1 = new RouterMock();%0A%09%09RouterMock r2 = new RouterMock();%0A%09%09r1.address = IPv4Address.createFromString(%221.2.3.4%22);%0A%09%09r2.address = IPv4Address.createFromString
+private Router createRouterMockWithAddress(String ip) %7B%0A%09%09final IPv4Address address = IPv4Address.createFromString(ip);%0A%09%09final Router router = createMock(%22mockRouter%22, Router.class);%0A%09%09expect(router.getAddress()).andReturn(address);%0A%09%09replay(router);%0A%09%09return router;%0A%09%7D%0A%09%0A%09@Test%0A%09public void testMaskFilter() %7B%0A%09%09final Router r1 = createRouterMockWithAddress(%221.2.3.4%22);%0A%09%09final Router r2 = createRouterMockWithAddress
 (%221.
-3
+7
 .3.4
@@ -1813,19 +1813,22 @@
 .4%22);%0A%09%09
-%0A%09%09
+final 
 RouterFi
@@ -1941,16 +1941,34 @@
 r(r2));%0A
+%09%09verify(r1, r2);%0A
 %09%7D%0A%09%0A%09%0A%7D

Use EasyMock