1 /*
2  * Copyright (C) 2018 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 <map>
18 #include <string>
19 #include <vector>
20 
21 #include <android-base/file.h>
22 #include <android-base/strings.h>
23 #include <gtest/gtest.h>
24 
25 #include "recovery_utils/parse_install_logs.h"
26 
TEST(ParseInstallLogsTest,EmptyFile)27 TEST(ParseInstallLogsTest, EmptyFile) {
28   TemporaryFile last_install;
29 
30   auto metrics = ParseLastInstall(last_install.path);
31   ASSERT_TRUE(metrics.empty());
32 }
33 
TEST(ParseInstallLogsTest,SideloadSmoke)34 TEST(ParseInstallLogsTest, SideloadSmoke) {
35   TemporaryFile last_install;
36   ASSERT_TRUE(android::base::WriteStringToFile("/cache/recovery/ota.zip\n0\n", last_install.path));
37   auto metrics = ParseLastInstall(last_install.path);
38   ASSERT_EQ(metrics.end(), metrics.find("ota_sideload"));
39 
40   ASSERT_TRUE(android::base::WriteStringToFile("/sideload/package.zip\n0\n", last_install.path));
41   metrics = ParseLastInstall(last_install.path);
42   ASSERT_NE(metrics.end(), metrics.find("ota_sideload"));
43 }
44 
TEST(ParseInstallLogsTest,ParseRecoveryUpdateMetrics)45 TEST(ParseInstallLogsTest, ParseRecoveryUpdateMetrics) {
46   std::vector<std::string> lines = {
47     "/sideload/package.zip",
48     "0",
49     "time_total: 300",
50     "uncrypt_time: 40",
51     "source_build: 4973410",
52     "bytes_written_system: " + std::to_string(1200 * 1024 * 1024),
53     "bytes_stashed_system: " + std::to_string(300 * 1024 * 1024),
54     "bytes_written_vendor: " + std::to_string(40 * 1024 * 1024),
55     "bytes_stashed_vendor: " + std::to_string(50 * 1024 * 1024),
56     "temperature_start: 37000",
57     "temperature_end: 38000",
58     "temperature_max: 39000",
59     "error: 22",
60     "cause: 55",
61   };
62 
63   auto metrics = ParseRecoveryUpdateMetrics(lines);
64 
65   std::map<std::string, int64_t> expected_result = {
66     { "ota_time_total", 300 },         { "ota_uncrypt_time", 40 },
67     { "ota_source_version", 4973410 }, { "ota_written_in_MiBs", 1240 },
68     { "ota_stashed_in_MiBs", 350 },    { "ota_temperature_start", 37000 },
69     { "ota_temperature_end", 38000 },  { "ota_temperature_max", 39000 },
70     { "ota_non_ab_error_code", 22 },   { "ota_non_ab_cause_code", 55 },
71   };
72 
73   ASSERT_EQ(expected_result, metrics);
74 }
75