|
| 1 | +package com.google.u2f.server.impl.attestation.android; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import com.google.gson.JsonArray; |
| 8 | +import com.google.gson.JsonElement; |
| 9 | +import com.google.gson.JsonObject; |
| 10 | + |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.junit.runners.JUnit4; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit tests for {@link AuthorizationList} |
| 22 | + */ |
| 23 | +@RunWith(JUnit4.class) |
| 24 | +public class AuthorizationListTest { |
| 25 | + private static final List<Purpose> EMPTY_PURPOSE = new ArrayList<Purpose>(); |
| 26 | + private static final List<Purpose> ONE_PURPOSE = Arrays.asList(Purpose.KM_PURPOSE_SIGN); |
| 27 | + private static final List<Purpose> TWO_PURPOSES = |
| 28 | + Arrays.asList(Purpose.KM_PURPOSE_SIGN, Purpose.KM_PURPOSE_VERIFY); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void toJson_nullValues() throws Exception { |
| 32 | + JsonObject json = new AuthorizationList(null, null).toJson(); |
| 33 | + |
| 34 | + assertFalse(json.has(AuthorizationList.JSON_ALGORITHM_KEY)); |
| 35 | + assertFalse(json.has(AuthorizationList.JSON_PURPOSE_KEY)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void toJson_emptyPurpose() throws Exception { |
| 40 | + AuthorizationList authorizationList = |
| 41 | + new AuthorizationList(EMPTY_PURPOSE, Algorithm.KM_ALGORITHM_EC); |
| 42 | + JsonObject json = authorizationList.toJson(); |
| 43 | + |
| 44 | + assertEquals( |
| 45 | + Algorithm.KM_ALGORITHM_EC.toString(), |
| 46 | + json.get(AuthorizationList.JSON_ALGORITHM_KEY).getAsString()); |
| 47 | + List<Purpose> extractedPurpose = extractPurposeListFromJsonArray( |
| 48 | + json.get(AuthorizationList.JSON_PURPOSE_KEY).getAsJsonArray()); |
| 49 | + |
| 50 | + assertTrue(EMPTY_PURPOSE.containsAll(extractedPurpose)); |
| 51 | + assertTrue(extractedPurpose.containsAll(EMPTY_PURPOSE)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void toJson_onePurpose() throws Exception { |
| 56 | + AuthorizationList authorizationList = |
| 57 | + new AuthorizationList(ONE_PURPOSE, Algorithm.KM_ALGORITHM_HMAC); |
| 58 | + JsonObject json = authorizationList.toJson(); |
| 59 | + |
| 60 | + assertEquals( |
| 61 | + Algorithm.KM_ALGORITHM_HMAC.toString(), |
| 62 | + json.get(AuthorizationList.JSON_ALGORITHM_KEY).getAsString()); |
| 63 | + List<Purpose> extractedPurpose = extractPurposeListFromJsonArray( |
| 64 | + json.get(AuthorizationList.JSON_PURPOSE_KEY).getAsJsonArray()); |
| 65 | + |
| 66 | + assertTrue(ONE_PURPOSE.containsAll(extractedPurpose)); |
| 67 | + assertTrue(extractedPurpose.containsAll(ONE_PURPOSE)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void toJson_twoPurposes() throws Exception { |
| 72 | + JsonObject json = new AuthorizationList(TWO_PURPOSES, Algorithm.KM_ALGORITHM_RSA).toJson(); |
| 73 | + |
| 74 | + assertEquals( |
| 75 | + Algorithm.KM_ALGORITHM_RSA.toString(), |
| 76 | + json.get(AuthorizationList.JSON_ALGORITHM_KEY).getAsString()); |
| 77 | + List<Purpose> extractedPurpose = extractPurposeListFromJsonArray( |
| 78 | + json.get(AuthorizationList.JSON_PURPOSE_KEY).getAsJsonArray()); |
| 79 | + |
| 80 | + assertTrue(TWO_PURPOSES.containsAll(extractedPurpose)); |
| 81 | + assertTrue(extractedPurpose.containsAll(TWO_PURPOSES)); |
| 82 | + } |
| 83 | + |
| 84 | + private List<Purpose> extractPurposeListFromJsonArray(JsonArray array) throws Exception { |
| 85 | + Iterator<JsonElement> iterator = array.iterator(); |
| 86 | + List<Purpose> result = new ArrayList<Purpose>(); |
| 87 | + while (iterator.hasNext()) { |
| 88 | + result.add(Purpose.fromString(iterator.next().getAsString())); |
| 89 | + } |
| 90 | + return result; |
| 91 | + } |
| 92 | +} |
0 commit comments