PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/cts/tests/tests/net/src/android/net/cts/DnsTest.java

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