Skip to content

Commit

Permalink
Handle leading zeros in PIN when pairing
Browse files Browse the repository at this point in the history
If a PIN code started with one or more zeros, those zeros would be
dropped and the verifcation would fail. This commit fixes that issue.
  • Loading branch information
postlund committed Dec 6, 2018
1 parent 62756e5 commit 9bd2042
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyatv/dmap/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _verify_pin(self, received_code):

merged = StringIO()
merged.write(self.pairing_guid)
for char in str(self.pin_code):
for char in str(self.pin_code).zfill(4):
merged.write(char)
merged.write("\x00")

Expand Down
32 changes: 21 additions & 11 deletions tests/dmap/test_pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
PAIRING_GUID2 = '1234ABCDE56789FF'
PAIRING_CODE2 = '58AD1D195B6DAA58AA2EA29DC25B81C3'

# Code is padded with zeros
PIN_CODE3 = 1
PAIRING_GUID3 = '7D1324235F535AE7'
PAIRING_CODE3 = 'A34C3361C7D57D61CA41F62A8042F069'

# Pairing guid is 8 bytes, which is 64 bits
RANDOM_128_BITS = 6558272190156386627
RANDOM_PAIRING_GUID = '5B03A9CF4A983143'
Expand Down Expand Up @@ -74,6 +79,22 @@ async def test_succesful_pairing(self):
self.assertEqual(parser.first(parsed, 'cmpa', 'cmnm'), REMOTE_NAME)
self.assertEqual(parser.first(parsed, 'cmpa', 'cmty'), 'iPhone')

async def test_succesful_pairing_with_any_pin(self):
await self._start(pin_code=None)

url = self._pairing_url('invalid_pairing_code')
_, status = await utils.simple_get(url, self.loop)

self.assertEqual(status, 200)

async def test_succesful_pairing_with_pin_leadering_zeros(self):
await self._start(pin_code=PIN_CODE3, pairing_guid=PAIRING_GUID3)

url = self._pairing_url(PAIRING_CODE3)
_, status = await utils.simple_get(url, self.loop)

self.assertEqual(status, 200)

async def test_pair_custom_pairing_guid(self):
await self._start(pin_code=PIN_CODE2, pairing_guid=PAIRING_GUID2)

Expand All @@ -85,17 +106,6 @@ async def test_pair_custom_pairing_guid(self):
self.assertEqual(parser.first(parsed, 'cmpa', 'cmpg'),
int(PAIRING_GUID2, 16))

async def test_succesful_pairing_with_any_pin(self):
await self._start(pin_code=None)

url = self._pairing_url('invalid_pairing_code')
data, _ = await utils.simple_get(url, self.loop)

parsed = parser.parse(data, tag_definitions.lookup_tag)
self.assertEqual(parser.first(parsed, 'cmpa', 'cmpg'), 1)
self.assertEqual(parser.first(parsed, 'cmpa', 'cmnm'), REMOTE_NAME)
self.assertEqual(parser.first(parsed, 'cmpa', 'cmty'), 'iPhone')

async def test_failed_pairing(self):
await self._start()

Expand Down

0 comments on commit 9bd2042

Please sign in to comment.