Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Maid 746 #154

Open
wants to merge 10 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/maidsafe/vault/tests/fake_routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FakeRouting {
template <typename DataType, typename CompletionToken>
PutReturn<CompletionToken> Put(Address /*to*/, DataType /*data*/, CompletionToken token) {
auto random(RandomInt32());
if (random >= 0 || std::abs(random) % 2 == 0 || std::abs(random) % 5 == 0)
if (random >= 0 || random % 2 == 0 || random % 5 == 0)
token(MakeError(CommonErrors::success));
else
token(MakeError(CommonErrors::defaulted));
Expand Down
173 changes: 173 additions & 0 deletions src/maidsafe/vault/tests/maid_manager_account_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* Copyright 2012 MaidSafe.net limited

This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License,
version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
licence you accepted on initial access to the Software (the "Licences").

By contributing code to the MaidSafe Software, or to this project generally, you agree to be
bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root
directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also
available at: http://www.maidsafe.net/licenses

Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed
under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied.

See the Licences for the specific language governing permissions and limitations relating to
use of the MaidSafe Software. */

#include "maidsafe/common/test.h"
#include "maidsafe/common/utils.h"
#include "maidsafe/common/data_types/immutable_data.h"

#include "maidsafe/vault/maid_manager/account.h"

namespace maidsafe {

namespace vault {

namespace test {


TEST(MaidManagerAccountTest, BEH_Construct) {
{
MaidManagerAccount::AccountName name(Identity(RandomAlphaNumericBytes(64)));
uint64_t data_stored(1), space_available(1);
MaidManagerAccount account(name, data_stored, space_available);

ASSERT_EQ(name, account.name());
ASSERT_EQ(data_stored, account.data_stored());
ASSERT_EQ(space_available, account.space_available());

ASSERT_NO_THROW(MaidManagerAccount account_copy(account));
ASSERT_NO_THROW(MaidManagerAccount(std::move(account)));
}
{
MaidManagerAccount::AccountName name1(Identity(RandomAlphaNumericBytes(64)));
uint64_t data1_stored(1), space1_available(1);
MaidManagerAccount account1(name1, data1_stored, space1_available);

ASSERT_EQ(name1, account1.name());
ASSERT_EQ(data1_stored, account1.data_stored());
ASSERT_EQ(space1_available, account1.space_available());

MaidManagerAccount::AccountName name2(Identity(RandomAlphaNumericBytes(64)));
uint64_t data2_stored(2), space2_available(2);
MaidManagerAccount account2(name2, data2_stored, space2_available);

ASSERT_EQ(name2, account2.name());
ASSERT_EQ(data2_stored, account2.data_stored());
ASSERT_EQ(space2_available, account2.space_available());

ASSERT_NO_THROW(account2 = account1);
ASSERT_NE(name2, account2.name());
ASSERT_NE(data2_stored, account2.data_stored());
ASSERT_NE(space2_available, account2.space_available());
ASSERT_EQ(account2.name(), account1.name());
ASSERT_EQ(account2.data_stored(), account1.data_stored());
ASSERT_EQ(account2.space_available(), account1.space_available());
}
}

TEST(MaidManagerAccountTest, BEH_SerialiseParse) {
MaidManagerAccount::AccountName name(Identity(RandomAlphaNumericBytes(64)));
uint64_t data_stored(0), space_available(100);
MaidManagerAccount account1(name, data_stored, space_available);

ASSERT_EQ(name, account1.name());
ASSERT_EQ(data_stored, account1.data_stored());
ASSERT_EQ(space_available, account1.space_available());

std::string serialised_account;
ASSERT_NO_THROW(serialised_account = account1.serialise());
MaidManagerAccount account2(serialised_account);

ASSERT_EQ(name, account2.name());
ASSERT_EQ(data_stored, account2.data_stored());
ASSERT_EQ(space_available, account2.space_available());
}

TEST(MaidManagerAccountTest, BEH_EqualityInequality) {
MaidManagerAccount::AccountName name(Identity(RandomAlphaNumericBytes(64)));
uint64_t data_stored(1), space_available(1);
MaidManagerAccount account1(name, data_stored, space_available),
account2(name, data_stored, space_available),
account3(name, data_stored, space_available + 1);

ASSERT_TRUE(account1 == account2);
ASSERT_FALSE(account1 == account3);
ASSERT_FALSE(account1 != account2);
ASSERT_TRUE(account1 != account3);
}

TEST(MaidManagerAccountTest, BEH_Ordering) {
MaidManagerAccount::AccountName name1(Identity(RandomAlphaNumericBytes(64))),
name2(Identity(RandomAlphaNumericBytes(64)));
const_cast<std::vector<byte>&>(name1.string())[0] = 'A';
const_cast<std::vector<byte>&>(name2.string())[0] = 'B';

uint64_t data_stored(1), space_available(1);
MaidManagerAccount account1(name1, data_stored, space_available),
account2(name2, data_stored, space_available),
account3(name1, data_stored, space_available);

ASSERT_TRUE(account1 <= account3);
ASSERT_TRUE(account1 >= account3);
ASSERT_FALSE(account1 < account3);
ASSERT_FALSE(account1 > account3);

ASSERT_TRUE(account1 <= account2);
ASSERT_FALSE(account1 >= account2);
ASSERT_TRUE(account1 < account2);
ASSERT_FALSE(account1 > account2);
}

TEST(MaidManagerAccountTest, BEH_AllowPut) {
MaidManagerAccount::AccountName name(Identity(RandomAlphaNumericBytes(64)));
uint64_t data_stored(0), space_available(100);
MaidManagerAccount account(name, data_stored, space_available);

ASSERT_EQ(name, account.name());
ASSERT_EQ(data_stored, account.data_stored());
ASSERT_EQ(space_available, account.space_available());

passport::Anpmid anpmid;
passport::Pmid pmid(anpmid);
passport::PublicAnpmid public_anpmid(anpmid);
passport::PublicPmid public_pmid(pmid);

ASSERT_EQ(MaidManagerAccount::Status::kOk, account.AllowPut(public_anpmid));
ASSERT_EQ(MaidManagerAccount::Status::kOk, account.AllowPut(public_pmid));

NonEmptyString content1(RandomBytes(2)), content2(RandomBytes(23)),
content3(RandomBytes(26));
ImmutableData data1(content1), data2(content2), data3(content3);

ASSERT_EQ(MaidManagerAccount::Status::kOk, account.AllowPut(data1));
ASSERT_EQ(MaidManagerAccount::Status::kLowSpace, account.AllowPut(data2));
ASSERT_EQ(MaidManagerAccount::Status::kNoSpace, account.AllowPut(data3));
}

TEST(MaidManagerAccountTest, BEH_PutDelete) {
MaidManagerAccount::AccountName name(Identity(RandomAlphaNumericBytes(64)));
uint64_t data_stored(0), space_available(100);
const uint64_t kSize(100);
MaidManagerAccount account(name, data_stored, space_available);

ASSERT_EQ(name, account.name());
ASSERT_EQ(data_stored, account.data_stored());
ASSERT_EQ(space_available, account.space_available());

account.PutData(kSize);
ASSERT_EQ(space_available, account.data_stored());
ASSERT_EQ(data_stored, account.space_available());
account.DeleteData(kSize);
ASSERT_EQ(data_stored, account.data_stored());
ASSERT_EQ(space_available, account.space_available());
}

} // namespace test

} // namespace vault

} // namespace maidsafe
167 changes: 167 additions & 0 deletions src/maidsafe/vault/tests/maid_manager_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* Copyright 2012 MaidSafe.net limited

This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License,
version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
licence you accepted on initial access to the Software (the "Licences").

By contributing code to the MaidSafe Software, or to this project generally, you agree to be
bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root
directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also
available at: http://www.maidsafe.net/licenses

Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed
under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied.

See the Licences for the specific language governing permissions and limitations relating to
use of the MaidSafe Software. */

#include "maidsafe/common/test.h"
#include "maidsafe/common/utils.h"

#include "maidsafe/vault/maid_manager/maid_manager.h"
#include "maidsafe/vault/vault.h"

namespace maidsafe {

namespace vault {

namespace test {


MaidManager<VaultFacade>::AccountName CreateAccount(MaidManager<VaultFacade>& maid_manager,
uint64_t space_offered) {
passport::Anmaid anmaid;
passport::Maid maid(anmaid);
passport::PublicAnmaid public_anmaid(anmaid);
passport::PublicMaid public_maid(maid);

for (;;) {
try {
maid_manager.HandleCreateAccount(public_maid, public_anmaid, space_offered);
EXPECT_TRUE(maid_manager.HasAccount(public_maid.Name()));
break;
}
catch (const maidsafe_error& error) {
EXPECT_EQ(make_error_code(VaultErrors::failed_to_handle_request), error.code());
EXPECT_FALSE(maid_manager.HasAccount(public_maid.Name()));
}
}

return public_maid.Name();
}

template <typename Data>
std::pair<routing::SourceAddress, Data> GetSourceAddressAndData(
const MaidManager<VaultFacade>::AccountName account_name, uint32_t data_size) {
Data data(NonEmptyString(RandomAlphaNumericString(data_size)));

auto node_address(routing::NodeAddress(routing::Address(account_name.string())));
auto group_address = boost::optional<routing::GroupAddress>();
auto reply_to_address = boost::optional<routing::ReplyToAddress>();
auto source_address(routing::SourceAddress(node_address, group_address, reply_to_address));

return std::make_pair(source_address, data);
}


TEST(MaidManagerTest, BEH_CreateAccount) {
MaidManager<VaultFacade> maid_manager;

passport::Anmaid anmaid;
passport::Maid maid(anmaid);
passport::PublicAnmaid public_anmaid(anmaid);
passport::PublicMaid public_maid(maid);

// FakeRouting has ~75% success rate for creating an account
try {
maid_manager.HandleCreateAccount(public_maid, public_anmaid);
ASSERT_TRUE(maid_manager.HasAccount(public_maid.Name()));
try {
// try creating an account for the same client
maid_manager.HandleCreateAccount(public_maid, public_anmaid);
}
catch (const maidsafe_error& error) {
ASSERT_EQ(make_error_code(VaultErrors::account_already_exists), error.code());
}
}
catch (const maidsafe_error& error) {
ASSERT_EQ(make_error_code(VaultErrors::failed_to_handle_request), error.code());
ASSERT_FALSE(maid_manager.HasAccount(public_maid.Name()));
}
}

TEST(MaidManagerTest, BEH_Put) {
using AccountName = MaidManager<VaultFacade>::AccountName;

MaidManager<VaultFacade> maid_manager;
AccountName account_name(CreateAccount(maid_manager, std::numeric_limits<uint64_t>().max()));

// any reasonable number of puts should succeed
uint32_t puts((RandomUint32() % 1000) + 1);
for (size_t i = 0; i != puts; ++i) {
auto args(GetSourceAddressAndData<ImmutableData>(account_name, 10));
auto result(maid_manager.HandlePut<ImmutableData>(args.first, args.second));

ASSERT_TRUE(result.valid());
ASSERT_EQ(1, result.value().size());
ASSERT_EQ(args.second.Name().string(), result.value()[0].first.data.string());
}
}

TEST(MaidManagerTest, BEH_PutToLimit) {
using AccountName = MaidManager<VaultFacade>::AccountName;

MaidManager<VaultFacade> maid_manager;
AccountName account_name(CreateAccount(maid_manager, 120));

for (size_t i = 0; i != 3; ++i) {
auto args(GetSourceAddressAndData<ImmutableData>(account_name, 10));
auto result(maid_manager.HandlePut<ImmutableData>(args.first, args.second));

ASSERT_TRUE(result.valid());
ASSERT_EQ(1, result.value().size());
ASSERT_EQ(args.second.Name().string(), result.value()[0].first.data.string());
}
}

TEST(MaidManagerTest, BEH_PutPastLimit) {
using AccountName = MaidManager<VaultFacade>::AccountName;

MaidManager<VaultFacade> maid_manager;
AccountName account_name(CreateAccount(maid_manager, 120));

for (size_t i = 0; i != 3; ++i) {
auto args(GetSourceAddressAndData<ImmutableData>(account_name, 10));
auto result(maid_manager.HandlePut<ImmutableData>(args.first, args.second));

ASSERT_TRUE(result.valid());
ASSERT_EQ(1, result.value().size());
ASSERT_EQ(args.second.Name().string(), result.value()[0].first.data.string());
}

auto args(GetSourceAddressAndData<ImmutableData>(account_name, 10));
auto result(maid_manager.HandlePut<ImmutableData>(args.first, args.second));

ASSERT_TRUE(!result.valid());
ASSERT_EQ(make_error_code(CommonErrors::cannot_exceed_limit), result.error().code());
}

TEST(MaidManagerTest, BEH_PutWithoutAccount) {
using AccountName = MaidManager<VaultFacade>::AccountName;

MaidManager<VaultFacade> maid_manager;
AccountName account_name(Identity(RandomAlphaNumericBytes(64)));

auto args(GetSourceAddressAndData<ImmutableData>(account_name, 10));
auto result(maid_manager.HandlePut<ImmutableData>(args.first, args.second));

ASSERT_TRUE(!result.valid());
ASSERT_EQ(make_error_code(VaultErrors::no_such_account), result.error().code());
}

} // namespace test

} // namespace vault

} // namespace maidsafe