package com.datatheorem.android.trustkit.config;


import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;


@RunWith(AndroidJUnit4.class)
public class DomainPinningPolicyTest {

    private final static Set<String> pins = new HashSet<>();
    static {
        pins.add("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
        pins.add("rFjc3wG7lTZe43zeYTvPq8k4xdDEutCmIhI5dn4oCeE=");
    }

    private final static Set<String> reportUris = new HashSet<>();
    static {
        reportUris.add("https://www.test.com");
        reportUris.add("https://www.test2.com");
    }

    private final static Date date = new Date();

    @Test
    public void testValidPolicy() throws MalformedURLException {
        DomainPinningPolicy policy = new DomainPinningPolicy("www.test.com", true, pins, true,
                date, reportUris, false);

        assertEquals("www.test.com", policy.getHostname());
        assertEquals(date, policy.getExpirationDate());

        Set<PublicKeyPin> expectedPins = new HashSet<>();
        for (String pinStr : pins) {
            expectedPins.add(new PublicKeyPin(pinStr));

        }
        assertEquals(expectedPins, policy.getPublicKeyPins());

        // Ensure the default report URI was added as shouldDisableDefaultReportUri is false
        Set<URL> expectedReportUris = new HashSet<>();
        for (String uriStr : reportUris) {
            expectedReportUris.add(new URL(uriStr));
        }
        expectedReportUris.add(new URL("https://overmind.datatheorem.com/trustkit/report"));
        assertEquals(expectedReportUris, policy.getReportUris());

        assertTrue(policy.shouldEnforcePinning());
        assertTrue(policy.shouldIncludeSubdomains());
    }

    @Test
    public void testValidPolicyInternationalizeHostname() throws MalformedURLException {
        DomainPinningPolicy policy = new DomainPinningPolicy("českárepublika.icom.museum", true,
                pins, true, date, reportUris, false);

        assertEquals(policy.getHostname(), "českárepublika.icom.museum");
    }

    @Test
    public void testBadPolicyOnlyOnePin() throws MalformedURLException {
        Set<String> badPins = new HashSet<>();
        badPins.add("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");

        boolean didReceiveConfigError = false;
        try {
            new DomainPinningPolicy("www.test.com", true, badPins, true, date, reportUris, false);
        }
        catch (ConfigurationException e) {
            if (e.getMessage().startsWith("Less than two pins")) {
                didReceiveConfigError = true;
            } else {
                throw e;
            }
        }
        assertTrue(didReceiveConfigError);
    }

    @Test
    public void testNoPinsButPinningEnforceDisabledShouldBeValid() throws MalformedURLException {
        Set<String> emptyPins = new HashSet<>();
        new DomainPinningPolicy("www.test.com", true, emptyPins, false, date, reportUris, false);
    }

    @Test
    public void testBadPolicyPinTld() throws MalformedURLException {
        boolean didReceiveConfigError = false;
        try {
            new DomainPinningPolicy("com", true, pins, true, date, reportUris, false);
        }
        catch (ConfigurationException e) {
            if (e.getMessage().startsWith("Tried to pin an invalid domain")) {
                didReceiveConfigError = true;
            } else {
                throw e;
            }
        }
        assertTrue(didReceiveConfigError);
    }
}

@@ -2682,32 +2682,24 @@
 );%0A        %7D
-%0A       
  catch (Conf
@@ -2714,32 +2714,32 @@
 nException e) %7B%0A
+
             if (
@@ -2947,32 +2947,37 @@
 gError);%0A    %7D%0A%0A
+    %0A
     @Test%0A    pu
@@ -3109,32 +3109,99 @@
 ew HashSet%3C%3E();%0A
+        boolean didReceivedConfigError = false;%0A%0A        try %7B%0A    
         new Doma
@@ -3249,20 +3249,19 @@
 tyPins, 
-fals
+tru
 e, date,
@@ -3277,24 +3277,304 @@
 is, false);%0A
+        %7D catch (ConfigurationException e) %7B%0A            if (e.getMessage().startsWith(%22An empty pin-set%22)) %7B%0A                didReceivedConfigError = true;%0A            %7D else %7B%0A                throw e;%0A            %7D%0A        %7D%0A        %0A        assertTrue(didReceivedConfigError);%0A
     %7D%0A%0A    @

add test based on the testBadPolicyOnlyOnePin