[Nym3-commit] r42 - trunk

nym3-devel@lists.noreply.org nym3-devel@lists.noreply.org
Thu, 29 Jul 2004 01:10:46 +0200


Author: jr
Date: 2004-07-29 01:10:41 +0200 (Thu, 29 Jul 2004)
New Revision: 42

Modified:
   trunk/Message.py
   trunk/TODO
Log:
rough testing of the parser :
_generate a command from good shaped, well sized arguments
_write the corresponding sendable string
_parse it
_write the corresponding string
_compare the two strings

some buglets repaired


Modified: trunk/Message.py
===================================================================
--- trunk/Message.py	2004-07-28 22:54:36 UTC (rev 41)
+++ trunk/Message.py	2004-07-28 23:10:41 UTC (rev 42)
@@ -32,14 +32,14 @@
 			return aux(n/256,chr(n%256)+ac,mod-1)
 	return aux(n,"",mod)
 
-def isMsgIdList(l):
+def isMidList(l):
 	"""true if l is a list of strings of length 20"""
-	def isMsgId(e):
-		if( len(e)==20 and types(e)==types.StringType ):
+	def isMid(e):
+		if( len(e)==20 and type(e)==types.StringType ):
 			return True
 		else:
 			return False
-	lb=map(isMsgId,l)
+	lb=map(isMid,l)
 	if(reduce(lambda x,y : x and y,lb)):
 		return True
 	else:
@@ -48,7 +48,7 @@
 surbLength = 2104
 sigLength = 256
 seqNoLength = 20
-msgIdLength = 20
+midLength = 20
 
 class StrReader:
 	"""wraps a string and gives method to read it chunk by chunk"""
@@ -118,19 +118,19 @@
 			return l
 		except ParseError: #is it really necessary?
 			raise
-	def readMsgId(self,start,cs):
-		"""Read a msgId
+	def readMid(self,start,cs):
+		"""Read a mid
 		Raise ParseError if it takes more characters from the string that previously advertised"""
-		if(self.b + msgIdLength - start > cs):
+		if(self.b + midLength - start > cs):
 			raise ParseError("Bad Formed Command")
-		return self.next(msgIdLength)
-	def readMsgIdList(self,start,cs):
-		"""Read a list of msgId
+		return self.next(midLength)
+	def readMidList(self,start,cs):
+		"""Read a list of mid
 		Raise ParseError if it takes more characters from the string that previously advertised"""
 		try:
 			l = []
 			while(self.b < start + cs):
-				l.append(self.readMsgId(start,cs))
+				l.append(self.readMid(start,cs))
 			return l
 		except ParseError: #is it really necessary?
 			raise
@@ -227,7 +227,7 @@
 		"""Fill a Create Object from a list of nyms and a proof of work"""
 		if(len(l)>255):
 			raise BadArgument("Create.fromData : len(l) too big")
-		for i in range(l):
+		for i in range(len(l)):
 			if(len(l[i])>255):
 				raise BadArgument("Create.fromData : nym too long")
 		self.list=l
@@ -238,19 +238,19 @@
 		raise ParseError if it is malformed"""
 		start=sr.b
 		nNym = ord(sr.next(1))
-		self.pW = "" #what is a proof of work?
+		self.pw = "" #what is a proof of work?
 		try:
-			self.nymList = sr.readNymList(start,cs)
+			self.list = sr.readNymList(start,cs)
 		except ParseError:
 			raise ParseError("Bad Formed Command : Create")
 		#nNym is redundant, we use it to make sure the message is well formed
-		if(nNym != len(self.nymList)):
+		if(nNym != len(self.list)):
 		   raise ParseError("Bad Formed Command : Create")
 	def __str__(self):
 		"""Give the string of the Command that would be sent in a control message"""
 		s=chr(len(self.list))+self.pw
 		for i in range(len(self.list)):
-			s+chr(len(self.list[i]))+self.list[i]
+			s=s+chr(len(self.list[i]))+self.list[i]
 		if(len(s)>(256*256*256)): #should not be possible, otherwise, bug
 			raise BadArgument("Create.__str__ : command body too long")
 		return chr(0)+intToStrBE(len(s),3)+s
@@ -373,31 +373,31 @@
 			raise ParseError("Bad Formed Command : Relay")
 	def __str__(self):
 		"""Give the string of the Command that would be sent in a control message"""
-		
+		s=intToStrBE(self.rs,2)+self.rt+self.ri+self.body
 		if(len(s)>(256*256*256)): #should not be possible, otherwise bug
 			raise BadArgument("Relay.__str__ : Command Body too long")
 		return chr(4)+intToStrBE(len(s),3)+s
 	
 class Get(CommandCToS):
 	"""Get command
-	self.l : list of msgId (list of str of len msgIdLength)"""
+	self.l : list of mid (list of str of len midLength)"""
 	def __init__(self):
 		"""Build a Get empty object"""
 		pass
 	def fromData(self,li):
 		"""Fill a Get Object from a list of message id (=string of len 20)"""
 		#check that each element of the list has 20 bytes
-		if( not isMsgIdList(li)):
+		if( not isMidList(li)):
 			raise BadArgument("Get.fromData : li is not a list of message id")
 		self.l=li
-		if(msgIdLength*len(self.l)>256*256*256):
+		if(midLength*len(self.l)>256*256*256):
 			raise BadArgument("Get.fromData : Command body too long")
 	def fromStrReader(self,sr,cs):
 		"""Fill a Get Object from a StrReader
 		raise ParseError if it is malformed"""
 		start=sr.b
 		try:
-			self.l=sr.readMsgIdList(self,start,cs)
+			self.l=sr.readMidList(start,cs)
 		except (ParseError,IndexError):
 			raise ParseError("Bad Formed Command : Get")
 	def __str__(self):
@@ -406,13 +406,13 @@
 		for i in range(len(self.l)):
 			s=s+self.l[i]
 		if(len(s)>(256*256*256)): #should not be possible, otherwise bug
-			raise BadArgument("Create.__str__ : command body too long")
+			raise BadArgument("Get.__str__ : command body too long")
 		return chr(5)+intToStrBE(len(s),3)+s
 	
 class Summarize(CommandCToS):
 	"""Summarize command
 	self.num : maximum number of synopsis to retrieve (int)
-	self.after : msgId older than the synopsese retrieved"""
+	self.after : mid older than the synopsese retrieved"""
 	def __init__(self):
 		"""Build a Summarize empty object"""
 		pass
@@ -421,18 +421,18 @@
 		the int is considered modulo 256^2"""
 		n_mod=n % (256*256)
 		self.num=n_mod
-		if(len(m)!=msgIdLength): # or m is not a str
+		if(len(m)!=midLength): # or m is not a str
 			raise BadArgument(" Summarize.fromData : m is not a valid message ID")
 		self.after=m
 		#command body is small enough
 	def fromStrReader(self,sr,cs):
 		"""Fill a Summarize Object from a StrReader
 		raise ParseError if it is malformed"""
-		if(cs != 2+msgIdLength):
+		if(cs != 2+midLength):
 			raise ParseError("Bad Formed Command : Summarize")
 		try:
 			self.num=strToIntBE(sr.next(2))
-			self.after=sr.next(msgIdLength)
+			self.after=sr.next(midLength)
 		except IndexError:
 			raise ParseError("Bad Formed Command : Summarize")
 	def __str__(self):
@@ -444,24 +444,24 @@
 	
 class Delete(CommandCToS):
 	"""Delete command
-	self.l : list of msgId (list of str of len msgIdLength)"""
+	self.l : list of mid (list of str of len midLength)"""
 	def __init__(self):
 		"""Build a Delete empty object"""
 		pass
-	def fromData(self,):
-		"""Fill a Delete Object from a list of message ID (string of length msgIdLength"""
+	def fromData(self,li):
+		"""Fill a Delete Object from a list of message ID (string of length midLength"""
 		#check that each element of the list has 20 bytes
-		if( not isMsgIdList(li)):
+		if( not isMidList(li)):
 			raise BadArgument("Delete.fromData : li is not a list of message id")
 		self.l=li
-		if(msgIdLength*len(self.l)>256*256*256):
+		if(midLength*len(self.l)>256*256*256):
 			raise BadArgument("Delete.fromData : Command body too long")
 	def fromStrReader(self,sr,cs):
 		"""Fill a Delete Object from a StrReader
 		raise ParseError if it is malformed"""
 		start=sr.b
 		try:
-			self.l=sr.readMsgIdList(self,start,cs)
+			self.l=sr.readMidList(start,cs)
 		except (ParseError,IndexError):
 			raise ParseError("Bad Formed Command : Delete")
 	def __str__(self):
@@ -470,7 +470,7 @@
 		for i in range(len(self.l)):
 			s=s+self.l[i]
 		if(len(s)>(256*256*256)): #should not be possible, otherwise bug
-			raise BadArgument("Create.__str__ : command body too long")
+			raise BadArgument("Delete.__str__ : command body too long")
 		return chr(7)+intToStrBE(len(s),3)+s
 	
 class Policy(CommandCToS):
@@ -520,7 +520,111 @@
 		raise ParseError if it is malformed"""
 
 
-H= Header()
-H.fromData("JR","01234567890123456789")
-print H
-del H
+if (__name__ == '__main__'):
+	import Mail
+	l=[]
+	for i in range(3):
+		l.append(Mail.genMid(length=20))
+	H= Header()
+	H.fromData("JR","01234567890123456789")
+	print H
+	del H
+	print "test Create"
+	C=Create()
+	C.fromData(["Lolo_","Marsux","Azatoth_","Keeh"])
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Create2"
+	C=Create2()
+	C.fromData("azertyuiop42")
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Surb"
+	C=Surb()
+	C.fromData("-"*2104)
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Newpk"
+	C=Newpk()
+	C.fromData("1"*256,"2"*128)
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Relay"
+	C=Relay()
+	C.fromData("ab","too lazy to make real ones here","this is the body of the email. It sholud contain some kind of header, but for testing the parser, who cares?")
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Get"
+	C=Get()
+	C.fromData(l)
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Summarize"
+	C=Summarize()
+	C.fromData(10,l[0])
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Delete"
+	C=Delete()
+	C.fromData(l)
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)
+	print "test Policy"
+	C=Policy()
+	C.fromData("HoldUntilAck","never")
+	s1=C.__str__()
+	S=StrReader(s1)
+	D=S.readCommandCToS()
+	s2=D.__str__()
+	print (s1==s2)
+	del(C)
+	del(D)
+	del(S)

Modified: trunk/TODO
===================================================================
--- trunk/TODO	2004-07-28 22:54:36 UTC (rev 41)
+++ trunk/TODO	2004-07-28 23:10:41 UTC (rev 42)
@@ -14,4 +14,6 @@
    sent(message.py)
  - add the print method which prints the content of a comand object in a user
    friendly form(message.py)
- - test the parser(message.py)
+ . test the parser(message.py)
+ - do further testing once we start using the commands in the client/server
+ - add the CommandSToC(message.py)
\ No newline at end of file