Exemple #1
0
def test_parse_binary_plist():
    data = {"key": "value"}
    in_data = tags.raw_tag("plst", plistlib.dumps(data,
                                                  fmt=plistlib.FMT_BINARY))
    parsed = parser.parse(in_data, lookup_tag)
    assert 1 == len(parsed)
    assert data, parser.first(parsed, "plst")
Exemple #2
0
    async def _do(self, action, retry=True, is_login=False, is_daap=True):
        resp, status = await action()
        if is_daap:
            resp = parser.parse(resp, lookup_tag)

        self._log_response(action.log_text, resp, is_daap)
        if 200 <= status < 300:
            return resp

        # Seems to be the case?
        if status == 500:
            raise exceptions.NotSupportedError(
                "command not supported at this stage")

        if not is_login:
            # If a request fails, try to login again before retrying
            _LOGGER.info("implicitly logged out, logging in again")
            await self.login()

        # Retry once if we got a bad response, otherwise bail out
        if retry:
            return await self._do(action,
                                  False,
                                  is_login=is_login,
                                  is_daap=is_daap)

        raise exceptions.AuthenticationError(f"failed to login: {status}")
Exemple #3
0
def test_parse_strings():
    in_data = tags.string_tag("stra", "") + tags.string_tag(
        "strb", "test string")
    parsed = parser.parse(in_data, lookup_tag)
    assert 2 == len(parsed)
    assert "" == parser.first(parsed, "stra")
    assert "test string" == parser.first(parsed, "strb")
Exemple #4
0
 async def handle_remote_button(self, request):
     """Handle remote control buttons."""
     self._verify_auth_parameters(request)
     content = await request.content.read()
     parsed = parser.parse(content, tag_definitions.lookup_tag)
     self.state.last_button_pressed = self._convert_button(parsed)
     self.state.last_button_action = InputAction.SingleTap
     self.state.buttons_press_count += 1
     return web.Response(status=200)
Exemple #5
0
def test_simple_pprint():
    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)
    assert (parser.pprint(parsed,
                          lookup_tag) == "cona: [container, container]\n"
            "  conb: [container, container 2]\n"
            "    uuu8: 12 [uint, uint8]\n")
Exemple #6
0
def test_parse_value_in_container():
    in_data = tags.container_tag(
        "cona",
        tags.uint8_tag("uuu8", 36) + tags.uint16_tag("uu16", 13000))
    parsed = parser.parse(in_data, lookup_tag)
    assert 1 == len(parsed)
    inner = parser.first(parsed, "cona")
    assert 2 == len(inner)
    assert 36 == parser.first(inner, "uuu8")
    assert 13000 == parser.first(inner, "uu16")
Exemple #7
0
def test_parse_uint_of_various_lengths():
    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)
    assert 4 == len(parsed)
    assert 12 == parser.first(parsed, "uuu8")
    assert 37888 == parser.first(parsed, "uu16")
    assert 305419896 == parser.first(parsed, "uu32")
    assert 8982983289232 == parser.first(parsed, "uu64")
Exemple #8
0
async def test_pair_custom_pairing_guid(mock_pairing):
    pairing, zeroconf, service = await mock_pairing(pin_code=PIN_CODE2,
                                                    pairing_guid=PAIRING_GUID2)

    url = pairing_url(zeroconf, PAIRING_CODE2)
    data, _ = await utils.simple_get(url)

    await pairing.finish()

    # Verify content returned in pairingresponse
    parsed = parser.parse(data, tag_definitions.lookup_tag)
    assert parser.first(parsed, "cmpa", "cmpg") == int(PAIRING_GUID2, 16)

    assert service.credentials == PAIRING_GUID2
Exemple #9
0
async def test_succesful_pairing(mock_pairing):
    pairing, zeroconf, service = await mock_pairing()

    url = pairing_url(zeroconf, PAIRING_CODE)
    data, _ = await utils.simple_get(url)

    await pairing.finish()

    # Verify content returned in pairingresponse
    parsed = parser.parse(data, tag_definitions.lookup_tag)
    assert parser.first(parsed, "cmpa", "cmpg") == 1
    assert parser.first(parsed, "cmpa", "cmnm") == REMOTE_NAME
    assert parser.first(parsed, "cmpa", "cmty") == "iPhone"

    assert service.credentials == PAIRING_GUID
Exemple #10
0
    async def perform_pairing(self, remote_name, expected_code, 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 = f"http://127.0.0.1:{port}"
        url = f"{server}/pair?pairingcode={expected_code}&servicename=test"
        data, _ = await utils.simple_get(url)

        # Verify content returned in pairingresponse
        parsed = parser.parse(data, tag_definitions.lookup_tag)
        assert parser.first(parsed, "cmpa", "cmpg") == 1
        assert parser.first(parsed, "cmpa", "cmnm") == remote_name
        assert parser.first(parsed, "cmpa", "cmty") == "iPhone"
Exemple #11
0
 def handle_set_parameter(self,
                          request: HttpRequest) -> Optional[HttpResponse]:
     """Handle incoming SET_PARAMETER request."""
     _LOGGER.debug("Received SET_PARAMETER: %s", request)
     if request.headers["Content-Type"] == "application/x-dmap-tagged":
         tags = parser.parse(request.body, lookup_tag)
         self.state.metadata.title = parser.first(tags, "mlit", "minm")
         self.state.metadata.artist = parser.first(tags, "mlit", "asar")
         self.state.metadata.album = parser.first(tags, "mlit", "asal")
     elif request.body.startswith("volume:"):
         self.state.volume = float(request.body.split(" ", maxsplit=1)[1])
         _LOGGER.debug("Changing volume to %f", self.state.volume)
     else:
         return HttpResponse(
             "RTSP",
             "1.0",
             501,
             "Not implemented",
             {"CSeq": request.headers["CSeq"]},
             b"",
         )
     return HttpResponse("RTSP", "1.0", 200, "OK",
                         {"CSeq": request.headers["CSeq"]}, b"")
Exemple #12
0
def test_ignore_value():
    elem = tags.uint8_tag("igno", 44)
    parsed = parser.parse(elem, lookup_tag)
    assert parser.first(parsed, "igno") is None
Exemple #13
0
def test_extract_simplified_container():
    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)
    assert 12 == parser.first(parsed, "cona", "conb", "uuu8")
Exemple #14
0
def test_parse_bytes():
    in_data = tags.raw_tag("byte", b"\x01\xAA\xFF\x45")
    parsed = parser.parse(in_data, lookup_tag)
    assert 1 == len(parsed)
    assert "0x01aaff45" == parser.first(parsed, "byte")
Exemple #15
0
def test_parse_bool():
    in_data = tags.bool_tag("bola", True) + tags.bool_tag("bolb", False)
    parsed = parser.parse(in_data, lookup_tag)
    assert 2 == len(parsed)
    assert parser.first(parsed, "bola")
    assert not parser.first(parsed, "bolb")
Exemple #16
0
def test_empty_data():
    assert parser.parse(b"", lookup_tag) == []