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
|
import time
from nose.tools import *
import voltron
from voltron.core import *
from voltron.api import *
from voltron.plugin import *
log = logging.getLogger('tests')
class APITestRequest(APIRequest):
_fields = {'target_id': False, 'address': False, 'count': True}
_types = {'target_id': int, 'address': int, 'count': int}
target_id = 0
address = None
count = None
class APITestResponse(APISuccessResponse):
_fields = {'disassembly': True}
class APITestPlugin(APIPlugin):
request = 'test'
request_class = APITestRequest
response_class = APITestResponse
def setup():
voltron.setup_env()
def teardown():
time.sleep(2)
def test_parent_message_validation_fail():
msg = APIMessage()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert exception
def test_parent_request_validation_fail():
msg = APIRequest()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert exception
def test_parent_request_type():
msg = APIRequest()
assert msg.type == 'request'
def test_parent_request_request():
msg = APIRequest()
assert msg.request is None
def test_parent_response_validation_fail():
msg = APIResponse()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert exception
def test_parent_response_type():
msg = APIResponse()
assert msg.type == 'response'
def test_parent_response_status():
msg = APIResponse()
assert msg.status is None
def test_success_response_validation_succeed():
msg = APISuccessResponse()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert not exception
def test_success_response_type():
msg = APISuccessResponse()
assert msg.type == 'response'
def test_success_response_status():
msg = APISuccessResponse()
assert msg.status == 'success'
def test_error_response_validation_fail():
msg = APIErrorResponse()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert exception
def test_error_response_type():
msg = APIErrorResponse()
assert msg.type == 'response'
def test_error_response_status():
msg = APIErrorResponse()
assert msg.status == 'error'
def test_invalid_request_error_response_validation_succeed():
msg = APIInvalidRequestErrorResponse()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert not exception
def test_invalid_request_error_response_type():
msg = APIInvalidRequestErrorResponse()
assert msg.type == 'response'
def test_invalid_request_error_response_status():
msg = APIInvalidRequestErrorResponse()
assert msg.status == 'error'
def test_test_request_validation_fail():
msg = APITestRequest()
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert exception
def test_test_request_validation_fail_with_param():
msg = APITestRequest(target_id=0)
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert exception
def test_test_request_validation_succeed_with_param():
msg = api_request('test', count=16)
exception = False
try:
msg.validate()
except MissingFieldError:
exception = True
assert not exception
assert msg.count == 16
def test_test_request_validation_succeed_with_data():
msg = APITestRequest('{"data":{"count":16}}')
exception = False
try:
msg.validate()
except MissingFieldError as e:
exception = True
assert not exception
assert msg.count == 16
def test_test_request_validation_succeed_by_assign():
msg = APITestRequest()
msg.count = 16
exception = False
try:
msg.validate()
except MissingFieldError as e:
exception = True
assert not exception
assert msg.count == 16
def test_test_request_string():
msg = APITestRequest(count=16)
assert json.loads(str(msg)) == {"request": "test", "type": "request", "block": False, "timeout": 10,
"data": {"count": 16, "target_id": 0, "address": None}}
def test_test_response_validation_fail():
msg = APITestResponse()
exception = False
try:
msg.validate()
except MissingFieldError as e:
exception = True
assert exception
def test_test_response_validation_fail_with_param():
msg = APITestResponse(thing=1)
exception = False
try:
msg.validate()
except MissingFieldError as e:
exception = True
assert exception
def test_test_response_validation_succeed_with_param():
msg = APITestResponse(disassembly="xxx")
exception = False
try:
msg.validate()
except MissingFieldError as e:
exception = True
assert not exception
def test_test_response_validation_succeed_with_data():
msg = APITestResponse('{"data":{"disassembly":"xxx"}}')
exception = False
try:
msg.validate()
except MissingFieldError as e:
exception = True
assert not exception
def test_test_response_validation_succeed_by_assign():
msg = APITestResponse()
msg.disassembly = "xxx"
exception = False
try:
msg.validate()
except MissingFieldError as e:
print(str(e))
exception = True
assert not exception
def test_test_response_string():
msg = APITestResponse(disassembly='xxx')
assert json.loads(str(msg)) == {"status": "success", "type": "response", "data": {"disassembly": "xxx"}}
class APIEncodeMsg(APIMessage):
_fields = {'enc': False}
_encode_fields = ['enc']
def test_encode_fields():
msg = APIEncodeMsg()
msg.enc = six.b('').join([six.int2byte(x) for x in range(0x0, 0xff)])
assert msg.to_dict()['data']['enc'] == six.text_type('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+')
assert msg.to_json() == '{"data": {"enc": "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"}, "type": null}'
msg2 = APIEncodeMsg(data=msg.to_json())
assert msg.to_dict() == msg2.to_dict()
assert msg.to_json() == msg2.to_json()
assert msg2.enc == msg.enc
msg3 = APIEncodeMsg()
msg3.enc = six.u('xxxx')
assert msg3.to_dict() == {'data': {'enc': 'eHh4eA=='}, 'type': None}
msg3.enc = six.b('xxxx')
assert msg3.to_dict() == {'data': {'enc': 'eHh4eA=='}, 'type': None}
msg4 = APIEncodeMsg()
msg4.from_dict(msg.to_dict())
assert msg4.to_dict() == msg.to_dict()
|