Skip to content

Commit 5644c4c

Browse files
committed
Create new test for Get-Credential which requires module to create a test host
1 parent d7b3124 commit 5644c4c

2 files changed

Lines changed: 303 additions & 0 deletions

File tree

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
$definition = @'
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Management.Automation;
6+
using System.Management.Automation.Host;
7+
using System.Globalization;
8+
using System.Collections.ObjectModel;
9+
using System.Security;
10+
using System.Collections;
11+
12+
namespace TestHost
13+
{
14+
public class TestHostRawUI : PSHostRawUserInterface
15+
{
16+
private ConsoleColor _backgroundColor = ConsoleColor.Black;
17+
public override ConsoleColor BackgroundColor
18+
{
19+
get { return _backgroundColor; }
20+
set { _backgroundColor = value; }
21+
}
22+
private ConsoleColor _foregroundColor = ConsoleColor.White;
23+
public override ConsoleColor ForegroundColor
24+
{
25+
get { return _foregroundColor; }
26+
set { _foregroundColor = value; }
27+
}
28+
private string _windowTitle = "title";
29+
public override string WindowTitle
30+
{
31+
get { return _windowTitle; }
32+
set { _windowTitle = value; }
33+
}
34+
private Size _bufferSize = new Size(10,10);
35+
public override Size BufferSize
36+
{
37+
get { return _bufferSize; }
38+
set { _bufferSize = value; }
39+
}
40+
private Coordinates _cursorPosition = new Coordinates(0, 0);
41+
public override Coordinates CursorPosition
42+
{
43+
get { return _cursorPosition; }
44+
set { _cursorPosition = value; }
45+
}
46+
private int _cursorSize = 10;
47+
public override int CursorSize
48+
{
49+
get { return _cursorSize; }
50+
set { _cursorSize = value; }
51+
}
52+
private bool _keyAvailable = false;
53+
public override bool KeyAvailable
54+
{
55+
get { return _keyAvailable; }
56+
}
57+
public override Size MaxPhysicalWindowSize
58+
{
59+
get { throw new NotImplementedException(); }
60+
}
61+
public override Size MaxWindowSize
62+
{
63+
get { throw new NotImplementedException(); }
64+
}
65+
private Coordinates _windowPosition = new Coordinates(0, 0);
66+
public override Coordinates WindowPosition
67+
{
68+
get { return _windowPosition; }
69+
70+
set { _windowPosition = value; }
71+
}
72+
private Size _windowSize = new Size(80, 40);
73+
public override Size WindowSize
74+
{
75+
get { return _windowSize; }
76+
set { _windowSize = value; }
77+
}
78+
public override BufferCell[,] GetBufferContents(Rectangle rectangle) { throw new NotImplementedException(); }
79+
public override void FlushInputBuffer() { throw new NotImplementedException(); }
80+
public override KeyInfo ReadKey(ReadKeyOptions options) { throw new NotImplementedException(); }
81+
public override void SetBufferContents(Coordinates origin, BufferCell[,] contents) { throw new NotImplementedException(); }
82+
public override void SetBufferContents(Rectangle rectangle, BufferCell fill) { throw new NotImplementedException(); }
83+
public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) { throw new NotImplementedException(); }
84+
}
85+
86+
public class Streams
87+
{
88+
public ArrayList ConsoleOutput = new ArrayList();
89+
public ArrayList Output = new ArrayList();
90+
public ArrayList Input = new ArrayList();
91+
public ArrayList Error = new ArrayList();
92+
public ArrayList Verbose = new ArrayList();
93+
public ArrayList Debug = new ArrayList();
94+
public ArrayList Warning = new ArrayList();
95+
public ArrayList Information = new ArrayList();
96+
public ArrayList Progress = new ArrayList();
97+
public ArrayList Prompt = new ArrayList();
98+
}
99+
public class TestPSHostUserInterface : PSHostUserInterface
100+
{
101+
private PSHostRawUserInterface _rawui = new TestHostRawUI();
102+
public string ReadLineData = "This is readline data";
103+
public int PromptedChoice = 0;
104+
public string StringForSecureString = "TEST";
105+
public string promptResponse = "this is a prompt response";
106+
107+
public Streams Streams = new Streams();
108+
public override PSHostRawUserInterface RawUI
109+
{
110+
get { return _rawui; }
111+
}
112+
113+
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
114+
{
115+
Streams.Prompt.Add(caption + ":" + message);
116+
Dictionary<string, PSObject> d = new Dictionary<string, PSObject>();
117+
d.Add(descriptions[0].ToString(), new PSObject(promptResponse));
118+
return d;
119+
}
120+
121+
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
122+
{
123+
Streams.Prompt.Add(caption + ":" + message);
124+
return PromptedChoice;
125+
}
126+
127+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
128+
{
129+
Streams.Prompt.Add("Credential:" + caption + ":" + message);
130+
SecureString ss = ReadLineAsSecureString();
131+
return new PSCredential(userName, ss);
132+
}
133+
134+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
135+
{
136+
Streams.Prompt.Add("Credential:" + caption + ":" + message);
137+
SecureString ss = ReadLineAsSecureString();
138+
return new PSCredential(userName, ss);
139+
}
140+
141+
public override string ReadLine()
142+
{
143+
return ReadLineData;
144+
}
145+
146+
public override SecureString ReadLineAsSecureString()
147+
{
148+
SecureString ss = new SecureString();
149+
foreach(char c in StringForSecureString.ToCharArray()) { ss.AppendChar(c); }
150+
return ss;
151+
}
152+
153+
public override void Write(string value)
154+
{
155+
Streams.ConsoleOutput.Add(value);
156+
}
157+
158+
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
159+
{
160+
Streams.ConsoleOutput.Add(value);
161+
}
162+
163+
public override void WriteDebugLine(string message)
164+
{
165+
Streams.Debug.Add(message);
166+
}
167+
168+
public override void WriteErrorLine(string value)
169+
{
170+
Streams.Error.Add(value);
171+
}
172+
173+
public override void WriteLine(string value)
174+
{
175+
Streams.ConsoleOutput.Add(value);
176+
}
177+
178+
public override void WriteProgress(long sourceId, ProgressRecord record)
179+
{
180+
Streams.Progress.Add(record);
181+
}
182+
183+
public override void WriteVerboseLine(string message)
184+
{
185+
Streams.Verbose.Add(message);
186+
}
187+
188+
public override void WriteWarningLine(string message)
189+
{
190+
Streams.Warning.Add(message);
191+
}
192+
}
193+
194+
public class TestHost : PSHost
195+
{
196+
private Guid _instanceId = Guid.NewGuid();
197+
private PSHostUserInterface _ui = new TestPSHostUserInterface();
198+
public string _hostName = "TEST HOST";
199+
public Version _version = new Version(6, 0);
200+
int promptLevel = 0;
201+
public override CultureInfo CurrentCulture
202+
{
203+
get { return Thread.CurrentThread.CurrentCulture; }
204+
}
205+
206+
public override CultureInfo CurrentUICulture
207+
{
208+
get { return Thread.CurrentThread.CurrentUICulture; }
209+
}
210+
211+
public override Guid InstanceId
212+
{
213+
get { return _instanceId; }
214+
}
215+
216+
public override string Name
217+
{
218+
get { return _hostName; }
219+
}
220+
221+
public override PSHostUserInterface UI
222+
{
223+
get { return _ui; }
224+
}
225+
226+
public override Version Version
227+
{
228+
get { return _version; }
229+
}
230+
231+
public override void EnterNestedPrompt()
232+
{
233+
promptLevel++;
234+
}
235+
236+
public override void ExitNestedPrompt()
237+
{
238+
promptLevel--;
239+
}
240+
241+
public override void NotifyBeginApplication()
242+
{
243+
throw new NotImplementedException();
244+
}
245+
246+
public override void NotifyEndApplication()
247+
{
248+
throw new NotImplementedException();
249+
}
250+
251+
public override void SetShouldExit(int exitCode)
252+
{
253+
throw new NotImplementedException();
254+
}
255+
}
256+
}
257+
258+
'@
259+
## '
260+
function New-TestHost
261+
{
262+
$references = "System","mscorlib","System.Management.Automation",
263+
"System.Collections.NonGeneric","System.Console","System.Collections",
264+
"System.Globalization"
265+
266+
if ( ! ("TestHost.TestHost" -as "type" )) {
267+
$t = add-Type -pass $definition -ref $references
268+
}
269+
270+
[TestHost.TestHost]::New()
271+
}
272+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
if ( ! (get-module -ea silentlycontinue TestHostCS ))
2+
{
3+
$root = git rev-parse --show-toplevel
4+
$pestertestroot = join-path $root test/powershell
5+
$common = join-path $pestertestroot Common
6+
$hostmodule = join-path $common TestHostCS.psm1
7+
import-module $hostmodule
8+
}
9+
Describe "Get-Credential Test" -tag "CI" {
10+
BeforeAll {
11+
$th = New-TestHost
12+
$th.UI.StringForSecureString = "This is a test"
13+
$rs = [runspacefactory]::Createrunspace($th)
14+
$rs.open()
15+
$ps = [powershell]::Create()
16+
$ps.Runspace = $rs
17+
$ps.Commands.Clear()
18+
}
19+
AfterAll {
20+
$rs.Close()
21+
$rs.Dispose()
22+
$ps.Dispose()
23+
}
24+
It "Get-Credential produces as credential object" {
25+
$cred = $ps.AddScript("Get-Credential -User Joe -Message Foo").Invoke() | Select-Object -First 1
26+
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
27+
$netcred = $cred.GetNetworkCredential()
28+
$netcred.UserName | Should be "Joe"
29+
$netcred.Password | Should be "this is a test"
30+
}
31+
}

0 commit comments

Comments
 (0)