/// <summary> /// Cleanups after a user project. /// </summary> private static void CleanUp(UserProjectSecurityChecker userProject, ProjectShim mainProjectShim) { String mainProject = String.Empty; if (userProject != null) { userProject.Dispose(); userProject = null; } if (mainProjectShim != null) { mainProject = mainProjectShim.FullFileName; mainProjectShim.ParentEngine.UnloadProject(mainProjectShim); mainProjectShim = null; } if (!String.IsNullOrEmpty(mainProject)) { try { File.Delete(mainProject); File.Delete(mainProject + ".user"); } catch (IOException) { } } }
/// <summary> /// Create a user security checker object that has its main project setup. /// </summary> internal static UserProjectSecurityChecker CreateUserProjectSecurityChecker(IServiceProvider sp, string fileContents, ProjectShim mainProjectShim) { string fileName = mainProjectShim.FullFileName + ".user"; File.WriteAllText(fileName, CleanupProjectXml(fileContents)); UserProjectSecurityChecker userProjectSecurityChecker = new UserProjectSecurityChecker(sp, fileName); PropertyInfo pi = userProjectSecurityChecker.GetType().GetProperty("MainProjectShim", BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(userProjectSecurityChecker, mainProjectShim, new object[] { }); return(userProjectSecurityChecker); }
public void TestImportOnUserProject() { UIThreadInvoker.Invoke((ThreadInvoker) delegate() { //Get the global service provider and the dte IServiceProvider sp = VsIdeTestHostContext.ServiceProvider; DTE dte = (DTE)sp.GetService(typeof(DTE)); string importedProjFilename = CreateTempFileOnDisk(@" <Project xmlns=`msbuildnamespace`> <PropertyGroup> <ReferencePath>c:\foobar</ReferencePath> </PropertyGroup> </Project> " ); string mainProject = CreateTempFileOnDisk(@" <Project DefaultTargets=`Build` xmlns=`msbuildnamespace`> <Import Project=`$(MSBuildBinPath)\Microsoft.CSharp.targets`/> </Project> " ); ProjectShim mainProjectShim = null; UserProjectSecurityChecker userProject = null; try { mainProjectShim = CreateProjectShim(mainProject); userProject = CreateUserProjectSecurityChecker(sp, String.Format(@" <Project DefaultTargets=`Build` xmlns=`msbuildnamespace`> <Import Project=`{0}`/> </Project> " , importedProjFilename), mainProjectShim); MethodInfo mi = userProject.GetType().GetMethod("IsProjectSafeWithImports", BindingFlags.Instance | BindingFlags.NonPublic); string[] message = new string[1] { String.Empty }; bool result = (bool)mi.Invoke(userProject, message); Assert.IsTrue(!result && !String.IsNullOrEmpty(message[0]), "A user project contained imports and was not considered unsafe."); } finally { TestProjectForSecurity.CleanUp(userProject, mainProjectShim); File.Delete(importedProjFilename); } }); }
public void TestMultipleFailuresOnUserProject() { UIThreadInvoker.Invoke((ThreadInvoker) delegate() { //Get the global service provider and the dte IServiceProvider sp = VsIdeTestHostContext.ServiceProvider; DTE dte = (DTE)sp.GetService(typeof(DTE)); string importedProjFilename = CreateTempFileOnDisk(@" <Project xmlns=`msbuildnamespace`> <PropertyGroup> <ReferencePath>c:\foobar</ReferencePath> </PropertyGroup> </Project> " ); string mainProject = CreateTempFileOnDisk(@" <Project DefaultTargets=`Build` xmlns=`msbuildnamespace`> <Import Project=`$(MSBuildBinPath)\Microsoft.CSharp.targets`/> </Project> " ); ProjectShim mainProjectShim = null; UserProjectSecurityChecker userProject = null; try { mainProjectShim = CreateProjectShim(mainProject); userProject = CreateUserProjectSecurityChecker(sp, String.Format(@" <Project DefaultTargets=`Build` xmlns=`msbuildnamespace`> <ItemGroup> <AppConfigFileDestination Include=`$(OutDir)$(TargetFileName).config`/> </ItemGroup> <Import Project=`{0}`/> </Project> " , importedProjFilename), mainProjectShim); string errorMessage; userProject.IsProjectSafeAtLoadTime(out errorMessage); Assert.IsTrue(errorMessage.Contains("1:") && errorMessage.Contains("2:"), "The error string returning from a project with multiple failures should contain the listed failures"); } finally { TestProjectForSecurity.CleanUp(userProject, mainProjectShim); File.Delete(importedProjFilename); } }); }
/// <summary> /// Cleanups after a user project. /// </summary> private static void CleanUp(UserProjectSecurityChecker userProject, ProjectShim mainProjectShim) { String mainProject = String.Empty; if(userProject != null) { userProject.Dispose(); userProject = null; } if(mainProjectShim != null) { mainProject = mainProjectShim.FullFileName; mainProjectShim.ParentEngine.UnloadProject(mainProjectShim); mainProjectShim = null; } if(!String.IsNullOrEmpty(mainProject)) { try { File.Delete(mainProject); File.Delete(mainProject + ".user"); } catch(IOException) { } } }
/// <summary> /// Create a user security checker object that has its main project setup. /// </summary> internal static UserProjectSecurityChecker CreateUserProjectSecurityChecker(IServiceProvider sp, string fileContents, ProjectShim mainProjectShim) { string fileName = mainProjectShim.FullFileName + ".user"; File.WriteAllText(fileName, CleanupProjectXml(fileContents)); UserProjectSecurityChecker userProjectSecurityChecker = new UserProjectSecurityChecker(sp, fileName); PropertyInfo pi = userProjectSecurityChecker.GetType().GetProperty("MainProjectShim", BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(userProjectSecurityChecker, mainProjectShim, new object[] { }); return userProjectSecurityChecker; }