PM: Changes in unit tests.
* Adds macros for checking null-ness of pointers in tests. This makes it
safer and less error-prone: gtest seems to be inconsistent in its
treatment of pointer values, so having our own wrappers and making
them perform uniformly is good.
* Regulates the use of ASSERT vs EXPECT in unit tests: the former must
be used if subsequent test logic depends on the condition tested (for
example, a pointer being dereferenced later is not null, or a provider
module initialized correctly). The latter will fail the test but would
allow it to keep executing (aka non-critical failure).
* General revision of existing unit test code. Minor changes in
random_provider.h.
BUG=None
TEST=Builds and passes unit tests.
Change-Id: I2ada0fbd96db4cb0c0d631c2e350873853116fc2
Reviewed-on: https://chromium-review.googlesource.com/184449
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/random_provider_unittest.cc b/policy_manager/random_provider_unittest.cc
index 265050e..b0a6a0a 100644
--- a/policy_manager/random_provider_unittest.cc
+++ b/policy_manager/random_provider_unittest.cc
@@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <string>
+
#include <base/memory/scoped_ptr.h>
#include <gtest/gtest.h>
-#include <string>
#include "policy_manager/random_provider.h"
#include "policy_manager/random_vars.h"
+#include "policy_manager/pmtest_utils.h"
using base::TimeDelta;
using std::string;
@@ -17,44 +19,49 @@
class PmRandomProviderTest : public ::testing::Test {
protected:
virtual void SetUp() {
- ASSERT_TRUE(var_random_seed == NULL);
+ default_timeout_ = TimeDelta::FromSeconds(1);
+
+ // All variables are initially null.
+ PMTEST_ASSERT_NULL(var_random_seed);
+
+ // The provider initializes correctly.
provider_ = new RandomProvider();
- ASSERT_TRUE(provider_);
- EXPECT_TRUE(provider_->Init());
+ PMTEST_ASSERT_NOT_NULL(provider_);
+ ASSERT_TRUE(provider_->Init());
+
+ // The provider initializes all variables with valid objects.
+ PMTEST_EXPECT_NOT_NULL(var_random_seed);
}
virtual void TearDown() {
delete provider_;
provider_ = NULL;
- ASSERT_TRUE(var_random_seed == NULL);
+ PMTEST_EXPECT_NULL(var_random_seed);
}
+ TimeDelta default_timeout_;
+
private:
RandomProvider* provider_;
};
-TEST_F(PmRandomProviderTest, InitFinalize) {
- // The provider should initialize the variable with a valid object.
- ASSERT_TRUE(var_random_seed != NULL);
-}
TEST_F(PmRandomProviderTest, GetRandomValues) {
- string errmsg;
+ // Should not return the same random seed repeatedly.
scoped_ptr<const uint64_t> value(
- var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
- ASSERT_TRUE(value != NULL);
+ var_random_seed->GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(value.get());
- bool always_returns_the_same_value = true;
// Test that at least the returned values are different. This test fails,
// by design, once every 2^320 runs.
+ bool is_same_value = true;
for (int i = 0; i < 5; i++) {
scoped_ptr<const uint64_t> other_value(
- var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
- ASSERT_TRUE(other_value != NULL);
- always_returns_the_same_value = always_returns_the_same_value &&
- *other_value == *value;
+ var_random_seed->GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(other_value.get());
+ is_same_value = is_same_value && *other_value == *value;
}
- EXPECT_FALSE(always_returns_the_same_value);
+ EXPECT_FALSE(is_same_value);
}
} // namespace chromeos_policy_manager