Best Python code snippet using locust
test_serialize.py
Source:test_serialize.py
...176 assert self.space.str_w(output[1]) == "a:2:{i:0;a:3:{i:0;i:1;i:2;i:1;i:1;R:4;}i:1;a:3:{i:0;i:1;i:2;R:4;i:1;R:4;}}"177 assert self.space.str_w(output[2]) == "a:2:{i:0;a:3:{i:0;i:1;i:2;i:1;i:1;R:4;}i:1;a:3:{i:0;i:1;i:2;i:1;i:1;R:7;}}"178 assert self.space.str_w(output[3]) == "a:3:{i:0;a:3:{i:0;i:1;i:2;i:1;i:1;R:4;}i:1;a:3:{i:0;i:1;i:2;i:1;i:1;R:7;}i:2;a:3:{i:0;i:1;i:2;i:1;i:1;R:10;}}"179 assert self.space.str_w(output[4]) == "a:2:{i:0;a:1:{i:0;a:2:{i:0;i:1;i:1;i:2;}}i:1;a:1:{i:0;a:2:{i:0;i:1;i:1;i:2;}}}"180 def test_unserialize(self):181 output = self.run('''182 function see($pickle) {183 echo unserialize($pickle);184 }185 see("b:1;");186 see("i:-3;");187 see("d:15;");188 see("d:15.2;");189 see('s:5:"acdef";');190 see("N;");191 see('a:3:{i:0;i:1;i:1;i:2;i:2;s:3:"xyz";}');192 ''')193 assert output[0].dump() == "true"194 assert output[1].dump() == "-3"195 assert output[2].dump() == "15.0"196 assert output[3].dump() == "15.2"197 assert output[4].dump() == "'acdef'"198 assert output[5].dump() == "NULL"199 assert output[6].dump() == "array(1, 2, 'xyz')"200 def test_unserialize_reference_1(self):201 output = self.run('''202 $pickle = 'a:2:{i:0;a:3:{i:0;i:10;i:2;i:20;i:1;R:4;}i:1;a:3:{i:0;i:30;i:2;i:40;i:1;R:7;}}';203 echo unserialize($pickle);204 $a = unserialize($pickle);205 $a[0][1]++;206 echo $a;207 $a = unserialize($pickle);208 $a[1][2]++;209 echo $a;210 ''')211 assert output[0].dump() == "array(array(10, 2=>20, 1=>20), array(30, 2=>40, 1=>40))"212 assert output[1].dump() == "array(array(10, 2=>21, 1=>21), array(30, 2=>40, 1=>40))"213 assert output[2].dump() == "array(array(10, 2=>20, 1=>20), array(30, 2=>41, 1=>41))"214 def test_unserialize_reference_2(self):215 output = self.run('''216 $pickle = 'a:2:{i:0;a:1:{i:0;i:5;}i:1;R:2;}';217 echo unserialize($pickle);218 $a = unserialize($pickle);219 $a[0][] = 6;220 echo $a;221 ''')222 assert output[0].dump() == "array(array(5), array(5))"223 assert output[1].dump() == "array(array(5, 6), array(5, 6))"224 def test_unserialize_non_reference_2(self):225 output = self.run('''226 $pickle = 'a:2:{i:0;a:1:{i:0;i:5;}i:1;r:2;}';227 echo unserialize($pickle);228 $a = unserialize($pickle);229 $a[0][] = 6;230 echo $a;231 ''')232 assert output[0].dump() == "array(array(5), array(5))"233 assert output[1].dump() == "array(array(5, 6), array(5))"234 def test_serialize_object(self):235 output = self.run('''236 class Aa { public $foo, $foo2; }237 $a = new aA;238 $a->foo = 42;239 $a->bar = 84;240 echo serialize($a);241 ''')242 assert self.space.str_w(output[0]) == 'O:2:"Aa":3:{s:3:"foo";i:42;s:4:"foo2";N;s:3:"bar";i:84;}'243 def test_unserialize_object(self):244 output = self.run('''245 class Aa { public $foo, $foo2; }246 $pickle = 'O:2:"aA":3:{s:3:"foo";i:42;s:4:"foo2";N;i:-5;i:84;}';247 echo unserialize($pickle);248 ''')249 assert output[0].dump() == 'instance(Aa: foo=>42, foo2=>NULL, -5=>84)'250 def test_serialize_object_references(self):251 output = self.run('''252 class Aa { public $foo; }253 $a = new aA;254 echo serialize(array($a, $a));255 echo serialize(array(&$a, &$a));256 ''')257 assert self.space.str_w(output[0]) == 'a:2:{i:0;O:2:"Aa":1:{s:3:"foo";N;}i:1;r:2;}'258 assert self.space.str_w(output[1]) == 'a:2:{i:0;O:2:"Aa":1:{s:3:"foo";N;}i:1;R:2;}'259 def test_unserialize_object_references(self):260 output = self.run('''261 class Aa { public $foo; }262 $pickle = 'a:2:{i:0;O:2:"Aa":1:{s:3:"foo";N;}i:1;r:2;}';263 $a = unserialize($pickle);264 $a[1] = 42;265 echo $a[0];266 $pickle = 'a:2:{i:0;O:2:"Aa":1:{s:3:"foo";N;}i:1;R:2;}';267 $a = unserialize($pickle);268 $a[1] = 42;269 echo $a[0];270 $pickle = 'a:2:{i:0;O:2:"Aa":1:{s:3:"foo";N;}i:1;r:2;}';271 $a = unserialize($pickle);272 $a[0]->foo = 42;273 echo $a[1];274 ''')275 assert output[0].dump() == 'instance(Aa: foo=>NULL)'276 assert output[1].dump() == '42'277 assert output[2].dump() == 'instance(Aa: foo=>42)'278 def test_unserialize_missing_class(self):279 output = self.run("""280 $pickle = 'O:7:\"Unknown\":0:{}';281 echo unserialize($pickle);282 $pickle = 'O:7:\"Unknown\":1:{s:2:"xy";i:5;}';283 echo unserialize($pickle);284 """)285 assert output[0].dump() == "instance(__PHP_Incomplete_Class: __PHP_Incomplete_Class_Name=>'Unknown')"286 assert output[1].dump() == "instance(__PHP_Incomplete_Class: __PHP_Incomplete_Class_Name=>'Unknown', xy=>5)"287 def test_unserialize_error_1(self):288 self.run("unserialize('');",289 ["Hippy warning: unserialize(): empty string"])290 self.run("unserialize('i:5');",291 ["Notice: unserialize(): Error at offset 0 of 3 bytes"])292 def test_unserialize_error_2(self):293 self.run("unserialize('a:1:{ ');",294 ["Notice: unserialize(): Error at offset 5 of 6 bytes"])295 self.run("unserialize('a:1:{i');",296 ["Notice: unserialize(): Error at offset 5 of 6 bytes"])297 self.run("unserialize('a:1:{ij');",298 ["Notice: unserialize(): Error at offset 5 of 7 bytes"])299 self.run("unserialize('a:1:{i:');",300 ["Notice: unserialize(): Error at offset 5 of 7 bytes"])301 self.run("unserialize('a:1:{i:5');",302 ["Notice: unserialize(): Error at offset 5 of 8 bytes"])303 self.run("unserialize('a:1:{i:;');",304 ["Notice: unserialize(): Error at offset 5 of 8 bytes"])305 self.run("unserialize('a:1:{i:x;');",306 ["Notice: unserialize(): Error at offset 5 of 9 bytes"])307 self.run("unserialize('a:1:{i:-;');",308 ["Notice: unserialize(): Error at offset 5 of 9 bytes"])309 def test_unserialize_error_3(self):310 self.run("unserialize('a:1:{s:');",311 ["Notice: unserialize(): Error at offset 5 of 7 bytes"])312 self.run("unserialize('a:1:{s:5');",313 ["Notice: unserialize(): Error at offset 5 of 8 bytes"])314 self.run("unserialize('a:1:{s:5:');",315 ["Notice: unserialize(): Error at offset 5 of 9 bytes"])316 self.run("unserialize('a:1:{s:5:\"');",317 ["Notice: unserialize(): Error at offset 7 of 10 bytes"])318 self.run("unserialize('a:1:{s:5:\"1234');",319 ["Notice: unserialize(): Error at offset 7 of 14 bytes"])320 self.run("unserialize('a:1:{s:5:\"12345');",321 ["Notice: unserialize(): Error at offset 15 of 15 bytes"])322 self.run("unserialize('a:1:{s:5:\"12345\"');",323 ["Notice: unserialize(): Error at offset 17 of 16 bytes"])324 self.run("unserialize('a:1:{s:5x:\"12345\";}');",325 ["Notice: unserialize(): Error at offset 5 of 19 bytes"])326 self.run("unserialize('a:1:{s:-1:\"\";}');",327 ["Notice: unserialize(): Error at offset 5 of 14 bytes"])328 def test_unserialize_error_4(self):329 self.run("unserialize('a:1:{}');",330 ["Notice: unserialize(): Unexpected end of serialized data",331 "Notice: unserialize(): Error at offset 5 of 6 bytes"])332 self.run("unserialize('a:2:{i:0;i:0;}');",333 ["Notice: unserialize(): Unexpected end of serialized data",334 "Notice: unserialize(): Error at offset 13 of 14 bytes"])335 self.run("unserialize('a:0:{i:0;i:0;}');",336 ["Notice: unserialize(): Error at offset 6 of 14 bytes"])337 self.run("unserialize('a:1:{i:0;}');",338 ["Notice: unserialize(): Unexpected end of serialized data",339 "Notice: unserialize(): Error at offset 9 of 10 bytes"])340 self.run("unserialize('a:1:{i:0;i:5}');",341 ["Notice: unserialize(): Error at offset 9 of 13 bytes"])342 self.run("unserialize('a:1:{i:0;i:5;');",343 ["Notice: unserialize(): Error at offset 14 of 13 bytes"])344 def test_unserialize_error_5(self):345 self.run("unserialize('d:;');",346 ["Notice: unserialize(): Error at offset 0 of 3 bytes"])347 self.run("unserialize('d:1E99x;');",348 ["Notice: unserialize(): Error at offset 0 of 8 bytes"])349 def test_unserialize_error_6(self):350 self.run("unserialize('b:2;');",351 ["Notice: unserialize(): Error at offset 0 of 4 bytes"])352 self.run("unserialize('b:-1;');",353 ["Notice: unserialize(): Error at offset 0 of 5 bytes"])354 self.run("unserialize('b:true;');",355 ["Notice: unserialize(): Error at offset 0 of 7 bytes"])356 def test_unserialize_error_7(self):357 self.run("unserialize('a:-1:{}');",358 ["Notice: unserialize(): Error at offset 0 of 7 bytes"])359 self.run("unserialize('a:x:{}');",360 ["Notice: unserialize(): Error at offset 0 of 6 bytes"])361 self.run("unserialize('a:1;');",362 ["Notice: unserialize(): Error at offset 0 of 4 bytes"])363 self.run("unserialize('a:1:5;');",364 ["Notice: unserialize(): Error at offset 0 of 6 bytes"])365 self.run("unserialize('a:1{:');",366 ["Notice: unserialize(): Error at offset 0 of 5 bytes"])367 self.run("unserialize('a:1:}');",368 ["Notice: unserialize(): Error at offset 0 of 5 bytes"])369 def test_unserialize_error_8(self):370 self.run("unserialize('O:-1:\"\":0:{}');",371 ["Notice: unserialize(): Error at offset 0 of 12 bytes"])372 self.run("unserialize('O:y:\"\":0:{}');",373 ["Notice: unserialize(): Error at offset 0 of 11 bytes"])374 # Skipped the next two tests, is it really important?375 #self.run("unserialize('O:0:\"\":0:{}');",376 # ["Notice: unserialize(): Error at offset 2 of 11 bytes"])377 #self.run("unserialize('O:1:\"+\":-1:{}');",378 # ["Notice: unserialize(): Error at offset 5 of 13 bytes"])379 self.run("unserialize('O:1:\"X\":-1:{}');")380 self.run("unserialize('O:1:\"X\":0:{s:3:\"foo\";i:10;}');",381 ["Notice: unserialize(): Error at offset 12 of 27 bytes"])382 self.run("unserialize('O:1:\"X\":1:{s:3:\"foo\";i:10;}');")383 self.run("unserialize('O:1:\"X\":0:{s:3:\"foo\";}');",384 ["Notice: unserialize(): Error at offset 12 of 22 bytes"])385 self.run("unserialize('O:1:\"X\":1:{s:3:\"foo\";}');",386 ["Notice: unserialize(): Unexpected end of serialized data",387 "Notice: unserialize(): Error at offset 21 of 22 bytes"])388 def test_unserialize_error_9(self):389 self.run("unserialize('a:1:{d:5;i:3;}');",390 ["Notice: unserialize(): Error at offset 9 of 14 bytes"])391 # Bad offset in the message in case of composite types used as392 # an array key393 #self.run("unserialize('a:1:{a:0:{}i:3;}');",394 # ["Notice: unserialize(): Error at offset 11 of 16 bytes"])395 self.run("unserialize('O:1:\"X\":1:{b:1;i:3;}');",396 ["Notice: unserialize(): Error at offset 15 of 20 bytes"])397 def test_unserialize_error_10(self):398 self.run("unserialize('R:1;');",399 ["Notice: unserialize(): Error at offset 4 of 4 bytes"])400 self.run("unserialize('R:0;');",401 ["Notice: unserialize(): Error at offset 4 of 4 bytes"])402 self.run("unserialize('R:;');",403 ["Notice: unserialize(): Error at offset 0 of 3 bytes"])404 self.run("unserialize('R:x;');",405 ["Notice: unserialize(): Error at offset 0 of 4 bytes"])406 self.run("unserialize('R:999999;');",407 ["Notice: unserialize(): Error at offset 9 of 9 bytes"])408 self.run("unserialize('R:-1;');",...
__test__.py
Source:__test__.py
...6465class TestUnserializeMethods(unittest.TestCase):66 def test_string(self):67 with self.assertRaises(Exception):68 unserialize('"str')69 self.assertEqual(unserialize('"str"'), "str")7071 def test_string_encoding(self):72 with self.assertRaises(Exception):73 unserialize('"ä¹\\"')74 self.assertEqual(unserialize('"ä¹"'), "ä¹")7576 with self.assertRaises(Exception):77 unserialize('"ä¹"', "gbk")78 self.assertEqual(unserialize('"ä¹\\"', "gbk"), "ä¹")7980 def test_bool(self):81 with self.assertRaises(Exception):82 unserialize("True")83 with self.assertRaises(Exception):84 unserialize("true1")85 self.assertEqual(unserialize("true"), True)86 self.assertEqual(unserialize("false"), False)8788 def test_number(self):89 with self.assertRaises(Exception):90 unserialize("1..")91 with self.assertRaises(Exception):92 unserialize("..1")93 with self.assertRaises(Exception):94 unserialize("1.1.")95 self.assertEqual(unserialize(".1"), 0.1)96 self.assertEqual(unserialize("0.1"), 0.1)97 self.assertEqual(unserialize("100"), 100)98 self.assertEqual(unserialize("100."), 100)99 self.assertEqual(unserialize("-.1"), -0.1)100 self.assertEqual(unserialize("-0.1"), -0.1)101 self.assertEqual(unserialize("-100"), -100)102 self.assertEqual(unserialize("-100."), -100)103104 def test_tuple(self):105 self.assertEqual(unserialize("1,2,3"), 1)106 self.assertEqual(unserialize("1", multival=True), tuple([1]))107 self.assertEqual(unserialize("1,2,3", multival=True), tuple([1, 2, 3]))108109 def test_list(self):110 with self.assertRaises(Exception):111 unserialize("{1")112 with self.assertRaises(Exception):113 unserialize("{1}}")114 with self.assertRaises(Exception):115 unserialize("{1,,}")116 self.assertEqual(unserialize("{}"), [])117 self.assertEqual(unserialize("{ }"), [])118 self.assertEqual(unserialize("{1}"), [1])119 self.assertEqual(unserialize("{1 }"), [1])120 self.assertEqual(unserialize("{ 1}"), [1])121 self.assertEqual(unserialize("{1,}"), [1])122 self.assertEqual(unserialize("{1, }"), [1])123 self.assertEqual(unserialize("{1 , }"), [1])124125 def test_list_multiple(self):126 self.assertEqual(127 unserialize('{1,0.2,"3",-4,-.5,-6.,true}'),128 [1, 0.2, "3", -4, -0.5, -6, True],129 )130131 def test_list_indent(self):132 self.assertEqual(unserialize('{\n 1,\n 2,\n "3",\n}'), [1, 2, "3"])133134 def test_nested_list(self):135 self.assertEqual(unserialize('{1,2,"3",{4}}'), [1, 2, "3", [4]])136137 def test_nested_list_indent(self):138 self.assertEqual(139 unserialize('{\n 1,\n 2,\n "3",\n {\n 4,\n },\n}'),140 [1, 2, "3", [4]],141 )142143 def test_dict(self):144 self.assertEqual(unserialize('{1,2,["3"]="3"}'), {1: 1, 2: 2, "3": "3"})145146 def test_dict_indent(self):147 self.assertEqual(148 unserialize('{\n 1,\n 2,\n ["3"] = "3",\n}'),149 {1: 1, 2: 2, "3": "3"},150 )151152 def test_nested_dict(self):153 self.assertEqual(154 unserialize('{1,2,["3"]={[3]="3"}}'),155 {1: 1, 2: 2, "3": {3: "3"}},156 )157158 def test_nested_dict_indent(self):159 self.assertEqual(160 unserialize('{\n 1,\n 2,\n ["3"] = {\n [3] = "3",\n },\n}'),161 {1: 1, 2: 2, "3": {3: "3"}},162 )163164 def test_comment(self):165 with self.assertRaises(Exception):166 self.assertEqual(unserialize("{ -- comment 1}"), [1])167 self.assertEqual(unserialize("{ -- comment\n1}"), [1])168169 with self.assertRaises(Exception):170 self.assertEqual(unserialize("{ --[[comment\n1}"), [1])171 self.assertEqual(unserialize("{ --[[comment]]1}"), [1])172 self.assertEqual(unserialize("{ --[[comment\n ]]\n1}"), [1])173174175if __name__ == "__main__":
...
PHPUnserialize.py
Source:PHPUnserialize.py
...15Usage:16# Create an instance of the unserialize engine17u = PHPUnserialize()18# unserialize some string into python data19data = u.unserialize(serialized_string)2021Please see README.txt for more information.22"""2324class PHPUnserialize(object):25 """26 Class to unserialize something from the PHP Serialize format.2728 Usage:29 u = PHPUnserialize()30 data = u.unserialize(serialized_string)31 """3233 def __init__(self):34 pass3536 def session_decode(self, data):37 """Thanks to Ken Restivo for suggesting the addition38 of session_encode39 """40 session = {}41 while len(data) > 0:42 m = re.match('^(\w+)\|', data)43 if m:44 key = m.group(1)45 offset = len(key)+146 (dtype, dataoffset, value) = self._unserialize(data, offset)47 offset = offset + dataoffset48 data = data[offset:]49 session[key] = value50 else:51 # No more stuff to decode 52 return session53 54 return session55 56 def unserialize(self, data):57 return self._unserialize(data, 0)[2]5859 def _unserialize(self, data, offset=0):60 """61 Find the next token and unserialize it.62 Recurse on array.6364 offset = raw offset from start of data65 66 return (type, offset, value)67 """6869 buf = []70 dtype = string.lower(data[offset:offset+1])7172 #print "# dtype =", dtype7374 # 't:' = 2 chars75 dataoffset = offset + 276 typeconvert = lambda x : x77 chars = datalength = 07879 # int => Integer80 if dtype == 'i':81 typeconvert = lambda x : int(x)82 (chars, readdata) = self.read_until(data, dataoffset, ';')83 # +1 for end semicolon84 dataoffset += chars + 18586 # bool => Boolean87 elif dtype == 'b':88 typeconvert = lambda x : (int(x) == 1)89 (chars, readdata) = self.read_until(data, dataoffset, ';')90 # +1 for end semicolon91 dataoffset += chars + 19293 # double => Floating Point94 elif dtype == 'd':95 typeconvert = lambda x : float(x)96 (chars, readdata) = self.read_until(data, dataoffset, ';')97 # +1 for end semicolon98 dataoffset += chars + 199100 # n => None101 elif dtype == 'n':102 readdata = None103104 # s => String105 elif dtype == 's':106 (chars, stringlength) = self.read_until(data, dataoffset, ':')107 # +2 for colons around length field108 dataoffset += chars + 2109110 # +1 for start quote111 (chars, readdata) = self.read_chars(data, dataoffset+1, int(stringlength))112 # +2 for endquote semicolon113 dataoffset += chars + 2114115 if chars != int(stringlength) != int(readdata):116 raise Exception("String length mismatch")117118 # array => Dict119 # If you originally serialized a Tuple or List, it will120 # be unserialized as a Dict. PHP doesn't have tuples or lists,121 # only arrays - so everything has to get converted into an array122 # when serializing and the original type of the array is lost123 elif dtype == 'a':124 readdata = {}125126 # How many keys does this list have?127 (chars, keys) = self.read_until(data, dataoffset, ':')128 # +2 for colons around length field129 dataoffset += chars + 2130131 # Loop through and fetch this number of key/value pairs132 for i in range(0, int(keys)):133 # Read the key134 (ktype, kchars, key) = self._unserialize(data, dataoffset)135 dataoffset += kchars136 #print "Key(%i) = (%s, %i, %s) %i" % (i, ktype, kchars, key, dataoffset)137138 # Read value of the key139 (vtype, vchars, value) = self._unserialize(data, dataoffset)140 dataoffset += vchars141 #print "Value(%i) = (%s, %i, %s) %i" % (i, vtype, vchars, value, dataoffset)142143 # Set the list element144 readdata[key] = value145146 # +1 for end semicolon147 dataoffset += 1148 #chars = int(dataoffset) - start149150 # I don't know how to unserialize this151 else:152 raise Exception("Unknown / Unhandled data type (%s)!" % dtype)153
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!