Home | History | Annotate | Download | only in captiveportal
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.net.captiveportal;
     18 
     19 import static junit.framework.Assert.assertEquals;
     20 import static junit.framework.Assert.assertNull;
     21 import static junit.framework.Assert.assertTrue;
     22 
     23 import android.support.test.filters.SmallTest;
     24 import android.support.test.runner.AndroidJUnit4;
     25 
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 
     29 import java.net.MalformedURLException;
     30 import java.net.URL;
     31 import java.text.ParseException;
     32 
     33 @RunWith(AndroidJUnit4.class)
     34 @SmallTest
     35 public class CaptivePortalProbeSpecTest {
     36 
     37     @Test
     38     public void testGetResult_Regex() throws MalformedURLException, ParseException {
     39         // 2xx status or 404, with an empty (match everything) location regex
     40         CaptivePortalProbeSpec statusRegexSpec = CaptivePortalProbeSpec.parseSpec(
     41                 "http://www.google.com@@/@@2[0-9]{2}|404@@/@@");
     42 
     43         // 404, or 301/302 redirect to some HTTPS page under google.com
     44         CaptivePortalProbeSpec redirectSpec = CaptivePortalProbeSpec.parseSpec(
     45                 "http://google.com@@/@@404|30[12]@@/@@https://([0-9a-z]+\\.)*google\\.com.*");
     46 
     47         assertSuccess(statusRegexSpec.getResult(200, null));
     48         assertSuccess(statusRegexSpec.getResult(299, "qwer"));
     49         assertSuccess(statusRegexSpec.getResult(404, null));
     50         assertSuccess(statusRegexSpec.getResult(404, ""));
     51 
     52         assertPortal(statusRegexSpec.getResult(300, null));
     53         assertPortal(statusRegexSpec.getResult(399, "qwer"));
     54         assertPortal(statusRegexSpec.getResult(500, null));
     55 
     56         assertSuccess(redirectSpec.getResult(404, null));
     57         assertSuccess(redirectSpec.getResult(404, ""));
     58         assertSuccess(redirectSpec.getResult(301, "https://www.google.com"));
     59         assertSuccess(redirectSpec.getResult(301, "https://www.google.com/test?q=3"));
     60         assertSuccess(redirectSpec.getResult(302, "https://google.com/test?q=3"));
     61 
     62         assertPortal(redirectSpec.getResult(299, "https://google.com/test?q=3"));
     63         assertPortal(redirectSpec.getResult(299, ""));
     64         assertPortal(redirectSpec.getResult(499, null));
     65         assertPortal(redirectSpec.getResult(301, "http://login.portal.example.com/loginpage"));
     66         assertPortal(redirectSpec.getResult(302, "http://www.google.com/test?q=3"));
     67     }
     68 
     69     @Test(expected = ParseException.class)
     70     public void testParseSpec_Empty() throws MalformedURLException, ParseException {
     71         CaptivePortalProbeSpec.parseSpec("");
     72     }
     73 
     74     @Test(expected = ParseException.class)
     75     public void testParseSpec_Null() throws MalformedURLException, ParseException {
     76         CaptivePortalProbeSpec.parseSpec(null);
     77     }
     78 
     79     @Test(expected = ParseException.class)
     80     public void testParseSpec_MissingParts() throws MalformedURLException, ParseException {
     81         CaptivePortalProbeSpec.parseSpec("http://google.com/@@/@@123");
     82     }
     83 
     84     @Test(expected = ParseException.class)
     85     public void testParseSpec_TooManyParts() throws MalformedURLException, ParseException {
     86         CaptivePortalProbeSpec.parseSpec("http://google.com/@@/@@123@@/@@456@@/@@extra");
     87     }
     88 
     89     @Test(expected = ParseException.class)
     90     public void testParseSpec_InvalidStatusRegex() throws MalformedURLException, ParseException {
     91         CaptivePortalProbeSpec.parseSpec("http://google.com/@@/@@unmatched(parenthesis@@/@@456");
     92     }
     93 
     94     @Test(expected = ParseException.class)
     95     public void testParseSpec_InvalidLocationRegex() throws MalformedURLException, ParseException {
     96         CaptivePortalProbeSpec.parseSpec("http://google.com/@@/@@123@@/@@unmatched[[]bracket");
     97     }
     98 
     99     @Test(expected = MalformedURLException.class)
    100     public void testParseSpec_EmptyURL() throws MalformedURLException, ParseException {
    101         CaptivePortalProbeSpec.parseSpec("@@/@@123@@/@@123");
    102     }
    103 
    104     @Test(expected = ParseException.class)
    105     public void testParseSpec_NoParts() throws MalformedURLException, ParseException {
    106         CaptivePortalProbeSpec.parseSpec("invalid");
    107     }
    108 
    109     @Test(expected = MalformedURLException.class)
    110     public void testParseSpec_RegexInvalidUrl() throws MalformedURLException, ParseException {
    111         CaptivePortalProbeSpec.parseSpec("notaurl@@/@@123@@/@@123");
    112     }
    113 
    114     @Test
    115     public void testParseSpecOrNull_UsesSpec() {
    116         final String specUrl = "http://google.com/probe";
    117         final String redirectUrl = "https://google.com/probe";
    118         CaptivePortalProbeSpec spec = CaptivePortalProbeSpec.parseSpecOrNull(
    119                 specUrl + "@@/@@302@@/@@" + redirectUrl);
    120         assertEquals(specUrl, spec.getUrl().toString());
    121 
    122         assertPortal(spec.getResult(302, "http://portal.example.com"));
    123         assertSuccess(spec.getResult(302, redirectUrl));
    124     }
    125 
    126     @Test
    127     public void testParseSpecOrNull_UsesFallback() throws MalformedURLException {
    128         CaptivePortalProbeSpec spec = CaptivePortalProbeSpec.parseSpecOrNull(null);
    129         assertNull(spec);
    130 
    131         spec = CaptivePortalProbeSpec.parseSpecOrNull("");
    132         assertNull(spec);
    133 
    134         spec = CaptivePortalProbeSpec.parseSpecOrNull("@@/@@ @@/@@ @@/@@");
    135         assertNull(spec);
    136 
    137         spec = CaptivePortalProbeSpec.parseSpecOrNull("invalid@@/@@123@@/@@456");
    138         assertNull(spec);
    139     }
    140 
    141     @Test
    142     public void testParseSpecOrUseStatusCodeFallback_EmptySpec() throws MalformedURLException {
    143         CaptivePortalProbeSpec spec = CaptivePortalProbeSpec.parseSpecOrNull("");
    144         assertNull(spec);
    145     }
    146 
    147     private void assertIsStatusSpec(CaptivePortalProbeSpec spec) {
    148         assertSuccess(spec.getResult(204, null));
    149         assertSuccess(spec.getResult(204, "1234"));
    150 
    151         assertPortal(spec.getResult(200, null));
    152         assertPortal(spec.getResult(301, null));
    153         assertPortal(spec.getResult(302, "1234"));
    154         assertPortal(spec.getResult(399, ""));
    155 
    156         assertFailed(spec.getResult(404, null));
    157         assertFailed(spec.getResult(500, "1234"));
    158     }
    159 
    160     private void assertPortal(CaptivePortalProbeResult result) {
    161         assertTrue(result.isPortal());
    162     }
    163 
    164     private void assertSuccess(CaptivePortalProbeResult result) {
    165         assertTrue(result.isSuccessful());
    166     }
    167 
    168     private void assertFailed(CaptivePortalProbeResult result) {
    169         assertTrue(result.isFailed());
    170     }
    171 }
    172