1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "odr_metrics_record.h"
18
19 #include <string.h>
20
21 #include <fstream>
22
23 #include "base/common_art_test.h"
24
25 namespace art {
26 namespace odrefresh {
27
28 class OdrMetricsRecordTest : public CommonArtTest {};
29
TEST_F(OdrMetricsRecordTest,HappyPath)30 TEST_F(OdrMetricsRecordTest, HappyPath) {
31 const OdrMetricsRecord expected {
32 .art_apex_version = 0x01233456'789abcde,
33 .trigger = 0x01020304,
34 .stage_reached = 0x11121314,
35 .status = 0x21222324,
36 .primary_bcp_compilation_seconds = 0x31323334,
37 .secondary_bcp_compilation_seconds = 0x41424344,
38 .system_server_compilation_seconds = 0x51525354,
39 .cache_space_free_start_mib = 0x61626364,
40 .cache_space_free_end_mib = 0x71727374
41 };
42
43 ScratchDir dir(/*keep_files=*/false);
44 std::string file_path = dir.GetPath() + "/metrics-record.txt";
45
46 {
47 std::ofstream ofs(file_path);
48 ofs << expected;
49 ASSERT_FALSE(ofs.fail());
50 ofs.close();
51 }
52
53 OdrMetricsRecord actual {};
54 {
55 std::ifstream ifs(file_path);
56 ifs >> actual;
57 ASSERT_TRUE(ifs.eof());
58 }
59
60 ASSERT_EQ(expected.art_apex_version, actual.art_apex_version);
61 ASSERT_EQ(expected.trigger, actual.trigger);
62 ASSERT_EQ(expected.stage_reached, actual.stage_reached);
63 ASSERT_EQ(expected.status, actual.status);
64 ASSERT_EQ(expected.primary_bcp_compilation_seconds, actual.primary_bcp_compilation_seconds);
65 ASSERT_EQ(expected.secondary_bcp_compilation_seconds, actual.secondary_bcp_compilation_seconds);
66 ASSERT_EQ(expected.system_server_compilation_seconds, actual.system_server_compilation_seconds);
67 ASSERT_EQ(expected.cache_space_free_start_mib, actual.cache_space_free_start_mib);
68 ASSERT_EQ(expected.cache_space_free_end_mib, actual.cache_space_free_end_mib);
69 ASSERT_EQ(0, memcmp(&expected, &actual, sizeof(expected)));
70 }
71
TEST_F(OdrMetricsRecordTest,EmptyInput)72 TEST_F(OdrMetricsRecordTest, EmptyInput) {
73 ScratchDir dir(/*keep_files=*/false);
74 std::string file_path = dir.GetPath() + "/metrics-record.txt";
75
76 std::ifstream ifs(file_path);
77 OdrMetricsRecord record;
78 ifs >> record;
79
80 ASSERT_TRUE(ifs.fail());
81 ASSERT_TRUE(!ifs);
82 }
83
TEST_F(OdrMetricsRecordTest,ClosedInput)84 TEST_F(OdrMetricsRecordTest, ClosedInput) {
85 ScratchDir dir(/*keep_files=*/false);
86 std::string file_path = dir.GetPath() + "/metrics-record.txt";
87
88 std::ifstream ifs(file_path);
89 ifs.close();
90
91 OdrMetricsRecord record;
92 ifs >> record;
93
94 ASSERT_TRUE(ifs.fail());
95 ASSERT_TRUE(!ifs);
96 }
97
TEST_F(OdrMetricsRecordTest,ClosedOutput)98 TEST_F(OdrMetricsRecordTest, ClosedOutput) {
99 ScratchDir dir(/*keep_files=*/false);
100 std::string file_path = dir.GetPath() + "/metrics-record.txt";
101
102 std::ofstream ofs(file_path);
103 ofs.close();
104
105 OdrMetricsRecord record {};
106 ofs << record;
107
108 ASSERT_TRUE(ofs.fail());
109 ASSERT_TRUE(!ofs.good());
110 }
111
112 } // namespace odrefresh
113 } // namespace art
114