This commit is contained in:
Jimmy Campbell 2017-06-21 12:40:15 -07:00
Родитель 9980790499
Коммит bc7e261b5b
5 изменённых файлов: 154 добавлений и 36 удалений

Просмотреть файл

@ -1,22 +1,35 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
Param(
[switch]
$ConfigureTestEnvironment
)
#Requires -RunAsAdministrator
#Requires -Version 4.0
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
function SetEnvironmentVariables {
if ($env:iis_admin_solution_dir -eq $null) {
setx iis_admin_solution_dir $((Resolve-Path (Join-Path $scriptDir "..")).Path) /m
}
}
try {
Push-Location $scriptDir
Write-Host "Setting environment variables."
SetEnvironmentVariables
if ($env:iis_admin_solution_dir -eq $null) {
$env:iis_admin_solution_dir = $((Resolve-Path (Join-Path $scriptDir "..")).Path)
setx iis_admin_solution_dir $env:iis_admin_solution_dir /m
Write-Verbose "iis_admin_solution_dir $env:iis_admin_solution_dir"
}
if ($env:iis_admin_test_dir -eq $null -and $ConfigureTestEnvironment) {
$env:iis_admin_test_dir = $([System.IO.Path]::Combine("$env:SystemDrive\", "tests\iisadmin"))
setx iis_admin_test_dir $env:iis_admin_test_dir /m
Write-Verbose "iis_admin_test_dir $env:iis_admin_test_dir"
}
if ($ConfigureTestEnvironment) {
Write-Host "Configuring test environment"
.\tests\Create-CcsInfrastructure.ps1
}
}
finally {
Pop-Location

Просмотреть файл

@ -20,7 +20,13 @@ function New-CcsSelfSignedCertificate($certName) {
$cert
}
$ccsPath = [System.IO.Path]::Combine($env:iis_admin_solution_dir, "test", $CCS_FOLDER_NAME)
$testRoot = $env:iis_admin_test_dir
if ([string]::IsNullOrEmpty($testRoot)) {
$testRoot = "$env:SystemDrive\tests\iisadmin"
}
$ccsPath = [System.IO.Path]::Combine($testRoot, $CCS_FOLDER_NAME)
if (-not(Test-Path $ccsPath)) {
New-Item -Type Directory -Path $ccsPath -ErrorAction Stop | Out-Null

Просмотреть файл

@ -15,16 +15,16 @@ namespace Microsoft.IIS.Administration.Tests
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
public class CentralCertificates
{
private static readonly string CERTIFICATES_API_PATH = $"{Configuration.TEST_SERVER_URL}/api/certificates";
private static readonly string STORES_API_PATH = $"{Configuration.TEST_SERVER_URL}/api/certificates/stores";
private static readonly string FOLDER_PATH = Path.Combine(Environment.ExpandEnvironmentVariables("%iis_admin_solution_dir%"), "test", FOLDER_NAME);
private static readonly string FOLDER_PATH = Path.Combine(Configuration.TEST_ROOT_PATH, FOLDER_NAME);
private const string NAME = "IIS Central Certificate Store";
private const string FOLDER_NAME = "CentralCertStore";
private const string USER_NAME = "";
private const string USER_PASS = "";
private const string CERT_NAME = "IISAdminLocalTest";
private const string PVK_PASS = "abcdefg";
private ITestOutputHelper _output;
@ -34,27 +34,37 @@ namespace Microsoft.IIS.Administration.Tests
_output = output;
}
[Fact]
public void CanEnable()
{
RequireCcsTestInfrastructure();
Assert.True(Disable());
Assert.True(Enable(FOLDER_PATH, USER_NAME, USER_PASS, PVK_PASS));
public static string CcsTestUsername {
get {
return Configuration.Raw.Value<string>("ccs_user") ?? "IisAdminCcsTestR";
}
}
[Fact]
public void PathMustBeAllowed()
public async Task CanEnable()
{
RequireCcsTestInfrastructure();
CcsUser user = await CcsUser.Get();
Assert.True(Disable());
Assert.True(Enable(FOLDER_PATH, user.Username, user.Password, PVK_PASS));
}
[Fact]
public async Task PathMustBeAllowed()
{
RequireCcsTestInfrastructure();
const string path = @"C:\Not\Allowed\Path";
Assert.True(Disable());
CcsUser user = await CcsUser.Get();
dynamic ccsInfo = new {
path = path,
identity = new {
username = USER_NAME,
password = USER_PASS
username = user.Username,
password = user.Password
},
private_key_password = PVK_PASS
};
@ -76,7 +86,7 @@ namespace Microsoft.IIS.Administration.Tests
dynamic ccsInfo = new {
path = FOLDER_PATH,
identity = new {
username = USER_NAME,
username = CcsTestUsername,
password = "fgsfds"
},
private_key_password = PVK_PASS
@ -94,22 +104,26 @@ namespace Microsoft.IIS.Administration.Tests
}
[Fact]
public void DynamicallyAddsToStores()
public async Task DynamicallyAddsToStores()
{
RequireCcsTestInfrastructure();
CcsUser user = await CcsUser.Get();
Assert.True(Disable());
Assert.False(GetStores().Any(store => store.Value<string>("name").Equals(NAME, StringComparison.OrdinalIgnoreCase)));
Assert.True(Enable(FOLDER_PATH, USER_NAME, USER_PASS, PVK_PASS));
Assert.True(Enable(FOLDER_PATH, user.Username, user.Password, PVK_PASS));
Assert.True(GetStores().Any(store => store.Value<string>("name").Equals(NAME, StringComparison.OrdinalIgnoreCase)));
Assert.True(Disable());
Assert.False(GetStores().Any(store => store.Value<string>("name").Equals(NAME, StringComparison.OrdinalIgnoreCase)));
}
[Fact]
public void CcsCertificatesShown()
public async Task CcsCertificatesShown()
{
RequireCcsTestInfrastructure();
Assert.True(Enable(FOLDER_PATH, USER_NAME, USER_PASS, PVK_PASS));
CcsUser user = await CcsUser.Get();
Assert.True(Enable(FOLDER_PATH, user.Username, user.Password, PVK_PASS));
Assert.True(GetCertificates().Any(cert => {
return cert.Value<string>("alias").Equals(CERT_NAME + ".pfx") &&
cert.Value<JObject>("store").Value<string>("name").Equals(NAME, StringComparison.OrdinalIgnoreCase);
@ -117,10 +131,12 @@ namespace Microsoft.IIS.Administration.Tests
}
[Fact]
public void CanCreateCcsBinding()
public async Task CanCreateCcsBinding()
{
RequireCcsTestInfrastructure();
Assert.True(Enable(FOLDER_PATH, USER_NAME, USER_PASS, PVK_PASS));
CcsUser user = await CcsUser.Get();
Assert.True(Enable(FOLDER_PATH, user.Username, user.Password, PVK_PASS));
JObject site;
const string siteName = "CcsBindingTestSite";
@ -236,11 +252,6 @@ namespace Microsoft.IIS.Administration.Tests
_output.WriteLine("Ccs test certificates not found");
throw new Exception();
}
if (!LocalUserExists(USER_NAME, USER_PASS)) {
_output.WriteLine("Ccs test user not found");
throw new Exception();
}
}
private static bool LocalUserExists(string username, string password)
@ -274,6 +285,9 @@ namespace Microsoft.IIS.Administration.Tests
token.Dispose();
return loggedOn;
}
}
class Interop
@ -297,4 +311,76 @@ namespace Microsoft.IIS.Administration.Tests
IntPtr pdwProfileLength,
IntPtr pQuotaLimits);
}
class CcsUser
{
private CcsUser() { }
public string Username { get; set; }
public string Password { get; set; }
public static async Task<CcsUser> Get()
{
var user = new CcsUser();
user.Username = CentralCertificates.CcsTestUsername;
user.Password = Guid.NewGuid().ToString();
try {
await GetLocalUser(user.Username);
await RemoveLocalUser(user.Username);
}
catch {
}
await CreateLocalUser(user.Username, user.Password);
return user;
}
public void GetAccess(string path)
{
}
private static Task GetLocalUser(string username)
{
return RunProcess("PowerShell.exe", $"Get-LocalUser -Name {username}");
}
private static Task CreateLocalUser(string username, string password)
{
return RunProcess("PowerShell.exe", $"New-LocalUser -Name {username} -Password $(ConvertTo-SecureString -AsPlainText -Force '{password}')");
}
private static Task RemoveLocalUser(string username)
{
return RunProcess("PowerShell.exe", $"Remove-LocalUser -Name {username}");
}
private static Task RunProcess(string tool, string arguments)
{
ProcessStartInfo info = new ProcessStartInfo(tool, arguments);
Process p = new Process()
{
StartInfo = info,
EnableRaisingEvents = true
};
var tcs = new TaskCompletionSource<int>();
p.Exited += (sender, args) => {
if (p.ExitCode != 0) {
tcs.SetException(new Exception($"Process exited with an error: {p.ExitCode}"));
}
else {
tcs.SetResult(p.ExitCode);
}
p.Dispose();
};
p.Start();
return tcs.Task;
}
}
}

Просмотреть файл

@ -4,6 +4,7 @@
namespace Microsoft.IIS.Administration.Tests {
using Newtonsoft.Json.Linq;
using System;
using System.IO;
public static class Configuration {
@ -18,30 +19,41 @@ namespace Microsoft.IIS.Administration.Tests {
return _config.Value<string>("test_server");
}
}
public static string TEST_PORT {
get {
return _config.Value<string>("test_port");
}
}
public static string TEST_SERVER_URL {
get {
return $"{TEST_SERVER}:{TEST_PORT}";
}
}
public static string TEST_ROOT_PATH {
get {
var val = _config.Value<string>("test_root_path");
if (string.IsNullOrEmpty(val)) {
val = System.AppContext.BaseDirectory;
val = AppContext.BaseDirectory;
}
return val;
return Environment.ExpandEnvironmentVariables(val);
}
}
public static JObject Raw {
get {
return _config;
}
}
private static void Initialize() {
var content = File.ReadAllText(Path.Combine(System.AppContext.BaseDirectory, "test.config.json"));
var content = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "test.config.json"));
_config = JObject.Parse(content);
}
}

Просмотреть файл

@ -1,5 +1,6 @@
{
"ccs_user": "IisAdminCcsTestR",
"test_server": "https://localhost",
"test_port": "44326",
"test_root_path": ""
"test_root_path": "%iis_admin_test_dir%"
}