tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bytestring.py (3913B)


      1 # -*- coding: UTF-8 -*-
      2 
      3 import WebIDL
      4 
      5 
      6 def WebIDLTest(parser, harness):
      7    parser.parse(
      8        """
      9        interface TestByteString {
     10          attribute ByteString bs;
     11          attribute DOMString ds;
     12          };
     13    """
     14    )
     15 
     16    results = parser.finish()
     17 
     18    harness.ok(True, "TestByteString interface parsed without error.")
     19 
     20    harness.check(len(results), 1, "Should be one production")
     21    harness.ok(isinstance(results[0], WebIDL.IDLInterface), "Should be an IDLInterface")
     22    iface = results[0]
     23    harness.check(
     24        iface.identifier.QName(), "::TestByteString", "Interface has the right QName"
     25    )
     26    harness.check(
     27        iface.identifier.name, "TestByteString", "Interface has the right name"
     28    )
     29    harness.check(iface.parent, None, "Interface has no parent")
     30 
     31    members = iface.members
     32    harness.check(len(members), 2, "Should be two productions")
     33 
     34    attr = members[0]
     35    harness.ok(isinstance(attr, WebIDL.IDLAttribute), "Should be an IDLAttribute")
     36    harness.check(
     37        attr.identifier.QName(), "::TestByteString::bs", "Attr has correct QName"
     38    )
     39    harness.check(attr.identifier.name, "bs", "Attr has correct name")
     40    harness.check(str(attr.type), "ByteString", "Attr type is the correct name")
     41    harness.ok(attr.type.isByteString(), "Should be ByteString type")
     42    harness.ok(attr.type.isString(), "Should be String collective type")
     43    harness.ok(not attr.type.isDOMString(), "Should be not be DOMString type")
     44 
     45    # now check we haven't broken DOMStrings in the process.
     46    attr = members[1]
     47    harness.ok(isinstance(attr, WebIDL.IDLAttribute), "Should be an IDLAttribute")
     48    harness.check(
     49        attr.identifier.QName(), "::TestByteString::ds", "Attr has correct QName"
     50    )
     51    harness.check(attr.identifier.name, "ds", "Attr has correct name")
     52    harness.check(str(attr.type), "String", "Attr type is the correct name")
     53    harness.ok(attr.type.isDOMString(), "Should be DOMString type")
     54    harness.ok(attr.type.isString(), "Should be String collective type")
     55    harness.ok(not attr.type.isByteString(), "Should be not be ByteString type")
     56 
     57    # Cannot represent constant ByteString in IDL.
     58    threw = False
     59    try:
     60        parser.parse(
     61            """
     62            interface ConstByteString {
     63              const ByteString foo = "hello"
     64              };
     65        """
     66        )
     67    except WebIDL.WebIDLError:
     68        threw = True
     69    harness.ok(
     70        threw, "Should have thrown a WebIDL error for ByteString default in interface"
     71    )
     72 
     73    # Can have optional ByteStrings with default values
     74    try:
     75        parser.parse(
     76            """
     77            interface OptionalByteString {
     78              undefined passByteString(optional ByteString arg = "hello");
     79              };
     80        """
     81        )
     82        parser.finish()
     83    except WebIDL.WebIDLError as e:
     84        harness.ok(
     85            False,
     86            "Should not have thrown a WebIDL error for ByteString "
     87            "default in dictionary. " + str(e),
     88        )
     89 
     90    # Can have a default ByteString value in a dictionary
     91    try:
     92        parser.parse(
     93            """
     94        dictionary OptionalByteStringDict {
     95          ByteString item = "some string";
     96        };
     97        """
     98        )
     99        parser.finish()
    100    except WebIDL.WebIDLError as e:
    101        harness.ok(
    102            False,
    103            "Should not have thrown a WebIDL error for ByteString "
    104            "default in dictionary. " + str(e),
    105        )
    106 
    107    # Don't allow control characters in ByteString literals
    108    threw = False
    109    try:
    110        parser.parse(
    111            """
    112        dictionary OptionalByteStringDict2 {
    113          ByteString item = "\x03";
    114        };
    115        """
    116        )
    117        parser.finish()
    118    except WebIDL.WebIDLError:
    119        threw = True
    120 
    121    harness.ok(
    122        threw,
    123        "Should have thrown a WebIDL error for invalid ByteString "
    124        "default in dictionary",
    125    )