github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/fs/ir_manager/gtest_ir_manager.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 21 #include "common/version.h" 22 #include "core/net_ipc/nipc_pkg.h" 23 #include "fs/ir_manager/ir_manager.h" 24 #include "fs/storage_holder.h" 25 #include "fs/util.h" 26 #include "test/fs/gtest_common.h" 27 #include <gtest/gtest.h> 28 29 typedef std::pair<std::string, neb::version> depend_t; 30 void gen_ir(const std::string &name, const neb::version &v, 31 neb::block_height_t height, const std::vector<depend_t> &depends, 32 neb::fs::rocksdb_storage *rs) { 33 34 nbre::NBREIR ir; 35 ir.set_name(name); 36 ir.set_version(v.data()); 37 ir.set_height(height); 38 for (auto &dep : depends) { 39 auto deps = ir.add_depends(); 40 deps->set_name(dep.first); 41 deps->set_version(dep.second.data()); 42 } 43 44 auto size = ir.ByteSizeLong(); 45 neb::bytes buf(size); 46 ir.SerializeToArray((void *)buf.value(), buf.size()); 47 48 neb::fs::ir_manager_helper::update_ir_list(name, rs); 49 neb::fs::ir_manager_helper::update_ir_versions(name, v.data(), rs); 50 neb::fs::ir_manager_helper::deploy_ir(name, v.data(), buf, rs); 51 } 52 53 std::shared_ptr<neb::fs::ir_manager> nbre_ptr; 54 TEST(test_fs, init) { nbre_ptr = std::make_shared<neb::fs::ir_manager>(); } 55 56 TEST(test_fs, read_nbre_by_height_simple) { 57 58 std::string name = "nr"; 59 neb::version v(0, 0, 1); 60 neb::block_height_t height = 123; 61 std::vector<depend_t> depends; 62 auto rs = neb::fs::storage_holder::instance().nbre_db_ptr(); 63 gen_ir(name, v, height, depends, rs); 64 65 auto ret_ptr = nbre_ptr->read_irs(name, height + 1, true); 66 auto ret = *ret_ptr; 67 EXPECT_EQ(ret.size(), 1); 68 auto it = ret.begin(); 69 auto nbre_ir_ptr = it; 70 EXPECT_EQ(nbre_ir_ptr->name(), name); 71 EXPECT_EQ(nbre_ir_ptr->version(), v.data()); 72 EXPECT_EQ(nbre_ir_ptr->height(), height); 73 EXPECT_EQ(nbre_ir_ptr->depends_size(), 0); 74 75 ret_ptr = nbre_ptr->read_irs(name, height, true); 76 ret = *ret_ptr; 77 EXPECT_EQ(ret.size(), 1); 78 it = ret.begin(); 79 nbre_ir_ptr = it; 80 EXPECT_EQ(nbre_ir_ptr->name(), name); 81 EXPECT_EQ(nbre_ir_ptr->version(), v.data()); 82 EXPECT_EQ(nbre_ir_ptr->height(), height); 83 EXPECT_EQ(nbre_ir_ptr->depends_size(), 0); 84 85 ret_ptr = nbre_ptr->read_irs(name, height - 1, true); 86 ret = *ret_ptr; 87 EXPECT_EQ(ret.size(), 0); 88 } 89 90 TEST(test_fs, read_nbre_by_height) { 91 92 std::string name = "dip"; 93 neb::version v(1, 2, 3); 94 neb::block_height_t height = 456; 95 std::vector<depend_t> depends; 96 depends.push_back(std::make_pair("nr", neb::version(0, 0, 1))); 97 auto rs = neb::fs::storage_holder::instance().nbre_db_ptr(); 98 gen_ir(name, v, height, depends, rs); 99 100 auto ret_ptr = nbre_ptr->read_irs(name, height, true); 101 auto ret = *ret_ptr; 102 EXPECT_EQ(ret.size(), 2); 103 auto it = ret.begin(); 104 auto nbre_ir_ptr = it; 105 EXPECT_EQ(nbre_ir_ptr->name(), name); 106 EXPECT_EQ(nbre_ir_ptr->version(), v.data()); 107 EXPECT_EQ(nbre_ir_ptr->height(), height); 108 EXPECT_EQ(nbre_ir_ptr->depends_size(), 1); 109 } 110 111 TEST(test_fs, read_nbre_by_name_version) { 112 113 std::string name = "dip"; 114 neb::version v(1, 2, 3); 115 neb::block_height_t height = 456; 116 117 std::shared_ptr<neb::fs::ir_manager> nbre_ptr = 118 std::make_shared<neb::fs::ir_manager>(); 119 120 auto nbreir_ptr = nbre_ptr->read_ir(name, v.data()); 121 EXPECT_EQ(nbreir_ptr->name(), name); 122 EXPECT_EQ(nbreir_ptr->version(), v.data()); 123 EXPECT_EQ(nbreir_ptr->height(), height); 124 EXPECT_EQ(nbreir_ptr->depends_size(), 1); 125 } 126 127 TEST(test_fs, get_auth_table) {}