summaryrefslogtreecommitdiff
path: root/libjsonpb
diff options
context:
space:
mode:
authorYifan Hong <elsk@google.com>2019-03-04 16:27:11 -0800
committerYifan Hong <elsk@google.com>2019-03-14 11:17:04 -0700
commit4b17e3be722d526e292890a9f75649ab7e084c62 (patch)
tree6a61845ffe39575a300a70b6f59b6fe7cbf39213 /libjsonpb
parent9604da99e1ba7aacef5394824c84678cef09172c (diff)
downloadextras-4b17e3be722d526e292890a9f75649ab7e084c62.tar.gz
libjsonverify: allow option for missing files.
When 'optional' is set to true: - If file is missing, test pass (with logs) - If file is present but empty, test fails - If file is present but in wrong format, test fails Test: vts_processgroup_validate_test Bug: 123664216 Change-Id: Ib6640c3c1ce159c5cac068042af564d98c9bbb1f
Diffstat (limited to 'libjsonpb')
-rw-r--r--libjsonpb/verify/include/jsonpb/json_schema_test.h27
1 files changed, 18 insertions, 9 deletions
diff --git a/libjsonpb/verify/include/jsonpb/json_schema_test.h b/libjsonpb/verify/include/jsonpb/json_schema_test.h
index 9a62ea9a..3db19310 100644
--- a/libjsonpb/verify/include/jsonpb/json_schema_test.h
+++ b/libjsonpb/verify/include/jsonpb/json_schema_test.h
@@ -17,6 +17,8 @@
#pragma once
+#include <unistd.h>
+
#include <memory>
#include <string>
@@ -41,12 +43,11 @@ class JsonSchemaTestConfig {
virtual ~JsonSchemaTestConfig() = default;
virtual std::unique_ptr<google::protobuf::Message> CreateMessage() const = 0;
virtual std::string file_path() const = 0;
- virtual std::string GetFileContent() const {
- std::string content;
- if (!android::base::ReadFileToString(file_path(), &content)) {
- return "";
- }
- return content;
+ /**
+ * If it returns true, tests are skipped when the file is not found.
+ */
+ virtual bool optional() const {
+ return false;
}
};
using JsonSchemaTestConfigFactory =
@@ -79,11 +80,19 @@ class JsonSchemaTest
auto&& config =
::testing::TestWithParam<JsonSchemaTestConfigFactory>::GetParam()();
file_path_ = config->file_path();
- json_ = config->GetFileContent();
- ASSERT_FALSE(json_.empty()) << "Cannot read " << config->file_path();
+
+ if (access(file_path_.c_str(), F_OK) == -1) {
+ ASSERT_EQ(ENOENT, errno) << "File '" << file_path_ << "' is not accessible: "
+ << strerror(errno);
+ ASSERT_TRUE(config->optional()) << "Missing mandatory file " << file_path_;
+ GTEST_SKIP();
+ }
+ ASSERT_TRUE(android::base::ReadFileToString(file_path_, &json_));
+ ASSERT_FALSE(json_.empty()) << "File '" << file_path_ << "' exists but is empty";
+
object_ = config->CreateMessage();
auto res = internal::JsonStringToMessage(json_, object_.get());
- ASSERT_TRUE(res.ok()) << "Invalid format of file " << config->file_path()
+ ASSERT_TRUE(res.ok()) << "Invalid format of file " << file_path_
<< ": " << res.error();
}
google::protobuf::Message* message() const {