Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2006 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;
     18 
     19 import android.test.suitebuilder.annotation.Suppress;
     20 import java.io.InputStream;
     21 import java.io.OutputStream;
     22 import java.net.Socket;
     23 import java.util.Arrays;
     24 import junit.framework.TestCase;
     25 
     26 public class SSLTest extends TestCase {
     27     //This test relies on network resources.
     28     @Suppress
     29     public void testCertificate() throws Exception {
     30         // test www.fortify.net/sslcheck.html
     31         Socket ssl = SSLCertificateSocketFactory.getDefault().createSocket("www.fortify.net",443);
     32         assertNotNull(ssl);
     33 
     34         OutputStream out = ssl.getOutputStream();
     35         assertNotNull(out);
     36 
     37         InputStream in = ssl.getInputStream();
     38         assertNotNull(in);
     39 
     40         String get = "GET /sslcheck.html HTTP/1.1\r\nHost: 68.178.217.222\r\n\r\n";
     41 
     42         // System.out.println("going for write...");
     43         out.write(get.getBytes());
     44 
     45         byte[] b = new byte[1024];
     46         // System.out.println("going for read...");
     47         int ret = in.read(b);
     48 
     49         // System.out.println(new String(b));
     50     }
     51 
     52     public void testStringsToLengthPrefixedBytes() {
     53         byte[] expected = {
     54                 6, 's', 'p', 'd', 'y', '/', '2',
     55                 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
     56         };
     57         assertTrue(Arrays.equals(expected, SSLCertificateSocketFactory.toLengthPrefixedList(
     58                 new byte[] { 's', 'p', 'd', 'y', '/', '2' },
     59                 new byte[] { 'h', 't', 't', 'p', '/', '1', '.', '1' })));
     60     }
     61 
     62     public void testStringsToLengthPrefixedBytesEmptyArray() {
     63         try {
     64             SSLCertificateSocketFactory.toLengthPrefixedList();
     65             fail();
     66         } catch (IllegalArgumentException expected) {
     67         }
     68     }
     69 
     70     public void testStringsToLengthPrefixedBytesEmptyByteArray() {
     71         try {
     72             SSLCertificateSocketFactory.toLengthPrefixedList(new byte[0]);
     73             fail();
     74         } catch (IllegalArgumentException expected) {
     75         }
     76     }
     77 
     78     public void testStringsToLengthPrefixedBytesOversizedInput() {
     79         try {
     80             SSLCertificateSocketFactory.toLengthPrefixedList(new byte[256]);
     81             fail();
     82         } catch (IllegalArgumentException expected) {
     83         }
     84     }
     85 }
     86