-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_usage.py
316 lines (290 loc) · 9.63 KB
/
test_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Python Test Repo Template
# ..................................
# Copyright (c) 2017-2022, Kendrick Walls
# ..................................
# Licensed under MIT (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# ..........................................
# http://www.github.com/reactive-firewall/python-repo/LICENSE.md
# ..........................................
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
try:
import sys
if sys.__name__ is None: # pragma: no branch
raise ImportError("[CWE-440] OMG! we could not import sys! ABORT. ABORT.")
except Exception as err: # pragma: no branch
raise ImportError(err)
try:
try:
import context
except Exception as ImportErr: # pragma: no branch
ImportErr = None
del ImportErr
from . import context
if context.__name__ is None:
raise ImportError("[CWE-758] Failed to import context")
else:
from context import multicast
from context import unittest as unittest
from context import subprocess as subprocess
except Exception:
raise ImportError("[CWE-758] Failed to import test context")
class MulticastTestSuite(context.BasicUsageTestSuite):
"""Special Multicast Usage test cases."""
__module__ = """tests.test_usage"""
def test_multicast_insane_none(self):
"""Tests the imposible state for CLI tools given bad tools"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(JUNK) == error""")
try:
self.assertIsNone(multicast.__main__.useTool("NoSuchTool"))
self.assertIsNone(multicast.__main__.useTool(None))
theResult = True
except Exception as err:
context.debugtestError(err)
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, fail_fixture)
def test_multicast_message_arg_main(self):
"""Tests the message argument for expected syntax given simple args"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(SAY, message) == error""")
try:
with self.assertRaises(SystemExit):
self.assertIsNotNone(multicast.__main__.useTool("SAY", ["--message"]))
self.assertIsNotNone(multicast.__main__.useTool("SAY", ["--message", "test"]))
theResult = True
except Exception as err:
context.debugtestError(err)
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, fail_fixture)
def test_multicast_hear_invalid_arg_main(self):
"""Tests the message argument for failure given invalid input"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(HEAR, junk) != 2""")
try:
with self.assertRaises(SystemExit):
self.assertIsNotNone(multicast.__main__.useTool("HEAR", ["--port", "test"]))
self.assertNotEqual(multicast.__main__.useTool("HEAR", ["--port", "test"]), 0)
self.assertNotEqual(multicast.__main__.useTool("HEAR", ["--port", "test"]), 1)
theResult = True
except Exception as err:
context.debugtestError(err)
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, fail_fixture)
@unittest.expectedFailure
def test_multicast_hexdump_arg_main(self):
"""Tests the hexdump argument for failure given future tools"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(HEAR, hex) == error""")
try:
with self.assertRaises(NotImplementedError):
self.assertIsNotNone(multicast.__main__.useTool("HEAR", ["--hex"]))
theResult = True
except Exception as err:
context.debugtestError(err)
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, fail_fixture)
def test_multicast_invalid_main(self):
"""Tests the NOOP state for multicast given bad input"""
theResult = False
fail_fixture = str("""multicast.__main__.main(NOOP) == empty""")
try:
self.assertIsNone(multicast.__main__.main(["NOOP"]))
theResult = True
except Exception as err:
context.debugtestError(err)
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, fail_fixture)
def test_multicast_help_arg_main(self):
"""Tests the HELP argument for help usage"""
theResult = False
fail_fixture = str("""multicast.__main__.useTool(HELP, []) == error""")
try:
self.assertIsNone(multicast.__main__.useTool("HELP", []))
theResult = True
except Exception as err:
context.debugtestError(err)
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, fail_fixture)
def debugIfNoneResult(thepython, theArgs, theOutput):
"""In case you need it."""
try:
if (str(theOutput) is not None):
theResult = True
else:
theResult = False
context.debugUnexpectedOutput(theOutput, None, thepython)
except Exception:
theResult = False
return theResult
class BasicIntegrationTestSuite(context.BasicUsageTestSuite):
"""Basic functional test cases."""
def setUp(self):
super(self.__class__, self).setUp()
if (self._thepython is None):
self.skipTest(str("""No python cmd to test with!"""))
def test_run_lib_command_plain(self):
"""Test case for multicast.__main__ help."""
theResult = False
fail_fixture = str("""multicast.__main__(--help) == not helpful""")
try:
if (self._thepython is not None):
theOutputtext = context.checkPythonCommand([
str(self._thepython),
str("-m"),
str("multicast"),
str("--help")
], stderr=subprocess.STDOUT)
self.assertIn(str("usage:"), str(theOutputtext))
if (str("usage:") in str(theOutputtext)):
theResult = True
else:
theResult = False
context.debugUnexpectedOutput(str("usage:"), str(theOutputtext), self._thepython)
except Exception as err:
context.debugtestError(err)
err = None
del err
self.fail(fail_fixture)
theResult = False
self.assertTrue(theResult, str("""Could Not find usage from multicast --help"""))
def test_run_lib_command_main(self):
"""Test case for multicast vs multicast.__main__"""
theResult = False
try:
theExpectedText = context.checkPythonCommand([
str(self._thepython),
str("-m"),
str("multicast.__main__")
], stderr=subprocess.STDOUT)
self.assertIsNotNone(theExpectedText)
theOutputtext = context.checkPythonCommand([
str(self._thepython),
str("-m"),
str("multicast")
], stderr=subprocess.STDOUT)
self.assertIn(str(theExpectedText), str(theOutputtext))
if (str(theExpectedText) in str(theOutputtext)):
theResult = True
else:
theResult = False
context.debugUnexpectedOutput(str(theExpectedText), str(theOutputtext), self._thepython)
except BaseException as err:
context.debugtestError(err)
err = None
del err
theResult = False
self.assertTrue(theResult, str("""Could Not swap multicast for multicast.__main__"""))
def test_version_has_value_case(self):
"""Test for result from --version argument: python -m multicast.* --version """
theResult = False
if (self._thepython is not None):
try:
for test_case in [".__main__", ""]:
args = [
str(self._thepython),
str("-m"),
str("multicast{}").format(
str(
test_case
)
),
str("--version")
]
theOutputtext = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
# now test it
try:
if isinstance(theOutputtext, bytes):
theOutputtext = theOutputtext.decode('utf8')
except UnicodeDecodeError:
theOutputtext = str(repr(bytes(theOutputtext)))
# ADD REAL VERSION TEST HERE
theResult = debugIfNoneResult(self._thepython, args, theOutputtext)
# or simply:
self.assertIsNotNone(theOutputtext)
except Exception as err:
context.debugtestError(err)
err = None
del err
theResult = False
self.assertTrue(theResult, str("""Could Not find version from multicast --version"""))
def test_profile_template_case(self):
"""Test case template for profiling"""
theResult = False
if (self._thepython is not None):
try:
for test_case in ["NOOP"]:
args = [
str(self._thepython),
str("-m"),
str("multicast"),
str("{}").format(
str(
test_case
)
)
]
theOutputtext = context.timePythonCommand(args, stderr=subprocess.STDOUT)
# now test it
try:
if isinstance(theOutputtext, bytes):
theOutputtext = theOutputtext.decode('utf8')
except UnicodeDecodeError:
theOutputtext = str(repr(bytes(theOutputtext)))
# or simply:
self.assertIsNotNone(theOutputtext)
theResult = True
except Exception as err:
context.debugtestError(err)
err = None
del err
theResult = False
assert theResult
# @unittest.expectedFailure
def test_fail_message_works_case(self):
"""Test case template for profiling"""
theResult = False
if (self._thepython is not None):
try:
for test_case in ["BAdInPut"]:
args = [
str(self._thepython),
str("-m"),
str("multicast"),
str("{}").format(
str(
test_case
)
)
]
theOutputtext = context.checkPythonCommand(args, stderr=subprocess.STDOUT)
# now test it
try:
if isinstance(theOutputtext, bytes):
theOutputtext = theOutputtext.decode('utf8')
except UnicodeDecodeError:
theOutputtext = str(repr(bytes(theOutputtext)))
theResult = debugIfNoneResult(self._thepython, args, theOutputtext)
# or simply:
self.assertIsNotNone(theOutputtext)
except Exception as err:
context.debugtestError(err)
err = None
del err
theResult = False
assert theResult
if __name__ == '__main__':
unittest.main()