Exemplo n.º 1
0
    def test_new_request_key_end(self):
        def getrandbits(i):
            self.assertEqual(i, 16)
            return 0x7766

        self.mock(random, 'getrandbits', getrandbits)
        days_until_end_of_the_world = 2**43 / 24. / 60. / 60. / 1000.
        num_days = int(days_until_end_of_the_world)
        # Remove 1ms to not overflow.
        num_seconds = (
            (days_until_end_of_the_world - num_days) * 24. * 60. * 60. - 0.001)
        self.assertEqual(101806, num_days)
        self.assertEqual(278, int(num_days / 365.3))
        now = (task_request._BEGINING_OF_THE_WORLD +
               datetime.timedelta(days=num_days, seconds=num_seconds))
        self.mock_now(now)
        key = task_request._new_request_key()
        # Remove the XOR.
        key_id = key.integer_id() ^ task_pack.TASK_REQUEST_KEY_ID_MASK
        #   7ffffffffff 7766 1
        #     ^          ^   ^
        #     |          |   |
        #  since 2010    | schema version
        #                |
        #               rand
        self.assertEqual('0x7ffffffffff77661', '0x%016x' % key_id)
Exemplo n.º 2
0
 def test_new_request_key_end(self):
   def getrandbits(i):
     self.assertEqual(i, 16)
     return 0x7766
   self.mock(random, 'getrandbits', getrandbits)
   days_until_end_of_the_world = 2**43 / 24. / 60. / 60. / 1000.
   num_days = int(days_until_end_of_the_world)
   # Remove 1ms to not overflow.
   num_seconds = (
       (days_until_end_of_the_world - num_days) * 24. * 60. * 60. - 0.001)
   self.assertEqual(101806, num_days)
   self.assertEqual(278, int(num_days / 365.3))
   now = (task_request._BEGINING_OF_THE_WORLD +
       datetime.timedelta(days=num_days, seconds=num_seconds))
   self.mock_now(now)
   key = task_request._new_request_key()
   # Remove the XOR.
   key_id = key.integer_id() ^ task_pack.TASK_REQUEST_KEY_ID_MASK
   #   7ffffffffff 7766 1
   #     ^          ^   ^
   #     |          |   |
   #  since 2010    | schema version
   #                |
   #               rand
   self.assertEqual('0x7ffffffffff77661', '0x%016x' % key_id)
Exemplo n.º 3
0
 def test_new_request_key(self):
     for _ in xrange(3):
         delta = utils.utcnow() - task_request._BEGINING_OF_THE_WORLD
         now = int(round(delta.total_seconds() * 1000.))
         key = task_request._new_request_key()
         # Remove the XOR.
         key_id = key.integer_id() ^ task_pack.TASK_REQUEST_KEY_ID_MASK
         timestamp = key_id >> 20
         randomness = (key_id >> 4) & 0xFFFF
         version = key_id & 0xF
         self.assertLess(abs(timestamp - now), 1000)
         self.assertEqual(1, version)
         if randomness:
             break
     else:
         self.fail('Failed to find randomness')
Exemplo n.º 4
0
 def test_new_request_key_zero(self):
   def getrandbits(i):
     self.assertEqual(i, 16)
     return 0x7766
   self.mock(random, 'getrandbits', getrandbits)
   self.mock_now(task_request._BEGINING_OF_THE_WORLD)
   key = task_request._new_request_key()
   # Remove the XOR.
   key_id = key.integer_id() ^ task_pack.TASK_REQUEST_KEY_ID_MASK
   #   00000000000 7766 1
   #     ^          ^   ^
   #     |          |   |
   #  since 2010    | schema version
   #                |
   #               rand
   self.assertEqual('0x0000000000077661', '0x%016x' % key_id)
Exemplo n.º 5
0
 def test_new_request_key(self):
   for _ in xrange(3):
     delta = utils.utcnow() - task_request._BEGINING_OF_THE_WORLD
     now = int(round(delta.total_seconds() * 1000.))
     key = task_request._new_request_key()
     # Remove the XOR.
     key_id = key.integer_id() ^ task_pack.TASK_REQUEST_KEY_ID_MASK
     timestamp = key_id >> 20
     randomness = (key_id >> 4) & 0xFFFF
     version = key_id & 0xF
     self.assertLess(abs(timestamp - now), 1000)
     self.assertEqual(1, version)
     if randomness:
       break
   else:
     self.fail('Failed to find randomness')
Exemplo n.º 6
0
    def test_new_request_key_zero(self):
        def getrandbits(i):
            self.assertEqual(i, 16)
            return 0x7766

        self.mock(random, 'getrandbits', getrandbits)
        self.mock_now(task_request._BEGINING_OF_THE_WORLD)
        key = task_request._new_request_key()
        # Remove the XOR.
        key_id = key.integer_id() ^ task_pack.TASK_REQUEST_KEY_ID_MASK
        #   00000000000 7766 1
        #     ^          ^   ^
        #     |          |   |
        #  since 2010    | schema version
        #                |
        #               rand
        self.assertEqual('0x0000000000077661', '0x%016x' % key_id)