Merge changes from topic 'framework-net-aosp'

* changes:
  DO NOT MERGE: IpConnectivityMetrics: rate limit ApfProgramEvents
  DO NOT MERGE: TokenBucket for rate-limiting and throttling
  DO NOT MERGE: IpConnectivityMetrics reads buffer size in settings
  DO NOT MERGE: CaptivePortalLogin: set mixed content policy to compatibility.
  DO NOT MERGE: Add IP conn metrics to dumpsys and bug reports
  DO NOT MERGE: IpConnectivity metrics: add version number
diff --git a/core/java/android/net/IIpConnectivityMetrics.aidl b/core/java/android/net/IIpConnectivityMetrics.aidl
index 8f634bb..d36b766 100644
--- a/core/java/android/net/IIpConnectivityMetrics.aidl
+++ b/core/java/android/net/IIpConnectivityMetrics.aidl
@@ -23,7 +23,8 @@
 interface IIpConnectivityMetrics {
 
     /**
-     * @return number of remaining available slots in buffer.
+     * @return the number of remaining available slots in buffer,
+     * or -1 if the event was dropped due to rate limiting.
      */
     int logEvent(in ConnectivityMetricsEvent event);
 }
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index db7baac..e52983e 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -7172,6 +7172,13 @@
         */
        public static final String MOBILE_DATA_ALWAYS_ON = "mobile_data_always_on";
 
+        /**
+         * Size of the event buffer for IP connectivity metrics.
+         * @hide
+         */
+        public static final String CONNECTIVITY_METRICS_BUFFER_SIZE =
+              "connectivity_metrics_buffer_size";
+
        /** {@hide} */
        public static final String NETSTATS_ENABLED = "netstats_enabled";
        /** {@hide} */
