• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from mojom_bindings_generator import MakeImportStackMessage
8from mojom_bindings_generator import ScrambleMethodOrdinals
9
10
11class FakeIface(object):
12  def __init__( self ):
13    self.name = None
14    self.methods = None
15
16
17class FakeMethod(object):
18  def __init__( self ):
19    self.ordinal = None
20    self.ordinal_comment = None
21
22
23class MojoBindingsGeneratorTest(unittest.TestCase):
24  """Tests mojo_bindings_generator."""
25
26  def testMakeImportStackMessage(self):
27    """Tests MakeImportStackMessage()."""
28    self.assertEquals(MakeImportStackMessage(["x"]), "")
29    self.assertEquals(MakeImportStackMessage(["x", "y"]),
30        "\n  y was imported by x")
31    self.assertEquals(MakeImportStackMessage(["x", "y", "z"]),
32        "\n  z was imported by y\n  y was imported by x")
33
34  def testScrambleMethodOrdinals(self):
35    """Tests ScrambleMethodOrdinals()."""
36    interface = FakeIface()
37    interface.name = 'RendererConfiguration'
38    interface.methods = [FakeMethod(), FakeMethod(), FakeMethod()]
39    ScrambleMethodOrdinals([interface], "foo")
40    # These next three values are hard-coded. If the generation algorithm
41    # changes from being based on sha256(seed + interface.name + str(i)) then
42    # these numbers will obviously need to change too.
43    #
44    # Note that hashlib.sha256('fooRendererConfiguration1').digest()[:4] is
45    # '\xa5\xbc\xf9\xca' and that hex(1257880741) = '0x4af9bca5'. The
46    # difference in 0x4a vs 0xca is because we only take 31 bits.
47    self.assertEquals(interface.methods[0].ordinal, 1257880741)
48    self.assertEquals(interface.methods[1].ordinal, 631133653)
49    self.assertEquals(interface.methods[2].ordinal, 549336076)
50
51
52if __name__ == "__main__":
53  unittest.main()
54