OptionHandlerTest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "OptionHandlerImpl.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "prefs.h"
  4. #include "Exception.h"
  5. namespace aria2 {
  6. class OptionHandlerTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(OptionHandlerTest);
  8. CPPUNIT_TEST(testNullOptionHandler);
  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_SUITE_END();
  25. public:
  26. void testNullOptionHandler();
  27. void testBooleanOptionHandler();
  28. void testNumberOptionHandler();
  29. void testNumberOptionHandler_min();
  30. void testNumberOptionHandler_max();
  31. void testNumberOptionHandler_min_max();
  32. void testUnitNumberOptionHandler();
  33. void testParameterOptionHandler_1argInit();
  34. void testParameterOptionHandler_2argsInit();
  35. void testParameterOptionHandler_listInit();
  36. void testDefaultOptionHandler();
  37. void testFloatNumberOptionHandler();
  38. void testFloatNumberOptionHandler_min();
  39. void testFloatNumberOptionHandler_max();
  40. void testFloatNumberOptionHandler_min_max();
  41. void testHttpProxyOptionHandler();
  42. };
  43. CPPUNIT_TEST_SUITE_REGISTRATION( OptionHandlerTest );
  44. void OptionHandlerTest::testNullOptionHandler()
  45. {
  46. NullOptionHandler handler;
  47. CPPUNIT_ASSERT(handler.canHandle("foo"));
  48. Option option;
  49. handler.parse(option, "bar");
  50. CPPUNIT_ASSERT(!option.defined("bar"));
  51. }
  52. void OptionHandlerTest::testBooleanOptionHandler()
  53. {
  54. BooleanOptionHandler handler("foo");
  55. CPPUNIT_ASSERT(handler.canHandle("foo"));
  56. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  57. Option option;
  58. handler.parse(option, V_TRUE);
  59. CPPUNIT_ASSERT_EQUAL(std::string(V_TRUE), option.get("foo"));
  60. handler.parse(option, V_FALSE);
  61. CPPUNIT_ASSERT_EQUAL(std::string(V_FALSE), option.get("foo"));
  62. try {
  63. handler.parse(option, "hello");
  64. CPPUNIT_FAIL("exception must be thrown.");
  65. } catch(Exception& e) {}
  66. CPPUNIT_ASSERT_EQUAL(std::string("true,false"),
  67. handler.createPossibleValuesString());
  68. }
  69. void OptionHandlerTest::testNumberOptionHandler()
  70. {
  71. NumberOptionHandler handler("foo");
  72. CPPUNIT_ASSERT(handler.canHandle("foo"));
  73. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  74. Option option;
  75. handler.parse(option, "0");
  76. CPPUNIT_ASSERT_EQUAL(std::string("0"), option.get("foo"));
  77. CPPUNIT_ASSERT_EQUAL(std::string("*-*"),
  78. handler.createPossibleValuesString());
  79. }
  80. void OptionHandlerTest::testNumberOptionHandler_min()
  81. {
  82. NumberOptionHandler handler("foo", "", "", 1);
  83. Option option;
  84. handler.parse(option, "1");
  85. CPPUNIT_ASSERT_EQUAL(std::string("1"), option.get("foo"));
  86. try {
  87. handler.parse(option, "0");
  88. CPPUNIT_FAIL("exception must be thrown.");
  89. } catch(Exception& e) {}
  90. CPPUNIT_ASSERT_EQUAL(std::string("1-*"),
  91. handler.createPossibleValuesString());
  92. }
  93. void OptionHandlerTest::testNumberOptionHandler_max()
  94. {
  95. NumberOptionHandler handler("foo", "", "", -1, 100);
  96. Option option;
  97. handler.parse(option, "100");
  98. CPPUNIT_ASSERT_EQUAL(std::string("100"), option.get("foo"));
  99. try {
  100. handler.parse(option, "101");
  101. CPPUNIT_FAIL("exception must be thrown.");
  102. } catch(Exception& e) {}
  103. CPPUNIT_ASSERT_EQUAL(std::string("*-100"),
  104. handler.createPossibleValuesString());
  105. }
  106. void OptionHandlerTest::testNumberOptionHandler_min_max()
  107. {
  108. NumberOptionHandler handler("foo", "", "", 1, 100);
  109. Option option;
  110. handler.parse(option, "1");
  111. CPPUNIT_ASSERT_EQUAL(std::string("1"), option.get("foo"));
  112. handler.parse(option, "100");
  113. CPPUNIT_ASSERT_EQUAL(std::string("100"), option.get("foo"));
  114. try {
  115. handler.parse(option, "0");
  116. CPPUNIT_FAIL("exception must be thrown.");
  117. } catch(Exception& e) {}
  118. try {
  119. handler.parse(option, "101");
  120. CPPUNIT_FAIL("exception must be thrown.");
  121. } catch(Exception& e) {}
  122. CPPUNIT_ASSERT_EQUAL(std::string("1-100"),
  123. handler.createPossibleValuesString());
  124. }
  125. void OptionHandlerTest::testUnitNumberOptionHandler()
  126. {
  127. UnitNumberOptionHandler handler("foo");
  128. CPPUNIT_ASSERT(handler.canHandle("foo"));
  129. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  130. Option option;
  131. handler.parse(option, "4294967296");
  132. CPPUNIT_ASSERT_EQUAL(std::string("4294967296"), option.get("foo"));
  133. handler.parse(option, "4096M");
  134. CPPUNIT_ASSERT_EQUAL(std::string("4294967296"), option.get("foo"));
  135. handler.parse(option, "4096K");
  136. CPPUNIT_ASSERT_EQUAL(std::string("4194304"), option.get("foo"));
  137. try {
  138. handler.parse(option, "K");
  139. CPPUNIT_FAIL("exception must be thrown.");
  140. } catch(Exception& e) {}
  141. try {
  142. handler.parse(option, "M");
  143. } catch(Exception& e) {}
  144. try {
  145. handler.parse(option, "");
  146. CPPUNIT_FAIL("exception must be thrown.");
  147. } catch(Exception& e) {}
  148. }
  149. void OptionHandlerTest::testParameterOptionHandler_1argInit()
  150. {
  151. ParameterOptionHandler handler("foo", "", "", "value1");
  152. CPPUNIT_ASSERT(handler.canHandle("foo"));
  153. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  154. Option option;
  155. handler.parse(option, "value1");
  156. CPPUNIT_ASSERT_EQUAL(std::string("value1"), option.get("foo"));
  157. try {
  158. handler.parse(option, "value3");
  159. CPPUNIT_FAIL("exception must be thrown.");
  160. } catch(Exception& e) {}
  161. CPPUNIT_ASSERT_EQUAL(std::string("value1"),
  162. handler.createPossibleValuesString());
  163. }
  164. void OptionHandlerTest::testParameterOptionHandler_2argsInit()
  165. {
  166. ParameterOptionHandler handler("foo", "", "", "value1", "value2");
  167. CPPUNIT_ASSERT(handler.canHandle("foo"));
  168. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  169. Option option;
  170. handler.parse(option, "value1");
  171. CPPUNIT_ASSERT_EQUAL(std::string("value1"), option.get("foo"));
  172. handler.parse(option, "value2");
  173. CPPUNIT_ASSERT_EQUAL(std::string("value2"), option.get("foo"));
  174. try {
  175. handler.parse(option, "value3");
  176. CPPUNIT_FAIL("exception must be thrown.");
  177. } catch(Exception& e) {}
  178. CPPUNIT_ASSERT_EQUAL(std::string("value1,value2"),
  179. handler.createPossibleValuesString());
  180. }
  181. void OptionHandlerTest::testParameterOptionHandler_listInit()
  182. {
  183. std::vector<std::string> validValues;
  184. validValues.push_back("value1");
  185. validValues.push_back("value2");
  186. ParameterOptionHandler handler("foo", "", "", validValues);
  187. CPPUNIT_ASSERT(handler.canHandle("foo"));
  188. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  189. Option option;
  190. handler.parse(option, "value1");
  191. CPPUNIT_ASSERT_EQUAL(std::string("value1"), option.get("foo"));
  192. handler.parse(option, "value2");
  193. CPPUNIT_ASSERT_EQUAL(std::string("value2"), option.get("foo"));
  194. try {
  195. handler.parse(option, "value3");
  196. CPPUNIT_FAIL("exception must be thrown.");
  197. } catch(Exception& e) {}
  198. CPPUNIT_ASSERT_EQUAL(std::string("value1,value2"),
  199. handler.createPossibleValuesString());
  200. }
  201. void OptionHandlerTest::testDefaultOptionHandler()
  202. {
  203. DefaultOptionHandler handler("foo");
  204. CPPUNIT_ASSERT(handler.canHandle("foo"));
  205. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  206. Option option;
  207. handler.parse(option, "bar");
  208. CPPUNIT_ASSERT_EQUAL(std::string("bar"), option.get("foo"));
  209. handler.parse(option, "");
  210. CPPUNIT_ASSERT_EQUAL(std::string(""), option.get("foo"));
  211. CPPUNIT_ASSERT_EQUAL(std::string(""), handler.createPossibleValuesString());
  212. handler.addTag("apple");
  213. CPPUNIT_ASSERT_EQUAL(std::string("apple"), handler.toTagString());
  214. handler.addTag("orange");
  215. CPPUNIT_ASSERT_EQUAL(std::string("apple, orange"), handler.toTagString());
  216. CPPUNIT_ASSERT(handler.hasTag("apple"));
  217. CPPUNIT_ASSERT(handler.hasTag("orange"));
  218. CPPUNIT_ASSERT(!handler.hasTag("pineapple"));
  219. }
  220. void OptionHandlerTest::testFloatNumberOptionHandler()
  221. {
  222. FloatNumberOptionHandler handler("foo");
  223. CPPUNIT_ASSERT(handler.canHandle("foo"));
  224. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  225. Option option;
  226. handler.parse(option, "1.0");
  227. CPPUNIT_ASSERT_EQUAL(std::string("1.0"), option.get("foo"));
  228. CPPUNIT_ASSERT_EQUAL(std::string("*-*"),
  229. handler.createPossibleValuesString());
  230. }
  231. void OptionHandlerTest::testFloatNumberOptionHandler_min()
  232. {
  233. FloatNumberOptionHandler handler("foo", "", "", 0.0);
  234. Option option;
  235. handler.parse(option, "0.0");
  236. CPPUNIT_ASSERT_EQUAL(std::string("0.0"), option.get("foo"));
  237. try {
  238. handler.parse(option, "-0.1");
  239. CPPUNIT_FAIL("exception must be thrown.");
  240. } catch(Exception& e) {}
  241. CPPUNIT_ASSERT_EQUAL(std::string("0.0-*"),
  242. handler.createPossibleValuesString());
  243. }
  244. void OptionHandlerTest::testFloatNumberOptionHandler_max()
  245. {
  246. FloatNumberOptionHandler handler("foo", "", "", -1, 10.0);
  247. Option option;
  248. handler.parse(option, "10.0");
  249. CPPUNIT_ASSERT_EQUAL(std::string("10.0"), option.get("foo"));
  250. try {
  251. handler.parse(option, "10.1");
  252. CPPUNIT_FAIL("exception must be thrown.");
  253. } catch(Exception& e) {}
  254. CPPUNIT_ASSERT_EQUAL(std::string("*-10.0"),
  255. handler.createPossibleValuesString());
  256. }
  257. void OptionHandlerTest::testFloatNumberOptionHandler_min_max()
  258. {
  259. FloatNumberOptionHandler handler("foo", "", "", 0.0, 10.0);
  260. Option option;
  261. handler.parse(option, "0.0");
  262. CPPUNIT_ASSERT_EQUAL(std::string("0.0"), option.get("foo"));
  263. handler.parse(option, "10.0");
  264. CPPUNIT_ASSERT_EQUAL(std::string("10.0"), option.get("foo"));
  265. try {
  266. handler.parse(option, "-0.1");
  267. CPPUNIT_FAIL("exception must be thrown.");
  268. } catch(Exception& e) {}
  269. try {
  270. handler.parse(option, "10.1");
  271. CPPUNIT_FAIL("exception must be thrown.");
  272. } catch(Exception& e) {}
  273. CPPUNIT_ASSERT_EQUAL(std::string("0.0-10.0"),
  274. handler.createPossibleValuesString());
  275. }
  276. void OptionHandlerTest::testHttpProxyOptionHandler()
  277. {
  278. HttpProxyOptionHandler handler(PREF_HTTP_PROXY, "", "");
  279. CPPUNIT_ASSERT(handler.canHandle(PREF_HTTP_PROXY));
  280. CPPUNIT_ASSERT(!handler.canHandle("foobar"));
  281. Option option;
  282. handler.parse(option, "proxy:65535");
  283. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:65535"),
  284. option.get(PREF_HTTP_PROXY));
  285. handler.parse(option, "http://proxy:8080");
  286. CPPUNIT_ASSERT_EQUAL(std::string("http://proxy:8080"),
  287. option.get(PREF_HTTP_PROXY));
  288. handler.parse(option, "");
  289. CPPUNIT_ASSERT(option.defined(PREF_HTTP_PROXY));
  290. CPPUNIT_ASSERT(option.blank(PREF_HTTP_PROXY));
  291. try {
  292. handler.parse(option, "http://bar:65536");
  293. CPPUNIT_FAIL("exception must be thrown.");
  294. } catch(Exception& e) {}
  295. CPPUNIT_ASSERT_EQUAL(std::string("[http://][USER:PASSWORD@]HOST[:PORT]"),
  296. handler.createPossibleValuesString());
  297. }
  298. } // namespace aria2