diff --git a/core/java/com/android/internal/util/TokenBucket.java b/core/java/com/android/internal/util/TokenBucket.java
new file mode 100644
index 0000000..effb82b
--- /dev/null
+++ b/core/java/com/android/internal/util/TokenBucket.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import android.os.SystemClock;
+
+import static com.android.internal.util.Preconditions.checkArgumentNonnegative;
+import static com.android.internal.util.Preconditions.checkArgumentPositive;
+
+/**
+ * A class useful for rate-limiting or throttling that stores and distributes tokens.
+ *
+ * A TokenBucket starts with a fixed capacity of tokens, an initial amount of tokens, and
+ * a fixed filling period (in milliseconds).
+ *
+ * For every filling period, the bucket gains one token, up to its maximum capacity from
+ * which point tokens simply overflow and are lost. Tokens can be obtained one by one or n by n.
+ *
+ * The available amount of tokens is computed lazily when the bucket state is inspected.
+ * Therefore it is purely synchronous and does not involve any asynchronous activity.
+ * It is not synchronized in any way and not a thread-safe object.
+ */
+public class TokenBucket {
+
+    private final int mFillDelta; // Time in ms it takes to generate one token.
+    private final int mCapacity;  // Maximum number of tokens that can be stored.
+    private long mLastFill;       // Last time in ms the bucket generated tokens.
+    private int mAvailable;       // Current number of available tokens.
+
+    /**
+     * Create a new TokenBucket.
+     * @param deltaMs the time in milliseconds it takes to generate a new token.
+     * Must be strictly positive.
+     * @param capacity the maximum token capacity. Must be strictly positive.
+     * @param tokens the starting amount of token. Must be positive or zero.
+     */
+    public TokenBucket(int deltaMs, int capacity, int tokens) {
+        mFillDelta = checkArgumentPositive(deltaMs, "deltaMs must be strictly positive");
+        mCapacity = checkArgumentPositive(capacity, "capacity must be strictly positive");
+        mAvailable = Math.min(checkArgumentNonnegative(tokens), mCapacity);
+        mLastFill = scaledTime();
+    }
+
+    /**
+     * Create a new TokenBucket that starts completely filled.
+     * @param deltaMs the time in milliseconds it takes to generate a new token.
+     * Must be strictly positive.
+     * @param capacity the maximum token capacity. Must be strictly positive.
+     */
+    public TokenBucket(int deltaMs, int capacity) {
+        this(deltaMs, capacity, capacity);
+    }
+
+    /** Reset this TokenBucket and set its number of available tokens. */
+    public void reset(int tokens) {
+        checkArgumentNonnegative(tokens);
+        mAvailable = Math.min(tokens, mCapacity);
+        mLastFill = scaledTime();
+    }
+
+    /** Returns this TokenBucket maximum token capacity. */
+    public int capacity() {
+        return mCapacity;
+    }
+
+    /** Returns this TokenBucket currently number of available tokens. */
+    public int available() {
+        fill();
+        return mAvailable;
+    }
+
+    /** Returns true if this TokenBucket as one or more tokens available. */
+    public boolean has() {
+        fill();
+        return mAvailable > 0;
+    }
+
+    /** Consumes a token from this TokenBucket and returns true if a token is available. */
+    public boolean get() {
+        return (get(1) == 1);
+    }
+
+    /**
+     * Try to consume many tokens from this TokenBucket.
+     * @param n the number of tokens to consume.
+     * @return the number of tokens that were actually consumed.
+     */
+    public int get(int n) {
+        fill();
+        if (n <= 0) {
+            return 0;
+        }
+        if (n > mAvailable) {
+            int got = mAvailable;
+            mAvailable = 0;
+            return got;
+        }
+        mAvailable -= n;
+        return n;
+    }
+
+    private void fill() {
+        final long now = scaledTime();
+        final int diff = (int) (now - mLastFill);
+        mAvailable = Math.min(mCapacity, mAvailable + diff);
+        mLastFill = now;
+    }
+
+    private long scaledTime() {
+        return SystemClock.elapsedRealtime() / mFillDelta;
+    }
+}
diff --git a/core/tests/coretests/src/android/util/TokenBucketTest.java b/core/tests/coretests/src/android/util/TokenBucketTest.java
new file mode 100644
index 0000000..f7ac20c
--- /dev/null
+++ b/core/tests/coretests/src/android/util/TokenBucketTest.java
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import android.os.SystemClock;
+import android.text.format.DateUtils;
+import junit.framework.TestCase;
+
+public class TokenBucketTest extends TestCase {
+
+    static final int FILL_DELTA_VERY_SHORT  = 1;
+    static final int FILL_DELTA_VERY_LONG   = Integer.MAX_VALUE;
+
+    public void testArgumentValidation() {
+        assertThrow(() -> new TokenBucket(0, 1, 1));
+        assertThrow(() -> new TokenBucket(1, 0, 1));
+        assertThrow(() -> new TokenBucket(1, 1, 0));
+        assertThrow(() -> new TokenBucket(0, 1));
+        assertThrow(() -> new TokenBucket(1, 0));
+        assertThrow(() -> new TokenBucket(-1, 1, 1));
+        assertThrow(() -> new TokenBucket(1, -1, 1));
+        assertThrow(() -> new TokenBucket(1, 1, -1));
+        assertThrow(() -> new TokenBucket(-1, 1));
+        assertThrow(() -> new TokenBucket(1, -1));
+
+        new TokenBucket(1000, 100, 0);
+        new TokenBucket(1000, 100, 10);
+        new TokenBucket(5000, 50);
+        new TokenBucket(5000, 1);
+    }
+
+    public void testInitialCapacity() {
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 1), 1);
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10), 10);
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 1000), 1000);
+
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 0), 0);
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 3), 3);
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 10), 10);
+
+        drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 100), 10);
+
+        drain(new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50), 50);
+        drain(new TokenBucket((int)DateUtils.HOUR_IN_MILLIS, 10), 10);
+        drain(new TokenBucket((int)DateUtils.DAY_IN_MILLIS, 200), 200);
+    }
+
+    public void testReset() {
+        TokenBucket tb = new TokenBucket(FILL_DELTA_VERY_LONG, 100, 10);
+        drain(tb, 10);
+
+        tb.reset(50);
+        drain(tb, 50);
+
+        tb.reset(50);
+        getOneByOne(tb, 10);
+        assertTrue(tb.has());
+
+        tb.reset(30);
+        drain(tb, 30);
+    }
+
+    public void testFill() throws Exception {
+        int delta = 50;
+        TokenBucket tb = new TokenBucket(delta, 10, 0);
+
+        assertEmpty(tb);
+
+        Thread.sleep(3 * delta / 2);
+
+        assertTrue(tb.has());
+    }
+
+    public void testRefill() throws Exception {
+        TokenBucket tb = new TokenBucket(FILL_DELTA_VERY_SHORT, 10, 10);
+
+        assertEquals(5, tb.get(5));
+        assertEquals(5, tb.get(5));
+
+        while (tb.available() < 10) {
+            Thread.sleep(2);
+        }
+
+        assertEquals(10, tb.get(10));
+
+        while (tb.available() < 10) {
+            Thread.sleep(2);
+        }
+
+        assertEquals(10, tb.get(100));
+    }
+
+    public void testAverage() throws Exception {
+        final int delta = 3;
+        final int want = 60;
+
+        long start = SystemClock.elapsedRealtime();
+        TokenBucket tb = new TokenBucket(delta, 20, 0);
+
+        for (int i = 0; i < want; i++) {
+            while (!tb.has()) {
+                Thread.sleep(5 * delta);
+            }
+            tb.get();
+        }
+
+        assertDuration(want * delta, SystemClock.elapsedRealtime() - start);
+    }
+
+    public void testBurst() throws Exception {
+        final int delta = 2;
+        final int capacity = 20;
+        final int want = 100;
+
+        long start = SystemClock.elapsedRealtime();
+        TokenBucket tb = new TokenBucket(delta, capacity, 0);
+
+        int total = 0;
+        while (total < want) {
+            while (!tb.has()) {
+                Thread.sleep(capacity * delta - 2);
+            }
+            total += tb.get(tb.available());
+        }
+
+        assertDuration(total * delta, SystemClock.elapsedRealtime() - start);
+    }
+
+    static void getOneByOne(TokenBucket tb, int n) {
+        while (n > 0) {
+            assertTrue(tb.has());
+            assertTrue(tb.available() >= n);
+            assertTrue(tb.get());
+            assertTrue(tb.available() >= n - 1);
+            n--;
+        }
+    }
+
+    void assertEmpty(TokenBucket tb) {
+        assertFalse(tb.has());
+        assertEquals(0, tb.available());
+        assertFalse(tb.get());
+    }
+
+    void drain(TokenBucket tb, int n) {
+        getOneByOne(tb, n);
+        assertEmpty(tb);
+    }
+
+    void assertDuration(long expected, long elapsed) {
+        String msg = String.format(
+            "expected elapsed time at least %d ms, but was %d ms", expected, elapsed);
+        elapsed += 1; // one millisecond extra guard
+        assertTrue(msg, elapsed >= expected);
+    }
+
+    void assertThrow(Fn fn)     {
+      try {
+          fn.call();
+          fail("expected n exception to be thrown.");
+      } catch (Throwable t) {}
+    }
+
+    interface Fn { void call(); }
+}
diff --git a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
index b58c87a..bb8eb2c 100644
--- a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
+++ b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
@@ -115,6 +115,7 @@
         myWebView.clearCache(true);
         WebSettings webSettings = myWebView.getSettings();
         webSettings.setJavaScriptEnabled(true);
