Add fuzzer for ParseNetAddress

Bug: 170243740
Test: Builds and runs
Change-Id: I88280e9b5a4980d725a4529d20c474e04fc9eee1
diff --git a/Android.bp b/Android.bp
index eaf89a8..9b3f1ae 100644
--- a/Android.bp
+++ b/Android.bp
@@ -241,3 +241,18 @@
         },
     },
 }
+
+cc_fuzz {
+    name: "libbase_parsenetaddress_fuzzer",
+    shared_libs: ["libbase"],
+    host_supported: true,
+    srcs: ["parsenetaddress_fuzzer.cpp"],
+    dictionary: "parsenetaddress_fuzzer.dict",
+
+    fuzz_config: {
+        cc: [
+            "android-bionic@google.com",
+        ],
+        componentid: 128577
+    }
+}
diff --git a/parsenetaddress_fuzzer.cpp b/parsenetaddress_fuzzer.cpp
new file mode 100644
index 0000000..382c139
--- /dev/null
+++ b/parsenetaddress_fuzzer.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+#include <fuzzer/FuzzedDataProvider.h>
+
+#include "android-base/parsenetaddress.h"
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t length) {
+  FuzzedDataProvider dataProvider(data, length);
+  std::string contents = dataProvider.ConsumeRemainingBytesAsString();
+  std::string canonical, host, error;
+  int port = 123;
+  android::base::ParseNetAddress(contents, &host, &port, &canonical, &error);
+
+  return 0;
+}
diff --git a/parsenetaddress_fuzzer.dict b/parsenetaddress_fuzzer.dict
new file mode 100644
index 0000000..3aa7760
--- /dev/null
+++ b/parsenetaddress_fuzzer.dict
@@ -0,0 +1,34 @@
+# Values from the parsenetaddress_test.cpp unit test
+
+"www.google.com"
+"www.google.com:123"
+"www.google.com"
+"www.google.com:666"
+"1.2.3.4"
+"1.2.3.4:123"
+"1.2.3.4"
+"1.2.3.4:666"
+"::1"
+"[::1]:123"
+"fe80::200:5aee:feaa:20a2"
+"[fe80::200:5aee:feaa:20a2]:123"
+"fe80::200:5aee:feaa:20a2"
+"[::1]:666"
+"[fe80::200:5aee:feaa:20a2]:666"
+"1.2.3.4:"
+"1.2.3.4::"
+":123"
+":1"
+"::::::::1"
+"[::1"
+"[::1]"
+"[::1]:"
+"[::1]::"
+"1.2.3.4:-1"
+"1.2.3.4:0"
+"1.2.3.4:65536"
+"1.2.3.4:hello"
+"[::1]:-1"
+"[::1]:0"
+"[::1]:65536"
+"[::1]:hello"