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); } }); }