/* * Copyright 2020 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. */ #include #include #include #include "bundler.h" #include "bundler_generated.h" #include "flatbuffers/flatbuffers.h" bool LoadBinarySchema(const char* filename, std::string* binary_schema); bool VerifyBinarySchema(const std::vector& raw_schema); bool CreateBinarySchemaBundle( flatbuffers::FlatBufferBuilder* builder, const std::vector& filenames, std::vector>* vector_map, std::list* bundled_names); int WriteHeaderFile(FILE* fp, const uint8_t* data, size_t data_len); class BundlerTest : public ::testing::Test { public: void SetUp() override {} void TearDown() override {} }; TEST_F(BundlerTest, LoadBinarySchema) { std::string string_schema; ASSERT_FALSE(LoadBinarySchema(nullptr, &string_schema)); ASSERT_DEATH(LoadBinarySchema("test.bfbs", nullptr), ""); ASSERT_TRUE(LoadBinarySchema("test.bfbs", &string_schema)); ASSERT_FALSE(LoadBinarySchema("does_not_exist.bfbs", &string_schema)); } TEST_F(BundlerTest, VerifyBinarySchema) { std::string string_schema; ASSERT_TRUE(LoadBinarySchema("test.bfbs", &string_schema)); std::vector raw_schema(string_schema.begin(), string_schema.end()); ASSERT_TRUE(VerifyBinarySchema(raw_schema)); std::vector bogus_raw_schema(string_schema.begin() + 1, string_schema.end()); ASSERT_FALSE(VerifyBinarySchema(bogus_raw_schema)); } TEST_F(BundlerTest, CreateBinarySchemaBundle) { flatbuffers::FlatBufferBuilder builder; std::vector filenames; std::vector> vector_map; std::list bundled_names; ASSERT_TRUE(CreateBinarySchemaBundle(&builder, filenames, &vector_map, &bundled_names)); ASSERT_EQ(0, vector_map.size()); } TEST_F(BundlerTest, WriteHeaderFile) { std::vector data; data.push_back(0x10); data.push_back(0x11); data.push_back(0x12); data.push_back(0x13); ASSERT_DEATH(WriteHeaderFile(nullptr, data.data(), data.size()), ""); FILE* fp = fopen("/tmp/test.h", "w+"); ASSERT_NE(fp, nullptr); WriteHeaderFile(fp, data.data(), data.size()); fseek(fp, 0L, SEEK_SET); char buf[16]; fread(buf, 1, 15, fp); buf[12] = '\0'; std::string s(buf); ASSERT_EQ("// Generated", s); fclose(fp); unlink("/tmp/test.h"); }