Add unit test for shared/unshared interface quotas
Test: as follows
- built
- flashed
- booted
- "runtest -x .../netd_unit_test.cpp" passes
Bug: 28362720
Bug: 38143143
Change-Id: I0b962898f9e3d7e86d5c0d0d01b79b3e3543b5ee
diff --git a/libnetdutils/include/netdutils/MockSyscalls.h b/libnetdutils/include/netdutils/MockSyscalls.h
index d6a02b5..f878acb 100644
--- a/libnetdutils/include/netdutils/MockSyscalls.h
+++ b/libnetdutils/include/netdutils/MockSyscalls.h
@@ -54,6 +54,12 @@
socklen_t* srclen));
MOCK_CONST_METHOD2(shutdown, Status(Fd fd, int how));
MOCK_CONST_METHOD1(close, Status(Fd fd));
+
+ MOCK_CONST_METHOD2(fopen,
+ StatusOr<UniqueFile>(const std::string& path, const std::string& mode));
+ MOCK_CONST_METHOD3(vfprintf, StatusOr<int>(FILE* file, const char* format, va_list ap));
+ MOCK_CONST_METHOD3(vfscanf, StatusOr<int>(FILE* file, const char* format, va_list ap));
+ MOCK_CONST_METHOD1(fclose, Status(FILE* file));
};
// For the lifetime of this mock, replace the contents of sSyscalls
diff --git a/libnetdutils/include/netdutils/StatusOr.h b/libnetdutils/include/netdutils/StatusOr.h
index 6afbfcb..d68cced 100644
--- a/libnetdutils/include/netdutils/StatusOr.h
+++ b/libnetdutils/include/netdutils/StatusOr.h
@@ -65,11 +65,7 @@
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const StatusOr<T>& s) {
- os << "StatusOr[status: " << s.status();
- if (isOk(s)) {
- os << ", value: " << s.value();
- }
- return os << "]";
+ return os << "StatusOr[status: " << s.status() << "]";
}
#define ASSIGN_OR_RETURN_IMPL(tmp, lhs, stmt) \
diff --git a/libnetdutils/include/netdutils/Syscalls.h b/libnetdutils/include/netdutils/Syscalls.h
index 48be5a2..eddd4cc 100644
--- a/libnetdutils/include/netdutils/Syscalls.h
+++ b/libnetdutils/include/netdutils/Syscalls.h
@@ -14,8 +14,10 @@
* limitations under the License.
*/
-#ifndef NETUTILS_SYSCALLS_H
-#define NETUTILS_SYSCALLS_H
+#ifndef NETDUTILS_SYSCALLS_H
+#define NETDUTILS_SYSCALLS_H
+
+#include <memory>
#include <poll.h>
#include <sys/eventfd.h>
@@ -27,6 +29,7 @@
#include "netdutils/Status.h"
#include "netdutils/StatusOr.h"
#include "netdutils/UniqueFd.h"
+#include "netdutils/UniqueFile.h"
namespace android {
namespace netdutils {
@@ -67,6 +70,35 @@
virtual Status close(Fd fd) const = 0;
+ virtual StatusOr<UniqueFile> fopen(const std::string& path, const std::string& mode) const = 0;
+
+ virtual StatusOr<int> vfprintf(FILE* file, const char* format, va_list ap) const = 0;
+
+ virtual StatusOr<int> vfscanf(FILE* file, const char* format, va_list ap) const = 0;
+
+ virtual Status fclose(FILE* file) const = 0;
+
+ // va_args helpers
+ // va_start doesn't work when the preceding argument is a reference
+ // type so we're forced to use const char*.
+ StatusOr<int> fprintf(FILE* file, const char* format, ...) const {
+ va_list ap;
+ va_start(ap, format);
+ auto result = vfprintf(file, format, ap);
+ va_end(ap);
+ return result;
+ }
+
+ // va_start doesn't work when the preceding argument is a reference
+ // type so we're forced to use const char*.
+ StatusOr<int> fscanf(FILE* file, const char* format, ...) const {
+ va_list ap;
+ va_start(ap, format);
+ auto result = vfscanf(file, format, ap);
+ va_end(ap);
+ return result;
+ }
+
// Templated helpers that forward directly to methods declared above
template <typename SockaddrT>
StatusOr<SockaddrT> getsockname(Fd sock) const {
@@ -151,4 +183,4 @@
} // namespace netdutils
} // namespace android
-#endif /* NETUTILS_SYSCALLS_H */
+#endif /* NETDUTILS_SYSCALLS_H */
diff --git a/libnetdutils/include/netdutils/UniqueFile.h b/libnetdutils/include/netdutils/UniqueFile.h
new file mode 100644
index 0000000..53552d2
--- /dev/null
+++ b/libnetdutils/include/netdutils/UniqueFile.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#ifndef NETDUTILS_UNIQUEFILE_H
+#define NETDUTILS_UNIQUEFILE_H
+
+#include <stdio.h>
+#include <memory>
+
+#include "netdutils/Fd.h"
+
+namespace android {
+namespace netdutils {
+
+struct UniqueFileDtor {
+ void operator()(FILE* file);
+};
+
+using UniqueFile = std::unique_ptr<FILE, UniqueFileDtor>;
+
+} // namespace netdutils
+} // namespace android
+
+#endif /* NETDUTILS_UNIQUEFILE_H */