tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. */
  20. exports.defineAutoTests = function () {
  21. var isAndroid = (cordova.platformId === "android");
  22. describe('Whitelist API (cordova.whitelist)', function () {
  23. it("should exist", function () {
  24. expect(cordova.whitelist).toBeDefined();
  25. });
  26. describe("Match function (cordova.whitelist.match) that checks URLs against patterns", function () {
  27. function expectMatchWithResult(result) {
  28. return (function (url, patterns, description) {
  29. description = description || ((result ? "should accept " : "should reject ") + url + " for " + JSON.stringify(patterns));
  30. this.result = result;
  31. describe("Match function", function () {
  32. if (!isAndroid) {
  33. pending("Whitelist Plugin only exists for Android");
  34. }
  35. // Timeout is 7.5 seconds to allow physical devices enough
  36. // time to query the response. This is important for some
  37. // Android devices.
  38. var originalTimeout,
  39. cb;
  40. beforeEach(function (done) {
  41. originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  42. jasmine.DEFAULT_TIMEOUT_INTERVAL = 7500;
  43. cb = jasmine.createSpy('spy').and.callFake(function () {
  44. done();
  45. });
  46. cordova.whitelist.match(url, patterns, cb);
  47. });
  48. afterEach(function () {
  49. jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
  50. });
  51. it(description, function () {
  52. expect(cb).toHaveBeenCalledWith(result);
  53. });
  54. });
  55. });
  56. }
  57. var itShouldMatch = expectMatchWithResult(true);
  58. var itShouldNotMatch = expectMatchWithResult(false);
  59. it("should exist", function () {
  60. expect(cordova.whitelist.match).toBeDefined();
  61. expect(typeof cordova.whitelist.match).toBe("function");
  62. });
  63. itShouldMatch('http://www.apache.org/', ['*'], "should accept any domain for *");
  64. itShouldNotMatch('http://www.apache.org/', [], "should not accept any domain for []");
  65. itShouldMatch('http://apache.org/', ['http://*.apache.org']);
  66. itShouldMatch('http://www.apache.org/', ['http://*.apache.org']);
  67. itShouldMatch('http://www.apache.org/some/path', ['http://*.apache.org']);
  68. itShouldMatch('http://some.domain.under.apache.org/', ['http://*.apache.org']);
  69. itShouldMatch('http://user:pass@apache.org/', ['http://*.apache.org']);
  70. itShouldMatch('http://user:pass@www.apache.org/', ['http://*.apache.org']);
  71. itShouldMatch('http://www.apache.org/?some=params', ['http://*.apache.org']);
  72. itShouldNotMatch('http://apache.com/', ['http://*.apache.org']);
  73. itShouldNotMatch('http://www.evil.com/?url=www.apache.org', ['http://*.apache.org']);
  74. itShouldNotMatch('http://www.evil.com/?url=http://www.apache.org', ['http://*.apache.org']);
  75. itShouldNotMatch('http://www.evil.com/?url=http%3A%2F%2Fwww%2Eapache%2Eorg', ['http://*.apache.org']);
  76. itShouldNotMatch('https://apache.org/', ['http://*.apache.org']);
  77. itShouldNotMatch('http://www.apache.org:pass@evil.com/', ['http://*.apache.org']);
  78. itShouldNotMatch('http://www.apache.org.evil.com/', ['http://*.apache.org']);
  79. itShouldMatch('http://www.apache.org/', ['http://*.apache.org', 'https://*.apache.org']);
  80. itShouldMatch('https://www.apache.org/', ['http://*.apache.org', 'https://*.apache.org']);
  81. itShouldNotMatch('ftp://www.apache.org/', ['http://*.apache.org', 'https://*.apache.org']);
  82. itShouldNotMatch('http://www.apache.com/', ['http://*.apache.org', 'https://*.apache.org']);
  83. itShouldMatch('http://www.apache.org/', ['http://www.apache.org']);
  84. itShouldNotMatch('http://build.apache.org/', ['http://www.apache.org']);
  85. itShouldNotMatch('http://apache.org/', ['http://www.apache.org']);
  86. itShouldMatch('http://www.apache.org/', ['http://*/*']);
  87. itShouldMatch('http://www.apache.org/foo/bar.html', ['http://*/*']);
  88. itShouldMatch('http://www.apache.org/foo', ['http://*/foo*']);
  89. itShouldMatch('http://www.apache.org/foo/bar.html', ['http://*/foo*']);
  90. itShouldNotMatch('http://www.apache.org/', ['http://*/foo*']);
  91. itShouldMatch('file:///foo', ['file:///*']);
  92. itShouldMatch('file:///foo', ['file:///foo*']);
  93. itShouldMatch('file:///foo/bar.html', ['file:///foo*']);
  94. itShouldNotMatch('file:///foo.html', []);
  95. itShouldNotMatch('http://www.apache.org/etc/foo', ['http://www.apache.org/foo*']);
  96. itShouldNotMatch('http://www.apache.org/foo', ['file:///foo*']);
  97. itShouldMatch('http://www.apache.org/', ['*://www.apache.org/*']);
  98. itShouldMatch('https://www.apache.org/', ['*://www.apache.org/*']);
  99. itShouldMatch('ftp://www.apache.org/', ['*://www.apache.org/*']);
  100. itShouldMatch('file://www.apache.org/', ['*://www.apache.org/*']);
  101. if (cordova.platformId == 'android')
  102. itShouldMatch('content://www.apache.org/', ['*://www.apache.org/*']);
  103. itShouldMatch('foo://www.apache.org/', ['*://www.apache.org/*']);
  104. itShouldNotMatch('http://www.apache.com/', ['*://www.apache.org/*']);
  105. itShouldMatch('http://www.apache.org/', ['*.apache.org']);
  106. itShouldMatch('https://www.apache.org/', ['*.apache.org']);
  107. itShouldNotMatch('ftp://www.apache.org/', ['*.apache.org']);
  108. itShouldMatch('http://www.apache.org:81/', ['http://www.apache.org:81/*']);
  109. itShouldMatch('http://user:pass@www.apache.org:81/foo/bar.html', ['http://www.apache.org:81/*']);
  110. itShouldNotMatch('http://www.apache.org:80/', ['http://www.apache.org:81/*']);
  111. itShouldNotMatch('http://www.apache.org/', ['http://www.apache.org:81/*']);
  112. itShouldNotMatch('http://www.apache.org:foo/', ['http://www.apache.org:81/*']);
  113. itShouldNotMatch('http://www.apache.org:81@www.apache.org/', ['http://www.apache.org:81/*']);
  114. itShouldNotMatch('http://www.apache.org:81@www.evil.com/', ['http://www.apache.org:81/*']);
  115. itShouldMatch('http://www.APAche.org/', ['*.apache.org']);
  116. itShouldMatch('http://WWw.apache.org/', ['*.apache.org']);
  117. itShouldMatch('http://www.apache.org/', ['*.APACHE.ORG']);
  118. itShouldMatch('HTTP://www.apache.org/', ['*.apache.org']);
  119. itShouldMatch('HTTP://www.apache.org/', ['http://*.apache.org']);
  120. itShouldMatch('http://www.apache.org/', ['HTTP://*.apache.org']);
  121. itShouldMatch('http://www.apache.org/foo/', ['*://*.apache.org/foo/*']);
  122. itShouldMatch('http://www.apache.org/foo/bar', ['*://*.apache.org/foo/*']);
  123. itShouldNotMatch('http://www.apache.org/bar/foo/', ['*://*.apache.org/foo/*']);
  124. itShouldNotMatch('http://www.apache.org/Foo/', ['*://*.apache.org/foo/*']);
  125. itShouldNotMatch('http://www.apache.org/Foo/bar', ['*://*.apache.org/foo/*']);
  126. });
  127. describe("Test function (cordova.whitelist.test) that checks against config.xml", function () {
  128. function expectTestWithResult(result) {
  129. return (function (url, description) {
  130. description = description || ((result ? "should accept " : "should reject ") + url);
  131. describe("Test function", function () {
  132. if (!isAndroid) {
  133. pending("Whitelist Plugin only exists for Android");
  134. }
  135. var cb,
  136. originalTimeout;
  137. beforeEach(function (done) {
  138. originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  139. jasmine.DEFAULT_TIMEOUT_INTERVAL = 7500;
  140. cb = jasmine.createSpy('spy').and.callFake(function (){
  141. done();
  142. });
  143. cordova.whitelist.test(url, cb);
  144. });
  145. afterEach(function () {
  146. jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
  147. });
  148. it(description, function () {
  149. expect(cb).toHaveBeenCalledWith(result);
  150. });
  151. });
  152. });
  153. }
  154. var itShouldAccept = expectTestWithResult(true);
  155. var itShouldReject = expectTestWithResult(false);
  156. it("should exist", function () {
  157. expect(cordova.whitelist.test).toBeDefined();
  158. expect(typeof cordova.whitelist.test).toBe("function");
  159. });
  160. itShouldAccept('http://apache.org');
  161. itShouldAccept('http://apache.org/');
  162. itShouldAccept('http://www.apache.org/');
  163. itShouldAccept('http://www.apache.org/some/path');
  164. itShouldAccept('http://some.domain.under.apache.org/');
  165. itShouldAccept('http://user:pass@apache.org/');
  166. itShouldAccept('http://user:pass@www.apache.org/');
  167. itShouldAccept('https://www.apache.org/');
  168. itShouldReject('ftp://www.apache.org/');
  169. itShouldReject('http://www.apache.com/');
  170. itShouldReject('http://www.apache.org:pass@evil.com/');
  171. itShouldReject('http://www.apache.org.evil.com/');
  172. itShouldAccept('file:///foo');
  173. if (cordova.platformId == 'android')
  174. itShouldReject('content:///foo');
  175. });
  176. });
  177. }