• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <unistd.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include <android-base/file.h>
25 #include <android-base/strings.h>
26 #include <gtest/gtest.h>
27 #include <json/reader.h>
28 #include <json/writer.h>
29 #include <jsonpb/jsonpb.h>
30 #include <jsonpb/verify.h>
31 
32 // JsonSchemaTest test that a given JSON file conforms to a given schema.
33 // This includes:
34 // - libprotobuf can parse the given JSON file using the given Prototype class
35 // - Additional checks on field names of the JSON file, and types of values.
36 
37 namespace android {
38 namespace jsonpb {
39 
40 class JsonSchemaTestConfig {
41  public:
42   virtual ~JsonSchemaTestConfig() = default;
43   virtual std::unique_ptr<google::protobuf::Message> CreateMessage() const = 0;
44   virtual std::string file_path() const = 0;
45   /**
46    * If it returns true, tests are skipped when the file is not found.
47    */
optional()48   virtual bool optional() const { return false; }
49 };
50 using JsonSchemaTestConfigFactory = std::function<std::unique_ptr<JsonSchemaTestConfig>()>;
51 
52 template <typename T>
53 class AbstractJsonSchemaTestConfig : public JsonSchemaTestConfig {
54  public:
AbstractJsonSchemaTestConfig(const std::string & path)55   AbstractJsonSchemaTestConfig(const std::string& path) : file_path_(path){};
CreateMessage()56   std::unique_ptr<google::protobuf::Message> CreateMessage() const override {
57     return std::make_unique<T>();
58   }
file_path()59   std::string file_path() const override { return file_path_; }
60 
61  private:
62   std::string file_path_;
63 };
64 
65 template <typename T>
MakeTestParam(const std::string & path)66 JsonSchemaTestConfigFactory MakeTestParam(const std::string& path) {
67   return [path]() { return std::make_unique<AbstractJsonSchemaTestConfig<T>>(path); };
68 }
69 
70 class JsonSchemaTest : public ::testing::TestWithParam<JsonSchemaTestConfigFactory> {
71  public:
SetUp()72   void SetUp() override {
73     auto&& config = ::testing::TestWithParam<JsonSchemaTestConfigFactory>::GetParam()();
74     file_path_ = config->file_path();
75 
76     if (access(file_path_.c_str(), F_OK) == -1) {
77       ASSERT_EQ(ENOENT, errno) << "File '" << file_path_
78                                << "' is not accessible: " << strerror(errno);
79       ASSERT_TRUE(config->optional()) << "Missing mandatory file " << file_path_;
80       GTEST_SKIP();
81     }
82     ASSERT_TRUE(android::base::ReadFileToString(file_path_, &json_));
83     ASSERT_FALSE(json_.empty()) << "File '" << file_path_ << "' exists but is empty";
84 
85     object_ = config->CreateMessage();
86     auto res = internal::JsonStringToMessage(json_, object_.get());
87     ASSERT_TRUE(res.ok()) << "Invalid format of file " << file_path_ << ": " << res.error();
88   }
message()89   google::protobuf::Message* message() const { return object_.get(); }
90   std::string file_path_;
91   std::string json_;
92   std::unique_ptr<google::protobuf::Message> object_;
93 };
94 
95 // Test that the JSON file has no fields unknown by the schema. See
96 // AllFieldsAreKnown() for more details.
TEST_P(JsonSchemaTest,NoUnknownFields)97 TEST_P(JsonSchemaTest, NoUnknownFields) {
98   std::string error;
99   EXPECT_TRUE(AllFieldsAreKnown(*object_, json_, &error))
100       << "File: " << file_path_ << ": " << error;
101 }
102 
TEST_P(JsonSchemaTest,EqReformattedJson)103 TEST_P(JsonSchemaTest, EqReformattedJson) {
104   std::string error;
105   EXPECT_TRUE(EqReformattedJson(json_, object_.get(), &error))
106       << "File: " << file_path_ << ": " << error;
107 }
108 
109 }  // namespace jsonpb
110 }  // namespace android
111