File: factory_ping_embargo_check_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,660 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/system/factory_ping_embargo_check.h"

#include "base/i18n/time_formatting.h"
#include "base/time/time.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"

namespace ash::system {

namespace {

// Returns a string which can be put into the one of the embargo end date VPD
// variables. If |days_offset| is 0, the return value represents the current
// day. If |days_offset| is positive, the return value represents |days_offset|
// days in the future. If |days_offset| is negative, the return value represents
// |days_offset| days in the past.
// For example, if the test runs on 2018-01-22 and |days_offset| is 3, the
// return value will be "2018-01-25". Similarly, if |days_offset| is -1, the
// return value will be "2018-01-21".
std::string GenerateEmbargoEndDate(int days_offset) {
  const base::Time target_time = base::Time::Now() + base::Days(days_offset);
  const std::string embargo_end_date_string =
      base::UnlocalizedTimeFormatWithPattern(target_time, "yyyy-MM-dd",
                                             icu::TimeZone::getGMT());

  // Sanity check that base::Time::FromUTCString can read back the format used
  // here.
  base::Time reparsed_time;
  EXPECT_TRUE(base::Time::FromUTCString(embargo_end_date_string.c_str(),
                                        &reparsed_time));
  EXPECT_EQ(target_time.ToDeltaSinceWindowsEpoch().InMicroseconds() /
                base::Time::kMicrosecondsPerDay,
            reparsed_time.ToDeltaSinceWindowsEpoch().InMicroseconds() /
                base::Time::kMicrosecondsPerDay);

  return embargo_end_date_string;
}

}  // namespace

class FactoryPingEmbargoCheckTest : public ::testing::Test {
 protected:
  FakeStatisticsProvider statistics_provider_;
};

// No RLZ embargo end date in VPD.
TEST_F(FactoryPingEmbargoCheckTest, NoValue) {
  EXPECT_EQ(FactoryPingEmbargoState::kMissingOrMalformed,
            GetRlzPingEmbargoState(&statistics_provider_));
}

// There is a malformed RLZ embargo end date in VPD.
TEST_F(FactoryPingEmbargoCheckTest, MalformedValue) {
  statistics_provider_.SetMachineStatistic(kRlzEmbargoEndDateKey, "blabla");
  EXPECT_EQ(FactoryPingEmbargoState::kMissingOrMalformed,
            GetRlzPingEmbargoState(&statistics_provider_));
}

// There is an RLZ embargo end date in VPD which is too far in the
// future to be plausible.
TEST_F(FactoryPingEmbargoCheckTest, InvalidValue) {
  statistics_provider_.SetMachineStatistic(
      kRlzEmbargoEndDateKey, GenerateEmbargoEndDate(15 /* days_offset */));
  EXPECT_EQ(FactoryPingEmbargoState::kInvalid,
            GetRlzPingEmbargoState(&statistics_provider_));
}

// The current time is before a (valid and plausible) RLZ embargo end date.
TEST_F(FactoryPingEmbargoCheckTest, EmbargoNotPassed) {
  statistics_provider_.SetMachineStatistic(
      kRlzEmbargoEndDateKey, GenerateEmbargoEndDate(1 /* days_offset */));
  EXPECT_EQ(FactoryPingEmbargoState::kNotPassed,
            GetRlzPingEmbargoState(&statistics_provider_));
}

// The current time is after a (valid and plausible) RLZ embargo end date.
TEST_F(FactoryPingEmbargoCheckTest, EmbargoPassed) {
  statistics_provider_.SetMachineStatistic(
      kRlzEmbargoEndDateKey, GenerateEmbargoEndDate(-1 /* days_offset */));
  EXPECT_EQ(FactoryPingEmbargoState::kPassed,
            GetRlzPingEmbargoState(&statistics_provider_));
}

}  // namespace ash::system