CommandLineParserTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.IO;
  2. using NUnit.Framework;
  3. using Unity.RenderStreaming.Signaling;
  4. using Unity.WebRTC;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. namespace Unity.RenderStreaming.RuntimeTest
  8. {
  9. /// <note>
  10. /// Commandline parser doesn't support mobile platforms.
  11. /// </note>
  12. [UnityPlatform(exclude = new[] { RuntimePlatform.IPhonePlayer, RuntimePlatform.Android })]
  13. class CommandLineParserTest
  14. {
  15. [Test]
  16. public void NothingArgument()
  17. {
  18. string[] arguments = { };
  19. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  20. Assert.That((string)CommandLineParser.SignalingUrl, Is.Null);
  21. Assert.That((int?)CommandLineParser.PollingInterval, Is.Null);
  22. }
  23. [Test]
  24. public void SignalingUrlArgument()
  25. {
  26. const string signalingUrl = "localhost:8080";
  27. string[] arguments = new[] { "-signalingUrl", signalingUrl };
  28. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  29. Assert.That((string)CommandLineParser.SignalingUrl, Is.EqualTo(signalingUrl));
  30. }
  31. [Test]
  32. public void PollingIntervalArgument()
  33. {
  34. const int pollingInterval = 5000;
  35. string[] arguments = { "-pollingInterval", pollingInterval.ToString() };
  36. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  37. Assert.That((int)CommandLineParser.PollingInterval, Is.EqualTo(pollingInterval));
  38. }
  39. [Test]
  40. public void SignalingTypeArgument()
  41. {
  42. string signalingType = "websocket";
  43. string[] arguments = { "-signalingType", signalingType };
  44. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  45. Assert.That((string)CommandLineParser.SignalingType, Is.EqualTo(signalingType));
  46. signalingType = "dummy";
  47. arguments = new[] { "-signalingType", signalingType };
  48. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  49. Assert.That((string)CommandLineParser.SignalingType, Is.EqualTo(signalingType));
  50. }
  51. [Test]
  52. public void IceServerUrlArgument()
  53. {
  54. string[] iceServerUrls = { "stun:stun.l.google.com:19302", "stun:stun.l.google.com:19303" };
  55. string[] arguments = { "-iceServerUrl", iceServerUrls[0], "-iceServerUrl", iceServerUrls[1] };
  56. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  57. Assert.That(CommandLineParser.IceServerUrls.Value, Has.Length.EqualTo(2));
  58. Assert.That(CommandLineParser.IceServerUrls.Value[0], Is.EqualTo(iceServerUrls[0]));
  59. Assert.That(CommandLineParser.IceServerUrls.Value[1], Is.EqualTo(iceServerUrls[1]));
  60. }
  61. [Test]
  62. public void IceServerUserNameArgument()
  63. {
  64. string iceServerUsername = "username";
  65. string[] arguments = { "-iceServerUsername", iceServerUsername };
  66. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  67. Assert.That((string)CommandLineParser.IceServerUsername, Is.EqualTo(iceServerUsername));
  68. }
  69. [Test]
  70. public void IceServerCredentialArgument()
  71. {
  72. string iceServerCredential = "password";
  73. string[] arguments = { "-iceServerCredential", iceServerCredential };
  74. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  75. Assert.That((string)CommandLineParser.IceServerCredential, Is.EqualTo(iceServerCredential));
  76. }
  77. [Test]
  78. public void IceServerCredentialTypeArgument()
  79. {
  80. string iceServerCredentialType = "password";
  81. string[] arguments = { "-iceServerCredentialType", iceServerCredentialType };
  82. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  83. Assert.That((IceCredentialType)CommandLineParser.IceServerCredentialType, Is.EqualTo(IceCredentialType.Password));
  84. iceServerCredentialType = "oauth";
  85. arguments = new[] { "-iceServerCredentialType", iceServerCredentialType };
  86. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  87. Assert.That((IceCredentialType)CommandLineParser.IceServerCredentialType, Is.EqualTo(IceCredentialType.OAuth));
  88. iceServerCredentialType = "dummy";
  89. arguments = new[] { "-iceServerCredentialType", iceServerCredentialType };
  90. Assert.That(CommandLineParser.TryParse(arguments), Is.False);
  91. Assert.That(CommandLineParser.IceServerCredentialType.Value, Is.Null);
  92. }
  93. [Test]
  94. public void ImportJsonArgument()
  95. {
  96. string filepath = "dummy.json";
  97. var file = File.Create(filepath);
  98. file.Close();
  99. string[] arguments = { "-importJson", filepath };
  100. Assert.That(CommandLineParser.TryParse(arguments), Is.False);
  101. Assert.That(CommandLineParser.ImportJson.Value, Is.Null);
  102. string json = "{\"signalingType\":\"websocket\",\"signalingUrl\":\"ws://localhost\",\"pollingInterval\":\"1\"}";
  103. File.WriteAllText(filepath, json);
  104. Assert.That(CommandLineParser.TryParse(arguments), Is.True);
  105. Assert.That(CommandLineParser.ImportJson.Value, Is.Not.Null);
  106. var info = CommandLineParser.ImportJson.Value.Value;
  107. Assert.That(info.signalingUrl, Is.EqualTo("ws://localhost"));
  108. Assert.That(info.signalingType, Is.EqualTo("websocket"));
  109. Assert.That(info.iceServers, Is.Null);
  110. Assert.That(info.pollingInterval, Is.EqualTo("1"));
  111. File.Delete(filepath);
  112. }
  113. [Test]
  114. public void ParseJson()
  115. {
  116. string json = "{\"signalingType\":\"websocket\",\"signalingUrl\":\"ws://localhost\",\"pollingInterval\":\"1\"}";
  117. var settings = JsonUtility.FromJson<CommandLineInfo>(json);
  118. Assert.That(settings.signalingUrl, Is.EqualTo("ws://localhost"));
  119. Assert.That(settings.signalingType, Is.EqualTo("websocket"));
  120. Assert.That(settings.iceServers, Is.Null);
  121. Assert.That(settings.pollingInterval, Is.EqualTo("1"));
  122. string json2 = "{\"iceServers\":[{\"credential\":\"pass\",\"username\":\"user\",\"credentialType\":\"password\"," +
  123. "\"urls\":[\"turn:192.168.10.10:3478?transport=udp\"]}]}";
  124. settings = JsonUtility.FromJson<CommandLineInfo>(json2);
  125. Assert.That(settings.signalingUrl, Is.Null);
  126. Assert.That(settings.signalingType, Is.Null);
  127. Assert.That(settings.iceServers, Has.Length.EqualTo(1));
  128. Assert.That(settings.iceServers[0].credential, Is.EqualTo("pass"));
  129. Assert.That(settings.iceServers[0].credentialType, Is.EqualTo(RTCIceCredentialType.Password));
  130. Assert.That(settings.iceServers[0].username, Is.EqualTo("user"));
  131. Assert.That(settings.iceServers[0].urls, Has.Length.EqualTo(1));
  132. Assert.That(settings.iceServers[0].urls[0], Is.EqualTo("turn:192.168.10.10:3478?transport=udp"));
  133. Assert.That(settings.pollingInterval, Is.Null);
  134. }
  135. }
  136. }