/neatx/test/python/neatx.protocol_test.py

http://neatx.googlecode.com/ · Python · 102 lines · 59 code · 22 blank · 21 comment · 1 complexity · 814329ea6838f596ba263907beab7277 MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2009 Google Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. # 02110-1301, USA.
  19. """Script for unittesting the protocol module"""
  20. import unittest
  21. from neatx import constants
  22. from neatx import errors
  23. from neatx import protocol
  24. from neatx import utils
  25. import mocks
  26. class TestParseParameters(unittest.TestCase):
  27. """Tests for ParseParameters"""
  28. def setUp(self):
  29. self._fake_logging = mocks.FakeLogging()
  30. def _DoTest(self, params, expected):
  31. result = protocol.ParseParameters(params, _logging=self._fake_logging)
  32. self.failUnlessEqual(result, expected)
  33. def _DoFailTest(self, params):
  34. self.failUnlessRaises(protocol.NxParameterParsingError,
  35. protocol.ParseParameters, params,
  36. _logging=self._fake_logging)
  37. def test(self):
  38. self._DoTest("", [])
  39. self._DoTest(" ", [])
  40. self._DoTest("\t", [])
  41. self._DoTest("--session=\"\"", [("session", "")])
  42. self._DoTest("--session=\"\" --name=\"\"", [("session", ""), ("name", "")])
  43. self._DoTest("--session=\"123\"", [("session", "123")])
  44. self._DoTest(" --session=\"123\"", [("session", "123")])
  45. self._DoTest("--session=\"123\" ", [("session", "123")])
  46. self._DoTest(" --session=\"123\" ", [("session", "123")])
  47. self._DoTest("--session=\"123\" --name=\"dummy\"",
  48. [("session", "123"), ("name", "dummy")])
  49. self._DoTest(" --session=\"123\" --name=\"dummy\" ",
  50. [("session", "123"), ("name", "dummy")])
  51. self._DoTest("\t--session=\"123\"\t--name=\"dummy\"\n",
  52. [("session", "123"), ("name", "dummy")])
  53. self._DoTest("--session=\" value with spaces \" --name=\" a b\tc \"\n",
  54. [("session", " value with spaces "), ("name", " a b\tc ")])
  55. self._DoTest("--name=\"x\" --name=\"y\"", [("name", "x"), ("name", "y")])
  56. self._DoFailTest(",")
  57. self._DoFailTest("-")
  58. self._DoFailTest("--x")
  59. self._DoFailTest("--xyz=")
  60. self._DoFailTest("--xyz=\"")
  61. self._DoFailTest("--xyz=\"\"\"")
  62. self._DoFailTest("--xyz=\"\"a")
  63. self._DoFailTest("--xyz=\"\" --name")
  64. self._DoFailTest("--xyz=\"\" --name=\"")
  65. self._DoFailTest("--xyz=\"\",--name=\"\"")
  66. self._DoFailTest("--xyz=\"\", --name=\"\"")
  67. self._DoFailTest("--xyz=\"\" , --name=\"\"")
  68. class TestUnquoteParameterValue(unittest.TestCase):
  69. """Tests for UnquoteParameterValue"""
  70. def _DoTest(self, value, expected):
  71. unquoted = protocol.UnquoteParameterValue(value)
  72. self.failUnlessEqual(unquoted, expected)
  73. def test(self):
  74. self._DoTest("abc", "abc")
  75. self._DoTest("x%3D%22abc%20+%20%22%3B%3A%3B%20/usr/bin/xterm",
  76. """x="abc + ";:; /usr/bin/xterm""")
  77. self._DoTest("%26%20-%20_%20/%20+%20%27%20%22%20(%20)%20%5B%20%5D%20%7C",
  78. """& - _ / + ' " ( ) [ ] |""")
  79. if __name__ == '__main__':
  80. unittest.main()