2016-04-08 72 views
0

如果我運行下面的測試用例多次:Pyelliptic與泡椒失敗有時

python3 -m unittest test_pyelliptic.py 

大約三分之一的15運行失敗。

測試用例:

import unittest, pyelliptic, pickle 

class PyellipticTestCase(unittest.TestCase): 
    def setUp(self): 
     self.alice = pyelliptic.ECC() 
     self.bob = pyelliptic.ECC() 

    def test_pickleSign(self): 
     tm = {"text":"contract","amount":12} 
     tms1 = {"doc":tm,"c1":self.bob.get_pubkey(),"s1":self.bob.sign(pickle.dumps(tm))} 
     tms2bb = {"doc":tms1,"c2":self.alice.get_pubkey(),"s2":self.alice.sign(pickle.dumps(tms1))} 
     tms2 = pickle.loads(pickle.dumps(tms2bb)) 
     self.assertEqual(tms2['s2'],tms2bb['s2']) 
     self.assertTrue(pyelliptic.ECC(pubkey=self.alice.get_pubkey()).verify(tms2bb['s2'],pickle.dumps(tms2['doc']))) #<--- FAILs sometimes 

我的系統:

  • OS:14.04.1 Ubuntu的(內核3.16.0-36-通用)
  • Phtyon3:Python的3.4.0(默認,2014年4月11日,13點05分18秒)
  • OpenSSL的:1.0.1f 2014年1月6日
  • pyelliptic:1.5.7

我也在Python 3.4.3的類似系統上測試過這個,並且可以重現這個行爲。

您能重現系統上的故障嗎? 如果是,是什麼原因造成的?

更新

如果我運行詳細測試(python3 -m單元測試-v test_pyelliptic2.py)它產生以下輸出(運行兩次):

[email protected]:~/test$ python3 -m unittest -v test_pyelliptic2.py 
test_pickleSign (test_pyelliptic2.PyellipticTestCase) ... ok 

---------------------------------------------------------------------- 
Ran 1 test in 0.012s 

OK 
[email protected]:~/test$ python3 -m unittest -v test_pyelliptic2.py 
test_pickleSign (test_pyelliptic2.PyellipticTestCase) ... FAIL 

====================================================================== 
FAIL: test_pickleSign (test_pyelliptic2.PyellipticTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "XX/test/test_pyelliptic2.py", line 14, in test_pickleSign 
    self.assertTrue(pyelliptic.ECC(pubkey=self.alice.get_pubkey()).verify(tms2bb['s2'],pickle.dumps(tms2['doc']))) #<--- FAILs sometimes 
AssertionError: False is not true 

---------------------------------------------------------------------- 
Ran 1 test in 0.013s 

FAILED (failures=1) 

回答

0

經過進一步研究事實證明,二進制pickle輸出取決於字典中元素的排序。 因此,以下測試用例失敗在二進制陣列的比較:

def test_pickleOrder(self): 
     d1 = {"test1":12,"test2":14.2,"test3":"test"} 
     d2 = {"test3":"test","test1":12,"test2":14.2}  
     self.assertEqual(d1,d2)  
     d1p = pickle.dumps(d1) 
     d2p = pickle.dumps(d2)  
     self.assertEqual(d1p,d2p) #<--- fails here 

順便說一句:這不僅是泡菜的情況下,也爲JSON和BSON模塊