OptionHandlerTest.cc 9.8 KB

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