Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Unit tests for pyatv.dmap.parser."""
import unittest
import plistlib
from pyatv import exceptions
from pyatv.dmap import (tags, parser)
TEST_TAGS = {
'uuu8': parser.DmapTag(tags.read_uint, 'uint8'),
'uu16': parser.DmapTag(tags.read_uint, 'uint16'),
'uu32': parser.DmapTag(tags.read_uint, 'uint32'),
'uu64': parser.DmapTag(tags.read_uint, 'uint64'),
'bola': parser.DmapTag(tags.read_bool, 'bool'),
'bolb': parser.DmapTag(tags.read_bool, 'bool'),
'stra': parser.DmapTag(tags.read_str, 'string'),
'strb': parser.DmapTag(tags.read_str, 'string'),
'cona': parser.DmapTag('container', 'container'),
'conb': parser.DmapTag('container', 'container 2'),
'igno': parser.DmapTag(tags.read_ignore, 'ignore'),
'plst': parser.DmapTag(tags.read_bplist, 'bplist'),
}
def lookup_tag(name):
return TEST_TAGS[name]
def test_parse_uint_of_various_lengths(self):
in_data = tags.uint8_tag('uuu8', 12) + \
tags.uint16_tag('uu16', 37888) + \
tags.uint32_tag('uu32', 305419896) + \
tags.uint64_tag('uu64', 8982983289232)
parsed = parser.parse(in_data, lookup_tag)
self.assertEqual(4, len(parsed))
self.assertEqual(12, parser.first(parsed, 'uuu8'))
self.assertEqual(37888, parser.first(parsed, 'uu16'))
self.assertEqual(305419896, parser.first(parsed, 'uu32'))
self.assertEqual(8982983289232, parser.first(parsed, 'uu64'))
def test_parse_value_in_container(self):
in_data = tags.container_tag('cona',
tags.uint8_tag('uuu8', 36) +
tags.uint16_tag('uu16', 13000))
parsed = parser.parse(in_data, lookup_tag)
self.assertEqual(1, len(parsed))
inner = parser.first(parsed, 'cona')
self.assertEqual(2, len(inner))
self.assertEqual(36, parser.first(inner, 'uuu8'))
self.assertEqual(13000, parser.first(inner, 'uu16'))
def test_parse_strings(self):
in_data = tags.string_tag('stra', '') + \
tags.string_tag('strb', 'test string')
parsed = parser.parse(in_data, lookup_tag)
self.assertEqual(2, len(parsed))
self.assertEqual('', parser.first(parsed, 'stra'))
self.assertEqual('test string', parser.first(parsed, 'strb'))
def test_parse_bool(self):
in_data = tags.bool_tag('bola', True) + \
tags.bool_tag('bolb', False)
parsed = parser.parse(in_data, lookup_tag)
self.assertEqual(2, len(parsed))
self.assertTrue(parser.first(parsed, 'bola'))
self.assertFalse(parser.first(parsed, 'bolb'))
def _convert_button(self, data):
value = parser.first(data, 'cmbe')
# Consider navigation buttons if six commands have been received
if self.buttons_press_count == 6:
if value == 'touchUp&time=6&point=20,250':
return 'up'
elif value == 'touchUp&time=6&point=20,275':
return 'down'
elif value == 'touchUp&time=7&point=50,100':
return 'left'
elif value == 'touchUp&time=7&point=75,100':
return 'right'
return value
def perform_pairing(self, pairing_response, port):
"""Pair with a remote client.
This will perform a GET-request to the specified port and hand over
information to the client (pyatv) so that the pairing process can be
completed.
"""
server = 'http://127.0.0.1:{}'.format(port)
url = '{}/pairing?pairingcode={}&servicename=test'.format(
server, pairing_response.pairing_code)
data, _ = yield from utils.simple_get(url, self.loop)
# Verify content returned in pairingresponse
parsed = parser.parse(data, tag_definitions.lookup_tag)
self.tc.assertEqual(parser.first(parsed, 'cmpa', 'cmpg'), 1)
self.tc.assertEqual(parser.first(parsed, 'cmpa', 'cmnm'),
pairing_response.remote_name)
self.tc.assertEqual(parser.first(parsed, 'cmpa', 'cmty'), 'ipod')
def test_extract_simplified_container(self):
elem = tags.uint8_tag('uuu8', 12)
inner = tags.container_tag('conb', elem)
in_data = tags.container_tag('cona', inner)
parsed = parser.parse(in_data, lookup_tag)
self.assertEqual(12, parser.first(parsed, 'cona', 'conb', 'uuu8'))
def perform_pairing(self, pairing_response, port):
"""Pair with a remote client.
This will perform a GET-request to the specified port and hand over
information to the client (pyatv) so that the pairing process can be
completed.
"""
server = 'http://127.0.0.1:{}'.format(port)
url = '{}/pairing?pairingcode={}&servicename=test'.format(
server, pairing_response.pairing_code)
data, _ = yield from utils.simple_get(url, self.loop)
# Verify content returned in pairingresponse
parsed = parser.parse(data, tag_definitions.lookup_tag)
self.tc.assertEqual(parser.first(parsed, 'cmpa', 'cmpg'), 1)
self.tc.assertEqual(parser.first(parsed, 'cmpa', 'cmnm'),
pairing_response.remote_name)
self.tc.assertEqual(parser.first(parsed, 'cmpa', 'cmty'), 'ipod')
def setUp(self):
self.config = conf.AppleTV(ADDRESS_1, NAME)
self.service_mock = MagicMock()
self.service_mock.protocol = const.Protocol.DMAP
self.service_mock.port = PORT_1
self.service_mock.identifier = IDENTIFIER_1
self.service_mock.credentials = None
self.service_mock2 = MagicMock()
self.service_mock2.protocol = const.Protocol.MRP
self.service_mock2.port = PORT_2
self.service_mock2.identifier = IDENTIFIER_2
self.airplay_mock = MagicMock()
self.airplay_mock.port = PORT_1
self.airplay_mock.protocol = Protocol.AirPlay