diff options
author | Kiwon Park <kiwonp@google.com> | 2022-10-04 21:22:24 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-10-04 21:22:24 +0000 |
commit | f96acefd82c526fecb70c996595a23c29ce4127f (patch) | |
tree | b138e70f841c3df4fd15489516f678bac7af39cf | |
parent | a7b5953ec98c9489d1e9396cb68179d7ab28d074 (diff) | |
parent | d3c48559792616fc148cbc24ac3da3a5e6084ad3 (diff) | |
download | service_entitlement-f96acefd82c526fecb70c996595a23c29ce4127f.tar.gz |
Add ability to save the HTTP request/responses. am: 00d7763e6d am: 54556c4613 am: 6c0fd028c7 am: d3c4855979
Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/service_entitlement/+/2240183
Change-Id: I8fe8c46b326c126508c1d1d9b892a888b2d8ea8d
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
5 files changed, 123 insertions, 22 deletions
diff --git a/java/com/android/libraries/entitlement/ServiceEntitlement.java b/java/com/android/libraries/entitlement/ServiceEntitlement.java index b52cd1f..5e38640 100644 --- a/java/com/android/libraries/entitlement/ServiceEntitlement.java +++ b/java/com/android/libraries/entitlement/ServiceEntitlement.java @@ -25,6 +25,8 @@ import com.android.libraries.entitlement.eapaka.EapAkaApi; import com.google.common.collect.ImmutableList; +import java.util.List; + /** * Implemnets protocol for carrier service entitlement configuration query and operation, based on * GSMA TS.43 spec. @@ -72,7 +74,28 @@ public class ServiceEntitlement { */ public ServiceEntitlement(Context context, CarrierConfig carrierConfig, int simSubscriptionId) { this.carrierConfig = carrierConfig; - this.eapAkaApi = new EapAkaApi(context, simSubscriptionId); + this.eapAkaApi = new EapAkaApi(context, simSubscriptionId, false); + } + + /** + * Creates an instance for service entitlement configuration query and operation for the + * carrier. + * + * @param context context of application + * @param carrierConfig carrier specific configs used in the queries and operations. + * @param simSubscriptionId the subscroption ID of the carrier's SIM on device. This indicates + * which SIM to retrieve IMEI/IMSI from and perform EAP-AKA authentication with. See {@link + * android.telephony.SubscriptionManager} for how to get the subscroption ID. + * @param saveHttpHistory set to {@code true} to save the history of request and response which + * can later be retrieved by {@code getHistory()}. Intended for debugging. + */ + public ServiceEntitlement( + Context context, + CarrierConfig carrierConfig, + int simSubscriptionId, + boolean saveHttpHistory) { + this.carrierConfig = carrierConfig; + this.eapAkaApi = new EapAkaApi(context, simSubscriptionId, saveHttpHistory); } @VisibleForTesting @@ -155,4 +178,19 @@ public class ServiceEntitlement { throws ServiceEntitlementException { return eapAkaApi.performEsimOdsaOperation(appId, carrierConfig, request, operation); } + + /** + * Retrieves the history of past HTTP request and responses if {@code saveHttpHistory} was set + * in constructor. + */ + public List<String> getHistory() { + return eapAkaApi.getHistory(); + } + + /** + * Clears the history of past HTTP request and responses. + */ + public void clearHistory() { + eapAkaApi.clearHistory(); + } } diff --git a/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java b/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java index d7d4644..a0c8c24 100644 --- a/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java +++ b/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java @@ -43,6 +43,8 @@ import com.google.common.net.HttpHeaders; import org.json.JSONException; import org.json.JSONObject; +import java.util.List; + public class EapAkaApi { private static final String TAG = "ServiceEntitlement"; @@ -94,8 +96,8 @@ public class EapAkaApi { private final int mSimSubscriptionId; private final HttpClient mHttpClient; - public EapAkaApi(Context context, int simSubscriptionId) { - this(context, simSubscriptionId, new HttpClient()); + public EapAkaApi(Context context, int simSubscriptionId, boolean saveHistory) { + this(context, simSubscriptionId, new HttpClient(saveHistory)); } @VisibleForTesting @@ -402,4 +404,18 @@ public class EapAkaApi { } return "0" + imsi + "@nai.epc.mnc" + mnc + ".mcc" + mcc + ".3gppnetwork.org"; } + + /** + * Retrieves the history of past HTTP request and responses. + */ + public List<String> getHistory() { + return mHttpClient.getHistory(); + } + + /** + * Clears the history of past HTTP request and responses. + */ + public void clearHistory() { + mHttpClient.clearHistory(); + } } diff --git a/java/com/android/libraries/entitlement/http/HttpClient.java b/java/com/android/libraries/entitlement/http/HttpClient.java index 9ccb5ee..39275e8 100644 --- a/java/com/android/libraries/entitlement/http/HttpClient.java +++ b/java/com/android/libraries/entitlement/http/HttpClient.java @@ -47,6 +47,7 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -55,9 +56,19 @@ public class HttpClient { private static final String TAG = "ServiceEntitlement"; private HttpURLConnection mConnection; + private boolean mSaveHistory; + private ArrayList<String> mHistory; + + public HttpClient(boolean saveHistory) { + mSaveHistory = saveHistory; + mHistory = new ArrayList<>(); + } @WorkerThread public HttpResponse request(HttpRequest request) throws ServiceEntitlementException { + if (mSaveHistory) { + mHistory.add(request.toString()); + } logPii("HttpClient.request url: " + request.url()); createConnection(request); logPii("HttpClient.request headers (partial): " + mConnection.getRequestProperties()); @@ -74,6 +85,9 @@ public class HttpClient { mConnection.connect(); // This is to trigger SocketTimeoutException early HttpResponse response = getHttpResponse(mConnection); Log.d(TAG, "HttpClient.response : " + response); + if (mSaveHistory) { + mHistory.add(response.toString()); + } return response; } catch (IOException ioe) { throw new ServiceEntitlementException( @@ -85,6 +99,20 @@ public class HttpClient { } } + /** + * Retrieves the history of past HTTP request and responses. + */ + public List<String> getHistory() { + return mHistory; + } + + /** + * Clears the history of past HTTP request and responses. + */ + public void clearHistory() { + mHistory.clear(); + } + private void createConnection(HttpRequest request) throws ServiceEntitlementException { try { URL url = new URL(request.url()); diff --git a/java/com/android/libraries/entitlement/http/HttpResponse.java b/java/com/android/libraries/entitlement/http/HttpResponse.java index f495578..f76fdd6 100644 --- a/java/com/android/libraries/entitlement/http/HttpResponse.java +++ b/java/com/android/libraries/entitlement/http/HttpResponse.java @@ -73,22 +73,4 @@ public abstract class HttpResponse { .setResponseMessage("") .setCookies(ImmutableList.of()); } - - @Override - public final String toString() { - return new StringBuilder("HttpResponse{") - .append("contentType=") - .append(contentType()) - .append(" body=(") - .append(body().length()) - .append(" characters)") - .append(" responseCode=") - .append(responseCode()) - .append(" responseMessage=") - .append(responseMessage()) - .append(" cookies=[") - .append(cookies().size()) - .append(" cookies]}") - .toString(); - } } diff --git a/tests/src/com/android/libraries/entitlement/http/HttpClientTest.java b/tests/src/com/android/libraries/entitlement/http/HttpClientTest.java index 505e8b5..9f05828 100644 --- a/tests/src/com/android/libraries/entitlement/http/HttpClientTest.java +++ b/tests/src/com/android/libraries/entitlement/http/HttpClientTest.java @@ -46,6 +46,7 @@ import org.junit.runner.RunWith; import java.net.HttpURLConnection; import java.net.URL; +import java.util.List; import java.util.Map; @RunWith(AndroidJUnit4.class) @@ -70,7 +71,7 @@ public class HttpClientTest { // Reset sFakeURLStreamHandler sFakeURLStreamHandler.stubResponse(ImmutableMap.of()); - mHttpClient = new HttpClient(); + mHttpClient = new HttpClient(true); } @Test @@ -244,4 +245,40 @@ public class HttpClientTest { assertThat(exception.getHttpStatus()).isEqualTo(0); assertThat(exception.getRetryAfter()).isEmpty(); } + + @Test + public void history() throws Exception { + FakeResponse responseContent = + FakeResponse.builder() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setResponseLocation(null) + .setResponseBody(TEST_RESPONSE_BODY.getBytes(UTF_8)) + .setContentType(CONTENT_TYPE_STRING_JSON) + .build(); + Map<String, FakeResponse> response = ImmutableMap.of(TEST_URL, responseContent); + sFakeURLStreamHandler.stubResponse(response); + HttpRequest request = + HttpRequest.builder() + .setUrl(TEST_URL) + .setRequestMethod(RequestMethod.GET) + .setTimeoutInSec(70) + .build(); + + HttpResponse httpResponse0 = mHttpClient.request(request); + HttpResponse httpResponse1 = mHttpClient.request(request); + List<String> history = mHttpClient.getHistory(); + + assertThat(history) + .containsExactly( + request.toString(), + httpResponse0.toString(), + request.toString(), + httpResponse1.toString()) + .inOrder(); + + mHttpClient.clearHistory(); + history = mHttpClient.getHistory(); + + assertThat(history).isEmpty(); + } } |