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 
18 #pragma once
19 
20 #include <unistd.h>
21 
22 #include <memory>
23 #include <string>
24 
25 #include <android-base/file.h>
26 #include <android-base/strings.h>
27 #include <gtest/gtest.h>
28 #include <json/reader.h>
29 #include <json/writer.h>
30 #include <jsonpb/jsonpb.h>
31 #include <jsonpb/verify.h>
32 
33 // JsonSchemaTest test that a given JSON file conforms to a given schema.
34 // This includes:
35 // - libprotobuf can parse the given JSON file using the given Prototype class
36 // - Additional checks on field names of the JSON file, and types of values.
37 
38 namespace android {
39 namespace jsonpb {
40 
41 class JsonSchemaTestConfig {
42  public:
43   virtual ~JsonSchemaTestConfig() = default;
44   virtual std::unique_ptr<google::protobuf::Message> CreateMessage() const = 0;
45   virtual std::string file_path() const = 0;
46   /**
47    * If it returns true, tests are skipped when the file is not found.
48    */
optional()49   virtual bool optional() const {
50     return false;
51   }
52 };
53 using JsonSchemaTestConfigFactory =
54     std::function<std::unique_ptr<JsonSchemaTestConfig>()>;
55 
56 template <typename T>
57 class AbstractJsonSchemaTestConfig : public JsonSchemaTestConfig {
58  public:
AbstractJsonSchemaTestConfig(const std::string & path)59   AbstractJsonSchemaTestConfig(const std::string& path) : file_path_(path){};
CreateMessage()60   std::unique_ptr<google::protobuf::Message> CreateMessage() const override {
61     return std::make_unique<T>();
62   }
file_path()63   std::string file_path() const override { return file_path_; }
64 
65  private:
66   std::string file_path_;
67 };
68 
69 template <typename T>
MakeTestParam(const std::string & path)70 JsonSchemaTestConfigFactory MakeTestParam(const std::string& path) {
71   return [path]() {
72     return std::make_unique<AbstractJsonSchemaTestConfig<T>>(path);
73   };
74 }
75 
76 class JsonSchemaTest
77     : public ::testing::TestWithParam<JsonSchemaTestConfigFactory> {
78  public:
SetUp()79   void SetUp() override {
80     auto&& config =
81         ::testing::TestWithParam<JsonSchemaTestConfigFactory>::GetParam()();
82     file_path_ = config->file_path();
83 
84     if (access(file_path_.c_str(), F_OK) == -1) {
85       ASSERT_EQ(ENOENT, errno) << "File '" << file_path_ << "' is not accessible: "
86                                << strerror(errno);
87       ASSERT_TRUE(config->optional()) << "Missing mandatory file " << file_path_;
88       GTEST_SKIP();
89     }
90     ASSERT_TRUE(android::base::ReadFileToString(file_path_, &json_));
91     ASSERT_FALSE(json_.empty()) << "File '" << file_path_ << "' exists but is empty";
92 
93     object_ = config->CreateMessage();
94     auto res = internal::JsonStringToMessage(json_, object_.get());
95     ASSERT_TRUE(res.ok()) << "Invalid format of file " << file_path_
96                           << ": " << res.error();
97   }
message()98   google::protobuf::Message* message() const {
99     return object_.get();
100   }
101   std::string file_path_;
102   std::string json_;
103   std::unique_ptr<google::protobuf::Message> object_;
104 };
105 
106 // Test that the JSON file has no fields unknown by the schema. See
107 // AllFieldsAreKnown() for more details.
TEST_P(JsonSchemaTest,NoUnknownFields)108 TEST_P(JsonSchemaTest, NoUnknownFields) {
109   std::string error;
110   EXPECT_TRUE(AllFieldsAreKnown(*object_, json_, &error))
111       << "File: " << file_path_ << ": " << error;
112 }
113 
TEST_P(JsonSchemaTest,EqReformattedJson)114 TEST_P(JsonSchemaTest, EqReformattedJson) {
115   std::string error;
116   EXPECT_TRUE(EqReformattedJson(json_, object_.get(), &error))
117       << "File: " << file_path_ << ": " << error;
118 }
119 
120 }  // namespace jsonpb
121 }  // namespace android
122