| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <base/scoped_ptr.h> | 5 #include <base/scoped_ptr.h> |
| 6 #include <gtest/gtest.h> | 6 #include <gtest/gtest.h> |
| 7 | 7 |
| 8 #include "src/data_plan.h" | 8 #include "src/data_plan.h" |
| 9 #include "src/json_reader.h" | 9 #include "src/json_reader.h" |
| 10 #include "src/policy.h" | 10 #include "src/policy.h" |
| 11 | 11 |
| 12 namespace cashew { | 12 namespace cashew { |
| 13 | 13 |
| 14 // test fixture for DataPlan unit tests | 14 // test fixture for DataPlan unit tests |
| 15 class DataPlanTest: public ::testing::Test { | 15 class DataPlanTest: public ::testing::Test { |
| 16 protected: | 16 protected: |
| 17 DataPlanTest() : policy_(NULL) {} | 17 DataPlanTest() : policy_(NULL), current_unlimited_plan_(NULL), |
| 18 current_available_metered_plan_(NULL) {} |
| 18 | 19 |
| 19 virtual ~DataPlanTest() { | 20 virtual ~DataPlanTest() { |
| 20 EXPECT_TRUE(policy_ == NULL); | 21 EXPECT_TRUE(policy_ == NULL); |
| 22 EXPECT_TRUE(current_unlimited_plan_ == NULL); |
| 23 EXPECT_TRUE(current_available_metered_plan_ == NULL); |
| 21 } | 24 } |
| 22 | 25 |
| 23 virtual void SetUp() { | 26 virtual void SetUp() { |
| 24 EXPECT_TRUE(policy_ == NULL); | 27 EXPECT_TRUE(policy_ == NULL); |
| 25 policy_ = Policy::GetPolicy(kTestCarrierName); | 28 policy_ = Policy::GetPolicy(kTestCarrierName); |
| 26 ASSERT_TRUE(policy_ != NULL); | 29 ASSERT_TRUE(policy_ != NULL); |
| 30 |
| 31 EXPECT_TRUE(current_unlimited_plan_ == NULL); |
| 32 DataPlan::Type type = DataPlan::kTypeUnlimited; |
| 33 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 34 base::Time now = base::Time::Now(); |
| 35 base::Time start_time = now - twelve_hours; |
| 36 base::Time end_time = now + twelve_hours; |
| 37 Bytes max_bytes = 0; |
| 38 Bytes used_bytes = 5 * 1024 * 1024; |
| 39 current_unlimited_plan_ = new(std::nothrow) DataPlan( |
| 40 "Current Unlimited Plan", type, now, start_time, end_time, max_bytes, |
| 41 used_bytes); |
| 42 ASSERT_TRUE(current_unlimited_plan_ != NULL); |
| 43 |
| 44 EXPECT_TRUE(current_available_metered_plan_ == NULL); |
| 45 type = DataPlan::kTypeMeteredFree; |
| 46 max_bytes = 100 * 1024 * 1024; |
| 47 current_available_metered_plan_ = new(std::nothrow) DataPlan( |
| 48 "Current Available Metered Plan", type, now, start_time, end_time, |
| 49 max_bytes, used_bytes); |
| 50 ASSERT_TRUE(current_available_metered_plan_ != NULL); |
| 27 } | 51 } |
| 28 | 52 |
| 29 virtual void TearDown() { | 53 virtual void TearDown() { |
| 54 delete current_available_metered_plan_; |
| 55 current_available_metered_plan_ = NULL; |
| 56 delete current_unlimited_plan_; |
| 57 current_unlimited_plan_ = NULL; |
| 30 delete policy_; | 58 delete policy_; |
| 31 policy_ = NULL; | 59 policy_ = NULL; |
| 32 } | 60 } |
| 33 | 61 |
| 34 // hardcoded JSON data plan representations | 62 // hardcoded JSON data plan representations |
| 35 static const char *kMeteredWithUsedBytesJSON; | 63 static const char *kMeteredWithUsedBytesJSON; |
| 36 static const char *kMeteredWithNoUsedBytesJSON; | 64 static const char *kMeteredWithNoUsedBytesJSON; |
| 37 static const char *kUnlimitedWithUsedBytesJSON; | 65 static const char *kUnlimitedWithUsedBytesJSON; |
| 38 static const char *kUnlimitedWithNoUsedBytesJSON; | 66 static const char *kUnlimitedWithNoUsedBytesJSON; |
| 39 static const char *k32BitSignedOverflowingMaxBytesJSON; | 67 static const char *k32BitSignedOverflowingMaxBytesJSON; |
| 40 static const char *k32BitUnsignedOverflowingMaxBytesJSON; | 68 static const char *k32BitUnsignedOverflowingMaxBytesJSON; |
| 41 | 69 |
| 42 // test carrier for constructing policy | 70 // test carrier for constructing policy |
| 43 static const char *kTestCarrierName; | 71 static const char *kTestCarrierName; |
| 44 | 72 |
| 73 // test policy used by FromDictionaryValue tests |
| 45 Policy *policy_; | 74 Policy *policy_; |
| 75 |
| 76 // current unlimited plan shared by multiple tests |
| 77 DataPlan *current_unlimited_plan_; |
| 78 |
| 79 // current available metered plan shared by multiple tests |
| 80 DataPlan *current_available_metered_plan_; |
| 46 }; | 81 }; |
| 47 | 82 |
| 48 typedef DataPlanTest DataPlanDeathTest; | 83 typedef DataPlanTest DataPlanDeathTest; |
| 49 | 84 |
| 50 const char *DataPlanTest::kMeteredWithUsedBytesJSON = | 85 const char *DataPlanTest::kMeteredWithUsedBytesJSON = |
| 51 "{\"lastUpdate\" : \"2010-09-20T12:01:00Z\"," | 86 "{\"lastUpdate\" : \"2010-09-20T12:01:00Z\"," |
| 52 "\"planName\" : \"Super-Awesome Chromium OS Plan\"," | 87 "\"planName\" : \"Super-Awesome Chromium OS Plan\"," |
| 53 "\"planType\" : \"METERED_PAID\"," | 88 "\"planType\" : \"METERED_PAID\"," |
| 54 "\"maxBytes\" : 104857600," | 89 "\"maxBytes\" : 104857600," |
| 55 "\"usedBytes\" : 52428800," | 90 "\"usedBytes\" : 52428800," |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 ASSERT_TRUE(value != NULL); | 274 ASSERT_TRUE(value != NULL); |
| 240 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); | 275 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); |
| 241 const DictionaryValue *dict_value = | 276 const DictionaryValue *dict_value = |
| 242 static_cast<const DictionaryValue*>(value.get()); | 277 static_cast<const DictionaryValue*>(value.get()); |
| 243 DataPlan *plan = DataPlan::FromDictionaryValue(dict_value, policy_); | 278 DataPlan *plan = DataPlan::FromDictionaryValue(dict_value, policy_); |
| 244 // we expect the call to have succeeded and max bytes to be set properly | 279 // we expect the call to have succeeded and max bytes to be set properly |
| 245 ASSERT_TRUE(plan != NULL); | 280 ASSERT_TRUE(plan != NULL); |
| 246 EXPECT_EQ(5000000000LL, plan->GetDataBytesMax()); | 281 EXPECT_EQ(5000000000LL, plan->GetDataBytesMax()); |
| 247 } | 282 } |
| 248 | 283 |
| 284 TEST_F(DataPlanTest, TotalBytesAfterCtor) { |
| 285 // this test uses |current_unlimited_plan_| created in fixture SetUp |
| 286 // we expect plan to be initialized with local bytes == 0 and total bytes == |
| 287 // |used_bytes| |
| 288 EXPECT_EQ(5 * 1024 * 1024, current_unlimited_plan_->GetDataBytesUsed()); |
| 289 EXPECT_EQ(0, current_unlimited_plan_->GetLocalBytesUsed()); |
| 290 EXPECT_EQ(5 * 1024 * 1024, current_unlimited_plan_->GetTotalBytesUsed()); |
| 291 } |
| 292 |
| 293 TEST_F(DataPlanTest, TotalBytesAfterSetLocalBytes) { |
| 294 // this test uses |current_unlimited_plan_| created in fixture SetUp |
| 295 Bytes local_bytes = 10 * 1024 * 1024; |
| 296 current_unlimited_plan_->SetLocalBytesUsed(local_bytes); |
| 297 // we expect that, after we set local bytes, total bytes == data bytes + |
| 298 // local bytes |
| 299 EXPECT_EQ(5 * 1024 * 1024, current_unlimited_plan_->GetDataBytesUsed()); |
| 300 EXPECT_EQ(local_bytes, current_unlimited_plan_->GetLocalBytesUsed()); |
| 301 EXPECT_EQ(15 * 1024 * 1024, current_unlimited_plan_->GetTotalBytesUsed()); |
| 302 } |
| 303 |
| 304 TEST_F(DataPlanTest, TotalBytesAfterOverflow) { |
| 305 // this test uses |current_unlimited_plan_| created in fixture SetUp |
| 306 Bytes local_bytes = kint64max; |
| 307 current_unlimited_plan_->SetLocalBytesUsed(local_bytes); |
| 308 // we expect that, after we set local bytes to a value that will cause the |
| 309 // total bytes computation to overflow, the overflow will be detected and |
| 310 // total bytes will be set to kint64max. |
| 311 EXPECT_EQ(5 * 1024 * 1024, current_unlimited_plan_->GetDataBytesUsed()); |
| 312 EXPECT_EQ(local_bytes, current_unlimited_plan_->GetLocalBytesUsed()); |
| 313 EXPECT_EQ(kint64max, current_unlimited_plan_->GetTotalBytesUsed()); |
| 314 } |
| 315 |
| 316 TEST_F(DataPlanTest, IsActiveCurrentPlan) { |
| 317 std::string name = "Current Plan"; |
| 318 DataPlan::Type type = DataPlan::kTypeMeteredFree; |
| 319 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 320 base::Time now = base::Time::Now(); |
| 321 base::Time start_time = now - twelve_hours; |
| 322 base::Time end_time = now + twelve_hours; |
| 323 Bytes max_bytes = 100 * 1024 * 1024; |
| 324 Bytes used_bytes = 5 * 1024 * 1024; |
| 325 DataPlan *plan = new(std::nothrow) DataPlan(name, type, now, start_time, |
| 326 end_time, max_bytes, used_bytes); |
| 327 ASSERT_TRUE(plan != NULL); |
| 328 // we expect that a current plan will be considered active |
| 329 EXPECT_TRUE(plan->IsActive()); |
| 330 delete plan; |
| 331 } |
| 332 |
| 333 TEST_F(DataPlanTest, IsActiveFuturePlan) { |
| 334 std::string name = "Future Plan"; |
| 335 DataPlan::Type type = DataPlan::kTypeMeteredFree; |
| 336 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 337 base::Time now = base::Time::Now(); |
| 338 base::Time start_time = now + twelve_hours; |
| 339 base::Time end_time = now + twelve_hours + twelve_hours; |
| 340 Bytes max_bytes = 100 * 1024 * 1024; |
| 341 Bytes used_bytes = 5 * 1024 * 1024; |
| 342 DataPlan *plan = new(std::nothrow) DataPlan(name, type, now, start_time, |
| 343 end_time, max_bytes, used_bytes); |
| 344 ASSERT_TRUE(plan != NULL); |
| 345 // we expect that a future plan will not be considered active |
| 346 EXPECT_FALSE(plan->IsActive()); |
| 347 delete plan; |
| 348 } |
| 349 |
| 350 TEST_F(DataPlanTest, IsActiveExpiredPlan) { |
| 351 std::string name = "Expired Plan"; |
| 352 DataPlan::Type type = DataPlan::kTypeMeteredFree; |
| 353 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 354 base::Time now = base::Time::Now(); |
| 355 base::Time start_time = now - twelve_hours - twelve_hours; |
| 356 base::Time end_time = now - twelve_hours; |
| 357 Bytes max_bytes = 100 * 1024 * 1024; |
| 358 Bytes used_bytes = 5 * 1024 * 1024; |
| 359 DataPlan *plan = new(std::nothrow) DataPlan(name, type, now, start_time, |
| 360 end_time, max_bytes, used_bytes); |
| 361 ASSERT_TRUE(plan != NULL); |
| 362 // we expect that an expired plan will not be considered active |
| 363 EXPECT_FALSE(plan->IsActive()); |
| 364 delete plan; |
| 365 } |
| 366 |
| 367 TEST_F(DataPlanTest, IsActiveJustStartedPlan) { |
| 368 std::string name = "Just Started Plan"; |
| 369 DataPlan::Type type = DataPlan::kTypeMeteredFree; |
| 370 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 371 base::Time now = base::Time::Now(); |
| 372 base::Time start_time = now; |
| 373 base::Time end_time = now + twelve_hours; |
| 374 Bytes max_bytes = 100 * 1024 * 1024; |
| 375 Bytes used_bytes = 5 * 1024 * 1024; |
| 376 DataPlan *plan = new(std::nothrow) DataPlan(name, type, now, start_time, |
| 377 end_time, max_bytes, used_bytes); |
| 378 ASSERT_TRUE(plan != NULL); |
| 379 // we expect that a plan with start time == now will be considered active |
| 380 EXPECT_TRUE(plan->IsActive()); |
| 381 delete plan; |
| 382 } |
| 383 |
| 384 TEST_F(DataPlanTest, IsActiveJustExpiredPlan) { |
| 385 std::string name = "Just Expired Plan"; |
| 386 DataPlan::Type type = DataPlan::kTypeMeteredFree; |
| 387 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 388 base::Time now = base::Time::Now(); |
| 389 base::Time start_time = now - twelve_hours; |
| 390 base::Time end_time = now; |
| 391 Bytes max_bytes = 100 * 1024 * 1024; |
| 392 Bytes used_bytes = 5 * 1024 * 1024; |
| 393 DataPlan *plan = new(std::nothrow) DataPlan(name, type, now, start_time, |
| 394 end_time, max_bytes, used_bytes); |
| 395 ASSERT_TRUE(plan != NULL); |
| 396 // we expect that a plan with end time == now will not be considered active |
| 397 EXPECT_FALSE(plan->IsActive()); |
| 398 delete plan; |
| 399 } |
| 400 |
| 401 TEST_F(DataPlanTest, IsActiveAvailableMeteredPlan) { |
| 402 // this test uses |current_available_metered_plan_| created in fixture SetUp |
| 403 // we expect that a current metered plan that is not yet consumed will be |
| 404 // considered active |
| 405 EXPECT_TRUE(current_available_metered_plan_->IsActive()); |
| 406 } |
| 407 |
| 408 TEST_F(DataPlanTest, IsActiveConsumedMeteredPlan) { |
| 409 std::string name = "Consumed Metered Plan"; |
| 410 DataPlan::Type type = DataPlan::kTypeMeteredFree; |
| 411 base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12); |
| 412 base::Time now = base::Time::Now(); |
| 413 base::Time start_time = now - twelve_hours; |
| 414 base::Time end_time = now + twelve_hours; |
| 415 Bytes max_bytes = 100 * 1024 * 1024; |
| 416 Bytes used_bytes = 100 * 1024 * 1024; |
| 417 DataPlan *plan = new(std::nothrow) DataPlan(name, type, now, start_time, |
| 418 end_time, max_bytes, used_bytes); |
| 419 ASSERT_TRUE(plan != NULL); |
| 420 // we expect that a current metered plan that is consumed will not be |
| 421 // considered active |
| 422 EXPECT_FALSE(plan->IsActive()); |
| 423 delete plan; |
| 424 } |
| 425 |
| 426 TEST_F(DataPlanTest, IsActiveConsumedLocalMeteredPlan) { |
| 427 // this test uses |current_available_metered_plan_| created in fixture SetUp |
| 428 current_available_metered_plan_->SetLocalBytesUsed(95 * 1024 * 1024); |
| 429 // we expect that a current metered plan that has used bytes < max bytes but |
| 430 // is consumed by virtue of local bytes + used bytes being >= max bytes will |
| 431 // not be considered active |
| 432 EXPECT_FALSE(current_available_metered_plan_->IsActive()); |
| 433 } |
| 434 |
| 435 TEST_F(DataPlanTest, IsActiveUnlimitedPlan) { |
| 436 // this test uses |current_unlimited_plan_| created in fixture SetUp |
| 437 // we expect that a current unlimited plan will be considered active, even if |
| 438 // its total bytes is greater than its (meaningless) max bytes |
| 439 EXPECT_TRUE(current_unlimited_plan_->IsActive()); |
| 440 } |
| 441 |
| 249 } // namespace cashew | 442 } // namespace cashew |
| OLD | NEW |