• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.cts;
18 
19 import android.os.SystemClock;
20 import android.test.AndroidTestCase;
21 import android.util.Log;
22 
23 import java.net.Inet4Address;
24 import java.net.Inet6Address;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.ArrayList;
28 
29 public class DnsTest extends AndroidTestCase {
30 
31     static {
32         System.loadLibrary("nativedns_jni");
33     }
34 
35     private static final boolean DBG = false;
36     private static final String TAG = "DnsTest";
37 
38     /**
39      * @return true on success
40      */
testNativeDns()41     private static native boolean testNativeDns();
42 
43     /**
44      * Verify:
45      * DNS works - forwards and backwards, giving ipv4 and ipv6
46      * Test that DNS work on v4 and v6 networks
47      * Test Native dns calls (4)
48      * Todo:
49      * Cache is flushed when we change networks
50      * have per-network caches
51      * No cache when there's no network
52      * Perf - measure size of first and second tier caches and their effect
53      * Assert requires network permission
54      */
testDnsWorks()55     public void testDnsWorks() {
56         InetAddress addrs[] = {};
57         try {
58             addrs = InetAddress.getAllByName("www.google.com");
59         } catch (UnknownHostException e) {}
60         assertTrue(addrs.length != 0);
61         boolean foundV4 = false, foundV6 = false;
62         for (InetAddress addr : addrs) {
63             if (addr instanceof Inet4Address) foundV4 = true;
64             else if (addr instanceof Inet6Address) foundV6 = true;
65             if (DBG) Log.e(TAG, "www.google.com gave " + addr.toString());
66         }
67 
68         // assertTrue(foundV4);
69         // assertTrue(foundV6);
70 
71         // We should have at least one of the addresses to connect!
72         assertTrue(foundV4 || foundV6);
73 
74         try {
75             addrs = InetAddress.getAllByName("ipv6.google.com");
76         } catch (UnknownHostException e) {}
77         assertTrue(addrs.length != 0);
78         foundV4 = false;
79         foundV6 = false;
80         for (InetAddress addr : addrs) {
81             if (addr instanceof Inet4Address) foundV4 = true;
82             else if (addr instanceof Inet6Address) foundV6 = true;
83             if (DBG) Log.e(TAG, "ipv6.google.com gave " + addr.toString());
84         }
85 
86         assertTrue(foundV4 == false);
87         assertTrue(foundV6 == true);
88 
89         assertTrue(testNativeDns());
90     }
91 
92     private static final String[] URLS = { "www.google.com", "ipv6.google.com", "www.yahoo.com",
93             "facebook.com", "youtube.com", "blogspot.com", "baidu.com", "wikipedia.org",
94 // live.com fails rev lookup.
95             "twitter.com", "qq.com", "msn.com", "yahoo.co.jp", "linkedin.com",
96             "taobao.com", "google.co.in", "sina.com.cn", "amazon.com", "wordpress.com",
97             "google.co.uk", "ebay.com", "yandex.ru", "163.com", "google.co.jp", "google.fr",
98             "microsoft.com", "paypal.com", "google.com.br", "flickr.com",
99             "mail.ru", "craigslist.org", "fc2.com", "google.it",
100 // "apple.com", fails rev lookup
101             "google.es",
102             "imdb.com", "google.ru", "soho.com", "bbc.co.uk", "vkontakte.ru", "ask.com",
103             "tumblr.com", "weibo.com", "go.com", "xvideos.com", "livejasmin.com", "cnn.com",
104             "youku.com", "blogspot.com", "soso.com", "google.ca", "aol.com", "tudou.com",
105             "xhamster.com", "megaupload.com", "ifeng.com", "zedo.com", "mediafire.com", "ameblo.jp",
106             "pornhub.com", "google.co.id", "godaddy.com", "adobe.com", "rakuten.co.jp", "about.com",
107             "espn.go.com", "4shared.com", "alibaba.com","ebay.de", "yieldmanager.com",
108             "wordpress.org", "livejournal.com", "google.com.tr", "google.com.mx", "renren.com",
109            "livedoor.com", "google.com.au", "youporn.com", "uol.com.br", "cnet.com", "conduit.com",
110             "google.pl", "myspace.com", "nytimes.com", "ebay.co.uk", "chinaz.com", "hao123.com",
111             "thepiratebay.org", "doubleclick.com", "alipay.com", "netflix.com", "cnzz.com",
112             "huffingtonpost.com", "twitpic.com", "weather.com", "babylon.com", "amazon.de",
113             "dailymotion.com", "orkut.com", "orkut.com.br", "google.com.sa", "odnoklassniki.ru",
114             "amazon.co.jp", "google.nl", "goo.ne.jp", "stumbleupon.com", "tube8.com", "tmall.com",
115             "imgur.com", "globo.com", "secureserver.net", "fileserve.com", "tianya.cn", "badoo.com",
116             "ehow.com", "photobucket.com", "imageshack.us", "xnxx.com", "deviantart.com",
117             "filestube.com", "addthis.com", "douban.com", "vimeo.com", "sogou.com",
118             "stackoverflow.com", "reddit.com", "dailymail.co.uk", "redtube.com", "megavideo.com",
119             "taringa.net", "pengyou.com", "amazon.co.uk", "fbcdn.net", "aweber.com", "spiegel.de",
120             "rapidshare.com", "mixi.jp", "360buy.com", "google.cn", "digg.com", "answers.com",
121             "bit.ly", "indiatimes.com", "skype.com", "yfrog.com", "optmd.com", "google.com.eg",
122             "google.com.pk", "58.com", "hotfile.com", "google.co.th",
123             "bankofamerica.com", "sourceforge.net", "maktoob.com", "warriorforum.com", "rediff.com",
124             "google.co.za", "56.com", "torrentz.eu", "clicksor.com", "avg.com",
125             "download.com", "ku6.com", "statcounter.com", "foxnews.com", "google.com.ar",
126             "nicovideo.jp", "reference.com", "liveinternet.ru", "ucoz.ru", "xinhuanet.com",
127             "xtendmedia.com", "naver.com", "youjizz.com", "domaintools.com", "sparkstudios.com",
128             "rambler.ru", "scribd.com", "kaixin001.com", "mashable.com", "adultfirendfinder.com",
129             "files.wordpress.com", "guardian.co.uk", "bild.de", "yelp.com", "wikimedia.org",
130             "chase.com", "onet.pl", "ameba.jp", "pconline.com.cn", "free.fr", "etsy.com",
131             "typepad.com", "youdao.com", "megaclick.com", "digitalpoint.com", "blogfa.com",
132             "salesforce.com", "adf.ly", "ganji.com", "wikia.com", "archive.org", "terra.com.br",
133             "w3schools.com", "ezinearticles.com", "wjs.com", "google.com.my", "clickbank.com",
134             "squidoo.com", "hulu.com", "repubblica.it", "google.be", "allegro.pl", "comcast.net",
135             "narod.ru", "zol.com.cn", "orange.fr", "soufun.com", "hatena.ne.jp", "google.gr",
136             "in.com", "techcrunch.com", "orkut.co.in", "xunlei.com",
137             "reuters.com", "google.com.vn", "hostgator.com", "kaskus.us", "espncricinfo.com",
138             "hootsuite.com", "qiyi.com", "gmx.net", "xing.com", "php.net", "soku.com", "web.de",
139             "libero.it", "groupon.com", "51.la", "slideshare.net", "booking.com", "seesaa.net",
140             "126.com", "telegraph.co.uk", "wretch.cc", "twimg.com", "rutracker.org", "angege.com",
141             "nba.com", "dell.com", "leboncoin.fr", "people.com", "google.com.tw", "walmart.com",
142             "daum.net", "2ch.net", "constantcontact.com", "nifty.com", "mywebsearch.com",
143             "tripadvisor.com", "google.se", "paipai.com", "google.com.ua", "ning.com", "hp.com",
144             "google.at", "joomla.org", "icio.us", "hudong.com", "csdn.net", "getfirebug.com",
145             "ups.com", "cj.com", "google.ch", "camzap.com", "wordreference.com", "tagged.com",
146             "wp.pl", "mozilla.com", "google.ru", "usps.com", "china.com", "themeforest.net",
147             "search-results.com", "tribalfusion.com", "thefreedictionary.com", "isohunt.com",
148             "linkwithin.com", "cam4.com", "plentyoffish.com", "wellsfargo.com", "metacafe.com",
149             "depositfiles.com", "freelancer.com", "opendns.com", "homeway.com", "engadget.com",
150             "10086.cn", "360.cn", "marca.com", "dropbox.com", "ign.com", "match.com", "google.pt",
151             "facemoods.com", "hardsextube.com", "google.com.ph", "lockerz.com", "istockphoto.com",
152             "partypoker.com", "netlog.com", "outbrain.com", "elpais.com", "fiverr.com",
153             "biglobe.ne.jp", "corriere.it", "love21cn.com", "yesky.com", "spankwire.com",
154             "ig.com.br", "imagevenue.com", "hubpages.com", "google.co.ve"};
155 
156 // TODO - this works, but is slow and cts doesn't do anything with the result.
157 // Maybe require a min performance, a min cache size (detectable) and/or move
158 // to perf testing
159     private static final int LOOKUP_COUNT_GOAL = URLS.length;
skiptestDnsPerf()160     public void skiptestDnsPerf() {
161         ArrayList<String> results = new ArrayList<String>();
162         int failures = 0;
163         try {
164             for (int numberOfUrls = URLS.length; numberOfUrls > 0; numberOfUrls--) {
165                 failures = 0;
166                 int iterationLimit = LOOKUP_COUNT_GOAL / numberOfUrls;
167                 long startTime = SystemClock.elapsedRealtimeNanos();
168                 for (int iteration = 0; iteration < iterationLimit; iteration++) {
169                     for (int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++) {
170                         try {
171                             InetAddress addr = InetAddress.getByName(URLS[urlIndex]);
172                         } catch (UnknownHostException e) {
173                             Log.e(TAG, "failed first lookup of " + URLS[urlIndex]);
174                             failures++;
175                             try {
176                                 InetAddress addr = InetAddress.getByName(URLS[urlIndex]);
177                             } catch (UnknownHostException ee) {
178                                 failures++;
179                                 Log.e(TAG, "failed SECOND lookup of " + URLS[urlIndex]);
180                             }
181                         }
182                     }
183                 }
184                 long endTime = SystemClock.elapsedRealtimeNanos();
185                 float nsPer = ((float)(endTime-startTime) / iterationLimit) / numberOfUrls/ 1000;
186                 String thisResult = new String("getByName for " + numberOfUrls + " took " +
187                         (endTime - startTime)/1000 + "(" + nsPer + ") with " +
188                         failures + " failures\n");
189                 Log.d(TAG, thisResult);
190                 results.add(thisResult);
191             }
192             // build up a list of addresses
193             ArrayList<byte[]> addressList = new ArrayList<byte[]>();
194             for (String url : URLS) {
195                 try {
196                     InetAddress addr = InetAddress.getByName(url);
197                     addressList.add(addr.getAddress());
198                 } catch (UnknownHostException e) {
199                     Log.e(TAG, "Exception making reverseDNS list: " + e.toString());
200                 }
201             }
202             for (int numberOfAddrs = addressList.size(); numberOfAddrs > 0; numberOfAddrs--) {
203                 int iterationLimit = LOOKUP_COUNT_GOAL / numberOfAddrs;
204                 failures = 0;
205                 long startTime = SystemClock.elapsedRealtimeNanos();
206                 for (int iteration = 0; iteration < iterationLimit; iteration++) {
207                     for (int addrIndex = 0; addrIndex < numberOfAddrs; addrIndex++) {
208                         try {
209                             InetAddress addr = InetAddress.getByAddress(addressList.get(addrIndex));
210                             String hostname = addr.getHostName();
211                         } catch (UnknownHostException e) {
212                             failures++;
213                             Log.e(TAG, "Failure doing reverse DNS lookup: " + e.toString());
214                             try {
215                                 InetAddress addr =
216                                         InetAddress.getByAddress(addressList.get(addrIndex));
217                                 String hostname = addr.getHostName();
218 
219                             } catch (UnknownHostException ee) {
220                                 failures++;
221                                 Log.e(TAG, "Failure doing SECOND reverse DNS lookup: " +
222                                         ee.toString());
223                             }
224                         }
225                     }
226                 }
227                 long endTime = SystemClock.elapsedRealtimeNanos();
228                 float nsPer = ((endTime-startTime) / iterationLimit) / numberOfAddrs / 1000;
229                 String thisResult = new String("getHostName for " + numberOfAddrs + " took " +
230                         (endTime - startTime)/1000 + "(" + nsPer + ") with " +
231                         failures + " failures\n");
232                 Log.d(TAG, thisResult);
233                 results.add(thisResult);
234             }
235             for (String result : results) Log.d(TAG, result);
236 
237             InetAddress exit = InetAddress.getByName("exitrightnow.com");
238             Log.e(TAG, " exit address= "+exit.toString());
239 
240         } catch (Exception e) {
241             Log.e(TAG, "bad URL in testDnsPerf: " + e.toString());
242         }
243     }
244 }
245