OptionHandlerTest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "OptionHandlerImpl.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Option.h"
  4. #include "prefs.h"
  5. #include "Exception.h"
  6. namespace aria2 {
  7. class OptionHandlerTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(OptionHandlerTest);
  9. CPPUNIT_TEST(testBooleanOptionHandler);
  10. CPPUNIT_TEST(testNumberOptionHandler);
  11. CPPUNIT_TEST(testNumberOptionHandler_min);
  12. CPPUNIT_TEST(testNumberOptionHandler_max);
  13. CPPUNIT_TEST(testNumberOptionHandler_min_max);
  14. CPPUNIT_TEST(testUnitNumberOptionHandler);
  15. CPPUNIT_TEST(testParameterOptionHandler_1argInit);
  16. CPPUNIT_TEST(testParameterOptionHandler_2argsInit);
  17. CPPUNIT_TEST(testParameterOptionHandler_listInit);
  18. CPPUNIT_TEST(testDefaultOptionHandler);
  19. CPPUNIT_TEST(testFloatNumberOptionHandler);
  20. CPPUNIT_TEST(testFloatNumberOptionHandler_min);
  21. CPPUNIT_TEST(testFloatNumberOptionHandler_max);
  22. CPPUNIT_TEST(testFloatNumberOptionHandler_min_max);
  23. CPPUNIT_TEST(testHttpProxyOptionHandler);
  24. CPPUNIT_TEST(testHttpProxyUserOptionHandler);
  25. CPPUNIT_TEST(testHttpProxyPasswdOptionHandler);
  26. CPPUNIT_TEST(testDeprecatedOptionHandler);
  27. CPPUNIT_TEST_SUITE_END();
  28. public:
  29. void testBooleanOptionHandler();
  30. void testNumberOptionHandler();
  31. void testNumberOptionHandler_min();
  32. void testNumberOptionHandler_max();
  33. void testNumberOptionHandler_min_max();
  34. void testUnitNumberOptionHandler();
  35. void testParameterOptionHandler_1argInit();
  36. void testParameterOptionHandler_2argsInit();
  37. void testParameterOptionHandler_listInit();
  38. void testDefaultOptionHandler();
  39. void testFloatNumberOptionHandler();
  40. void testFloatNumberOptionHandler_min();
  41. void testFloatNumberOptionHandler_max();
  42. void testFloatNumberOptionHandler_min_max();
  43. void testHttpProxyOptionHandler();
  44. void testHttpProxyUserOptionHandler();
  45. void testHttpProxyPasswdOptionHandler();
  46. void testDeprecatedOptionHandler();
  47. };
  48. CPPUNIT_TEST_SUITE_REGISTRATION( OptionHandlerTest );
  49. void OptionHandlerTest::testBooleanOptionHandler()
  50. {
  51. BooleanOptionHandler handler(PREF_DAEMON);
  52. CPPUNIT_ASSERT(handler.canHandle(PREF_DAEMON->k));
  53. CPPUNIT_ASSERT(!handler.canHandle(PREF_DIR->k));
  54. Option option;
  55. handler.parse(option, A2_V_TRUE);
  56. CPPUNIT_ASSERT_EQUAL(std::string(A2_V_TRUE), option.get(PREF_DAEMON));
  57. handler.parse(option, A2_V_FALSE);
  58. CPPUNIT_ASSERT_EQUAL(std::string(A2_V_FALSE), option.get(PREF_DAEMON));
  59. try {
  60. handler.parse(option, "hello");
  61. CPPUNIT_FAIL("exception must be thrown.");
  62. } catch(Exception& e) {}
  63. CPPUNIT_ASSERT_EQUAL(std::string("true, false"),
  64. handler.createPossibleValuesString());
  65. }
  66. void OptionHandlerTest::testNumberOptionHandler()
  67. {
  68. NumberOptionHandler handler(PREF_TIMEOUT);
  69. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  70. CPPUNIT_ASSERT(!handler.canHandle(PREF_DIR->k));
  71. Option option;
  72. handler.parse(option, "0");
  73. CPPUNIT_ASSERT_EQUAL(std::string("0"), option.get(PREF_TIMEOUT));
  74. CPPUNIT_ASSERT_EQUAL(std::string("*-*"),
  75. handler.createPossibleValuesString());
  76. }
  77. void OptionHandlerTest::testNumberOptionHandler_min()
  78. {
  79. NumberOptionHandler handler(PREF_TIMEOUT, "", "", 1);
  80. Option option;
  81. handler.parse(option, "1");
  82. CPPUNIT_ASSERT_EQUAL(std::string("1"), option.get(PREF_TIMEOUT));
  83. try {
  84. handler.parse(option, "0");
  85. CPPUNIT_FAIL("exception must be thrown.");
  86. } catch(Exception& e) {}
  87. CPPUNIT_ASSERT_EQUAL(std::string("1-*"),
  88. handler.createPossibleValuesString());
  89. }
  90. void OptionHandlerTest::testNumberOptionHandler_max()
  91. {
  92. NumberOptionHandler handler(PREF_TIMEOUT, "", "", -1, 100);
  93. Option option;
  94. handler.parse(option, "100");
  95. CPPUNIT_ASSERT_EQUAL(std::string("100"), option.get(PREF_TIMEOUT));
  96. try {
  97. handler.parse(option, "101");
  98. CPPUNIT_FAIL("exception must be thrown.");
  99. } catch(Exception& e) {}
  100. CPPUNIT_ASSERT_EQUAL(std::string("*-100"),
  101. handler.createPossibleValuesString());
  102. }
  103. void OptionHandlerTest::testNumberOptionHandler_min_max()
  104. {
  105. NumberOptionHandler handler(PREF_TIMEOUT, "", "", 1, 100);
  106. Option option;
  107. handler.parse(option, "1");
  108. CPPUNIT_ASSERT_EQUAL(std::string("1"), option.get(PREF_TIMEOUT));
  109. handler.parse(option, "100");
  110. CPPUNIT_ASSERT_EQUAL(std::string("100"), option.get(PREF_TIMEOUT));
  111. try {
  112. handler.parse(option, "0");
  113. CPPUNIT_FAIL("exception must be thrown.");
  114. } catch(Exception& e) {}
  115. try {
  116. handler.parse(option, "101");
  117. CPPUNIT_FAIL("exception must be thrown.");
  118. } catch(Exception& e) {}
  119. CPPUNIT_ASSERT_EQUAL(std::string("1-100"),
  120. handler.createPossibleValuesString());
  121. }
  122. void OptionHandlerTest::testUnitNumberOptionHandler()
  123. {
  124. UnitNumberOptionHandler handler(PREF_TIMEOUT);
  125. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  126. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  127. Option option;
  128. handler.parse(option, "4294967296");
  129. CPPUNIT_ASSERT_EQUAL(std::string("4294967296"), option.get(PREF_TIMEOUT));
  130. handler.parse(option, "4096M");
  131. CPPUNIT_ASSERT_EQUAL(std::string("4294967296"), option.get(PREF_TIMEOUT));
  132. handler.parse(option, "4096K");
  133. CPPUNIT_ASSERT_EQUAL(std::string("4194304"), option.get(PREF_TIMEOUT));
  134. try {
  135. handler.parse(option, "K");
  136. CPPUNIT_FAIL("exception must be thrown.");
  137. } catch(Exception& e) {}
  138. try {
  139. handler.parse(option, "M");
  140. } catch(Exception& e) {}
  141. try {
  142. handler.parse(option, "");
  143. CPPUNIT_FAIL("exception must be thrown.");
  144. } catch(Exception& e) {}
  145. }
  146. void OptionHandlerTest::testParameterOptionHandler_1argInit()
  147. {
  148. ParameterOptionHandler handler(PREF_TIMEOUT, "", "", "value1");
  149. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  150. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  151. Option option;
  152. handler.parse(option, "value1");
  153. CPPUNIT_ASSERT_EQUAL(std::string("value1"), option.get(PREF_TIMEOUT));
  154. try {
  155. handler.parse(option, "value3");
  156. CPPUNIT_FAIL("exception must be thrown.");
  157. } catch(Exception& e) {}
  158. CPPUNIT_ASSERT_EQUAL(std::string("value1"),
  159. handler.createPossibleValuesString());
  160. }
  161. void OptionHandlerTest::testParameterOptionHandler_2argsInit()
  162. {
  163. ParameterOptionHandler handler(PREF_TIMEOUT, "", "", "value1", "value2");
  164. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  165. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  166. Option option;
  167. handler.parse(option, "value1");
  168. CPPUNIT_ASSERT_EQUAL(std::string("value1"), option.get(PREF_TIMEOUT));
  169. handler.parse(option, "value2");
  170. CPPUNIT_ASSERT_EQUAL(std::string("value2"), option.get(PREF_TIMEOUT));
  171. try {
  172. handler.parse(option, "value3");
  173. CPPUNIT_FAIL("exception must be thrown.");
  174. } catch(Exception& e) {}
  175. CPPUNIT_ASSERT_EQUAL(std::string("value1, value2"),
  176. handler.createPossibleValuesString());
  177. }
  178. void OptionHandlerTest::testParameterOptionHandler_listInit()
  179. {
  180. std::vector<std::string> validValues;
  181. validValues.push_back("value1");
  182. validValues.push_back("value2");
  183. ParameterOptionHandler handler(PREF_TIMEOUT, "", "", validValues);
  184. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  185. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  186. Option option;
  187. handler.parse(option, "value1");
  188. CPPUNIT_ASSERT_EQUAL(std::string("value1"), option.get(PREF_TIMEOUT));
  189. handler.parse(option, "value2");
  190. CPPUNIT_ASSERT_EQUAL(std::string("value2"), option.get(PREF_TIMEOUT));
  191. try {
  192. handler.parse(option, "value3");
  193. CPPUNIT_FAIL("exception must be thrown.");
  194. } catch(Exception& e) {}
  195. CPPUNIT_ASSERT_EQUAL(std::string("value1, value2"),
  196. handler.createPossibleValuesString());
  197. }
  198. void OptionHandlerTest::testDefaultOptionHandler()
  199. {
  200. DefaultOptionHandler handler(PREF_TIMEOUT);
  201. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  202. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  203. Option option;
  204. handler.parse(option, "bar");
  205. CPPUNIT_ASSERT_EQUAL(std::string("bar"), option.get(PREF_TIMEOUT));
  206. handler.parse(option, "");
  207. CPPUNIT_ASSERT_EQUAL(std::string(""), option.get(PREF_TIMEOUT));
  208. CPPUNIT_ASSERT_EQUAL(std::string(""), handler.createPossibleValuesString());
  209. handler.addTag("apple");
  210. CPPUNIT_ASSERT_EQUAL(std::string("apple"), handler.toTagString());
  211. handler.addTag("orange");
  212. CPPUNIT_ASSERT_EQUAL(std::string("apple, orange"), handler.toTagString());
  213. CPPUNIT_ASSERT(handler.hasTag("apple"));
  214. CPPUNIT_ASSERT(handler.hasTag("orange"));
  215. CPPUNIT_ASSERT(!handler.hasTag("pineapple"));
  216. }
  217. void OptionHandlerTest::testFloatNumberOptionHandler()
  218. {
  219. FloatNumberOptionHandler handler(PREF_TIMEOUT);
  220. CPPUNIT_ASSERT(handler.canHandle(PREF_TIMEOUT->k));
  221. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  222. Option option;
  223. handler.parse(option, "1.0");
  224. CPPUNIT_ASSERT_EQUAL(std::string("1.0"), option.get(PREF_TIMEOUT));
  225. CPPUNIT_ASSERT_EQUAL(std::string("*-*"),
  226. handler.createPossibleValuesString());
  227. }
  228. void OptionHandlerTest::testFloatNumberOptionHandler_min()
  229. {
  230. FloatNumberOptionHandler handler(PREF_TIMEOUT, "", "", 0.0);
  231. Option option;
  232. handler.parse(option, "0.0");
  233. CPPUNIT_ASSERT_EQUAL(std::string("0.0"), option.get(PREF_TIMEOUT));
  234. try {
  235. handler.parse(option, "-0.1");
  236. CPPUNIT_FAIL("exception must be thrown.");
  237. } catch(Exception& e) {}
  238. CPPUNIT_ASSERT_EQUAL(std::string("0.0-*"),
  239. handler.createPossibleValuesString());
  240. }
  241. void OptionHandlerTest::testFloatNumberOptionHandler_max()
  242. {
  243. FloatNumberOptionHandler handler(PREF_TIMEOUT, "", "", -1, 10.0);
  244. Option option;
  245. handler.parse(option, "10.0");
  246. CPPUNIT_ASSERT_EQUAL(std::string("10.0"), option.get(PREF_TIMEOUT));
  247. try {
  248. handler.parse(option, "10.1");
  249. CPPUNIT_FAIL("exception must be thrown.");
  250. } catch(Exception& e) {}
  251. CPPUNIT_ASSERT_EQUAL(std::string("*-10.0"),
  252. handler.createPossibleValuesString());
  253. }
  254. void OptionHandlerTest::testFloatNumberOptionHandler_min_max()
  255. {
  256. FloatNumberOptionHandler handler(PREF_TIMEOUT, "", "", 0.0, 10.0);
  257. Option option;
  258. handler.parse(option, "0.0");
  259. CPPUNIT_ASSERT_EQUAL(std::string("0.0"), option.get(PREF_TIMEOUT));
  260. handler.parse(option, "10.0");
  261. CPPUNIT_ASSERT_EQUAL(std::string("10.0"), option.get(PREF_TIMEOUT));
  262. try {
  263. handler.parse(option, "-0.1");
  264. CPPUNIT_FAIL("exception must be thrown.");
  265. } catch(Exception& e) {}
  266. try {
  267. handler.parse(option, "10.1");
  268. CPPUNIT_FAIL("exception must be thrown.");
  269. } catch(Exception& e) {}
  270. CPPUNIT_ASSERT_EQUAL(std::string("0.0-10.0"),
  271. handler.createPossibleValuesString());
  272. }
  273. void OptionHandlerTest::testHttpProxyOptionHandler()
  274. {
  275. HttpProxyOptionHandler handler(PREF_HTTP_PROXY, "", "");
  276. CPPUNIT_ASSERT(handler.canHandle(PREF_HTTP_PROXY->k));
  277. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  278. Option option;
  279. handler.parse(option, "proxy:65535");
  280. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:65535/"),
  281. option.get(PREF_HTTP_PROXY));
  282. handler.parse(option, "http://proxy:8080");
  283. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:8080/"),
  284. option.get(PREF_HTTP_PROXY));
  285. handler.parse(option, "");
  286. CPPUNIT_ASSERT(option.defined(PREF_HTTP_PROXY));
  287. CPPUNIT_ASSERT(option.blank(PREF_HTTP_PROXY));
  288. try {
  289. handler.parse(option, "http://bar:65536");
  290. CPPUNIT_FAIL("exception must be thrown.");
  291. } catch(Exception& e) {}
  292. CPPUNIT_ASSERT_EQUAL(std::string("[http://][USER:PASSWORD@]HOST[:PORT]"),
  293. handler.createPossibleValuesString());
  294. handler.parse(option, "http://user%40:passwd%40@proxy:8080");
  295. CPPUNIT_ASSERT_EQUAL(std::string("http://user%40:passwd%40@proxy:8080/"),
  296. option.get(PREF_HTTP_PROXY));
  297. option.put(PREF_HTTP_PROXY_USER, "proxy@user");
  298. handler.parse(option, "http://proxy:8080");
  299. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user@proxy:8080/"),
  300. option.get(PREF_HTTP_PROXY));
  301. option.put(PREF_HTTP_PROXY_PASSWD, "proxy@passwd");
  302. handler.parse(option, "http://proxy:8080");
  303. CPPUNIT_ASSERT_EQUAL
  304. (std::string("http://proxy%40user:proxy%40passwd@proxy:8080/"),
  305. option.get(PREF_HTTP_PROXY));
  306. handler.parse(option, "http://user:passwd@proxy:8080");
  307. CPPUNIT_ASSERT_EQUAL(std::string("http://user:passwd@proxy:8080/"),
  308. option.get(PREF_HTTP_PROXY));
  309. option.put(PREF_HTTP_PROXY_PASSWD, "");
  310. handler.parse(option, "http://proxy:8080");
  311. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user:@proxy:8080/"),
  312. option.get(PREF_HTTP_PROXY));
  313. handler.parse(option, "http://[::1]:8080");
  314. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user:@[::1]:8080/"),
  315. option.get(PREF_HTTP_PROXY));
  316. }
  317. void OptionHandlerTest::testHttpProxyUserOptionHandler()
  318. {
  319. HttpProxyUserOptionHandler handler(PREF_HTTP_PROXY_USER, "", "");
  320. Option option;
  321. handler.parse(option, "proxyuser");
  322. CPPUNIT_ASSERT_EQUAL(std::string("proxyuser"),
  323. option.get(PREF_HTTP_PROXY_USER));
  324. option.put(PREF_HTTP_PROXY, "http://proxy:8080");
  325. handler.parse(option, "proxy@user");
  326. CPPUNIT_ASSERT_EQUAL(std::string("proxy@user"),
  327. option.get(PREF_HTTP_PROXY_USER));
  328. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy%40user@proxy:8080"),
  329. option.get(PREF_HTTP_PROXY));
  330. option.put(PREF_HTTP_PROXY, "http://user@proxy:8080");
  331. handler.parse(option, "proxyuser");
  332. CPPUNIT_ASSERT_EQUAL(std::string("http://proxyuser@proxy:8080"),
  333. option.get(PREF_HTTP_PROXY));
  334. option.put(PREF_HTTP_PROXY, "http://user:passwd%40@proxy:8080");
  335. handler.parse(option, "proxyuser");
  336. CPPUNIT_ASSERT_EQUAL(std::string("http://proxyuser:passwd%40@proxy:8080"),
  337. option.get(PREF_HTTP_PROXY));
  338. handler.parse(option, "");
  339. CPPUNIT_ASSERT_EQUAL(std::string("http://:passwd%40@proxy:8080"),
  340. option.get(PREF_HTTP_PROXY));
  341. }
  342. void OptionHandlerTest::testHttpProxyPasswdOptionHandler()
  343. {
  344. HttpProxyPasswdOptionHandler handler(PREF_HTTP_PROXY_PASSWD, "", "");
  345. Option option;
  346. handler.parse(option, "proxypasswd");
  347. CPPUNIT_ASSERT_EQUAL(std::string("proxypasswd"),
  348. option.get(PREF_HTTP_PROXY_PASSWD));
  349. option.put(PREF_HTTP_PROXY, "http://proxy:8080");
  350. handler.parse(option, "proxy@passwd");
  351. CPPUNIT_ASSERT_EQUAL(std::string("proxy@passwd"),
  352. option.get(PREF_HTTP_PROXY_PASSWD));
  353. CPPUNIT_ASSERT_EQUAL(std::string("http://:proxy%40passwd@proxy:8080"),
  354. option.get(PREF_HTTP_PROXY));
  355. option.put(PREF_HTTP_PROXY, "http://:pass@proxy:8080");
  356. handler.parse(option, "proxypasswd");
  357. CPPUNIT_ASSERT_EQUAL(std::string("http://:proxypasswd@proxy:8080"),
  358. option.get(PREF_HTTP_PROXY));
  359. option.put(PREF_HTTP_PROXY, "http://user%40:pass@proxy:8080");
  360. handler.parse(option, "proxypasswd");
  361. CPPUNIT_ASSERT_EQUAL(std::string("http://user%40:proxypasswd@proxy:8080"),
  362. option.get(PREF_HTTP_PROXY));
  363. handler.parse(option, "");
  364. CPPUNIT_ASSERT_EQUAL(std::string("http://user%40:@proxy:8080"),
  365. option.get(PREF_HTTP_PROXY));
  366. }
  367. void OptionHandlerTest::testDeprecatedOptionHandler()
  368. {
  369. {
  370. DeprecatedOptionHandler handler
  371. (SharedHandle<OptionHandler>(new DefaultOptionHandler(PREF_TIMEOUT)));
  372. Option option;
  373. handler.parse(option, "foo");
  374. CPPUNIT_ASSERT(!option.defined(PREF_TIMEOUT));
  375. }
  376. {
  377. DeprecatedOptionHandler handler
  378. (SharedHandle<OptionHandler>(new DefaultOptionHandler(PREF_TIMEOUT)),
  379. SharedHandle<OptionHandler>(new DefaultOptionHandler(PREF_DIR)));
  380. Option option;
  381. handler.parse(option, "foo");
  382. CPPUNIT_ASSERT(!option.defined(PREF_TIMEOUT));
  383. CPPUNIT_ASSERT_EQUAL(std::string("foo"), option.get(PREF_DIR));
  384. }
  385. }
  386. } // namespace aria2