Skip to content

Commit

Permalink
Set return value for add() method
Browse files Browse the repository at this point in the history
Add tests for existing file and denied file

Signed-off-by: dumitru <[email protected]>
  • Loading branch information
x3medima17 committed Jan 17, 2018
1 parent cc8813f commit 6aedbc7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
-B$IROHA_BUILD \
-DCMAKE_BUILD_TYPE=Debug \
-DIROHA_VERSION=$(eval echo "$IROHA_VERSION")
- run:
name: whoami
command: whoami
- run:
name: make
command: |
Expand Down
9 changes: 5 additions & 4 deletions irohad/ametsuchi/impl/flat_file/flat_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ std::unique_ptr<FlatFile> FlatFile::create(const std::string &path) {
return std::make_unique<FlatFile>(*res, path, private_tag{});
}

void FlatFile::add(Identifier id, const std::vector<uint8_t> &block) {
bool FlatFile::add(Identifier id, const std::vector<uint8_t> &block) {
if (id != current_id_ + 1) {
log_->warn("Cannot append non-consecutive block");
return;
return false;
}

auto next_id = id;
Expand All @@ -120,13 +120,13 @@ void FlatFile::add(Identifier id, const std::vector<uint8_t> &block) {
if (boost::filesystem::exists(file_name)) {
// File already exist
log_->warn("insertion for {} failed, because file already exists", id);
return;
return false;
}
// New file will be created
boost::filesystem::ofstream file(file_name.native(), std::ofstream::binary);
if (not file.is_open()) {
log_->warn("Cannot open file by index {} for writing", id);
return;
return false;
}

auto val_size =
Expand All @@ -137,6 +137,7 @@ void FlatFile::add(Identifier id, const std::vector<uint8_t> &block) {

// Update internals, release lock
current_id_ = next_id;
return true;
}

nonstd::optional<std::vector<uint8_t>> FlatFile::get(Identifier id) const {
Expand Down
2 changes: 1 addition & 1 deletion irohad/ametsuchi/impl/flat_file/flat_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace iroha {
* @param id - reference key
* @param blob - data associated with key
*/
void add(Identifier id, const std::vector<uint8_t> &blob);
bool add(Identifier id, const std::vector<uint8_t> &blob);

/**
* Get data associated with
Expand Down
35 changes: 31 additions & 4 deletions test/module/irohad/ametsuchi/flat_file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

#include <sys/stat.h>
#include "ametsuchi/impl/flat_file/flat_file.hpp"
#include "ametsuchi/impl/flat_file/flat_file.cpp"

Expand Down Expand Up @@ -137,13 +138,39 @@ TEST_F(BlStore_Test, CreateFileEmptyDir) {


TEST_F(BlStore_Test, GetNonExistingFile) {
auto store = FlatFile::create(block_store_path);
auto bl_store = FlatFile::create(block_store_path);
Identifier id = 98759385; //random number that will not exist
auto res = store->get(id);
auto res = bl_store->get(id);
ASSERT_EQ(res, nonstd::nullopt);
}

TEST_F(BlStore_Test, GetDirectory) {
auto store = FlatFile::create(block_store_path);
ASSERT_EQ(store->directory(), block_store_path);
auto bl_store = FlatFile::create(block_store_path);
ASSERT_EQ(bl_store->directory(), block_store_path);
}

TEST_F(BlStore_Test, GetDeniedBlock) {
std::vector<uint8_t> block(100000, 5);
auto bl_store = FlatFile::create(block_store_path);
auto id = 1u;
bl_store->add(id, block);

auto filename = boost::filesystem::path{block_store_path} / id_to_name(id);
chmod(filename.string().data(), 0);

auto res = bl_store->get(id);
ASSERT_EQ(res, nonstd::nullopt);
}

TEST_F(BlStore_Test, AddExistingId) {
std::vector<uint8_t> block(100000, 5);
auto bl_store = FlatFile::create(block_store_path);
auto id = 1u;
const auto file_name = boost::filesystem::path{block_store_path} / id_to_name(id);
std::ofstream fout(file_name.string());
fout.close();

auto res = bl_store->add(id, block);
ASSERT_FALSE(res);

}

0 comments on commit 6aedbc7

Please sign in to comment.