|
| 1 | +package io.split.android.client.network; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | + |
| 8 | +import com.google.gson.Gson; |
| 9 | +import com.google.gson.GsonBuilder; |
| 10 | +import com.google.gson.reflect.TypeToken; |
| 11 | + |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.lang.reflect.Type; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +public class CertificatePinSerializerTest { |
| 20 | + |
| 21 | + private Gson mGson; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() { |
| 25 | + mGson = new GsonBuilder() |
| 26 | + .registerTypeAdapter(CertificatePin.class, new CertificatePinSerializer()) |
| 27 | + .create(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void serializeSinglePin() { |
| 32 | + CertificatePin pin = new CertificatePin(new byte[]{1, 2, 3}, "sha256"); |
| 33 | + |
| 34 | + String json = mGson.toJson(pin); |
| 35 | + |
| 36 | + assertEquals("{\"algo\":\"sha256\",\"pin\":[1,2,3]}", json); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void serializeNegativeByteValues() { |
| 41 | + CertificatePin pin = new CertificatePin(new byte[]{-80, 50, -99, -126, 11}, "sha256"); |
| 42 | + |
| 43 | + String json = mGson.toJson(pin); |
| 44 | + |
| 45 | + assertEquals("{\"algo\":\"sha256\",\"pin\":[-80,50,-99,-126,11]}", json); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void deserializeSinglePin() { |
| 50 | + String json = "{\"algo\":\"sha1\",\"pin\":[-116,-73,-94,-80,55]}"; |
| 51 | + |
| 52 | + CertificatePin pin = mGson.fromJson(json, CertificatePin.class); |
| 53 | + |
| 54 | + assertNotNull(pin); |
| 55 | + assertEquals("sha1", pin.getAlgorithm()); |
| 56 | + assertArrayEquals(new byte[]{-116, -73, -94, -80, 55}, pin.getPin()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void roundTripPreservesData() { |
| 61 | + CertificatePin original = new CertificatePin(new byte[]{-116, -123, 30, -25}, "sha256"); |
| 62 | + |
| 63 | + String json = mGson.toJson(original); |
| 64 | + CertificatePin deserialized = mGson.fromJson(json, CertificatePin.class); |
| 65 | + |
| 66 | + assertNotNull(deserialized); |
| 67 | + assertEquals(original.getAlgorithm(), deserialized.getAlgorithm()); |
| 68 | + assertArrayEquals(original.getPin(), deserialized.getPin()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void roundTripMapOfSets() { |
| 73 | + String expectedJson = "{\"events.split.io\":[{\"algo\":\"sha256\",\"pin\":[-80,50,-99,-126,11]},{\"algo\":\"sha1\",\"pin\":[-116,-73,-94,-80,55]}],\"sdk.split.io\":[{\"algo\":\"sha256\",\"pin\":[-116,-123,30,-25]}]}"; |
| 74 | + |
| 75 | + Type type = new TypeToken<Map<String, Set<CertificatePin>>>() { |
| 76 | + }.getType(); |
| 77 | + Map<String, Set<CertificatePin>> deserialized = mGson.fromJson(expectedJson, type); |
| 78 | + |
| 79 | + assertNotNull(deserialized); |
| 80 | + assertEquals(2, deserialized.size()); |
| 81 | + assertEquals(2, deserialized.get("events.split.io").size()); |
| 82 | + assertEquals(1, deserialized.get("sdk.split.io").size()); |
| 83 | + |
| 84 | + // Re-serialize and deserialize |
| 85 | + String reserialized = mGson.toJson(deserialized, type); |
| 86 | + Map<String, Set<CertificatePin>> roundTripped = mGson.fromJson(reserialized, type); |
| 87 | + |
| 88 | + assertNotNull(roundTripped); |
| 89 | + assertEquals(deserialized.size(), roundTripped.size()); |
| 90 | + for (Map.Entry<String, Set<CertificatePin>> entry : deserialized.entrySet()) { |
| 91 | + Set<CertificatePin> originalPins = entry.getValue(); |
| 92 | + Set<CertificatePin> roundTrippedPins = roundTripped.get(entry.getKey()); |
| 93 | + assertNotNull(roundTrippedPins); |
| 94 | + assertEquals(originalPins.size(), roundTrippedPins.size()); |
| 95 | + assertEquals(originalPins, roundTrippedPins); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void deserializeWithUnknownFieldsIsIgnored() { |
| 101 | + String json = "{\"algo\":\"sha256\",\"pin\":[1,2],\"extra\":\"ignored\"}"; |
| 102 | + |
| 103 | + CertificatePin pin = mGson.fromJson(json, CertificatePin.class); |
| 104 | + |
| 105 | + assertNotNull(pin); |
| 106 | + assertEquals("sha256", pin.getAlgorithm()); |
| 107 | + assertArrayEquals(new byte[]{1, 2}, pin.getPin()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void deserializeMissingFieldsResultsInNulls() { |
| 112 | + String json = "{}"; |
| 113 | + |
| 114 | + CertificatePin pin = mGson.fromJson(json, CertificatePin.class); |
| 115 | + |
| 116 | + assertNotNull(pin); |
| 117 | + assertNull(pin.getAlgorithm()); |
| 118 | + assertNull(pin.getPin()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void serializeEmptyPinArray() { |
| 123 | + CertificatePin pin = new CertificatePin(new byte[]{}, "sha256"); |
| 124 | + |
| 125 | + String json = mGson.toJson(pin); |
| 126 | + |
| 127 | + assertEquals("{\"algo\":\"sha256\",\"pin\":[]}", json); |
| 128 | + } |
| 129 | +} |
0 commit comments