def testKnownValues(self): # random strings of lengths 0-19 data = [ ('', ''), ('\xf9', 'yF=='), ('*\xc9', '9gZ='), ('T\xe7`', 'KDSV'), ('\xd2\xe9H\x0c', 'oi_72-=='), ('K\x84\x03\xeb\xe8', 'HsF2uyV='), ('\xebl\xe5\xa3\xa3\xf8', 'uqn_cuEs'), ('\x04\x88yR\xef\xa1M', '07WtJiyWIF=='), ('h\x8c\xa2\xb8h\x8c\x19v', 'P7mXi5XB5MN='), ('\x06\xc7_M\x19$\x88v\xb4', '0gSUIGZZX6Po'), ('\x1d\xab\xefI\xf7\x7fY\xa4\r\xe8', '6PjjHUSzLPFCu-=='), ('\xa4=\xe6\x1b\x00\xb1\r\xba\xcc\xca\xf4', 'd2ra5k1l2QfBmjF='), ('\xd7\xac\xa8\x97\xc2\x14\x16)\xf5"\xc8d', 'pumc_w7J4Xbp7gWZ'), ('\xab\xb3%\xd3&I\xfd\x9cc\x91\x17\xd7\xdf', 'evB_omO8zOlYZGUMrk=='), ('O\x8dO\xf4\nd\xc4\xf5W]\xdf\xd3\xa9\xfe', 'IspEx-dZlEKMMSzIeUs='), ("\xca\xdc\x8d'\xf0\xc5a\x93b\x1c@4\xdaC\x9a", 'mhmC8z24NOCX63-oqZDP'), ('\xe1\x00\xf7\xd8p\xef\x08v\xca\x9b\x81INPvu', 'sF2rq62j16Q9as48I_0qSF=='), ('8_\xaeS\xb9\xa9\xb7\x1e\x99\x8c\x06\xc7\xa9\xa2F\xb5\x0f', 'D4yiJvadhluOY-Q6eP85hFw='), ('"\xc3)!J H\x98\ro\xe1\\\xc2a\xc9\xe2v\xe8', '7gBd7JcVH8VCQy4Rka68sbQc'), ('\xe3\xb1?_\x97a<\xc5\xf5Cj\x86\xbeB\xc3F\xcc\x1ai', 'sv3zMtSWEBMpFqe5jZA2GgkPPF=='), ] for s, expected in data: try: self.assertEqual(base64hex.B64HexEncode(s), expected) self.assertEqual(base64hex.B64HexDecode(expected), s) self.assertEqual(base64hex.B64HexEncode(s, padding=False), expected.rstrip('=')) self.assertEqual(base64hex.B64HexDecode(expected.rstrip('='), padding=False), s) except: logging.info("failed on %r", (s, expected)) raise
def testEncodeDecode(self): num_trials = 1000 for i in xrange(num_trials): s = self._RandomString() enc = base64hex.B64HexEncode(s) dec = base64hex.B64HexDecode(enc) self.assertEqual(s, dec)
def UnpackLocation(value): """Converts from a packed, base64-hex-encoded representation of latitude, longitude and accuracy into a Location namedtuple. """ packed = base64hex.B64HexDecode(value) latitude, longitude, accuracy = struct.unpack('>ddd', packed) return Location(latitude=latitude, longitude=longitude, accuracy=accuracy)
def UnpackFreight(self, col, posting): # TODO(spencer): the rstrip() below is necessary as data in the # index has already been encoded with a bug in the base64 padding # We need to rebuild the index before reverting this. posting = base64hex.B64HexDecode(posting.rstrip('='), padding=False) assert not (len(posting) % 2), repr(posting) return [struct.unpack('>H', posting[i:i+2])[0] for i in xrange(0, len(posting), 2)]
def UnpackSortKeyPrefix(prefix): """Returns the timestamp in the provided sort key prefix. The timestamp may be reversed, if reverse=True was specified when the sort key prefix was created. """ timestamp, random_bits = struct.unpack('>IH', base64hex.B64HexDecode(prefix)) return timestamp
def UnpackPlacemark(value): """Converts from a comma-separated, base64-hex-encoded representation of hierarchical place names into 'Placemark' named tuple with an utf-8 encoded place names. """ pm_values = [] for x in value.split(','): try: # TODO(spencer): the rstrip() below is necessary as data in the # index has already been encoded with a bug in the base64 padding # We need to rebuild the index before reverting this. decoded_x = base64hex.B64HexDecode(x.rstrip('='), padding=False).decode('utf-8') except: decoded_x = '' pm_values.append(decoded_x) return Placemark(*pm_values)
def DeconstructAssetId(id_prefix, asset_id): """Deconstructs an asset id that was previously constructed according to the rules of "ConstructAssetId" (i.e. no timestamp). Returns a tuple: (device_id, uniquifier) """ assert IdPrefix.IsValid(id_prefix), id_prefix assert asset_id[0] == id_prefix, asset_id # Decode the bytes, which must be base-64 hex encoded. byte_str = base64hex.B64HexDecode(asset_id[1:], padding=False) # Decode the device_id and the uniquifier. device_id, num_bytes = util.DecodeVarLengthNumber(byte_str) uniquifier = _DecodeUniquifier(byte_str[num_bytes:]) # Return all parts as a tuple. return device_id, uniquifier
def DeconstructTimestampAssetId(id_prefix, asset_id, reverse_ts=True): """Deconstructs an asset id that was previously constructed according to the rules of "ConstructTimestampAssetId" (i.e. includes timestamp). Returns a tuple: (timestamp, device_id, uniquifier) """ assert IdPrefix.IsValid(id_prefix), id_prefix assert asset_id[0] == id_prefix, asset_id # Decode the bytes, which must be base-64 hex encoded. byte_str = base64hex.B64HexDecode(asset_id[1:], padding=False) # Decode the 4-byte timestamp and reverse it if requested. timestamp, = struct.unpack('>I', byte_str[:4]) if reverse_ts: timestamp = (1L << 32) - timestamp - 1 # Decode the device_id and the uniquifier. device_id, num_bytes = util.DecodeVarLengthNumber(byte_str[4:]) uniquifier = _DecodeUniquifier(byte_str[4 + num_bytes:]) # Return all parts as a tuple. return timestamp, device_id, uniquifier