PageRenderTime 66ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/java/server/tags/1_0_2_RC1/src/test/java/org/red5/io/AMF3IOTest.java

https://github.com/mondain/red5
Java | 230 lines | 164 code | 23 blank | 43 comment | 7 complexity | 34d16940a36dba4526db5a45e5bfd97a MD5 | raw file
  1. /*
  2. * RED5 Open Source Flash Server - http://code.google.com/p/red5/
  3. *
  4. * Copyright 2006-2013 by respective authors (see below). All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.red5.io;
  19. import java.util.List;
  20. import java.util.Vector;
  21. import junit.framework.Assert;
  22. import org.apache.mina.core.buffer.IoBuffer;
  23. import org.junit.Test;
  24. import org.red5.io.amf3.ByteArray;
  25. import org.red5.io.amf3.Input;
  26. import org.red5.io.amf3.Output;
  27. import org.red5.io.object.Deserializer;
  28. import org.red5.io.object.Serializer;
  29. import org.red5.io.utils.HexDump;
  30. import org.red5.server.net.rtmp.message.StreamAction;
  31. /*
  32. * @author The Red5 Project
  33. * @author Luke Hubbard, Codegent Ltd (luke@codegent.com)
  34. * @author Art Clarke, Vlideshow Inc (aclarke@vlideshow.com)
  35. */
  36. public class AMF3IOTest extends AbstractIOTest {
  37. IoBuffer buf;
  38. /** {@inheritDoc} */
  39. @Override
  40. void dumpOutput() {
  41. buf.flip();
  42. System.err.println(HexDump.formatHexDump(buf.getHexDump()));
  43. }
  44. /** {@inheritDoc} */
  45. @Override
  46. void resetOutput() {
  47. setupIO();
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. void setupIO() {
  52. buf = IoBuffer.allocate(0); // 1kb
  53. buf.setAutoExpand(true);
  54. buf.setAutoShrink(true);
  55. in = new Input(buf);
  56. out = new Output(buf);
  57. }
  58. @Test
  59. public void testEnum() {
  60. log.debug("Testing Enum");
  61. Serializer.serialize(out, StreamAction.CONNECT);
  62. dumpOutput();
  63. Object object = Deserializer.deserialize(in, StreamAction.class);
  64. log.debug("Enums - {} {}", object.getClass().getName(), StreamAction.CONNECT.getClass().getName());
  65. Assert.assertEquals(object.getClass().getName(), StreamAction.CONNECT.getClass().getName());
  66. resetOutput();
  67. }
  68. @Test
  69. public void testByteArray() {
  70. log.debug("Testing ByteArray");
  71. // just some ones and such
  72. ByteArray baIn = new ByteArray();
  73. baIn.writeBytes(new byte[] { (byte) 0, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42,
  74. (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x99 });
  75. Serializer.serialize(out, baIn);
  76. dumpOutput();
  77. ByteArray baOut = Deserializer.deserialize(in, ByteArray.class);
  78. Assert.assertNotNull(baOut);
  79. Assert.assertEquals(baIn.length(), baOut.length());
  80. for (int i = 0; i < baOut.length(); i++) {
  81. System.err.println("Byte: " + baOut.readByte());
  82. }
  83. resetOutput();
  84. }
  85. @Test
  86. public void testVectorRoundTrip() {
  87. log.debug("Testing Vector on a round trip");
  88. Vector<String> vIn = new Vector<String>();
  89. vIn.add("This is my vector and her name is Sally");
  90. Serializer.serialize(out, vIn);
  91. dumpOutput();
  92. // test fails without enforcing amf3 here
  93. ((org.red5.io.amf3.Input) in).enforceAMF3();
  94. Vector<String> vOut = Deserializer.deserialize(in, Vector.class);
  95. Assert.assertNotNull(vOut);
  96. Assert.assertEquals(vIn.size(), vOut.size());
  97. for (int i = 0; i < vOut.size(); i++) {
  98. System.err.println("Element: " + vOut.elementAt(i));
  99. }
  100. resetOutput();
  101. }
  102. @Test
  103. public void testVectorIntInput() {
  104. log.debug("Testing Vector<int>");
  105. //0D090000000002000007D07FFFFFFF80000000
  106. byte[] v = new byte[] { (byte) 0x0D, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0xD0,
  107. (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
  108. in = new Input(IoBuffer.wrap(v));
  109. List<Object> vectorOut = Deserializer.deserialize(in, null);
  110. //[2, 2000, 2147483647, -2147483648]
  111. Assert.assertNotNull(vectorOut);
  112. Assert.assertEquals(vectorOut.size(), 4);
  113. for (int i = 0; i < vectorOut.size(); i++) {
  114. System.err.println("Vector: " + vectorOut.get(i));
  115. }
  116. resetOutput();
  117. }
  118. @Test
  119. public void testVectorUIntInput() {
  120. log.debug("Testing Vector<uint>");
  121. //0E090000000002000007D0FFFFFFFF00000000
  122. byte[] v = new byte[] { (byte) 0x0E, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0xD0,
  123. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
  124. in = new Input(IoBuffer.wrap(v));
  125. List<Object> vectorOut = Deserializer.deserialize(in, null);
  126. //[2, 2000, 4294967295, 0]
  127. Assert.assertNotNull(vectorOut);
  128. Assert.assertEquals(vectorOut.size(), 4);
  129. for (int i = 0; i < vectorOut.size(); i++) {
  130. System.err.println("Vector: " + vectorOut.get(i));
  131. }
  132. resetOutput();
  133. }
  134. @Test
  135. public void testVectorNumberInput() {
  136. log.debug("Testing Vector<Number>");
  137. //0F0F003FF199999999999ABFF199999999999A7FEFFFFFFFFFFFFF0000000000000001FFF8000000000000FFF00000000000007FF0000000000000
  138. byte[] v = new byte[] { (byte) 0x0F, (byte) 0x0F, (byte) 0x00, (byte) 0x3F, (byte) 0xF1, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x9A,
  139. (byte) 0xBF, (byte) 0xF1, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x9A, (byte) 0x7F, (byte) 0xEF, (byte) 0xFF, (byte) 0xFF,
  140. (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
  141. (byte) 0xFF, (byte) 0xF8, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xFF, (byte) 0xF0, (byte) 0x00, (byte) 0x00,
  142. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x7F, (byte) 0xF0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
  143. in = new Input(IoBuffer.wrap(v));
  144. ((org.red5.io.amf3.Input) in).enforceAMF3();
  145. List<Double> vectorOut = Deserializer.deserialize(in, null);
  146. //[1.1, -1.1, 1.7976931348623157E308, 4.9E-324, NaN, -Infinity, Infinity]
  147. Assert.assertNotNull(vectorOut);
  148. Assert.assertEquals(vectorOut.size(), 7);
  149. for (int i = 0; i < vectorOut.size(); i++) {
  150. System.err.println("Vector: " + vectorOut.get(i));
  151. }
  152. resetOutput();
  153. }
  154. @Test
  155. public void testVectorMixedInput() {
  156. log.debug("Testing Vector<Object>");
  157. //100700010607666f6f010a13256f72672e726564352e74673742e466f6f33000403
  158. //[foo, null, org.red5.test.Foo3[foo=0]] // Foo3 is a class instance
  159. byte[] v2 = new byte[] { (byte) 0x10, (byte) 0x07, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x07, (byte) 0x66, (byte) 0x6f, (byte) 0x6f, (byte) 0x01, (byte) 0x0a,
  160. (byte) 0x13, (byte) 0x25, (byte) 0x6f, (byte) 0x72, (byte) 0x67, (byte) 0x2e, (byte) 0x72, (byte) 0x65, (byte) 0x64, (byte) 0x35, (byte) 0x2e, (byte) 0x74,
  161. (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x2e, (byte) 0x46, (byte) 0x6f, (byte) 0x6f, (byte) 0x33, (byte) 0x00, (byte) 0x04, (byte) 0x03 };
  162. in = new Input(IoBuffer.wrap(v2));
  163. ((org.red5.io.amf3.Input) in).enforceAMF3();
  164. List<Object> vectorOut = Deserializer.deserialize(in, null);
  165. Assert.assertNotNull(vectorOut);
  166. Assert.assertEquals(vectorOut.size(), 3);
  167. for (int i = 0; i < vectorOut.size(); i++) {
  168. System.err.println("Vector: " + vectorOut.get(i));
  169. }
  170. resetOutput();
  171. }
  172. @SuppressWarnings("unused")
  173. @Test
  174. public void testVectorStringInput() {
  175. log.debug("Testing Vector<String>");
  176. //[Paul, ]
  177. byte[] v = new byte[] { (byte) 0x10, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06,
  178. (byte) 0x01 };
  179. //[Paul, Paul]
  180. byte[] v1 = new byte[] { (byte) 0x10, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06,
  181. (byte) 0x00 };
  182. //[Paul, Paul, Paul]
  183. byte[] v2 = new byte[] { (byte) 0x10, (byte) 0x07, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06,
  184. (byte) 0x00, (byte) 0x06, (byte) 0x00 };
  185. //[Paul, Tawnya]
  186. byte[] v3 = new byte[] { (byte) 0x10, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06,
  187. (byte) 0x0d, (byte) 0x54, (byte) 0x61, (byte) 0x77, (byte) 0x6e, (byte) 0x79, (byte) 0x61 };
  188. //[1.0, 3.0, aaa, 5.0, aaa, aaa, 5.0, bb, bb]
  189. byte[] v4 = new byte[] { (byte) 0x10, (byte) 0x13, (byte) 0x00, (byte) 0x01, (byte) 0x04, (byte) 0x01, (byte) 0x04, (byte) 0x03, (byte) 0x06, (byte) 0x07, (byte) 0x61,
  190. (byte) 0x61, (byte) 0x61, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x05,
  191. (byte) 0x62, (byte) 0x62, (byte) 0x06, (byte) 0x02 };
  192. //[1.0, 3.0, aaa, [1, 2]]
  193. byte[] v5 = new byte[] { (byte) 0x10, (byte) 0x09, (byte) 0x00, (byte) 0x01, (byte) 0x04, (byte) 0x01, (byte) 0x04, (byte) 0x03, (byte) 0x06, (byte) 0x07, (byte) 0x61,
  194. (byte) 0x61, (byte) 0x61, (byte) 0x0d, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  195. (byte) 0x02 };
  196. in = new Input(IoBuffer.wrap(v5));
  197. ((org.red5.io.amf3.Input) in).enforceAMF3();
  198. List<Object> vectorOut = Deserializer.deserialize(in, null);
  199. //[Paul, ]
  200. Assert.assertNotNull(vectorOut);
  201. //Assert.assertEquals(vectorOut.size(), 4);
  202. for (int i = 0; i < vectorOut.size(); i++) {
  203. System.err.println("Vector: " + vectorOut.get(i));
  204. }
  205. resetOutput();
  206. }
  207. }