Refactor UDP Socket to allow for Mocks

- Change platform/api/udp_socket to an abstract class
- Change platform/posix/udp_socket to an implementation of the abstract class
- Add platform/test/fake_udp_socket for testing

This fake_udp_socket class is needed for my upcoming Networking Pt 2 change
This change includes udp_socket changes from Networking Changes Pt 1 ongoing CL
to make merging the two easier, since this part of that CL seems to have
reached consensus.

Change-Id: Ia18671756d7457594b4fdceffef6c33e5fd4771a
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/1680971
Commit-Queue: Ryan Keane <rwkeane@google.com>
Reviewed-by: mark a. foltz <mfoltz@chromium.org>
Reviewed-by: Max Yakimakha <yakimakha@chromium.org>
Reviewed-by: Yuri Wiitala <miu@chromium.org>
diff --git a/platform/api/udp_socket_unittest.cc b/platform/api/udp_socket_unittest.cc
new file mode 100644
index 0000000..7814032
--- /dev/null
+++ b/platform/api/udp_socket_unittest.cc
@@ -0,0 +1,37 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "platform/api/udp_socket.h"
+
+#include "gtest/gtest.h"
+#include "platform/test/mock_udp_socket.h"
+
+namespace openscreen {
+namespace platform {
+
+// Under some conditions, calling a callback can result in an exception
+// "terminate called after throwing an instance of 'std::bad_function_call'"
+// which will then crash the running code. This test ensures that deleting a
+// new, unmodified UDP Socket object doesn't hit this edge case.
+TEST(UdpSocketTest, TestDeletionWithoutCallbackSet) {
+  UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4);
+  delete socket;
+}
+
+TEST(UdpSocketTest, TestCallbackCalledOnDeletion) {
+  UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4);
+  int call_count = 0;
+  std::function<void(UdpSocket*)> callback = [&call_count](UdpSocket* socket) {
+    call_count++;
+  };
+  socket->SetDeletionCallback(callback);
+
+  EXPECT_EQ(call_count, 0);
+  delete socket;
+
+  EXPECT_EQ(call_count, 1);
+}
+
+}  // namespace platform
+}  // namespace openscreen