Skip to content

Commit

Permalink
Moving primitive factories to corresponding subdirectories.
Browse files Browse the repository at this point in the history
Change-Id: I78628a62983c472f0cb5b73ce6f4e68714637398
ORIGINAL_AUTHOR=Bartosz Przydatek <[email protected]>

GitOrigin-RevId: a80036ee74ca7a6dcb7b5f84c2dc395a350cd43a
  • Loading branch information
przydatek authored and thaidn committed Mar 27, 2017
1 parent 0cea72d commit a6c4334
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 73 deletions.
84 changes: 29 additions & 55 deletions cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,34 @@ licenses(["notice"]) # Apache 2.0

# TODO(przydatek): consider adding string_view/string_piece to util
# instead of relying on stringpiece in protobuf.

# public libraries

# TODO(przydatek): how to export .h-files of factories in this "main" library?
# (i.e. headers from //cc/aead:aead_factory and //cc/mac:mac_factory)
cc_library(
name = "cc",
hdrs = [
"aead.h",
"key_manager.h",
"keyset_handle.h",
"mac.h",
"registry.h",
],
visibility = ["//visibility:public"],
deps = [
":aead",
":key_manager",
":keyset_handle",
":mac",
":registry",
],
)

cc_library(
name = "aead",
hdrs = ["aead.h"],
visibility = ["//visibility:public"],
deps = [
"//cc/util:statusor",
"@com_github_google_protobuf//:protobuf_lite",
Expand All @@ -14,6 +39,7 @@ cc_library(
cc_library(
name = "mac",
hdrs = ["mac.h"],
visibility = ["//visibility:public"],
deps = [
"//cc/util:status",
"//cc/util:statusor",
Expand All @@ -25,6 +51,7 @@ cc_library(
name = "registry",
srcs = ["registry.h"],
hdrs = ["registry.h"],
visibility = ["//visibility:public"],
deps = [
":key_manager",
"//cc/util:errors",
Expand All @@ -39,6 +66,7 @@ cc_library(
name = "keyset_handle",
srcs = ["core/keyset_handle.cc"],
hdrs = ["keyset_handle.h"],
visibility = ["//visibility:public"],
deps = [
"//proto:cc_tink",
],
Expand All @@ -48,6 +76,7 @@ cc_library(
name = "key_manager",
srcs = ["key_manager.h"],
hdrs = ["key_manager.h"],
visibility = ["//visibility:public"],
deps = [
"//cc/util:errors",
"//cc/util:status",
Expand All @@ -56,63 +85,8 @@ cc_library(
],
)

cc_library(
name = "aead_factory",
srcs = ["core/aead_factory.cc"],
hdrs = ["aead_factory.h"],
deps = [
":aead",
":key_manager",
":keyset_handle",
"//cc/util:errors",
"//cc/util:status",
"//cc/util:statusor",
],
)

cc_library(
name = "mac_factory",
srcs = ["core/mac_factory.cc"],
hdrs = ["mac_factory.h"],
deps = [
":key_manager",
":keyset_handle",
":mac",
"//cc/util:status",
"//cc/util:statusor",
],
)

# tests

cc_test(
name = "aead_factory_test",
size = "small",
srcs = ["core/aead_factory_test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
":aead",
":aead_factory",
"//cc/util:status",
"//cc/util:statusor",
"@gtest//:gtest",
],
)

cc_test(
name = "mac_factory_test",
size = "small",
srcs = ["core/mac_factory_test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
":mac",
":mac_factory",
"//cc/util:status",
"//cc/util:statusor",
"@gtest//:gtest",
],
)

cc_test(
name = "registry_test",
size = "small",
Expand Down
32 changes: 32 additions & 0 deletions cc/aead/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
licenses(["notice"]) # Apache 2.0

cc_library(
name = "aead_factory",
srcs = ["aead_factory.cc"],
hdrs = ["aead_factory.h"],
visibility = ["//visibility:public"],
deps = [
"//cc:aead",
"//cc:key_manager",
"//cc:keyset_handle",
"//cc/util:errors",
"//cc/util:status",
"//cc/util:statusor",
],
)

# tests

cc_test(
name = "aead_factory_test",
size = "small",
srcs = ["aead_factory_test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
":aead_factory",
"//cc:aead",
"//cc/util:status",
"//cc/util:statusor",
"@gtest//:gtest",
],
)
2 changes: 1 addition & 1 deletion cc/core/aead_factory.cc → cc/aead/aead_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////

#include "cc/aead_factory.h"
#include "cc/aead/aead_factory.h"

#include "cc/aead.h"
#include "cc/util/errors.h"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
////////////////////////////////////////////////////////////////////////////////

#include "cc/aead.h"
#include "cc/aead_factory.h"
#include "cc/aead/aead_factory.h"
#include "cc/util/status.h"
#include "cc/util/statusor.h"
#include "gtest/gtest.h"
Expand Down
32 changes: 21 additions & 11 deletions cc/core/registry_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class RegistryTest : public ::testing::Test {
}
};

template <class K, class F>
class TestAeadKeyManager :
public KeyManager<Aead> {
class TestAeadKeyManager : public KeyManager<Aead> {
public:
TestAeadKeyManager(const std::string& key_type) {
key_types_.push_back(key_type);
Expand Down Expand Up @@ -81,21 +79,33 @@ TEST_F(RegistryTest, testBasic) {
EXPECT_EQ(util::error::NOT_FOUND,
manager_result.status().error_code());

registry.RegisterKeyManager(key_type_1,
new TestAeadKeyManager<AesCtrHmacAeadKey,
AesCtrHmacAeadKeyFormat>(key_type_1));
registry.RegisterKeyManager(key_type_2,
new TestAeadKeyManager<AesGcmKey,
AesGcmKeyFormat>(key_type_2));
TestAeadKeyManager* null_key_manager = nullptr;
util::Status status = registry.RegisterKeyManager(key_type_1,
null_key_manager);
EXPECT_FALSE(status.ok());
EXPECT_EQ(util::error::INVALID_ARGUMENT, status.error_code()) << status;

status = registry.RegisterKeyManager(key_type_1,
new TestAeadKeyManager(key_type_1));
EXPECT_TRUE(status.ok()) << status;

status = registry.RegisterKeyManager(key_type_1,
new TestAeadKeyManager(key_type_1));
EXPECT_FALSE(status.ok());
EXPECT_EQ(util::error::ALREADY_EXISTS, status.error_code()) << status;

status = registry.RegisterKeyManager(key_type_2,
new TestAeadKeyManager(key_type_2));
EXPECT_TRUE(status.ok()) << status;

manager_result = registry.get_manager<Aead>(key_type_1);
EXPECT_TRUE(manager_result.ok());
EXPECT_TRUE(manager_result.ok()) << manager_result.status();
auto manager = manager_result.ValueOrDie();
EXPECT_TRUE(manager->DoesSupport(key_type_1));
EXPECT_FALSE(manager->DoesSupport(key_type_2));

manager_result = registry.get_manager<Aead>(key_type_2);
EXPECT_TRUE(manager_result.ok());
EXPECT_TRUE(manager_result.ok()) << manager_result.status();
manager = manager_result.ValueOrDie();
EXPECT_TRUE(manager->DoesSupport(key_type_2));
EXPECT_FALSE(manager->DoesSupport(key_type_1));
Expand Down
31 changes: 31 additions & 0 deletions cc/mac/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
licenses(["notice"]) # Apache 2.0

cc_library(
name = "mac_factory",
srcs = ["mac_factory.cc"],
hdrs = ["mac_factory.h"],
visibility = ["//visibility:public"],
deps = [
"//cc:key_manager",
"//cc:keyset_handle",
"//cc:mac",
"//cc/util:status",
"//cc/util:statusor",
],
)

# tests

cc_test(
name = "mac_factory_test",
size = "small",
srcs = ["mac_factory_test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
":mac_factory",
"//cc:mac",
"//cc/util:status",
"//cc/util:statusor",
"@gtest//:gtest",
],
)
2 changes: 1 addition & 1 deletion cc/core/mac_factory.cc → cc/mac/mac_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////

#include "cc/mac_factory.h"
#include "cc/mac/mac_factory.h"

#include "cc/mac.h"
#include "google/protobuf/stubs/stringpiece.h"
Expand Down
6 changes: 3 additions & 3 deletions cc/mac_factory.h → cc/mac/mac_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//
///////////////////////////////////////////////////////////////////////////////

#ifndef TINK_MAC_FACTORY_H_
#define TINK_MAC_FACTORY_H_
#ifndef TINK_MAC_MAC_FACTORY_H_
#define TINK_MAC_MAC_FACTORY_H_

#include "cc/key_manager.h"
#include "cc/keyset_handle.h"
Expand Down Expand Up @@ -83,4 +83,4 @@ class MacFactory {
} // namespace crypto
} // namespace cloud

#endif // TINK_MAC_FACTORY_H_
#endif // TINK_MAC_MAC_FACTORY_H_
2 changes: 1 addition & 1 deletion cc/core/mac_factory_test.cc → cc/mac/mac_factory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
////////////////////////////////////////////////////////////////////////////////

#include "cc/mac.h"
#include "cc/mac_factory.h"
#include "cc/mac/mac_factory.h"
#include "cc/util/status.h"
#include "cc/util/statusor.h"
#include "gtest/gtest.h"
Expand Down
4 changes: 4 additions & 0 deletions cc/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class Registry {
template <class P>
util::Status RegisterKeyManager(const std::string& type_url,
KeyManager<P>* manager) {
if (manager == nullptr) {
return util::Status(util::error::INVALID_ARGUMENT,
"Parameter 'manager' must be non-null.");
}
std::unique_ptr<void, void(*)(void*)>
entry(manager, delete_manager<P>);
if (!manager->DoesSupport(type_url)) {
Expand Down

0 comments on commit a6c4334

Please sign in to comment.