PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ServiceManagement/Services/Commands.Test/CloudService/Development/EnableAzureRemoteDesktopCommandTest.cs

https://gitlab.com/jslee1/azure-powershell
C# | 313 lines | 235 code | 28 blank | 50 comment | 11 complexity | 14e422915f900ce0e105f528ea4153aa MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Security;
  18. using Microsoft.WindowsAzure.Commands.CloudService.Development;
  19. using Microsoft.WindowsAzure.Commands.CloudService.Development.Scaffolding;
  20. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  21. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  22. using Microsoft.WindowsAzure.Commands.Utilities.CloudService;
  23. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  24. using Microsoft.WindowsAzure.Commands.Utilities.Common.XmlSchema.ServiceConfigurationSchema;
  25. using Microsoft.WindowsAzure.Commands.Utilities.Common.XmlSchema.ServiceDefinitionSchema;
  26. using Microsoft.WindowsAzure.Commands.Common;
  27. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  28. using Xunit;
  29. using Microsoft.Azure.Commands.Common.Authentication;
  30. namespace Microsoft.WindowsAzure.Commands.Test.CloudService.Development
  31. {
  32. /// <summary>
  33. /// Basic unit tests for the Enable-AzureServiceProjectRemoteDesktop enableRDCmdlet.
  34. /// </summary>
  35. public class EnableAzureRemoteDesktopCommandTest : SMTestBase
  36. {
  37. static private MockCommandRuntime mockCommandRuntime;
  38. static private EnableAzureServiceProjectRemoteDesktopCommand enableRDCmdlet;
  39. private AddAzureNodeWebRoleCommand addNodeWebCmdlet;
  40. private AddAzureNodeWorkerRoleCommand addNodeWorkerCmdlet;
  41. public EnableAzureRemoteDesktopCommandTest()
  42. {
  43. AzurePowerShell.ProfileDirectory = Test.Utilities.Common.Data.AzureSdkAppDir;
  44. mockCommandRuntime = new MockCommandRuntime();
  45. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand();
  46. addNodeWorkerCmdlet = new AddAzureNodeWorkerRoleCommand();
  47. enableRDCmdlet = new EnableAzureServiceProjectRemoteDesktopCommand();
  48. addNodeWorkerCmdlet.CommandRuntime = mockCommandRuntime;
  49. addNodeWebCmdlet.CommandRuntime = mockCommandRuntime;
  50. enableRDCmdlet.CommandRuntime = mockCommandRuntime;
  51. }
  52. /// <summary>
  53. /// Invoke the Enable-AzureServiceProjectRemoteDesktop enableRDCmdlet.
  54. /// </summary>
  55. /// <param name="username">Username.</param>
  56. /// <param name="password">Password.</param>
  57. public static void EnableRemoteDesktop(string username, string password)
  58. {
  59. SecureString securePassword = null;
  60. if (password != null)
  61. {
  62. securePassword = new SecureString();
  63. foreach (char ch in password)
  64. {
  65. securePassword.AppendChar(ch);
  66. }
  67. securePassword.MakeReadOnly();
  68. }
  69. if (enableRDCmdlet == null)
  70. {
  71. enableRDCmdlet = new EnableAzureServiceProjectRemoteDesktopCommand();
  72. if (mockCommandRuntime == null)
  73. {
  74. mockCommandRuntime = new MockCommandRuntime();
  75. }
  76. enableRDCmdlet.CommandRuntime = mockCommandRuntime;
  77. }
  78. enableRDCmdlet.Username = username;
  79. enableRDCmdlet.Password = securePassword;
  80. enableRDCmdlet.EnableRemoteDesktop();
  81. }
  82. public static void VerifyWebRole(WebRole role, bool isForwarder)
  83. {
  84. Assert.Equal(isForwarder ? 1 : 0, role.Imports.Where(i => i.moduleName == "RemoteForwarder").Count());
  85. Assert.Equal(1, role.Imports.Where(i => i.moduleName == "RemoteAccess").Count());
  86. }
  87. public static void VerifyWorkerRole(WorkerRole role, bool isForwarder)
  88. {
  89. Assert.Equal(isForwarder ? 1 : 0, role.Imports.Where(i => i.moduleName == "RemoteForwarder").Count());
  90. Assert.Equal(1, role.Imports.Where(i => i.moduleName == "RemoteAccess").Count());
  91. }
  92. public static void VerifyRoleSettings(CloudServiceProject service)
  93. {
  94. IEnumerable<RoleSettings> settings =
  95. Enumerable.Concat(
  96. service.Components.CloudConfig.Role,
  97. service.Components.LocalConfig.Role);
  98. foreach (RoleSettings roleSettings in settings)
  99. {
  100. Assert.Equal(
  101. 1,
  102. roleSettings
  103. .Certificates
  104. .Where(c => c.name == "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption")
  105. .Count());
  106. }
  107. }
  108. /// <summary>
  109. /// Perform basic parameter validation.
  110. /// </summary>
  111. [Fact]
  112. [Trait(Category.AcceptanceType, Category.CheckIn)]
  113. public void EnableRemoteDesktopBasicParameterValidation()
  114. {
  115. using (FileSystemHelper files = new FileSystemHelper(this))
  116. {
  117. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  118. files.CreateNewService("NEW_SERVICE");
  119. Testing.AssertThrows<ArgumentException>(
  120. () => EnableRemoteDesktop(null, null));
  121. Testing.AssertThrows<ArgumentException>(
  122. () => EnableRemoteDesktop(string.Empty, string.Empty));
  123. Testing.AssertThrows<ArgumentException>(
  124. () => EnableRemoteDesktop("user", null));
  125. Testing.AssertThrows<ArgumentException>(
  126. () => EnableRemoteDesktop("user", string.Empty));
  127. Testing.AssertThrows<ArgumentException>(
  128. () => EnableRemoteDesktop("user", "short"));
  129. Testing.AssertThrows<ArgumentException>(
  130. () => EnableRemoteDesktop("user", "onlylower"));
  131. Testing.AssertThrows<ArgumentException>(
  132. () => EnableRemoteDesktop("user", "ONLYUPPER"));
  133. Testing.AssertThrows<ArgumentException>(
  134. () => EnableRemoteDesktop("user", "1234567890"));
  135. }
  136. }
  137. /// <summary>
  138. /// Enable remote desktop for an empty service.
  139. /// </summary>
  140. [Fact]
  141. [Trait(Category.AcceptanceType, Category.CheckIn)]
  142. public void EnableRemoteDesktopForEmptyService()
  143. {
  144. using (FileSystemHelper files = new FileSystemHelper(this))
  145. {
  146. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  147. files.CreateNewService("NEW_SERVICE");
  148. Testing.AssertThrows<InvalidOperationException>(() =>
  149. EnableRemoteDesktop("user", "GoodPassword!"));
  150. }
  151. }
  152. /// <summary>
  153. /// Enable remote desktop for a simple web role.
  154. /// </summary>
  155. [Fact]
  156. [Trait(Category.AcceptanceType, Category.CheckIn)]
  157. public void EnableRemoteDesktopForWebRole()
  158. {
  159. using (FileSystemHelper files = new FileSystemHelper(this))
  160. {
  161. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  162. string rootPath = files.CreateNewService("NEW_SERVICE");
  163. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WebRole", Instances = 1 };
  164. addNodeWebCmdlet.ExecuteCmdlet();
  165. EnableRemoteDesktop("user", "GoodPassword!");
  166. // Verify the role has been setup with forwarding, access,
  167. // and certs
  168. CloudServiceProject service = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  169. VerifyWebRole(service.Components.Definition.WebRole[0], true);
  170. VerifyRoleSettings(service);
  171. }
  172. }
  173. /// <summary>
  174. /// Enable remote desktop for web and worker roles.
  175. /// </summary>
  176. [Fact]
  177. [Trait(Category.AcceptanceType, Category.CheckIn)]
  178. public void EnableRemoteDesktopForWebAndWorkerRoles()
  179. {
  180. using (FileSystemHelper files = new FileSystemHelper(this))
  181. {
  182. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  183. string rootPath = files.CreateNewService("NEW_SERVICE");
  184. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WebRole", Instances = 1 };
  185. addNodeWebCmdlet.ExecuteCmdlet();
  186. addNodeWorkerCmdlet = new AddAzureNodeWorkerRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WorkerRole", Instances = 1 };
  187. addNodeWorkerCmdlet.ExecuteCmdlet();
  188. mockCommandRuntime.ResetPipelines();
  189. EnableRemoteDesktop("user", "GoodPassword!");
  190. // Verify the roles have been setup with forwarding, access,
  191. // and certs
  192. CloudServiceProject service = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  193. VerifyWebRole(service.Components.Definition.WebRole[0], false);
  194. VerifyWorkerRole(service.Components.Definition.WorkerRole[0], true);
  195. VerifyRoleSettings(service);
  196. Assert.Equal<int>(0, mockCommandRuntime.OutputPipeline.Count);
  197. }
  198. }
  199. /// <summary>
  200. /// Enable remote desktop for multiple web and worker roles.
  201. /// </summary>
  202. [Fact]
  203. [Trait(Category.AcceptanceType, Category.CheckIn)]
  204. public void EnableRemoteDesktopForMultipleWebAndWorkerRolesTwice()
  205. {
  206. using (FileSystemHelper files = new FileSystemHelper(this))
  207. {
  208. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  209. string rootPath = files.CreateNewService("NEW_SERVICE");
  210. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WebRole_1", Instances = 1 };
  211. addNodeWebCmdlet.ExecuteCmdlet();
  212. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WebRole_2", Instances = 1 };
  213. addNodeWebCmdlet.ExecuteCmdlet();
  214. addNodeWorkerCmdlet = new AddAzureNodeWorkerRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WorkerRole_1", Instances = 1 };
  215. addNodeWorkerCmdlet.ExecuteCmdlet();
  216. addNodeWorkerCmdlet = new AddAzureNodeWorkerRoleCommand() { RootPath = rootPath, CommandRuntime = mockCommandRuntime, Name = "WorkerRole_2", Instances = 1 };
  217. addNodeWorkerCmdlet.ExecuteCmdlet();
  218. mockCommandRuntime.ResetPipelines();
  219. enableRDCmdlet.PassThru = true;
  220. EnableRemoteDesktop("user", "GoodPassword!");
  221. enableRDCmdlet.PassThru = false;
  222. EnableRemoteDesktop("other", "OtherPassword!");
  223. // Verify the roles have been setup with forwarding, access,
  224. // and certs
  225. CloudServiceProject service = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  226. VerifyWebRole(service.Components.Definition.WebRole[0], false);
  227. VerifyWebRole(service.Components.Definition.WebRole[0], false);
  228. VerifyWorkerRole(service.Components.Definition.WorkerRole[0], true);
  229. VerifyWorkerRole(service.Components.Definition.WorkerRole[1], false);
  230. VerifyRoleSettings(service);
  231. Assert.Equal<int>(1, mockCommandRuntime.OutputPipeline.Count);
  232. Assert.True((bool)mockCommandRuntime.OutputPipeline[0]);
  233. }
  234. }
  235. /// <summary>
  236. /// Enable remote desktop for a simple web role.
  237. /// </summary>
  238. [Fact]
  239. [Trait(Category.AcceptanceType, Category.CheckIn)]
  240. public void EnableRemoteDesktopUnicode()
  241. {
  242. using (FileSystemHelper files = new FileSystemHelper(this))
  243. {
  244. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  245. string rootPath = files.CreateNewService("NEW_SERVICE");
  246. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand()
  247. {
  248. RootPath = rootPath,
  249. CommandRuntime = mockCommandRuntime,
  250. Name = "WebRole",
  251. Instances = 1
  252. };
  253. addNodeWebCmdlet.ExecuteCmdlet();
  254. EnableRemoteDesktop("㯑䲘䄂㮉", "㯑䲘䄂㮉㮉㮉㮉L");
  255. // Verify the role has been setup with forwarding, access,
  256. // and certs
  257. CloudServiceProject service = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  258. VerifyWebRole(service.Components.Definition.WebRole[0], true);
  259. VerifyRoleSettings(service);
  260. }
  261. }
  262. /// <summary>
  263. /// Enable remote desktop using short unicode password.
  264. /// </summary>
  265. [Fact]
  266. [Trait(Category.AcceptanceType, Category.CheckIn)]
  267. public void EnableRemoteDesktopUnicodeAndShortPasswordFails()
  268. {
  269. using (FileSystemHelper files = new FileSystemHelper(this))
  270. {
  271. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  272. string rootPath = files.CreateNewService("NEW_SERVICE");
  273. addNodeWebCmdlet = new AddAzureNodeWebRoleCommand()
  274. {
  275. RootPath = rootPath,
  276. CommandRuntime = mockCommandRuntime,
  277. Name = "WebRole",
  278. Instances = 1
  279. };
  280. addNodeWebCmdlet.ExecuteCmdlet();
  281. Testing.AssertThrows<ArgumentException>(() => EnableRemoteDesktop("㯑䲘䄂㮉", "㯑䲘"));
  282. }
  283. }
  284. }
  285. }