| // Copyright 2026 Google LLC | |
| // | |
| // 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 | |
| // | |
| // https://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 "salt/random_salt_generator.h" | |
| #include <string> | |
| #include <utility> | |
| #include "absl/container/flat_hash_set.h" | |
| #include "absl/status/status_matchers.h" | |
| #include "absl/status/statusor.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "salt/salt_generator.h" | |
| namespace credentio { | |
| namespace { | |
| using ::absl_testing::IsOkAndHolds; | |
| using ::testing::SizeIs; | |
| TEST(RandomSaltGeneratorTest, GeneratesSaltOfDefaultLength) { | |
| RandomSaltGenerator salt_generator; | |
| EXPECT_THAT(salt_generator.Generate(), | |
| IsOkAndHolds(SizeIs(kDefaultSaltLength))); | |
| } | |
| TEST(RandomSaltGeneratorTest, DefaultReturnsSameInstance) { | |
| EXPECT_EQ(&RandomSaltGenerator::Default(), &RandomSaltGenerator::Default()); | |
| EXPECT_THAT(RandomSaltGenerator::Default().Generate(), | |
| IsOkAndHolds(SizeIs(kDefaultSaltLength))); | |
| } | |
| TEST(RandomSaltGeneratorTest, GeneratesUniqueSalts) { | |
| constexpr int kNumSalts = 1000; | |
| RandomSaltGenerator salt_generator; | |
| absl::flat_hash_set<std::string> salts; | |
| for (int i = 0; i < kNumSalts; ++i) { | |
| absl::StatusOr<std::string> salt = salt_generator.Generate(); | |
| ABSL_ASSERT_OK(salt); | |
| salts.insert(*std::move(salt)); | |
| } | |
| EXPECT_THAT(salts, SizeIs(kNumSalts)); | |
| } | |
| } // namespace | |
| } // namespace credentio |