/src/test/java/com/notnoop/apns/internal/ApnsFeedbackParsingUtils.java

http://github.com/notnoop/java-apns · Java · 162 lines · 108 code · 21 blank · 33 comment · 15 complexity · 481c6727a2618a045fdaeccdca35617b MD5 · raw file

  1. /*
  2. * Copyright 2009, Mahmood Ali.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Mahmood Ali. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. package com.notnoop.apns.internal;
  32. import java.util.Arrays;
  33. import java.util.Collection;
  34. import java.util.Date;
  35. import java.util.Map;
  36. import java.util.Random;
  37. import static org.hamcrest.CoreMatchers.*;
  38. import static org.junit.Assert.*;
  39. public class ApnsFeedbackParsingUtils {
  40. static byte[] pack(byte[]... args) {
  41. int total = 0;
  42. for (byte[] arg : args)
  43. total += arg.length;
  44. byte[] result = new byte[total];
  45. int index = 0;
  46. for (byte[] arg : args) {
  47. System.arraycopy(arg, 0, result, index, arg.length);
  48. index += arg.length;
  49. }
  50. return result;
  51. }
  52. static byte[] simpleDevice = new byte[32];
  53. static byte[] firstDevice = new byte[32];
  54. static byte[] secondDevice = new byte[32];
  55. static byte[] thirdDevice = new byte[32];
  56. static {
  57. Random random = new Random();
  58. do {
  59. random.nextBytes(simpleDevice);
  60. random.nextBytes(firstDevice);
  61. random.nextBytes(secondDevice);
  62. random.nextBytes(thirdDevice);
  63. } while (Arrays.equals(firstDevice, secondDevice)
  64. || (Arrays.equals(secondDevice, thirdDevice))
  65. || (Arrays.equals(firstDevice, thirdDevice)));
  66. }
  67. static int simpleDate;
  68. public static byte[] simple = pack(
  69. /* time_t */ new byte[] {0, 0, 0, 0},
  70. /* length */ new byte[] { 0, 32 },
  71. /* device token */ simpleDevice
  72. );
  73. static int firstDate = 10;
  74. static int secondDate = 1 << 8;
  75. static int thirdDate = secondDate;
  76. public static byte[] three = pack(
  77. /* first message */
  78. /* time_t */ new byte[] {0, 0, 0, 10},
  79. /* length */ new byte[] { 0, 32 },
  80. /* device token */ firstDevice,
  81. /* second device */
  82. /* time_t */ new byte[] {0, 0, 1, 0},
  83. /* length */ new byte[] { 0, 32 },
  84. /* device token */ secondDevice,
  85. /* Duplicate time */
  86. /* time_t */ new byte[] {0, 0, 1, 0},
  87. /* length */ new byte[] { 0, 32 },
  88. /* device token */ thirdDevice
  89. );
  90. protected static void checkRawSimple(Map<byte[], Integer> simpleParsed) {
  91. assertEquals(1, simpleParsed.size());
  92. assertThat(simpleParsed.keySet(), hasItem(simpleDevice));
  93. for (Map.Entry<byte[], Integer> e : simpleParsed.entrySet()) {
  94. byte[] device = e.getKey();
  95. Integer date = e.getValue();
  96. if (Arrays.equals(simpleDevice, device)) {
  97. assertEquals(simpleDate, (int)date);
  98. } else {
  99. fail("Unexpected value in collection");
  100. }
  101. }
  102. }
  103. protected static void checkRawThree(Map<byte[], Integer> threeParsed) {
  104. assertEquals(3, threeParsed.size());
  105. Collection<byte[]> devices = threeParsed.keySet();
  106. assertThat(devices, hasItems(firstDevice, secondDevice, thirdDevice));
  107. for (Map.Entry<byte[], Integer> e : threeParsed.entrySet()) {
  108. byte[] device = e.getKey();
  109. Integer date = e.getValue();
  110. if (Arrays.equals(firstDevice, device)) {
  111. assertEquals(firstDate, (int)date);
  112. } else if (Arrays.equals(secondDevice, device)) {
  113. assertEquals(secondDate, (int)date);
  114. } else if (Arrays.equals(thirdDevice, device)) {
  115. assertEquals(thirdDate, (int)date);
  116. } else {
  117. fail("Unexpected value in collection");
  118. }
  119. }
  120. }
  121. public static void checkParsedSimple(Map<String, Date> simpleParsed) {
  122. Date sd = new Date(simpleDate * 1000L);
  123. String deviceToken = Utilities.encodeHex(simpleDevice);
  124. assertEquals(1, simpleParsed.size());
  125. assertThat(simpleParsed.keySet(), hasItem(deviceToken));
  126. assertEquals(sd, simpleParsed.get(deviceToken));
  127. }
  128. public static void checkParsedThree(Map<String, Date> threeParsed) {
  129. Date d1 = new Date(firstDate * 1000L);
  130. String dt1 = Utilities.encodeHex(firstDevice);
  131. Date d2 = new Date(secondDate * 1000L);
  132. String dt2 = Utilities.encodeHex(secondDevice);
  133. Date d3 = new Date(thirdDate * 1000L);
  134. String dt3 = Utilities.encodeHex(thirdDevice);
  135. assertEquals(3, threeParsed.size());
  136. assertThat(threeParsed.keySet(), hasItems(dt1, dt2, dt3));
  137. assertEquals(d1, threeParsed.get(dt1));
  138. assertEquals(d2, threeParsed.get(dt2));
  139. assertEquals(d3, threeParsed.get(dt3));
  140. }
  141. }