End2End Automated Testing: Setting Network Permissions Automatically

When setting network permissions, you are going to want to use a random permission name that you can clean up once the test is complete.  The "-q" in the call for CasPol is very important, because it quietly and automatically sets the permission. You can implement the basic code for starting a process using the System.Diagnostics .Net library.
 
Private Const FRAMEWORK_DIRECTORY As String = "Microsoft.NET\Framework\v2.0.50727"
 
Public Sub AssignNetworkPermission(ByVal remoteDirectory As String, ByVal permissionName As String)
    RemoveNetworkPermision(permissionName)
 
    Dim processInfo As New ProcessStartInfo(String.Format("{0}\..\{1}\caspol.exe", _
            Environment.SystemDirectory, FRAMEWORK_DIRECTORY), _
            String.Format(" -q -machine -addgroup LocalIntranet_Zone -url file:""{0}\*"" FullTrust -name ""{1}""", _
            remoteDirectory, permissionName) _
            )
    processInfo.UseShellExecute = False
    processInfo.RedirectStandardOutput = True
    processInfo.CreateNoWindow = False
 
    RunProcess(processInfo, True)
End Sub
 
Public Sub RemoveNetworkPermision(ByVal permissionName As String)
    Dim permissionProcess As New ProcessStartInfo(String.Format("{0}\..\{1}\caspol.exe", _
            Environment.SystemDirectory, FRAMEWORK_DIRECTORY), _
            String.Format(" -q -machine -remgroup ""{0}""", permissionName) _
            )
    permissionProcess.UseShellExecute = False
    permissionProcess.RedirectStandardOutput = True
    permissionProcess.CreateNoWindow = False
 
    RunProcess(permissionProcess, True)
End Sub
 
Private Sub RunProcess(ByVal processInfo As ProcessStartInfo, ByVal waitForExit As Boolean)
    Using process As New Process()
        process.StartInfo = processInfo
        process.Start()
 
        If (waitForExit) Then
            process.WaitForExit()
        End If
    End Using
End Sub

posted @ Monday, June 23, 2008 8:41 AM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
Please add 4 and 3 and type the answer here:
 

Live Comment Preview:

 
«August»
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456