ClearKeyUtil { public static byte[] adjustResponseData(byte[] response) { if (Util.SDK_INT >= 27) { return response; } try { JSONObject responseJson = new JSONObject(Util.fromUtf8Bytes(response)); StringBuilder adjustedResponseBuilder = new StringBuilder("{\"keys\":["); JSONArray keysArray = responseJson.getJSONArray("keys"); for (int i = 0; i < keysArray.length(); i++) { if (i != 0) { adjustedResponseBuilder.append(","); } JSONObject key = keysArray.getJSONObject(i); adjustedResponseBuilder.append("{\"k\":\""); adjustedResponseBuilder.append(base64UrlToBase64(key.getString("k"))); adjustedResponseBuilder.append("\",\"kid\":\""); adjustedResponseBuilder.append(base64UrlToBase64(key.getString("kid"))); adjustedResponseBuilder.append("\",\"kty\":\""); adjustedResponseBuilder.append(key.getString("kty")); adjustedResponseBuilder.append("\"}"); } adjustedResponseBuilder.append("]}"); return Util.getUtf8Bytes(adjustedResponseBuilder.toString()); } catch (JSONException e) { Log.e(TAG, "Failed to adjust response data: " + Util.fromUtf8Bytes(response), e); return response; } } private  ClearKeyUtil(); static byte[] adjustRequestData(byte[] request); static byte[] adjustResponseData(byte[] response);  }
@Config(sdk = 26) @Test public void testAdjustSingleKeyResponseDataV26() { byte[] expected = Util.getUtf8Bytes( "{" + "\"keys\":[" + "{" + "\"k\":\"abc/def+\",\"kid\":\"ab/cde+f\",\"kty\":\"o_c-t\"" + "}" + "]" + "}"); assertThat(ClearKeyUtil.adjustResponseData(SINGLE_KEY_RESPONSE)).isEqualTo(expected); }
@Config(sdk = 26) @Test public void testAdjustMultiKeyResponseDataV26() { byte[] expected = Util.getUtf8Bytes( "{" + "\"keys\":[" + "{" + "\"k\":\"abc/def+\",\"kid\":\"ab/cde+f\",\"kty\":\"oct\"" + "},{" + "\"k\":\"ghi/jkl+\",\"kid\":\"gh/ijk+l\",\"kty\":\"oct\"" + "}" + "]" + "}"); assertThat(ClearKeyUtil.adjustResponseData(MULTI_KEY_RESPONSE)).isEqualTo(expected); }
@Config(sdk = 27) @Test public void testAdjustResponseDataV27() { assertThat(ClearKeyUtil.adjustResponseData(SINGLE_KEY_RESPONSE)).isEqualTo(SINGLE_KEY_RESPONSE); }