From 530437ea7ad67db8c024203ac231f2d8320c3ddb Mon Sep 17 00:00:00 2001 From: Gil Date: Thu, 30 Nov 2017 12:26:16 -0800 Subject: Move sources into firebase::firestore and internal namespaces (#494) * Move sources into firebase::firestore and internal namespaces Combine support and core/util packages; this distinction wasn't really paying its freight. --- Firestore/CMakeLists.txt | 7 +-- .../Example/Firestore.xcodeproj/project.pbxproj | 36 ++++--------- .../Example/Tests/Util/FSTIntegrationTestCase.mm | 8 +-- Firestore/Source/API/FIRCollectionReference.mm | 6 ++- Firestore/Source/Remote/FSTExponentialBackoff.mm | 6 ++- Firestore/core/.clang-format | 8 +++ Firestore/core/src/firebase/firestore/base/port.h | 33 ++++++++++++ .../src/firebase/firestore/util/CMakeLists.txt | 19 +++++++ .../core/src/firebase/firestore/util/autoid.cc | 56 ++++++++++++++++++++ .../core/src/firebase/firestore/util/autoid.h | 33 ++++++++++++ .../src/firebase/firestore/util/secure_random.h | 59 ++++++++++++++++++++++ .../firestore/util/secure_random_arc4random.cc | 37 ++++++++++++++ .../test/firebase/firestore/util/CMakeLists.txt | 23 +++++++++ .../test/firebase/firestore/util/autoid_test.cc | 36 +++++++++++++ .../firebase/firestore/util/secure_random_test.cc | 32 ++++++++++++ Firestore/src/.clang-format | 8 --- Firestore/src/core/util/CMakeLists.txt | 22 -------- Firestore/src/core/util/autoid.cc | 52 ------------------- Firestore/src/core/util/autoid.h | 29 ----------- Firestore/src/support/CMakeLists.txt | 18 ------- Firestore/src/support/port.h | 33 ------------ Firestore/src/support/secure_random.h | 55 -------------------- Firestore/src/support/secure_random_arc4random.cc | 33 ------------ Firestore/test/core/util/CMakeLists.txt | 22 -------- Firestore/test/core/util/autoid_test.cc | 33 ------------ Firestore/test/support/CMakeLists.txt | 22 -------- Firestore/test/support/secure_random_test.cc | 32 ------------ 27 files changed, 361 insertions(+), 397 deletions(-) create mode 100644 Firestore/core/.clang-format create mode 100644 Firestore/core/src/firebase/firestore/base/port.h create mode 100644 Firestore/core/src/firebase/firestore/util/CMakeLists.txt create mode 100644 Firestore/core/src/firebase/firestore/util/autoid.cc create mode 100644 Firestore/core/src/firebase/firestore/util/autoid.h create mode 100644 Firestore/core/src/firebase/firestore/util/secure_random.h create mode 100644 Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc create mode 100644 Firestore/core/test/firebase/firestore/util/CMakeLists.txt create mode 100644 Firestore/core/test/firebase/firestore/util/autoid_test.cc create mode 100644 Firestore/core/test/firebase/firestore/util/secure_random_test.cc delete mode 100644 Firestore/src/.clang-format delete mode 100644 Firestore/src/core/util/CMakeLists.txt delete mode 100644 Firestore/src/core/util/autoid.cc delete mode 100644 Firestore/src/core/util/autoid.h delete mode 100644 Firestore/src/support/CMakeLists.txt delete mode 100644 Firestore/src/support/port.h delete mode 100644 Firestore/src/support/secure_random.h delete mode 100644 Firestore/src/support/secure_random_arc4random.cc delete mode 100644 Firestore/test/core/util/CMakeLists.txt delete mode 100644 Firestore/test/core/util/autoid_test.cc delete mode 100644 Firestore/test/support/CMakeLists.txt delete mode 100644 Firestore/test/support/secure_random_test.cc (limited to 'Firestore') diff --git a/Firestore/CMakeLists.txt b/Firestore/CMakeLists.txt index 119af5e..82e1903 100644 --- a/Firestore/CMakeLists.txt +++ b/Firestore/CMakeLists.txt @@ -14,8 +14,5 @@ include(${PROJECT_SOURCE_DIR}/cmake/external/googletest.cmake) -add_subdirectory(src/support) -add_subdirectory(test/support) - -add_subdirectory(src/core/util) -add_subdirectory(test/core/util) +add_subdirectory(core/src/firebase/firestore/util) +add_subdirectory(core/test/firebase/firestore/util) diff --git a/Firestore/Example/Firestore.xcodeproj/project.pbxproj b/Firestore/Example/Firestore.xcodeproj/project.pbxproj index ea9ae44..437b661 100644 --- a/Firestore/Example/Firestore.xcodeproj/project.pbxproj +++ b/Firestore/Example/Firestore.xcodeproj/project.pbxproj @@ -24,8 +24,8 @@ /* Begin PBXBuildFile section */ 3B843E4C1F3A182900548890 /* remote_store_spec_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 3B843E4A1F3930A400548890 /* remote_store_spec_test.json */; }; - 5463926E1FBEDE28005031AA /* secure_random_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 5463926D1FBEDE28005031AA /* secure_random_test.cc */; }; - 5471DCF91FBF99AC00656CD3 /* autoid_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 5471DCF71FBF99A300656CD3 /* autoid_test.cc */; }; + 54740A571FC914BA00713A1A /* secure_random_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A531FC913E500713A1A /* secure_random_test.cc */; }; + 54740A581FC914F000713A1A /* autoid_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A521FC913E500713A1A /* autoid_test.cc */; }; 54764FAB1FAA0C320085E60A /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54764FAA1FAA0C320085E60A /* string_util_test.cc */; }; 54764FAF1FAA21B90085E60A /* FSTGoogleTestTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */; }; 5491BC721FB44593008B3588 /* FSTIntegrationTestCase.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5491BC711FB44593008B3588 /* FSTIntegrationTestCase.mm */; }; @@ -185,8 +185,8 @@ 3B843E4A1F3930A400548890 /* remote_store_spec_test.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = remote_store_spec_test.json; sourceTree = ""; }; 42491D7DC8C8CD245CC22B93 /* Pods-SwiftBuildTest.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftBuildTest.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftBuildTest/Pods-SwiftBuildTest.debug.xcconfig"; sourceTree = ""; }; 4EBC5F5ABE1FD097EFE5E224 /* Pods-Firestore_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example/Pods-Firestore_Example.release.xcconfig"; sourceTree = ""; }; - 5463926D1FBEDE28005031AA /* secure_random_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = secure_random_test.cc; path = ../../test/support/secure_random_test.cc; sourceTree = ""; }; - 5471DCF71FBF99A300656CD3 /* autoid_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = autoid_test.cc; path = ../../test/core/util/autoid_test.cc; sourceTree = ""; }; + 54740A521FC913E500713A1A /* autoid_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = autoid_test.cc; path = ../../core/test/firebase/firestore/util/autoid_test.cc; sourceTree = ""; }; + 54740A531FC913E500713A1A /* secure_random_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = secure_random_test.cc; path = ../../core/test/firebase/firestore/util/secure_random_test.cc; sourceTree = ""; }; 54764FAA1FAA0C320085E60A /* string_util_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = string_util_test.cc; path = ../../Port/string_util_test.cc; sourceTree = ""; }; 54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = FSTGoogleTestTests.mm; path = GoogleTest/FSTGoogleTestTests.mm; sourceTree = ""; }; 5491BC711FB44593008B3588 /* FSTIntegrationTestCase.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTIntegrationTestCase.mm; sourceTree = ""; }; @@ -368,26 +368,11 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 5463926F1FBEDE2F005031AA /* support */ = { + 54740A561FC913EB00713A1A /* util */ = { isa = PBXGroup; children = ( - 5463926D1FBEDE28005031AA /* secure_random_test.cc */, - ); - name = support; - sourceTree = ""; - }; - 5471DCFA1FBF99B100656CD3 /* core */ = { - isa = PBXGroup; - children = ( - 5471DCFB1FBF99B800656CD3 /* util */, - ); - name = core; - sourceTree = ""; - }; - 5471DCFB1FBF99B800656CD3 /* util */ = { - isa = PBXGroup; - children = ( - 5471DCF71FBF99A300656CD3 /* autoid_test.cc */, + 54740A521FC913E500713A1A /* autoid_test.cc */, + 54740A531FC913E500713A1A /* secure_random_test.cc */, ); name = util; sourceTree = ""; @@ -395,9 +380,8 @@ 54764FAC1FAA0C390085E60A /* GoogleTests */ = { isa = PBXGroup; children = ( - 5471DCFA1FBF99B100656CD3 /* core */, 54764FAD1FAA0C650085E60A /* Port */, - 5463926F1FBEDE2F005031AA /* support */, + 54740A561FC913EB00713A1A /* util */, 54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */, ); name = GoogleTests; @@ -1186,7 +1170,6 @@ DE51B1F81F0D491F0013853F /* FSTWatchChange+Testing.m in Sources */, DE51B1EB1F0D490D0013853F /* FSTWriteGroupTests.mm in Sources */, DE51B2011F0D493E0013853F /* FSTHelpers.m in Sources */, - 5471DCF91FBF99AC00656CD3 /* autoid_test.cc in Sources */, DE51B1F61F0D491B0013853F /* FSTSerializerBetaTests.m in Sources */, DE51B1F01F0D49140013853F /* FSTFieldValueTests.m in Sources */, 5491BC721FB44593008B3588 /* FSTIntegrationTestCase.mm in Sources */, @@ -1195,6 +1178,7 @@ DE51B1EC1F0D49140013853F /* FSTDatabaseIDTests.m in Sources */, DE51B1ED1F0D49140013853F /* FSTDocumentKeyTests.m in Sources */, DE51B1D41F0D48CD0013853F /* FSTViewTests.m in Sources */, + 54740A581FC914F000713A1A /* autoid_test.cc in Sources */, DE51B1F41F0D491B0013853F /* FSTRemoteEventTests.m in Sources */, 54E928241F33953300C1953E /* FSTEventAccumulator.m in Sources */, DE51B1D11F0D48CD0013853F /* FSTTargetIDGeneratorTests.m in Sources */, @@ -1223,7 +1207,6 @@ DE51B1D91F0D490D0013853F /* FSTEagerGarbageCollectorTests.m in Sources */, DE51B1E21F0D490D0013853F /* FSTMutationQueueTests.m in Sources */, DE51B1E81F0D490D0013853F /* FSTLevelDBKeyTests.mm in Sources */, - 5463926E1FBEDE28005031AA /* secure_random_test.cc in Sources */, DE51B1E31F0D490D0013853F /* FSTPersistenceTestHelpers.m in Sources */, DE51B1CF1F0D48CD0013853F /* FSTQueryListenerTests.m in Sources */, DE51B1DA1F0D490D0013853F /* FSTLevelDBLocalStoreTests.m in Sources */, @@ -1234,6 +1217,7 @@ DE51B1E41F0D490D0013853F /* FSTQueryCacheTests.m in Sources */, DE51B1CD1F0D48CD0013853F /* FSTDatabaseInfoTests.m in Sources */, DE51B1F21F0D49140013853F /* FSTPathTests.m in Sources */, + 54740A571FC914BA00713A1A /* secure_random_test.cc in Sources */, DE51B1DD1F0D490D0013853F /* FSTLocalStoreTests.m in Sources */, D5B25474286C9800CE42B8C2 /* FSTTestDispatchQueue.m in Sources */, ); diff --git a/Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm b/Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm index 2c29bf0..3d30a77 100644 --- a/Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm +++ b/Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm @@ -21,7 +21,7 @@ #import #import -#include "Firestore/src/core/util/autoid.h" +#include "Firestore/core/src/firebase/firestore/util/autoid.h" #import "Firestore/Source/API/FIRFirestore+Internal.h" #import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h" @@ -32,6 +32,8 @@ #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h" #import "Firestore/Example/Tests/Util/FSTTestDispatchQueue.h" +using firebase::firestore::util::CreateAutoId; + NS_ASSUME_NONNULL_BEGIN @interface FIRFirestore (Testing) @@ -165,7 +167,7 @@ NS_ASSUME_NONNULL_BEGIN } - (NSString *)documentPath { - std::string autoId = firestore::CreateAutoId(); + std::string autoId = CreateAutoId(); return [NSString stringWithFormat:@"test-collection/%s", autoId.c_str()]; } @@ -174,7 +176,7 @@ NS_ASSUME_NONNULL_BEGIN } - (FIRCollectionReference *)collectionRef { - std::string autoId = firestore::CreateAutoId(); + std::string autoId = CreateAutoId(); NSString *collectionName = [NSString stringWithFormat:@"test-collection-%s", autoId.c_str()]; return [self.db collectionWithPath:collectionName]; } diff --git a/Firestore/Source/API/FIRCollectionReference.mm b/Firestore/Source/API/FIRCollectionReference.mm index 7b487c6..92cccc6 100644 --- a/Firestore/Source/API/FIRCollectionReference.mm +++ b/Firestore/Source/API/FIRCollectionReference.mm @@ -16,7 +16,7 @@ #import "FIRCollectionReference.h" -#include "Firestore/src/core/util/autoid.h" +#include "Firestore/core/src/firebase/firestore/util/autoid.h" #import "Firestore/Source/API/FIRDocumentReference+Internal.h" #import "Firestore/Source/API/FIRQuery+Internal.h" @@ -27,6 +27,8 @@ #import "Firestore/Source/Util/FSTAssert.h" #import "Firestore/Source/Util/FSTUsageValidation.h" +using firebase::firestore::util::CreateAutoId; + NS_ASSUME_NONNULL_BEGIN @interface FIRCollectionReference () @@ -103,7 +105,7 @@ NS_ASSUME_NONNULL_BEGIN } - (FIRDocumentReference *)documentWithAutoID { - NSString *autoID = [NSString stringWithUTF8String:firestore::CreateAutoId().c_str()]; + NSString *autoID = [NSString stringWithUTF8String:CreateAutoId().c_str()]; FSTDocumentKey *key = [FSTDocumentKey keyWithPath:[self.query.path pathByAppendingSegment:autoID]]; return [FIRDocumentReference referenceWithKey:key firestore:self.firestore]; diff --git a/Firestore/Source/Remote/FSTExponentialBackoff.mm b/Firestore/Source/Remote/FSTExponentialBackoff.mm index ad27c25..8077357 100644 --- a/Firestore/Source/Remote/FSTExponentialBackoff.mm +++ b/Firestore/Source/Remote/FSTExponentialBackoff.mm @@ -18,11 +18,13 @@ #include -#include "Firestore/src/support/secure_random.h" +#include "Firestore/core/src/firebase/firestore/util/secure_random.h" #import "Firestore/Source/Util/FSTDispatchQueue.h" #import "Firestore/Source/Util/FSTLogger.h" +using firebase::firestore::util::SecureRandom; + @interface FSTExponentialBackoff () - (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue initialDelay:(NSTimeInterval)initialDelay @@ -37,7 +39,7 @@ @end @implementation FSTExponentialBackoff { - firestore::SecureRandom _secureRandom; + SecureRandom _secureRandom; } - (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue diff --git a/Firestore/core/.clang-format b/Firestore/core/.clang-format new file mode 100644 index 0000000..7a0e321 --- /dev/null +++ b/Firestore/core/.clang-format @@ -0,0 +1,8 @@ +BasedOnStyle: Google +Standard: Cpp11 +ColumnLimit: 80 +BinPackParameters: false +AllowAllParametersOfDeclarationOnNextLine: true +SpacesInContainerLiterals: true +PointerAlignment: Left +AllowShortFunctionsOnASingleLine: None diff --git a/Firestore/core/src/firebase/firestore/base/port.h b/Firestore/core/src/firebase/firestore/base/port.h new file mode 100644 index 0000000..37d1041 --- /dev/null +++ b/Firestore/core/src/firebase/firestore/base/port.h @@ -0,0 +1,33 @@ +/* + * Copyright 2017 Google + * + * 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. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_BASE_PORT_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_BASE_PORT_H_ + +#if defined(__APPLE__) +// On Apple platforms we support building via Cocoapods without CMake. When +// building this way we can't test the presence of features so predefine all +// the platform-support feature macros to their expected values. + +// All supported Apple platforms have arc4random(3). +#define HAVE_ARC4RANDOM 1 + +#else + +#error "Unknown platform." +#endif // defined(__APPLE__) + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_BASE_PORT_H_ diff --git a/Firestore/core/src/firebase/firestore/util/CMakeLists.txt b/Firestore/core/src/firebase/firestore/util/CMakeLists.txt new file mode 100644 index 0000000..ce81363 --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright 2017 Google +# +# 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. + +add_library( + firebase_firestore_util + autoid.cc + secure_random_arc4random.cc +) diff --git a/Firestore/core/src/firebase/firestore/util/autoid.cc b/Firestore/core/src/firebase/firestore/util/autoid.cc new file mode 100644 index 0000000..10a2cde --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/autoid.cc @@ -0,0 +1,56 @@ +/* + * Copyright 2017 Google + * + * 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 "Firestore/core/src/firebase/firestore/util/autoid.h" + +#include +#include + +#include "Firestore/core/src/firebase/firestore/util/secure_random.h" + +namespace firebase { +namespace firestore { +namespace util { + +namespace { + +const int kAutoIdLength = 20; +const char kAutoIdAlphabet[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + +SecureRandom shared_random; + +} // namespace + +std::string CreateAutoId() { + std::string auto_id; + auto_id.reserve(kAutoIdLength); + + // -2 here because: + // * sizeof(kAutoIdAlphabet) includes the trailing null terminator + // * uniform_int_distribution is inclusive on both sizes + std::uniform_int_distribution letters(0, sizeof(kAutoIdAlphabet) - 2); + + for (int i = 0; i < kAutoIdLength; i++) { + int rand_index = letters(shared_random); + auto_id.append(1, kAutoIdAlphabet[rand_index]); + } + return auto_id; +} + +} // namespace util +} // namespace firestore +} // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/util/autoid.h b/Firestore/core/src/firebase/firestore/util/autoid.h new file mode 100644 index 0000000..22d7d7d --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/autoid.h @@ -0,0 +1,33 @@ +/* + * Copyright 2017 Google + * + * 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. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_AUTOID_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_AUTOID_H_ + +#include + +namespace firebase { +namespace firestore { +namespace util { + +// Generates a random ID suitable for use as a document ID. +std::string CreateAutoId(); + +} // namespace util +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_AUTOID_H_ diff --git a/Firestore/core/src/firebase/firestore/util/secure_random.h b/Firestore/core/src/firebase/firestore/util/secure_random.h new file mode 100644 index 0000000..72be1bd --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/secure_random.h @@ -0,0 +1,59 @@ +/* + * Copyright 2017 Google + * + * 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. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_SECURE_RANDOM_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_SECURE_RANDOM_H_ + +#include + +#include + +namespace firebase { +namespace firestore { +namespace util { + +// A "secure" pseudorandom number generator, suitable for generating +// unguessable identifiers. This exists because +// +// * std::mt19937 is blazing fast but its outputs can be guessed once enough +// previous outputs have been observed. +// * std::random_device isn't guaranteed to come from a secure PRNG or be +// fast. +// +// The implementation satisfies the C++11 UniformRandomBitGenerator concept and +// delegates to an implementation that generates high quality random values +// quickly with periodic reseeding. +class SecureRandom { + public: + // C++11 UniformRandomBitGenerator interface + using result_type = uint32_t; + + static constexpr result_type min() { + return std::numeric_limits::min(); + } + + static constexpr result_type max() { + return std::numeric_limits::max(); + } + + result_type operator()(); +}; + +} // namespace util +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_SECURE_RANDOM_H_ diff --git a/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc b/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc new file mode 100644 index 0000000..a76ade3 --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc @@ -0,0 +1,37 @@ +/* + * Copyright 2017 Google + * + * 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 "Firestore/core/src/firebase/firestore/util/secure_random.h" + +#include "Firestore/core/src/firebase/firestore/base/port.h" + +#if HAVE_ARC4RANDOM + +#include + +namespace firebase { +namespace firestore { +namespace util { + +SecureRandom::result_type SecureRandom::operator()() { + return arc4random(); +} + +} // namespace util +} // namespace firestore +} // namespace firebase + +#endif // HAVE_ARC4RANDOM diff --git a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt new file mode 100644 index 0000000..223fa41 --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright 2017 Google +# +# 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. + +cc_test( + firebase_firestore_util_test + autoid_test.cc + secure_random_test.cc +) +target_link_libraries( + firebase_firestore_util_test + firebase_firestore_util +) diff --git a/Firestore/core/test/firebase/firestore/util/autoid_test.cc b/Firestore/core/test/firebase/firestore/util/autoid_test.cc new file mode 100644 index 0000000..730c683 --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/autoid_test.cc @@ -0,0 +1,36 @@ +/* + * Copyright 2017 Google + * + * 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 "Firestore/core/src/firebase/firestore/util/autoid.h" + +#include + +#include "gtest/gtest.h" + +using firebase::firestore::util::CreateAutoId; + +TEST(AutoId, IsSane) { + for (int i = 0; i < 50; i++) { + std::string auto_id = CreateAutoId(); + EXPECT_EQ(20, auto_id.length()); + for (int pos = 0; pos < 20; pos++) { + char c = auto_id[pos]; + EXPECT_TRUE(isalpha(c) || isdigit(c)) + << "Should be printable ascii character: '" << c << "' in \"" + << auto_id << "\""; + } + } +} diff --git a/Firestore/core/test/firebase/firestore/util/secure_random_test.cc b/Firestore/core/test/firebase/firestore/util/secure_random_test.cc new file mode 100644 index 0000000..f96f3de --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/secure_random_test.cc @@ -0,0 +1,32 @@ +/* + * Copyright 2017 Google + * + * 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 "Firestore/core/src/firebase/firestore/util/secure_random.h" + +#include "gtest/gtest.h" + +using firebase::firestore::util::SecureRandom; + +TEST(SecureRandomTest, ResultsAreBounded) { + SecureRandom rng; + + // Verify that values are on the min/max closed interval. + for (int i = 0; i < 1000; i++) { + SecureRandom::result_type value = rng(); + EXPECT_GE(value, rng.min()); + EXPECT_LE(value, rng.max()); + } +} diff --git a/Firestore/src/.clang-format b/Firestore/src/.clang-format deleted file mode 100644 index 7a0e321..0000000 --- a/Firestore/src/.clang-format +++ /dev/null @@ -1,8 +0,0 @@ -BasedOnStyle: Google -Standard: Cpp11 -ColumnLimit: 80 -BinPackParameters: false -AllowAllParametersOfDeclarationOnNextLine: true -SpacesInContainerLiterals: true -PointerAlignment: Left -AllowShortFunctionsOnASingleLine: None diff --git a/Firestore/src/core/util/CMakeLists.txt b/Firestore/src/core/util/CMakeLists.txt deleted file mode 100644 index 6b08d8e..0000000 --- a/Firestore/src/core/util/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2017 Google -# -# 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. - -add_library( - firestore_core_util - autoid.cc -) -target_link_libraries( - firestore_core_util - firestore_support -) diff --git a/Firestore/src/core/util/autoid.cc b/Firestore/src/core/util/autoid.cc deleted file mode 100644 index 762bbcc..0000000 --- a/Firestore/src/core/util/autoid.cc +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/src/core/util/autoid.h" - -#include -#include - -#include "Firestore/src/support/secure_random.h" - -namespace { - -const int kAutoIdLength = 20; -const char kAutoIdAlphabet[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - -firestore::SecureRandom shared_random; - -} // namespace - -namespace firestore { - -std::string CreateAutoId() { - std::string auto_id; - auto_id.reserve(kAutoIdLength); - - // -2 here because: - // * sizeof(kAutoIdAlphabet) includes the trailing null terminator - // * uniform_int_distribution is inclusive on both sizes - std::uniform_int_distribution letters(0, sizeof(kAutoIdAlphabet) - 2); - - for (int i = 0; i < kAutoIdLength; i++) { - int rand_index = letters(shared_random); - auto_id.append(1, kAutoIdAlphabet[rand_index]); - } - return auto_id; -} - -} // namespace firestore diff --git a/Firestore/src/core/util/autoid.h b/Firestore/src/core/util/autoid.h deleted file mode 100644 index 13486af..0000000 --- a/Firestore/src/core/util/autoid.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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. - */ - -#ifndef FIRESTORE_SRC_CORE_UTIL_AUTOID_H_ -#define FIRESTORE_SRC_CORE_UTIL_AUTOID_H_ - -#include - -namespace firestore { - -// Generates a random ID suitable for use as a document ID. -std::string CreateAutoId(); - -} // namespace firestore - -#endif // FIRESTORE_SRC_CORE_UTIL_AUTOID_H_ diff --git a/Firestore/src/support/CMakeLists.txt b/Firestore/src/support/CMakeLists.txt deleted file mode 100644 index 8110c6f..0000000 --- a/Firestore/src/support/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2017 Google -# -# 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. - -add_library( - firestore_support - secure_random_arc4random.cc -) diff --git a/Firestore/src/support/port.h b/Firestore/src/support/port.h deleted file mode 100644 index 6af898a..0000000 --- a/Firestore/src/support/port.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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. - */ - -#ifndef FIRESTORE_SRC_SUPPORT_PORT_H_ -#define FIRESTORE_SRC_SUPPORT_PORT_H_ - -#if defined(__APPLE__) -// On Apple platforms we support building via Cocoapods without CMake. When -// building this way we can't test the presence of features so predefine all -// the platform-support feature macros to their expected values. - -// All supported Apple platforms have arc4random(3). -#define HAVE_ARC4RANDOM 1 - -#else - -#error "Unknown platform." -#endif // defined(__APPLE__) - -#endif // FIRESTORE_SRC_SUPPORT_PORT_H_ diff --git a/Firestore/src/support/secure_random.h b/Firestore/src/support/secure_random.h deleted file mode 100644 index 92a3eaf..0000000 --- a/Firestore/src/support/secure_random.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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. - */ - -#ifndef FIRESTORE_SRC_SUPPORT_SECURE_RANDOM_H_ -#define FIRESTORE_SRC_SUPPORT_SECURE_RANDOM_H_ - -#include - -#include - -namespace firestore { - -// A "secure" pseudorandom number generator, suitable for generating -// unguessable identifiers. This exists because -// -// * std::mt19937 is blazing fast but its outputs can be guessed once enough -// previous outputs have been observed. -// * std::random_device isn't guaranteed to come from a secure PRNG or be -// fast. -// -// The implementation satisfies the C++11 UniformRandomBitGenerator concept and -// delegates to an implementation that generates high quality random values -// quickly with periodic reseeding. -class SecureRandom { - public: - // C++11 UniformRandomBitGenerator interface - using result_type = uint32_t; - - static constexpr result_type min() { - return std::numeric_limits::min(); - } - - static constexpr result_type max() { - return std::numeric_limits::max(); - } - - result_type operator()(); -}; - -} // namespace firestore - -#endif // FIRESTORE_SRC_SUPPORT_SECURE_RANDOM_H_ diff --git a/Firestore/src/support/secure_random_arc4random.cc b/Firestore/src/support/secure_random_arc4random.cc deleted file mode 100644 index 4cd7f9d..0000000 --- a/Firestore/src/support/secure_random_arc4random.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/src/support/secure_random.h" - -#include "Firestore/src/support/port.h" - -#if HAVE_ARC4RANDOM - -#include - -namespace firestore { - -SecureRandom::result_type SecureRandom::operator()() { - return arc4random(); -} - -} // namespace firestore - -#endif // HAVE_ARC4RANDOM diff --git a/Firestore/test/core/util/CMakeLists.txt b/Firestore/test/core/util/CMakeLists.txt deleted file mode 100644 index 1989c0e..0000000 --- a/Firestore/test/core/util/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2017 Google -# -# 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. - -cc_test( - firestore_core_util_test - autoid_test.cc -) -target_link_libraries( - firestore_core_util_test - firestore_core_util -) diff --git a/Firestore/test/core/util/autoid_test.cc b/Firestore/test/core/util/autoid_test.cc deleted file mode 100644 index b17a34e..0000000 --- a/Firestore/test/core/util/autoid_test.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/src/core/util/autoid.h" - -#include - -#include "gtest/gtest.h" - -TEST(AutoId, IsSane) { - for (int i = 0; i < 50; i++) { - std::string auto_id = firestore::CreateAutoId(); - EXPECT_EQ(20, auto_id.length()); - for (int pos = 0; pos < 20; pos++) { - char c = auto_id[pos]; - EXPECT_TRUE(isalpha(c) || isdigit(c)) - << "Should be printable ascii character: '" << c << "' in \"" << auto_id << "\""; - } - } -} diff --git a/Firestore/test/support/CMakeLists.txt b/Firestore/test/support/CMakeLists.txt deleted file mode 100644 index 7de5367..0000000 --- a/Firestore/test/support/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2017 Google -# -# 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. - -cc_test( - firestore_support_test - secure_random_test.cc -) -target_link_libraries( - firestore_support_test - firestore_support -) diff --git a/Firestore/test/support/secure_random_test.cc b/Firestore/test/support/secure_random_test.cc deleted file mode 100644 index 244227b..0000000 --- a/Firestore/test/support/secure_random_test.cc +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/src/support/secure_random.h" - -#include "gtest/gtest.h" - -using firestore::SecureRandom; - -TEST(SecureRandomTest, ResultsAreBounded) { - SecureRandom rng; - - // Verify that values are on the min/max closed interval. - for (int i = 0; i < 1000; i++) { - SecureRandom::result_type value = rng(); - EXPECT_GE(value, rng.min()); - EXPECT_LE(value, rng.max()); - } -} -- cgit v1.2.3