def test_is_block(self): """:: IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+""" tests = { # positive and negative tests 'BasicLatin': ("ABC", ul("\xc0\xdf\xa9")), 'Latin-1Supplement': (ul("\xc0\xdf\xa9"), "ABC"), 'CurrencySymbols': (u8(b'\xe2\x82\xa4\xe2\x82\xa9\xe2\x82\xac'), ul("\x24\xa2\xa3")), 'NumberForms': ( u8(b'\xe2\x85\x95\xe2\x85\x96\xe2\x85\x97\xe2\x85\x98'), "1/5 2/5 3/5 4/5") } for b in dict_keys(tests): p = xsi.RegularExpressionParser("Is" + b) cclass = p.require_is_block() self.assertTrue(p.the_char is None) t1, t2 = tests[b] for c in t1: self.assertTrue(cclass.test(c), "%s not in %s" % (repr(c), b)) for c in t2: self.assertFalse(cclass.test(c), "%s in Is%s" % (repr(c), b)) p = xsi.RegularExpressionParser("IsNumberFoams") try: cclass = p.require_is_block() self.fail("IsNumberFoams") except xsi.RegularExpressionError: pass
def test_char_prop(self): """:: charProp ::= IsCategory | IsBlock""" tests = { # positive and negative tests 'Nd': (u8(b'123\xdb\xb1\xdb\xb2\xdb\xb3'), u8(b'ABC\xe2\x85\x95\xe2\x85\x96\xe2\x85\x97\xe2\x85\x98')), 'S': (u8(b'+<=>\xe2\x81\x84\xe2\x82\xac'), "(){}"), 'IsBasicLatin': ("ABC", ul("\xc0\xdf\xa9")), 'IsLatin-1Supplement': (ul("\xc0\xdf\xa9"), "ABC"), 'IsCurrencySymbols': (u8(b'\xe2\x82\xa4\xe2\x82\xa9\xe2\x82\xac'), ul("\x24\xa2\xa3")), 'IsNumberForms': ( u8(b'\xe2\x85\x95\xe2\x85\x96\xe2\x85\x97\xe2\x85\x98'), "1/5 2/5 3/5 4/5") } for b in dict_keys(tests): p = xsi.RegularExpressionParser(b) cclass = p.require_char_prop() self.assertTrue(p.the_char is None) t1, t2 = tests[b] for c in t1: self.assertTrue(cclass.test(c), "%s not in %s" % (repr(c), b)) for c in t2: self.assertFalse(cclass.test(c), "%s in %s" % (repr(c), b))
def test_parse_hex_digit(self): p = unicode5.BasicParser( u8(b"0123456789abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5" b"\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9")) result = [] while p.the_char is not None: digit = p.parse_hex_digit() if digit is not None: result.append(digit) else: p.next_char() self.assertTrue(ul('').join(result) == ul('0123456789abcdefABCDEF')) # and now binary p = unicode5.BasicParser(b"0123456789abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ") result = [] while p.the_char is not None: digit = p.parse_hex_digit() if digit is not None: result.append(digit) else: p.next_char() self.assertTrue(join_bytes(result) == b'0123456789abcdefABCDEF')
def test_parseurn(self): """An URN ends when an octet/character from the excluded character set (<excluded>) is encountered.""" tests = [ 'urn:foo:bar\x00wrong', 'urn:foo:bar wrong', 'urn:foo:bar\\wrong', 'urn:foo:bar"wrong', 'urn:foo:bar&wrong', 'urn:foo:bar<wrong', 'urn:foo:bar>wrong', 'urn:foo:bar[wrong', 'urn:foo:bar]wrong', 'urn:foo:bar^wrong', 'urn:foo:bar`wrong', 'urn:foo:bar{wrong', 'urn:foo:bar|wrong', 'urn:foo:bar}wrong', 'urn:foo:bar~wrong', ul('urn:foo:bar\x7fwrong'), ul(b'urn:foo:bar\x9fwrong'), ul(b'urn:foo:bar\xff')] for src in tests: dst = urn.parse_urn(src) self.assertTrue(dst == 'urn:foo:bar', "parse_urn(%s) == %s" % (repr(src), repr(dst)))
def test_parse_hex_digit(self): p = unicode5.BasicParser( u8(b"0123456789abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5" b"\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9")) result = [] while p.the_char is not None: digit = p.parse_hex_digit() if digit is not None: result.append(digit) else: p.next_char() self.assertTrue(ul('').join(result) == ul('0123456789abcdefABCDEF')) # and now binary p = unicode5.BasicParser( b"0123456789abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ") result = [] while p.the_char is not None: digit = p.parse_hex_digit() if digit is not None: result.append(digit) else: p.next_char() self.assertTrue(join_bytes(result) == b'0123456789abcdefABCDEF')
def test_is_block(self): """:: IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+""" tests = { # positive and negative tests 'BasicLatin': ("ABC", ul("\xc0\xdf\xa9")), 'Latin-1Supplement': (ul("\xc0\xdf\xa9"), "ABC"), 'CurrencySymbols': (u8(b'\xe2\x82\xa4\xe2\x82\xa9\xe2\x82\xac'), ul("\x24\xa2\xa3")), 'NumberForms': (u8(b'\xe2\x85\x95\xe2\x85\x96\xe2\x85\x97\xe2\x85\x98'), "1/5 2/5 3/5 4/5") } for b in dict_keys(tests): p = xsi.RegularExpressionParser("Is" + b) cclass = p.require_is_block() self.assertTrue(p.the_char is None) t1, t2 = tests[b] for c in t1: self.assertTrue(cclass.test(c), "%s not in %s" % (repr(c), b)) for c in t2: self.assertFalse(cclass.test(c), "%s in Is%s" % (repr(c), b)) p = xsi.RegularExpressionParser("IsNumberFoams") try: cclass = p.require_is_block() self.fail("IsNumberFoams") except xsi.RegularExpressionError: pass
def test_char_prop(self): """:: charProp ::= IsCategory | IsBlock""" tests = { # positive and negative tests 'Nd': (u8(b'123\xdb\xb1\xdb\xb2\xdb\xb3'), u8(b'ABC\xe2\x85\x95\xe2\x85\x96\xe2\x85\x97\xe2\x85\x98')), 'S': (u8(b'+<=>\xe2\x81\x84\xe2\x82\xac'), "(){}"), 'IsBasicLatin': ("ABC", ul("\xc0\xdf\xa9")), 'IsLatin-1Supplement': (ul("\xc0\xdf\xa9"), "ABC"), 'IsCurrencySymbols': (u8(b'\xe2\x82\xa4\xe2\x82\xa9\xe2\x82\xac'), ul("\x24\xa2\xa3")), 'IsNumberForms': (u8(b'\xe2\x85\x95\xe2\x85\x96\xe2\x85\x97\xe2\x85\x98'), "1/5 2/5 3/5 4/5") } for b in dict_keys(tests): p = xsi.RegularExpressionParser(b) cclass = p.require_char_prop() self.assertTrue(p.the_char is None) t1, t2 = tests[b] for c in t1: self.assertTrue(cclass.test(c), "%s not in %s" % (repr(c), b)) for c in t2: self.assertFalse(cclass.test(c), "%s in %s" % (repr(c), b))
def test_literals(self): container = MockContainer(container=self.container, dbapi=MockAPI(0), max_connections=5) # format each type of literal v = edm.SimpleValue.from_type(edm.SimpleType.Binary) v.set_from_value(b'1234') self.assertTrue(container.prepare_sql_literal(v) == "X'31323334'") v = edm.SimpleValue.from_type(edm.SimpleType.Boolean) v.set_from_value(True) self.assertTrue(container.prepare_sql_literal(v) == "TRUE") v = edm.SimpleValue.from_type(edm.SimpleType.Byte) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.DateTime) v.set_from_value(iso.TimePoint.from_str('1972-03-03T09:45:00.000')) # discard fractional seconds self.assertTrue( container.prepare_sql_literal(v) == "'1972-03-03T09:45:00'") v = edm.SimpleValue.from_type(edm.SimpleType.DateTimeOffset) v.set_from_value(iso.TimePoint.from_str('1972-03-03T09:45:00.000Z')) # discard fractional seconds self.assertTrue( container.prepare_sql_literal(v) == "'1972-03-03T09:45:00Z'") v = edm.SimpleValue.from_type(edm.SimpleType.Time) v.set_from_value(iso.Time.from_str('09:45:00.000')) # discard fractional seconds self.assertTrue(container.prepare_sql_literal(v) == "'09:45:00'") v = edm.SimpleValue.from_type(edm.SimpleType.Decimal) v.set_from_value(decimal.Decimal('3.14')) self.assertTrue(container.prepare_sql_literal(v) == "3.14") v = edm.SimpleValue.from_type(edm.SimpleType.Double) v.set_from_value(3.14) self.assertTrue(container.prepare_sql_literal(v) == "3.14") v = edm.SimpleValue.from_type(edm.SimpleType.Single) v.set_from_value(3.14) self.assertTrue(container.prepare_sql_literal(v) == "3.14") v = edm.SimpleValue.from_type(edm.SimpleType.Guid) v.set_from_value(uuid.UUID(int=3)) self.assertTrue( container.prepare_sql_literal(v) == "X'00000000000000000000000000000003'") v = edm.SimpleValue.from_type(edm.SimpleType.Int16) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.Int32) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.Int64) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.String) v.set_from_value(ul("Dave's Caf\xe9")) self.assertTrue( container.prepare_sql_literal(v) == ul("'Dave''s Caf\xe9'")) v = edm.SimpleValue.from_type(edm.SimpleType.SByte) v.set_from_value(-3) self.assertTrue(container.prepare_sql_literal(v) == "-3")
def run_constructor(self): path = self.fs() self.assertTrue(isinstance(path, vfs.VirtualFilePath), "VirtualFilePath abstract class") self.assertTrue(path.is_empty(), "Empty path creation") self.assertTrue(path.is_single_component(), "Empty path is a single component") self.assertFalse(path.is_dirlike(), "Empty path is not diretory like") self.assertFalse(path.is_root(), "Empty path is not the root") self.assertFalse(path, "Non-zero test of empty path") path = self.fs('hello') self.assertFalse(path.is_empty(), "Single path component") self.assertTrue(path.is_single_component(), "path is a single component") self.assertFalse(path.is_dirlike(), "path is not diretory like") self.assertFalse(path.is_root(), "path is not the root") self.assertTrue(path.to_bytes() == b"hello", "convert to binary str") self.assertTrue(to_text(path) == ul("hello"), "convert to text") self.assertTrue(path, "Non-zero test of non-empty path") # create a path from a path path = self.fs(path) self.assertTrue(to_text(path) == ul("hello"), "check path") # create a path from a string and instance mix hello_world = ul("hello") + to_text(self.fs.sep) + ul("world") path = self.fs(path, 'world') self.assertTrue(to_text(path) == hello_world) # create a path from a string ending with the separator path = self.fs(ul('hello') + to_text(self.fs.sep), 'world') self.assertTrue(to_text(path) == hello_world) self.assertTrue(str(path) == str(hello_world)) path = self.fs(ul('Caf\xe9')) self.assertTrue(path.to_bytes() == ul('Caf\xe9').encode(path.codec), "convert to binary string") self.assertTrue(to_text(path) == ul('Caf\xe9'), "convert to text") # create a path with a trailing sep hello = self.fs.path_str('hello') + self.fs.sep path = self.fs(hello) self.assertFalse(path.is_empty(), "Trailing slash non empty") self.assertFalse(path.is_single_component(), "trailing slash is a single component") self.assertTrue(path.is_dirlike(), "trailing slash diretory like") self.assertFalse(path.is_root(), "trailing slash not the root") self.assertTrue(to_text(path) == ul(hello), "convert to text") # create a path with a trailing sep and current dir indicator hello = self.fs.path_str('hello') + self.fs.sep + self.fs.curdir path = self.fs(hello) self.assertFalse(path.is_empty(), "Trailing dot-slash non empty") self.assertFalse(path.is_single_component(), "trailing dot-slash is a single component") self.assertTrue(path.is_dirlike(), "trailing dot-slash diretory like") self.assertFalse(path.is_root(), "trailing dot-slash not the root") self.assertTrue(to_text(path) == ul(hello), "convert to text") # bad argument types raise TypeError try: path = self.fs(45) self.fail("constructor requires string argument") except TypeError: pass
def test_match_one(self): p = unicode5.BasicParser(ul("hello")) self.assertTrue(p.match_one(ul("hello"))) self.assertTrue(p.match_one(ul("h"))) self.assertFalse(p.match_one(ul("e"))) p = unicode5.BasicParser(b"hello") self.assertTrue(p.match_one(b"hello")) self.assertTrue(p.match_one(b"h")) self.assertFalse(p.match_one(b"e"))
def test_literals(self): container = MockContainer(container=self.container, dbapi=MockAPI(0), max_connections=5) # format each type of literal v = edm.SimpleValue.from_type(edm.SimpleType.Binary) v.set_from_value(b'1234') self.assertTrue(container.prepare_sql_literal(v) == "X'31323334'") v = edm.SimpleValue.from_type(edm.SimpleType.Boolean) v.set_from_value(True) self.assertTrue(container.prepare_sql_literal(v) == "TRUE") v = edm.SimpleValue.from_type(edm.SimpleType.Byte) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.DateTime) v.set_from_value(iso.TimePoint.from_str('1972-03-03T09:45:00.000')) # discard fractional seconds self.assertTrue(container.prepare_sql_literal(v) == "'1972-03-03T09:45:00'") v = edm.SimpleValue.from_type(edm.SimpleType.DateTimeOffset) v.set_from_value(iso.TimePoint.from_str('1972-03-03T09:45:00.000Z')) # discard fractional seconds self.assertTrue(container.prepare_sql_literal(v) == "'1972-03-03T09:45:00Z'") v = edm.SimpleValue.from_type(edm.SimpleType.Time) v.set_from_value(iso.Time.from_str('09:45:00.000')) # discard fractional seconds self.assertTrue(container.prepare_sql_literal(v) == "'09:45:00'") v = edm.SimpleValue.from_type(edm.SimpleType.Decimal) v.set_from_value(decimal.Decimal('3.14')) self.assertTrue(container.prepare_sql_literal(v) == "3.14") v = edm.SimpleValue.from_type(edm.SimpleType.Double) v.set_from_value(3.14) self.assertTrue(container.prepare_sql_literal(v) == "3.14") v = edm.SimpleValue.from_type(edm.SimpleType.Single) v.set_from_value(3.14) self.assertTrue(container.prepare_sql_literal(v) == "3.14") v = edm.SimpleValue.from_type(edm.SimpleType.Guid) v.set_from_value(uuid.UUID(int=3)) self.assertTrue(container.prepare_sql_literal(v) == "X'00000000000000000000000000000003'") v = edm.SimpleValue.from_type(edm.SimpleType.Int16) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.Int32) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.Int64) v.set_from_value(3) self.assertTrue(container.prepare_sql_literal(v) == "3") v = edm.SimpleValue.from_type(edm.SimpleType.String) v.set_from_value(ul("Dave's Caf\xe9")) self.assertTrue(container.prepare_sql_literal(v) == ul("'Dave''s Caf\xe9'")) v = edm.SimpleValue.from_type(edm.SimpleType.SByte) v.set_from_value(-3) self.assertTrue(container.prepare_sql_literal(v) == "-3")
def test_constructor(self): e = structures.XMLEntity(b"<hello>") self.assertTrue(e.line_num == 1) self.assertTrue(e.line_pos == 1) self.assertTrue(is_unicode(e.the_char) and e.the_char == '<') e = structures.XMLEntity(ul("<hello>")) self.assertTrue(is_unicode(e.the_char) and e.the_char == '<') e = structures.XMLEntity(StringIO(ul("<hello>"))) self.assertTrue(e.line_num == 1) self.assertTrue(e.line_pos == 1) self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
def test_branch(self): """:: branch ::= piece* """ p = xsi.RegularExpressionParser( ul("A[A-z-[\[-\]]](Hello(Mum)|(Dad))A{0,0}[A-Z]{0,1}(Hello)" "{0,}B{1,}[@-\xA9]?)")) self.assertTrue( p.require_branch() == ul( "A[A-Z_-z^](Hello(Mum)|(Dad))[A-Z]?(Hello)*B+[@-\xA9]?"), "Branch") self.assertTrue(p.the_char == ")")
def test_branch(self): """:: branch ::= piece* """ p = xsi.RegularExpressionParser( ul("A[A-z-[\[-\]]](Hello(Mum)|(Dad))A{0,0}[A-Z]{0,1}(Hello)" "{0,}B{1,}[@-\xA9]?)")) self.assertTrue( p.require_branch() == ul("A[A-Z_-z^](Hello(Mum)|(Dad))[A-Z]?(Hello)*B+[@-\xA9]?"), "Branch") self.assertTrue(p.the_char == ")")
def test_match_insensitive(self): p = unicode5.BasicParser(ul("heLLo")) p.next_char() save_pos = p.pos self.assertTrue(p.match_insensitive(ul("ell"))) self.assertTrue(p.pos == save_pos) self.assertFalse(p.match_insensitive(ul("hell"))) self.assertTrue(p.pos == save_pos) p = unicode5.BasicParser(b"heLLo") p.next_char() self.assertTrue(p.match_insensitive(b"ell")) self.assertFalse(p.match_insensitive(b"hell"))
def test_parse_integer(self): p = unicode5.BasicParser(ul("23p")) # all defaults, unbounded self.assertTrue(p.parse_integer() == 23) self.assertTrue(p.pos == 2) p.setpos(1) # provide a minimum value self.assertTrue(p.parse_integer(4) is None) self.assertTrue(p.parse_integer(2) == 3) p.setpos(1) # provide a minimum and maximum value self.assertTrue(p.parse_integer(0, 2) is None) self.assertTrue(p.parse_integer(1, 4) == 3) p.setpos(0) # min value < 0, should throw an error try: p.parse_integer(-1) self.fail("min = -1 didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 0) # min value > max, should throw an error try: p.parse_integer(3, 1) self.fail("min > max didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 0) # check we can exceed ordinary integer sizes istr = ul("123456789" + "0" * 256) p = unicode5.BasicParser(istr) # test max digits self.assertTrue(p.parse_integer(0, None, 10) == 1234567890) # check wide zeros self.assertTrue(p.parse_integer(0, None, 10) == 0) self.assertTrue(p.pos == 20) p.setpos(0) # check large numbers self.assertTrue(p.parse_integer(0, None, 15) == 123456789000000) # test Arabic digits, should not parse! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertTrue(p.parse_integer() is None) p.next_char() # test binary forms p = unicode5.BasicParser(b"234p") self.assertTrue(p.parse_integer(max_digits=1) == 2) self.assertTrue(p.parse_integer(0, 2) is None) self.assertTrue(p.parse_integer() == 34) p.next_char() self.assertTrue(p.parse_integer() is None)
def test_getcwd(self): wd = self.fs.getcwd() self.assertTrue(isinstance(wd, self.fs)) self.assertTrue(isinstance(wd, vfs.VirtualFilePath)) self.assertTrue(to_text(wd) == ul('C:\\home')) # the current drive letter is used to make a path absolute path = self.fs(ul('\\home')) self.assertTrue(path.isabs(), "Missing path letter still absolute") apath = path.abspath() self.assertTrue(apath != path, "Path should change for abspath") self.assertTrue(apath.splitdrive()[0] == ul('C:')) # check that the drive is not absolute self.assertFalse(apath.splitdrive()[0].isabs())
def test_length(self): es = self.schema['SampleEntities.Employees'] self.assertTrue(isinstance(es, edm.EntitySet)) with es.open() as collection: self.assertTrue(len(collection) == 0, "Length on load") self.employees.data["ABCDE"] = ( ul("ABCDE"), ul("John Smith"), None, None) self.assertTrue(len(collection) == 1, "Length after insert") self.employees.data["FGHIJ"] = ( ul("FGHIJ"), ul("Jane Smith"), None, None) self.assertTrue(len(collection) == 2, "Length after 2xinsert") del collection["ABCDE"] self.assertTrue(len(collection) == 1, "Length after delete")
def test_length(self): es = self.schema['SampleEntities.Employees'] self.assertTrue(isinstance(es, edm.EntitySet)) with es.open() as collection: self.assertTrue(len(collection) == 0, "Length on load") self.employees.data["ABCDE"] = (ul("ABCDE"), ul("John Smith"), None, None) self.assertTrue(len(collection) == 1, "Length after insert") self.employees.data["FGHIJ"] = (ul("FGHIJ"), ul("Jane Smith"), None, None) self.assertTrue(len(collection) == 2, "Length after 2xinsert") del collection["ABCDE"] self.assertTrue(len(collection) == 1, "Length after delete")
def test_nonces(self): with self.silo['Consumers'].open() as collection: consumer = lti.ToolConsumer.new_from_values( collection.new_entity(), self.cipher, 'default', key="12345", secret=ul("secret")) collection.insert_entity(consumer.entity) provider = lti.ToolProvider( self.container['Consumers'], self.container['Nonces'], self.cipher) consumer = provider.lookup_consumer('12345') self.assertTrue(provider.validate_timestamp_and_nonce( consumer.key, 0, '9e4a4b085c8c46d6aae6b5d9c8a15418', None)) # the same nonce should now evaluate to True self.assertFalse(provider.validate_timestamp_and_nonce( consumer.key, 0, '9e4a4b085c8c46d6aae6b5d9c8a15418', None)) # but a different one is False self.assertTrue(provider.validate_timestamp_and_nonce( consumer.key, 0, '8f274dca508711c2e70a67ab68fcc1f2', None)) # now at 89:59 we are still not allowed to reuse the nonce self.mock_time.tick(89*60+59.0) self.assertFalse(provider.validate_timestamp_and_nonce( consumer.key, 0, '9e4a4b085c8c46d6aae6b5d9c8a15418', None)) # but if we tick over 90 mins we're good to go again self.mock_time.tick(1.001) self.assertTrue(provider.validate_timestamp_and_nonce( consumer.key, 0, '9e4a4b085c8c46d6aae6b5d9c8a15418', None))
def test_output(self): txt_out = io.StringIO() save_stdout = sys.stdout try: sys.stdout = txt_out py2.output(py2.ul("Going to the\nCaf\xe9")) finally: sys.stdout = save_stdout self.assertTrue(txt_out.getvalue() == py2.ul("Going to the\nCaf\xe9")) bin_out = io.BytesIO() try: sys.stdout = bin_out py2.output(py2.ul("Going to the\nCaf\xe9")) finally: sys.stdout = save_stdout self.assertTrue(bin_out.getvalue() == b"Going to the\nCaf\xc3\xa9")
def test_entity_tag(self): try: etag = EntityTag() self.fail("Required tag in constructor") except TypeError: pass etag = EntityTag("hello") self.assertTrue(etag.weak, "ETag constructor makes weak tags") etag = EntityTag("hello", False) self.assertFalse(etag.weak, "ETag constructor with strong tag") self.assertTrue(etag.tag, "ETag constructor tag not None") etag = EntityTag.from_str('W/"hello"') self.assertTrue(etag.weak, "Failed to parse weak tag") self.assertTrue(etag.tag == b"hello", "Failed to parse ETag value") etag = EntityTag.from_str('w/ "h\\"ello"') self.assertTrue( etag.weak, "Failed to parse weak tag with lower case 'w'") self.assertTrue( etag.tag == b'h"ello', "Failed to unpick quoted pair from ETag value; found %s" % repr(etag.tag)) etag = EntityTag.from_str('"hello"') self.assertFalse(etag.weak, "Failed to parse strong tag") self.assertTrue(etag.tag == b"hello", "Failed to parse ETag value") etag = EntityTag.from_str(ul('"hello"')) self.assertFalse(etag.weak, "Failed to parse strong tag") self.assertTrue(etag.tag == b"hello", "Failed to parse ETag value")
def test_canonicalize_data(self): try: uri.canonicalize_data(ul('Caf\xe9')) self.fail("non-ASCII character for canonicalisation") except UnicodeEncodeError: pass self.assertTrue(uri.canonicalize_data( "%2D%5F%2e%21%7e%2A%27%28%29%41%5a%61%7A%30%39") == "-_.!~*'()AZaz09", "unreserved characters are unescaped") self.assertTrue( uri.canonicalize_data('"<[one #word\x09or two\r\n]>"', allowed_test=uri.is_allowed_2396) == '%22%3C%5Bone%20%23word%09or%20two%0D%0A%5D%3E%22', "escape chars neither unreserved nor reserved (rfc2396)") self.assertTrue( uri.canonicalize_data('"<[one #word\x09or two\r\n]>"') == '%22%3C[one%20%23word%09or%20two%0D%0A]%3E%22', "escape chars neither unreserved nor reserved") # passing is_alphanum effectively causes 'marks' to stay as-is self.assertTrue(uri.canonicalize_data( "%2D%5F%2e%21%7e%2A%27%28%29%41%5a%61%7A%30%39", uri.is_alphanum) == "%2D%5F%2E%21%7E%2A%27%28%29AZaz09", "(only) unreserved characters are unescaped") # passing lambda: x:False effectively causes everything to stay as-is self.assertTrue(uri.canonicalize_data( "%2D%5F%2e%21%7e%2A%27%28%29%41%5a%61%7A%30%39", lambda x: False) == "%2D%5F%2E%21%7E%2A%27%28%29%41%5A%61%7A%30%39", "no characters are unescaped")
def test_open_w_r(self): ss = blockstore.StreamStore(bs=self.bs, ls=self.ls, entity_set=self.cdef['Streams']) s1 = ss.new_stream("text/plain") self.assertTrue(s1['md5'].value == hashlib.md5().digest()) with ss.open_stream(s1, 'w') as s: self.assertFalse(s.closed) self.assertFalse(s.readable()) self.assertTrue(s.writable()) self.assertTrue(s.tell() == 0) # try writing a multi-block string nbytes = 0 fox = b"The quick brown fox jumped over the lazy dog" cafe = ul("Caf\xe9").encode('utf-8') data = fox + cafe + fox while nbytes < len(data): nbytes += s.write(data[nbytes:]) self.assertTrue(s.tell() == nbytes) self.assertTrue(s1['size'].value == nbytes) self.assertTrue(s1['md5'].value == hashlib.md5(data).digest()) with self.cdef['BlockLists'].open() as blocks: # data should spill over to 2 blocks self.assertTrue(len(blocks) == 2) with ss.open_stream(s1, 'r') as s: self.assertFalse(s.closed) self.assertTrue(s.readable()) self.assertFalse(s.writable()) self.assertTrue(s.tell() == 0) rdata = s.read() self.assertTrue(rdata == data, "Read back %s" % repr(rdata)) self.assertTrue(s.tell() == nbytes)
def test_getcroot(self): wd = self.fs.getcroot() self.assertTrue(isinstance(wd, self.fs)) self.assertTrue(isinstance(wd, vfs.VirtualFilePath)) self.assertTrue(to_text(wd) == ul('/')) self.assertTrue(wd.to_bytes() == b'/') self.assertTrue(isinstance(wd.to_bytes(), bytes))
def test_entity_tag(self): try: etag = EntityTag() self.fail("Required tag in constructor") except TypeError: pass etag = EntityTag("hello") self.assertTrue(etag.weak, "ETag constructor makes weak tags") etag = EntityTag("hello", False) self.assertFalse(etag.weak, "ETag constructor with strong tag") self.assertTrue(etag.tag, "ETag constructor tag not None") etag = EntityTag.from_str('W/"hello"') self.assertTrue(etag.weak, "Failed to parse weak tag") self.assertTrue(etag.tag == b"hello", "Failed to parse ETag value") etag = EntityTag.from_str('w/ "h\\"ello"') self.assertTrue(etag.weak, "Failed to parse weak tag with lower case 'w'") self.assertTrue( etag.tag == b'h"ello', "Failed to unpick quoted pair from ETag value; found %s" % repr(etag.tag)) etag = EntityTag.from_str('"hello"') self.assertFalse(etag.weak, "Failed to parse strong tag") self.assertTrue(etag.tag == b"hello", "Failed to parse ETag value") etag = EntityTag.from_str(ul('"hello"')) self.assertFalse(etag.weak, "Failed to parse strong tag") self.assertTrue(etag.tag == b"hello", "Failed to parse ETag value")
def test_boundary_delimiter(self): # boundary_delimiter is read from the MediaType ct = MediaType.from_str( "multipart/mixed; boundary=gc0p4Jq0M2Yt08j34c0p") boundary = multipart.get_boundary_delimiter(ct) self.assertTrue(boundary == "\r\n--gc0p4Jq0M2Yt08j34c0p") ct = MediaType.from_str( 'multipart/mixed; boundary="gc0pJq0M:08jU534c0p"') boundary = multipart.get_boundary_delimiter(ct) self.assertTrue(boundary == "\r\n--gc0pJq0M:08jU534c0p") # boundary delimiters and headers are always 7bit US-ASCII # (non-US-ASCII encoding deprecated) ct = MediaType.from_str( ul('multipart/mixed; boundary="gc0pJq0M\xa608jU534c0p"')) try: boundary = multipart.get_boundary_delimiter(ct) self.fail("8-bit boundary") except multipart.MultipartError: pass # must be no longer than 70 characters, not counting the two # leading hyphens ct = MediaType.from_str( "multipart/mixed; boundary=abcdefghijklmnopqrstuvwxyz1234567890" "abcdefghijklmnopqrstuvwxyz12345678") self.assertTrue(len(multipart.get_boundary_delimiter(ct)) == 74) ct = MediaType.from_str( "multipart/mixed; boundary=abcdefghijklmnopqrstuvwxyz1234567890" "abcdefghijklmnopqrstuvwxyz123456789") try: multipart.get_boundary_delimiter(ct) self.fail("long boundary") except multipart.MultipartError: pass
def test_launch(self): command = "POST" url = "http://www.example.com/launch" headers = {'Content-Type': 'application/x-www-form-urlencoded'} query_string = "context_id=456434513&context_label=SI182&"\ "context_title=Design%20of%20Personal%20Environments&"\ "launch_presentation_css_url=http%3A%2F%2Fwww.imsglobal.org%2F"\ "developers%2FLTI%2Ftest%2Fv1p1%2Flms.css&"\ "launch_presentation_document_target=frame&"\ "launch_presentation_locale=en-US&"\ "launch_presentation_return_url=http%3A%2F%2Fwww.imsglobal.org%2F"\ "developers%2FLTI%2Ftest%2Fv1p1%2Flms_return.php&"\ "lis_outcome_service_url=http%3A%2F%2Fwww.imsglobal.org%2F"\ "developers%2FLTI%2Ftest%2Fv1p1%2Fcommon%2F"\ "tool_consumer_outcome.php%3Fb64%3DMTIzNDU6OjpzZWNyZXQ%3D&"\ "lis_person_contact_email_primary=user%40school.edu&"\ "lis_person_name_family=Public&"\ "lis_person_name_full=Jane%20Q.%20Public&"\ "lis_person_name_given=Given&"\ "lis_person_sourcedid=school.edu%3Auser&"\ "lis_result_sourcedid=feb-123-456-2929%3A%3A28883&"\ "lti_message_type=basic-lti-launch-request&"\ "lti_version=LTI-1p0&"\ "oauth_callback=about%3Ablank&oauth_consumer_key=12345&"\ "oauth_nonce=45f32b44e314244a222d0e070fa55384&"\ "oauth_signature=SKhxr%2Bx4p9jVO6sFxKdpA5neDtg%3D&"\ "oauth_signature_method=HMAC-SHA1&"\ "oauth_timestamp=1420370306&"\ "oauth_version=1.0&"\ "resource_link_description=A%20weekly%20blog.&"\ "resource_link_id=120988f929-274612&"\ "resource_link_title=Weekly%20Blog&"\ "roles=Instructor&"\ "tool_consumer_info_product_family_code=ims&"\ "tool_consumer_info_version=1.1&"\ "tool_consumer_instance_description="\ "University%20of%20School%20%28LMSng%29&"\ "tool_consumer_instance_guid=lmsng.school.edu&"\ "user_id=292832126" with self.silo['Consumers'].open() as collection: consumer = lti.ToolConsumer.new_from_values( collection.new_entity(), self.cipher, 'default', key="12345", secret=ul("secret")) collection.insert_entity(consumer.entity) provider = lti.ToolProvider( self.container['Consumers'], self.container['Nonces'], self.cipher) consumer, parameters = provider.launch( command, url, headers, query_string) self.assertTrue(consumer.key == '12345') self.assertTrue('user_id' in parameters) self.assertTrue(parameters['user_id'] == '292832126') # we should have an exception if we change the parameters! try: consumer, parameters = provider.launch( "POST", url, headers, query_string + "%custom_value=X") self.fail("LTI launch with bad signature") except lti.LTIAuthenticationError: pass
def test_parse_insensitive(self): p = unicode5.BasicParser(ul("heLLo")) p.next_char() match = ul("ell") save_pos = p.pos self.assertTrue(p.parse_insensitive(match) == ul("eLL")) self.assertTrue(p.pos == save_pos + 3) p.setpos(save_pos) self.assertTrue(p.parse_insensitive(ul("hell")) is None) self.assertTrue(p.pos == save_pos) p = unicode5.BasicParser(b"heLLo") p.next_char() save_pos = p.pos self.assertTrue(p.parse_insensitive(b"ell") == b"eLL") p.setpos(save_pos) self.assertTrue(p.parse_insensitive(b"hell") is None) self.assertTrue(p.pos == save_pos)
def run_dirs(self): dpath = self.fs.mkdtemp('.d', 'test-') try: self.assertTrue(isinstance(dpath, vfs.VirtualFilePath)) self.assertTrue(dpath.exists() and dpath.isdir()) new_path = dpath.join("test-directory") self.assertFalse(new_path.exists() or new_path.isdir()) new_path.mkdir() self.assertTrue(new_path.exists() and new_path.isdir()) deep_path = dpath.join("missing", "dir") self.assertFalse(deep_path.exists() or deep_path.isdir()) try: deep_path.mkdir() self.fail("Missing parent test") except: pass deep_path.makedirs() self.assertTrue(deep_path.exists() and deep_path.isdir()) new_file = new_path.join('hello') f = new_file.open('w') f.write(ul("Hello")) f.close() self.assertTrue(new_file.exists() and new_file.isfile() and not new_file.isdir()) new_copy = new_path.join('hello-again') self.assertFalse( new_copy.exists() or new_copy.isfile() or new_copy.isdir()) new_file.copy(new_copy) self.assertTrue(new_copy.exists() and new_copy.isfile() and not new_copy.isdir()) f = new_copy.open('r') data = f.read() f.close() self.assertTrue(data == "Hello", "Copy data test") new_file.remove() self.assertFalse( new_file.exists() or new_file.isfile() or new_file.isdir()) listing = dpath.listdir() found = False for node in listing: if "test-directory" == node: found = True break self.assertTrue( found, "Couldn't find test-directory in new directory") finally: dpath.rmtree(True) def norandom(n): raise NotImplementedError try: save_random = os.urandom os.urandom = norandom # check that we can work even without urandom dpath = self.fs.mkdtemp('.d', 'test-') dpath.rmtree(True) finally: os.urandom = save_random
def test_parse_digits(self): p = unicode5.BasicParser(ul("23p")) # min value of 0 self.assertTrue(p.parse_digits(0) == ul("23")) self.assertTrue(p.pos == 2) # min value of 2, should fail p.setpos(1) self.assertTrue(p.parse_digits(2) is None) # shouldn't move the parser self.assertTrue(p.pos == 1) # min value of 0, should throw an error try: p.parse_digits(-1) self.fail("min=-1 didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 1) # min value > max, should throw an error try: p.parse_digits(3, 1) self.fail("min > max didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 1) # check we can exceed ordinary integer sizes istr = ul("123456789" + "0" * 256) p = unicode5.BasicParser(istr) self.assertTrue(len(p.parse_digits(0, 256)) == 256) # and check that runs of 0 don't mean a thing self.assertTrue(p.parse_digits(0, 256) == ul("000000000")) # test Arabic digits, should not parse! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertTrue(p.parse_digits(1) is None) p.next_char() # test binary forms p = unicode5.BasicParser(b"234p") # unlike parse_digit we return a string, even if only one digit self.assertTrue(p.parse_digits(1, 1) == b"2") self.assertTrue(p.parse_digits(1) == b"34") p.next_char() self.assertTrue(p.parse_digits(1) is None) self.assertTrue(p.parse_digits(0) == b"")
class DriveSystem(vfs.MemFilePath): fs_name = "drivefs.pyslet.org" supports_drives = True sep = ul("\\") # must override these to prevent mixed instances _wd = None _fsdir = {}
def test_content_description(self): part = multipart.MessagePart() # any text, though non-ascii will be problematic 'the mechanism # specified in RFC 2047' is broken and not work implementing. we # treat this field as ASCII text or raw bytes with spaces trimmed part.set_content_description(" About my content ") self.assertTrue(part.get_header('Content-Description') == b"About my content") self.assertTrue(part.get_content_description() == b"About my content") try: part.set_content_description(ul("Caf\xe9")) self.fail("Content description should ASCII encode") except UnicodeError: pass # OK to pass raw bytes part.set_content_description(ul("Caf\xe9").encode('iso-8859-1')) self.assertTrue(part.get_header('Content-Description') == b"Caf\xe9") self.assertTrue(part.get_content_description() == b"Caf\xe9")
def test_default_key(self): with self.silo['Consumers'].open() as collection: entity = collection.new_entity() consumer = lti.ToolConsumer.new_from_values( entity, self.cipher, 'default', secret=ul("secret")) # we can default the key self.assertTrue(consumer.key) self.assertTrue(consumer.secret == 'secret') collection.insert_entity(entity) consumer2 = lti.ToolConsumer.new_from_values( collection.new_entity(), self.cipher, 'default', secret=ul("secret")) # secret need not be unique self.assertTrue(consumer2.key != consumer.key) self.assertTrue(consumer2.secret == 'secret') # Fine to persist to consumers with different keys, same # secret collection.insert_entity(consumer2.entity)
def test_require_end(self): p = unicode5.BasicParser("hello") for i in range3(5): try: p.require_end() self.fail("require_end failed to raise exception") except unicode5.ParserError as e: self.assertTrue(e.production == ul("end")) p.next_char() p.require_end()
def test_piece(self): """:: piece ::= atom quantifier? """ tests = { 'A': "A", '[A-z-[\[-\]]]': "[A-Z_-z^]", '(Hello(Mum)|(Dad))': "(Hello(Mum)|(Dad))", "A{0,0}": "", '[A-Z]{0,1}': "[A-Z]?", '(Hello){0,}': "(Hello)*", 'B{1,}': "B+", ul('[@-\xA9]?'): ul("[@-\xA9]?"), '(Bye)*': "(Bye)*", 'X+': "X+", '[45]{099,}': "[45]{99,}", '(45){0}': "", '@{99}': "@{99}", 'A{99,1}': xsi.RegularExpressionParser, 'A{1,99}': "A{1,99}", 'A{0,99}': "A{,99}", 'A{,99}': xsi.RegularExpressionParser, '$': "\\$", '^': "\\^", 'A{1,99': xsi.RegularExpressionParser, '\\{{0,1}': "\\{?", '\\??': "\\??" } for b in dict_keys(tests): p = xsi.RegularExpressionParser(b) t = tests[b] try: result = p.require_piece() self.assertTrue(result == t, "Mismatched piece: %s expected %s" % (repr(result), repr(t))) self.assertTrue(p.the_char is None) except xsi.RegularExpressionError: if t is xsi.RegularExpressionParser: pass else: logging.debug("Failed to parse %s" % repr(b)) raise
def test_piece(self): """:: piece ::= atom quantifier? """ tests = { 'A': "A", '[A-z-[\[-\]]]': "[A-Z_-z^]", '(Hello(Mum)|(Dad))': "(Hello(Mum)|(Dad))", "A{0,0}": "", '[A-Z]{0,1}': "[A-Z]?", '(Hello){0,}': "(Hello)*", 'B{1,}': "B+", ul('[@-\xA9]?'): ul("[@-\xA9]?"), '(Bye)*': "(Bye)*", 'X+': "X+", '[45]{099,}': "[45]{99,}", '(45){0}': "", '@{99}': "@{99}", 'A{99,1}': xsi.RegularExpressionParser, 'A{1,99}': "A{1,99}", 'A{0,99}': "A{,99}", 'A{,99}': xsi.RegularExpressionParser, '$': "\\$", '^': "\\^", 'A{1,99': xsi.RegularExpressionParser, '\\{{0,1}': "\\{?", '\\??': "\\??" } for b in dict_keys(tests): p = xsi.RegularExpressionParser(b) t = tests[b] try: result = p.require_piece() self.assertTrue( result == t, "Mismatched piece: %s expected %s" % (repr(result), repr(t))) self.assertTrue(p.the_char is None) except xsi.RegularExpressionError: if t is xsi.RegularExpressionParser: pass else: logging.debug("Failed to parse %s" % repr(b)) raise
def test_reflection(self): """Test the built-in handling of reflective attributes and elements.""" reflective_xml = ul("""<?xml version="1.0" encoding="UTF-8"?> <reflection btest="Hello"><etest>Hello Again</etest></reflection>""") f = StringIO(reflective_xml) d = ReflectiveDocument() d.read(src=f) root = d.root self.assertTrue(isinstance(root, ReflectiveElement)) self.assertTrue(root.bTest, "Attribute relfection") self.assertTrue(root.child, "Element relfection")
def test_parse_digit(self): p = unicode5.BasicParser(ul("2p")) self.assertTrue(p.parse_digit() == ul("2")) self.assertTrue(p.pos == 1) self.assertTrue(p.parse_digit() is None) p.next_char() self.assertTrue(p.parse_digit() is None) # test Arabic digits, should not parse! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertTrue(p.parse_digit() is None) p.next_char() # test binary forms p = unicode5.BasicParser(b"2p") self.assertTrue(p.parse_digit() == byte(b"2")) self.assertTrue(p.parse_digit() is None) p.next_char() self.assertTrue(p.parse_digit() is None)