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/generic_variables_unittest.cc b/policy_manager/generic_variables_unittest.cc
index 15ea3d7..e14dc50 100644
--- a/policy_manager/generic_variables_unittest.cc
+++ b/policy_manager/generic_variables_unittest.cc
@@ -2,61 +2,70 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <base/memory/scoped_ptr.h>
#include <gtest/gtest.h>
+
#include "policy_manager/generic_variables.h"
+#include "policy_manager/pmtest_utils.h"
using base::TimeDelta;
-using std::string;
namespace chromeos_policy_manager {
-TEST(PmCopyVariableTest, SimpleTest) {
- int obj_int = 5;
+class PmCopyVariableTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ default_timeout_ = TimeDelta::FromSeconds(1);
+ }
- CopyVariable<int> var("var", obj_int);
+ TimeDelta default_timeout_;
+};
- string errmsg = "Nope";
- const int* res_1 = var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
- EXPECT_NE(res_1, static_cast<void*>(NULL));
- EXPECT_EQ(5, *res_1);
+TEST_F(PmCopyVariableTest, SimpleTest) {
+ // Tests that copies are generated as intended.
+ int source = 5;
+ CopyVariable<int> var("var", source);
- obj_int = 42;
+ // Generate and validate a copy.
+ scoped_ptr<const int> copy_1(var.GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(copy_1.get());
+ EXPECT_EQ(5, *copy_1);
- // Check the result in res_1 is actually a new copy.
- EXPECT_EQ(5, *res_1);
+ // Assign a different value to the source variable.
+ source = 42;
- const int* res_2 = var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
- EXPECT_NE(res_2, static_cast<void*>(NULL));
- EXPECT_EQ(42, *res_2);
+ // Check that the content of the copy was not affected (distinct instance).
+ EXPECT_EQ(5, *copy_1);
- delete res_1;
- delete res_2;
+ // Generate and validate a second copy.
+ scoped_ptr<const int> copy_2(var.GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(copy_2.get());
+ EXPECT_EQ(42, *copy_2);
}
-class ConstructorTestClass {
- public:
- ConstructorTestClass(void) : copied_(false) {}
- ConstructorTestClass(const ConstructorTestClass& /* ignored */)
+class CopyConstructorTestClass {
+ public:
+ CopyConstructorTestClass(void) : copied_(false) {}
+ CopyConstructorTestClass(const CopyConstructorTestClass& /* ignored */)
: copied_(true) {}
// Tells if the instance was constructed using the copy-constructor.
bool copied_;
};
-TEST(PmCopyVariableTest, UseCopyConstructorTest) {
- ConstructorTestClass obj;
- ASSERT_FALSE(obj.copied_);
- string errmsg;
- CopyVariable<ConstructorTestClass> var("var", obj);
- const ConstructorTestClass* value =
- var.GetValue(TimeDelta::FromSeconds(1), &errmsg);
- EXPECT_NE(value, static_cast<void*>(NULL));
- EXPECT_TRUE(value->copied_);
+TEST_F(PmCopyVariableTest, UseCopyConstructorTest) {
+ // Ensures that CopyVariables indeed uses the copy contructor.
+ const CopyConstructorTestClass source;
+ ASSERT_FALSE(source.copied_);
- delete value;
+ CopyVariable<CopyConstructorTestClass> var("var", source);
+ scoped_ptr<const CopyConstructorTestClass> copy(
+ var.GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(copy.get());
+ EXPECT_TRUE(copy->copied_);
}
} // namespace chromeos_policy_manager
diff --git a/policy_manager/pmtest_utils.h b/policy_manager/pmtest_utils.h
new file mode 100644
index 0000000..6adfec4
--- /dev/null
+++ b/policy_manager/pmtest_utils.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H
+
+// Convenience macros for checking null-ness of pointers.
+//
+// Purportedly, gtest should support pointer comparison when the first argument
+// is a pointer (e.g. NULL). It is therefore appropriate to use
+// {ASSERT,EXPECT}_{EQ,NE} for our purposes. However, gtest fails to realize
+// that NULL is a pointer when used with the _NE variants, unless we explicitly
+// cast it to a pointer type, and so we inject this casting.
+//
+// Note that checking nullness of the content of a scoped_ptr requires getting
+// the inner pointer value via get().
+#define PMTEST_ASSERT_NULL(p) ASSERT_EQ(NULL, p)
+#define PMTEST_ASSERT_NOT_NULL(p) ASSERT_NE(reinterpret_cast<void*>(NULL), p)
+#define PMTEST_EXPECT_NULL(p) EXPECT_EQ(NULL, p)
+#define PMTEST_EXPECT_NOT_NULL(p) EXPECT_NE(reinterpret_cast<void*>(NULL), p)
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PMTEST_UTILS_H
diff --git a/policy_manager/random_provider.cc b/policy_manager/random_provider.cc
index 225b896..431e77b 100644
--- a/policy_manager/random_provider.cc
+++ b/policy_manager/random_provider.cc
@@ -10,6 +10,7 @@
#include <base/stringprintf.h>
#include "policy_manager/random_provider.h"
+#include "policy_manager/variable.h"
using std::string;
@@ -54,9 +55,9 @@
}
private:
- DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable);
-
file_util::ScopedFILE fp_;
+
+ DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable);
};
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