+        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
         mWebViewClient = new MyWebViewClient();
         myWebView.setWebViewClient(mWebViewClient);
         myWebView.setWebChromeClient(new MyWebChromeClient());
diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java b/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java
index f1ef947..f1d01e0 100644
--- a/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java
+++ b/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java
@@ -48,6 +48,10 @@
         final IpConnectivityLog log = new IpConnectivityLog();
         log.events = toProto(events);
         log.droppedEvents = dropped;
+        if ((log.events.length > 0) || (dropped > 0)) {
+            // Only write version number if log has some information at all.
+            log.version = IpConnectivityMetrics.VERSION;
+        }
         return IpConnectivityLog.toByteArray(log);
     }
 
diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java
index 28e724c..be68173 100644
--- a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java
+++ b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java
@@ -19,19 +19,25 @@
 import android.content.Context;
 import android.net.ConnectivityMetricsEvent;
 import android.net.IIpConnectivityMetrics;
+import android.net.metrics.ApfProgramEvent;
 import android.net.metrics.IpConnectivityLog;
 import android.os.IBinder;
 import android.os.Parcelable;
+import android.provider.Settings;
 import android.text.TextUtils;
+import android.text.format.DateUtils;
+import android.util.ArrayMap;
 import android.util.Base64;
 import android.util.Log;
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.TokenBucket;
 import com.android.server.SystemService;
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.function.ToIntFunction;
 
 import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;
 
@@ -40,10 +46,21 @@
     private static final String TAG = IpConnectivityMetrics.class.getSimpleName();
     private static final boolean DBG = false;
 
