Commit 36c8ca99 authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

ukm: Encode string before hashing

In Python 3 only bytes objects can be hashed. This means we need to encode() beforehand.
Python 2 doesn't care, so this way it works on both.

Traceback (most recent call last):
  File "../../tools/metrics/ukm/gen_builders.py", line 35, in <module>
    sys.exit(main(sys.argv))
  File "../../tools/metrics/ukm/gen_builders.py", line 29, in main
    builders_template.WriteFiles(args.output, relpath, data)
  File "C:\Google\chromium\src\tools\metrics\ukm\builders_template.py", line 93, in WriteFiles
    HEADER.WriteFile(outdir, relpath, data)
  File "C:\Google\chromium\src\tools\metrics\ukm\codegen.py", line 89, in WriteFile
    output.write(self._StampFileCode(relpath, data))
  File "C:\Google\chromium\src\tools\metrics\ukm\codegen.py", line 73, in _StampFileCode
    event_code = "".join(
  File "C:\Google\chromium\src\tools\metrics\ukm\codegen.py", line 74, in <genexpr>
    self._StampEventCode(file_info, event)
  File "C:\Google\chromium\src\tools\metrics\ukm\codegen.py", line 62, in _StampEventCode
    event_info = EventInfo(event)
  File "C:\Google\chromium\src\tools\metrics\ukm\codegen.py", line 36, in __init__
    self.hash = HashName(json_obj['name'])
  File "C:\Google\chromium\src\tools\metrics\ukm\codegen.py", line 23, in HashName
    return struct.unpack('>Q', hashlib.md5(name).digest()[:8])[0]
TypeError: Unicode-objects must be encoded before hashing

Bug: 941669
Change-Id: I063ed268a8326b5acedbddc2628af7b015edb7cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2082551Reviewed-by: default avatarBrian White <bcwhite@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
Auto-Submit: Raul Tambre <raul@tambre.ee>
Cr-Commit-Position: refs/heads/master@{#756295}
parent 9459f14e
......@@ -16,6 +16,7 @@ namespace base {
// Make sure our ID hashes are the same as what we see on the server side.
TEST(MetricsUtilTest, HashMetricName) {
// The cases must match those in //tools/metrics/ukm/codegen_test.py.
static const struct {
std::string input;
std::string output;
......
......@@ -104,6 +104,7 @@ group("metrics_python_tests") {
"//tools/metrics/ukm/ukm.xml",
"//tools/metrics/ukm/codegen.py",
"//tools/metrics/ukm/codegen_test.py",
"//tools/metrics/ukm/builders_template.py",
"//tools/metrics/ukm/decode_template.py",
"//tools/metrics/ukm/gen_builders.py",
......
......@@ -24,6 +24,7 @@ sys.exit(typ.main(tests=resolve(
'histograms/generate_expired_histograms_array_unittest.py',
'histograms/pretty_print_test.py',
'rappor/rappor_model_test.py',
'ukm/codegen_test.py',
'ukm/gen_builders_test.py',
'ukm/ukm_model_test.py',
'ukm/xml_validations_test.py',
......
......@@ -18,9 +18,9 @@ def sanitize_name(name):
def HashName(name):
# This must match the hash function in base/metrics/metric_hashes.cc
# This must match the hash function in //base/metrics/metrics_hashes.cc.
# >Q: 8 bytes, big endian.
return struct.unpack('>Q', hashlib.md5(name).digest()[:8])[0]
return struct.unpack('>Q', hashlib.md5(name.encode()).digest()[:8])[0]
class FileInfo(object):
......
#!/usr/bin/env python
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import codegen
class CodegenTest(unittest.TestCase):
def testHash(self):
# Must match those in //base/metrics/metrics_hashes_unittest.cc.
self.assertEqual(codegen.HashName('Back'), 0x0557fa923dcee4d0)
self.assertEqual(codegen.HashName('Forward'), 0x67d2f6740a8eaebf)
self.assertEqual(codegen.HashName('NewTab'), 0x290eb683f96572f1)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment