scyther/gui/Scytherthread.py

349 lines
11 KiB
Python
Raw Normal View History

2006-08-02 13:59:57 +01:00
#!/usr/bin/python
#---------------------------------------------------------------------------
""" Import externals """
import wx
import wx.lib.newevent
import os
import sys
import re
import threading
import StringIO
#---------------------------------------------------------------------------
""" Import scyther components """
import XMLReader
""" Import scyther-gui components """
import Tempfile
import Claim
import Preference
import Scyther
2006-08-03 14:40:39 +01:00
import Attackwindow
2006-08-02 13:59:57 +01:00
#---------------------------------------------------------------------------
""" Global declaration """
(UpdateAttackEvent, EVT_UPDATE_ATTACK) = wx.lib.newevent.NewEvent()
busy = threading.Semaphore()
#---------------------------------------------------------------------------
class ScytherThread(threading.Thread):
# Override Thread's __init__ method to accept the parameters needed:
2006-08-03 13:06:43 +01:00
def __init__ ( self, mainwin, spdl, details, verifywin ):
2006-08-02 13:59:57 +01:00
2006-08-03 13:06:43 +01:00
self.mainwin = mainwin
self.verifywin = verifywin
2006-08-02 13:59:57 +01:00
self.spdl = spdl
self.details = details
self.claims = []
threading.Thread.__init__ ( self )
def run(self):
evt = UpdateAttackEvent(status="Running Scyther...")
2006-08-03 13:06:43 +01:00
wx.PostEvent(self.mainwin, evt)
2006-08-02 13:59:57 +01:00
self.claimResults()
# Results are done (claimstatus can be reported)
2006-08-02 13:59:57 +01:00
evt = UpdateAttackEvent(status="Done.")
2006-08-03 13:06:43 +01:00
wx.PostEvent(self.mainwin, evt)
2006-08-03 13:06:43 +01:00
# Shoot down the verification window and let the RunScyther function handle the rest
self.mainwin.verified = True
self.verifywin.Destroy()
2006-08-02 13:59:57 +01:00
def claimResults(self):
""" Convert spdl to result (using Scyther)
2006-08-03 13:06:43 +01:00
The list of claim goes back to self.mainwin.claims, which is a
property of the main window
"""
2006-08-02 13:59:57 +01:00
scyther = Scyther.Scyther()
2006-08-02 23:07:29 +01:00
2006-08-03 13:06:43 +01:00
scyther.options = self.mainwin.settings.ScytherArguments()
2006-08-02 13:59:57 +01:00
if sys.platform.startswith('win'):
scyther.program = "c:\\Scyther.exe"
if not os.path.isfile(scyther.program):
print "I can't find the Scyther executable %s" % (scyther.program)
scyther.setInput(self.spdl)
2006-08-03 13:06:43 +01:00
self.mainwin.claims = scyther.verify()
2006-08-02 13:59:57 +01:00
self.summary = str(scyther)
2006-08-03 13:06:43 +01:00
class AttackThread(threading.Thread):
# Override Thread's __init__ method to accept the parameters needed:
def __init__ ( self, mainwin, resultwin ):
self.mainwin = mainwin
self.resultwin = resultwin
threading.Thread.__init__ ( self )
def run(self):
evt = UpdateAttackEvent(status="Generating attack graphs...")
wx.PostEvent(self.mainwin, evt)
# create the images in the background
self.makeImages()
2006-08-02 13:59:57 +01:00
def makeImages(self):
""" create images """
2006-08-03 13:06:43 +01:00
for cl in self.mainwin.claims:
for attack in cl.attacks:
self.makeImage(attack)
2006-08-03 14:40:39 +01:00
if cl.button:
cl.button.Enable()
def makeImage(self,attack):
""" create image for this particular attack """
(fd2,fpname2) = Tempfile.tempcleaned(".png")
pw,pr = os.popen2("dot -Tpng -o%s" % (fpname2))
pw.write(attack.scytherDot)
pw.close()
2006-08-03 14:40:39 +01:00
pr.close()
attack.pngfile = fpname2 # this is where the file name is stored
2006-08-03 13:06:43 +01:00
class VerificationWindow(wx.Dialog):
def __init__(
self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
style=wx.DEFAULT_DIALOG_STYLE
):
# Instead of calling wx.Dialog.__init__ we precreate the dialog
# so we can set an extra style that must be set before
# creation, and then we create the GUI dialog using the Create
# method.
pre = wx.PreDialog()
pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
pre.Create(parent, ID, title, pos, size, style)
# This next step is the most important, it turns this Python
# object into the real wrapper of the dialog (instead of pre)
# as far as the wxPython extension is concerned.
self.PostCreate(pre)
# Now continue with the normal construction of the dialog
# contents
sizer = wx.BoxSizer(wx.VERTICAL)
2006-08-03 14:40:39 +01:00
label = wx.StaticText(self, -1, "Verifying protocol")
2006-08-03 13:06:43 +01:00
sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
btnsizer = wx.StdDialogButtonSizer()
if wx.Platform != "__WXMSW__":
btn = wx.ContextHelpButton(self)
btnsizer.AddButton(btn)
btn = wx.Button(self, wx.ID_OK)
btn.SetHelpText("The OK button completes the dialog")
btn.SetDefault()
btnsizer.AddButton(btn)
btn = wx.Button(self, wx.ID_CANCEL)
btn.SetHelpText("The Cancel button cnacels the dialog. (Cool, huh?)")
btnsizer.AddButton(btn)
btnsizer.Realize()
sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(sizer)
sizer.Fit(self)
#---------------------------------------------------------------------------
2006-08-03 14:40:39 +01:00
class ResultWindow(wx.Frame):
2006-08-03 13:06:43 +01:00
def __init__(
2006-08-03 14:40:39 +01:00
self, mainwindow, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
2006-08-03 13:06:43 +01:00
style=wx.DEFAULT_DIALOG_STYLE
):
2006-08-02 13:59:57 +01:00
2006-08-03 14:40:39 +01:00
self.mainwindow = mainwindow
2006-08-03 13:06:43 +01:00
# Instead of calling wx.Dialog.__init__ we precreate the dialog
# so we can set an extra style that must be set before
# creation, and then we create the GUI dialog using the Create
# method.
pre = wx.PreDialog()
2006-08-03 14:40:39 +01:00
pre.Create(mainwindow, ID, title, pos, size, style)
2006-08-03 13:06:43 +01:00
# This next step is the most important, it turns this Python
# object into the real wrapper of the dialog (instead of pre)
# as far as the wxPython extension is concerned.
self.PostCreate(pre)
# Now continue with the normal construction of the dialog
# contents
sizer = wx.BoxSizer(wx.VERTICAL)
2006-08-03 14:40:39 +01:00
# set up grid
claims = mainwindow.claims
self.grid = grid = wx.GridBagSizer(8,1+len(claims))
grid.Add(wx.StaticText(self,-1,"Protocol "),(0,0))
grid.Add(wx.StaticText(self,-1,"Role "),(0,1))
grid.Add(wx.StaticText(self,-1,"Label "),(0,2))
grid.Add(wx.StaticText(self,-1,"Claim type "),(0,3))
grid.Add(wx.StaticText(self,-1,"Parameter "),(0,4))
grid.Add(wx.StaticText(self,-1,"Status "),(0,5))
grid.Add(wx.StaticText(self,-1,"View "),(0,6))
lastprot = None
lastrole = None
for i in range(len(claims)-1,-1,-1):
2006-08-03 14:40:39 +01:00
cl = claims[i]
# we reverse the display order of the claims!
y = len(claims)-i
2006-08-03 14:40:39 +01:00
prot = str(cl.protocol)
if prot != lastprot:
grid.Add(wx.StaticText(self,-1,prot),(y,0))
2006-08-03 14:40:39 +01:00
lastprot = prot
role = str(cl.role)
if role != lastrole:
grid.Add(wx.StaticText(self,-1,role),(y,1))
2006-08-03 14:40:39 +01:00
lastrole = role
grid.Add(wx.StaticText(self,-1,str(cl.shortlabel)),(y,2))
grid.Add(wx.StaticText(self,-1,str(cl.claimtype)),(y,3))
grid.Add(wx.StaticText(self,-1,str(cl.parameter)),(y,4))
2006-08-03 14:40:39 +01:00
if cl.okay:
okay = "Ok"
else:
okay = "Fail"
grid.Add(wx.StaticText(self,-1,okay),(y,5))
2006-08-03 14:40:39 +01:00
# add view button (if needed)
n = len(cl.attacks)
if n > 0:
# Aha, something to show
blabel = "%i %s" % (n,cl.stateName(n))
cl.button = wx.Button(self,-1,blabel)
cl.button.Disable()
grid.Add(cl.button,(y,6))
2006-08-03 14:40:39 +01:00
self.Bind(wx.EVT_BUTTON, self.onViewButton,cl.button)
else:
cl.button = None
# remark something about completeness
remark = ""
if not cl.complete:
if n == 0:
# no attacks, no states within bounds
remark = "(within bounds)"
else:
# some attacks/states within bounds
remark = "(at least, maybe more)"
else:
if n == 0:
# no attacks, no states
remark = ""
else:
# there exist n states/attacks (within any number of runs)
remark = "(exactly)"
grid.Add(wx.StaticText(self,-1,remark),(y,7))
2006-08-03 14:40:39 +01:00
sizer.Add(grid, 0,wx.ALIGN_CENTRE|wx.ALL,5)
# separator
2006-08-03 13:06:43 +01:00
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
btnsizer = wx.StdDialogButtonSizer()
2006-08-03 14:40:39 +01:00
btn = wx.Button(self, wx.ID_OK)
2006-08-03 13:13:47 +01:00
btn.SetHelpText("Close window")
2006-08-03 13:06:43 +01:00
btn.SetDefault()
2006-08-03 14:40:39 +01:00
self.Bind(wx.EVT_BUTTON,self.onCloseButton,btn)
2006-08-03 13:06:43 +01:00
btnsizer.AddButton(btn)
2006-08-03 14:40:39 +01:00
btnsizer.Realize()
2006-08-03 13:06:43 +01:00
2006-08-03 14:40:39 +01:00
sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL|wx.ALIGN_RIGHT, 5)
2006-08-03 13:06:43 +01:00
self.SetSizer(sizer)
sizer.Fit(self)
2006-08-03 14:40:39 +01:00
def onViewButton(self,evt):
btn = evt.GetEventObject()
(y,x) = self.grid.GetItemPosition(btn)
n = len(self.mainwindow.claims)
cln = n-y
2006-08-03 14:40:39 +01:00
cl = self.mainwindow.claims[cln]
w = Attackwindow.AttackWindow(cl)
def onCloseButton(self,evt):
del(self.thread)
self.Destroy()
2006-08-03 13:06:43 +01:00
#---------------------------------------------------------------------------
def RunScyther(mainwin,mode):
2006-08-02 13:59:57 +01:00
global busy
if (busy.acquire(False)):
2006-08-03 13:06:43 +01:00
# Verification window
verifywin = VerificationWindow(mainwin,-1,mode)
verifywin.CenterOnScreen()
2006-08-02 13:59:57 +01:00
# start the thread
2006-08-03 13:06:43 +01:00
2006-08-03 14:40:39 +01:00
verifywin.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
2006-08-02 13:59:57 +01:00
2006-08-03 13:06:43 +01:00
mainwin.verified = False
mainwin.settings.mode = mode
t = ScytherThread(mainwin,mainwin.control.GetValue(),"",verifywin)
2006-08-02 13:59:57 +01:00
t.start()
2006-08-03 13:06:43 +01:00
# start the window and show until something happens
# if it terminates, this is a cancel, and should also kill the thread. (what happens to a spawned Scyther in that case?)
# if the thread terminames, it should close the window normally, and we end up here as well.
val = verifywin.ShowModal()
verifywin.Destroy()
# kill thread anyway
del(t)
# Cursor back to normal
2006-08-03 14:40:39 +01:00
verifywin.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
2006-08-03 13:06:43 +01:00
if mainwin.verified:
# Great, we verified stuff, progress to the claim report
2006-08-03 14:40:39 +01:00
title = "Scyther results : %s" % mode
resultwin = ResultWindow(mainwin,-1,title)
2006-08-03 13:06:43 +01:00
t = AttackThread(mainwin,resultwin)
t.start()
2006-08-03 14:40:39 +01:00
resultwin.thread = t
2006-08-03 13:06:43 +01:00
resultwin.CenterOnScreen()
2006-08-03 14:40:39 +01:00
resultwin.Show(1)
2006-08-03 13:06:43 +01:00
busy.release()
2006-08-02 13:59:57 +01:00