+    // The logical version numbers of ipconnectivity.proto, corresponding to the
+    // "version" field of IpConnectivityLog.
+    private static final int NYC      = 0;
+    private static final int NYC_MR1  = 1;
+    private static final int NYC_MR2  = 2;
+    public static final int VERSION   = NYC_MR2;
+
     private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME;
 
     // Default size of the event buffer. Once the buffer is full, incoming events are dropped.
     private static final int DEFAULT_BUFFER_SIZE = 2000;
+    // Maximum size of the event buffer.
+    private static final int MAXIMUM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE * 10;
+
+    private static final int ERROR_RATE_LIMITED = -1;
 
     // Lock ensuring that concurrent manipulations of the event buffer are correct.
     // There are three concurrent operations to synchronize:
@@ -62,10 +79,19 @@
     private int mDropped;
     @GuardedBy("mLock")
     private int mCapacity;
+    @GuardedBy("mLock")
+    private final ArrayMap<Class<?>, TokenBucket> mBuckets = makeRateLimitingBuckets();
+
+    private final ToIntFunction<Context> mCapacityGetter;
+
+    public IpConnectivityMetrics(Context ctx, ToIntFunction<Context> capacityGetter) {
+        super(ctx);
+        mCapacityGetter = capacityGetter;
+        initBuffer();
+    }
 
     public IpConnectivityMetrics(Context ctx) {
-        super(ctx);
-        initBuffer();
+        this(ctx, READ_BUFFER_SIZE);
     }
 
     @Override
@@ -86,7 +112,7 @@
 
     @VisibleForTesting
     public int bufferCapacity() {
-        return DEFAULT_BUFFER_SIZE; // TODO: read from config
+        return mCapacityGetter.applyAsInt(getContext());
     }
 
     private void initBuffer() {
@@ -104,6 +130,10 @@
             if (event == null) {
                 return left;
             }
+            if (isRateLimited(event)) {
+                // Do not count as a dropped event. TODO: consider adding separate counter
+                return ERROR_RATE_LIMITED;
+            }
             if (left == 0) {
                 mDropped++;
                 return 0;
@@ -113,6 +143,11 @@
         }
     }
 
+    private boolean isRateLimited(ConnectivityMetricsEvent event) {
+        TokenBucket tb = mBuckets.get(event.data.getClass());
+        return (tb != null) && !tb.get();
+    }
+
     private String flushEncodedOutput() {
         final ArrayList<ConnectivityMetricsEvent> events;
         final int dropped;
@@ -186,6 +221,7 @@
         static final String CMD_FLUSH   = "flush";
         static final String CMD_LIST    = "list";
         static final String CMD_STATS   = "stats";
+        static final String CMD_DUMPSYS = "-a"; // dumpsys.cpp dumps services with "-a" as arguments
         static final String CMD_DEFAULT = CMD_STATS;
 
         @Override
@@ -203,6 +239,8 @@
                 case CMD_FLUSH:
                     cmdFlush(fd, pw, args);
                     return;
+                case CMD_DUMPSYS:
+                    // Fallthrough to CMD_LIST when dumpsys.cpp dumps services states (bug reports)
                 case CMD_LIST:
                     cmdList(fd, pw, args);
                     return;
@@ -226,4 +264,20 @@
             getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics");
         }
     };
+
+    private static final ToIntFunction<Context> READ_BUFFER_SIZE = (ctx) -> {
+        int size = Settings.Global.getInt(ctx.getContentResolver(),
+                Settings.Global.CONNECTIVITY_METRICS_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
+        if (size <= 0) {
+            return DEFAULT_BUFFER_SIZE;
+        }
+        return Math.min(size, MAXIMUM_BUFFER_SIZE);
+    };
+
+    private static ArrayMap<Class<?>, TokenBucket> makeRateLimitingBuckets() {
+        ArrayMap<Class<?>, TokenBucket> map = new ArrayMap<>();
+        // one token every minute, 50 tokens max: burst of ~50 events every hour.
+        map.put(ApfProgramEvent.class, new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50));
+        return map;
+    }
 }
diff --git a/services/core/proto/ipconnectivity.proto b/services/core/proto/ipconnectivity.proto
index e0d7f09..29b318f 100644
--- a/services/core/proto/ipconnectivity.proto
+++ b/services/core/proto/ipconnectivity.proto
@@ -53,6 +53,7 @@
 
   // The event type code of the probe, represented by constants defined in
   // android.net.metrics.IpReachabilityEvent.
