1 /* 2 * Copyright (C) 2011 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.http.cts; 18 19 import android.net.http.SslCertificate; 20 import android.net.http.SslError; 21 22 23 import java.util.Date; 24 25 import junit.framework.TestCase; 26 27 public class SslErrorTest extends TestCase { 28 private SslCertificate mCertificate; 29 30 @Override 31 protected void setUp() throws Exception { 32 super.setUp(); 33 mCertificate = new SslCertificate("foo", "bar", new Date(42), new Date(43)); 34 } 35 36 public void testHasError() { 37 SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate); 38 assertTrue(error.hasError(SslError.SSL_EXPIRED)); 39 assertFalse(error.hasError(SslError.SSL_UNTRUSTED)); 40 } 41 42 public void testAddError() { 43 SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate); 44 assertFalse(error.hasError(SslError.SSL_UNTRUSTED)); 45 error.addError(SslError.SSL_UNTRUSTED); 46 assertTrue(error.hasError(SslError.SSL_UNTRUSTED)); 47 } 48 49 public void testAddErrorIgnoresInvalidValues() { 50 SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate); 51 error.addError(42); 52 assertFalse(error.hasError(42)); 53 } 54 55 public void testConstructorIgnoresInvalidValues() { 56 SslError error = new SslError(42, mCertificate); 57 assertFalse(error.hasError(42)); 58 } 59 60 public void testGetPrimaryError() { 61 SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate); 62 error.addError(SslError.SSL_UNTRUSTED); 63 assertEquals(error.getPrimaryError(), SslError.SSL_UNTRUSTED); 64 } 65 66 public void testGetPrimaryErrorWithEmptySet() { 67 SslError error = new SslError(42, mCertificate); 68 assertEquals(error.getPrimaryError(), -1); 69 } 70 71 public void testGetUrl() { 72 SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate, "foo"); 73 assertEquals(error.getUrl(), "foo"); 74 } 75 76 public void testGetUrlWithDeprecatedConstructor() { 77 SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate); 78 assertEquals(error.getUrl(), ""); 79 } 80 } 81