+  // NUD_FAILED_ORGANIC and PROVISIONING_LOST_ORGANIC recorded since version 1.
   optional int32 event_type = 2;
 };
 
@@ -126,11 +127,12 @@
 
   // Lifetime duration in milliseconds of a DhcpClient state, or transition
   // time in milliseconds between specific pairs of DhcpClient's states.
-  // Only populated when state_transition is populated.
+  // Only populated since version 1, when state_transition is populated.
   optional int32 duration_ms = 4;
 }
 
 // Represents the generation of an Android Packet Filter program.
+// Since version 1.
 message ApfProgramEvent {
   // Lifetime of the program in seconds.
   optional int64 lifetime = 1;
@@ -154,6 +156,7 @@
 
 // Represents Router Advertisement listening statistics for an interface with
 // Android Packet Filter enabled.
+// Since version 1.
 message ApfStatistics {
   // The time interval in milliseconds these stastistics cover.
   optional int64 duration_ms = 1;
@@ -183,6 +186,7 @@
 
 // Represents the reception of a Router Advertisement packet for an interface
 // with Android Packet Filter enabled.
+// Since version 1.
 message RaEvent {
   // All lifetime values are expressed in seconds. The default value for an
   // option lifetime that was not present in the RA option list is -1.
@@ -271,4 +275,11 @@
 
   // The number of events that had to be dropped due to a full buffer.
   optional int32 dropped_events = 2;
+
+  // The version number of the metrics events being collected.
+  //  nyc-dev: not populated, implicitly 0
+  //  nyc-dr1: not populated, implicitly 1 (sailfish and marlin only)
+  //  nyc-mr1: not populated, implicitly 1
+  //  nyc-mr2: 2
+  optional int32 version = 3;
 };
diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
index aed3635..84f0f90 100644
--- a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
+++ b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
@@ -71,7 +71,8 @@
                 "    transport_types: 3",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -93,7 +94,8 @@
                 "    state_transition: \"SomeState\"",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -114,7 +116,8 @@
                 "    state_transition: \"\"",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -160,7 +163,8 @@
                 "    return_codes: 178",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -181,7 +185,8 @@
                 "    latency_ms: 5678",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -200,7 +205,8 @@
                 "    if_name: \"wlan0\"",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -223,7 +229,8 @@
                 "    >",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -248,7 +255,8 @@
                 "    probe_result: 204",
                 "    probe_type: 1",
                 "  >",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -274,7 +282,8 @@
                 "    program_length: 2048",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -305,7 +314,8 @@
                 "    zero_lifetime_ras: 1",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
@@ -332,7 +342,8 @@
                 "    router_lifetime: 2000",
                 "  >",
                 "  time_ms: 1",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, ev);
     }
diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
index 3fc89b9..aa491bb 100644
--- a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
+++ b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.net.ConnectivityMetricsEvent;
 import android.net.IIpConnectivityMetrics;
+import android.net.metrics.ApfProgramEvent;
 import android.net.metrics.ApfStats;
 import android.net.metrics.DefaultNetworkEvent;
 import android.net.metrics.DhcpClientEvent;
@@ -57,7 +58,7 @@
 
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mService = new IpConnectivityMetrics(mCtx);
+        mService = new IpConnectivityMetrics(mCtx, (ctx) -> 2000);
     }
 
     public void testLoggingEvents() throws Exception {
@@ -112,6 +113,27 @@
         assertEquals("", output3);
     }
 
+    public void testRateLimiting() {
+        final IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
+        final ApfProgramEvent ev = new ApfProgramEvent(0, 0, 0, 0, 0);
+        final long fakeTimestamp = 1;
+
+        int attempt = 100; // More than burst quota, but less than buffer size.
+        for (int i = 0; i < attempt; i++) {
+            logger.log(ev);
+        }
+
+        String output1 = getdump("flush");
+        assertFalse("".equals(output1));
+
+        for (int i = 0; i < attempt; i++) {
+            assertFalse("expected event to be dropped", logger.log(fakeTimestamp, ev));
+        }
+
+        String output2 = getdump("flush");
+        assertEquals("", output2);
+    }
+
     public void testEndToEndLogging() {
         IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
 
@@ -204,7 +226,8 @@
                 "    router_lifetime: 2000",
                 "  >",
                 "  time_ms: 700",
-                ">");
+                ">",
+                "version: 2");
 
         verifySerialization(want, getdump("flush"));
     }