• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "sdc_file.h"
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "android-base/file.h"
24 #include "base/common_art_test.h"
25 #include "base/macros.h"
26 #include "base/os.h"
27 #include "gmock/gmock.h"
28 #include "gtest/gtest.h"
29 
30 namespace art HIDDEN {
31 
32 using ::android::base::ReadFileToString;
33 using ::android::base::WriteStringToFile;
34 using ::testing::HasSubstr;
35 using ::testing::StartsWith;
36 
37 class SdcFileTestBase : public CommonArtTest {
38  protected:
SetUp()39   void SetUp() override {
40     CommonArtTest::SetUp();
41 
42     scratch_dir_ = std::make_unique<ScratchDir>();
43     test_file_ = scratch_dir_->GetPath() + "test.sdc";
44   }
45 
TearDown()46   void TearDown() override {
47     scratch_dir_.reset();
48     CommonArtTest::TearDown();
49   }
50 
51   std::unique_ptr<ScratchDir> scratch_dir_;
52   std::string test_file_;
53 };
54 
55 class SdcReaderTest : public SdcFileTestBase {};
56 
TEST_F(SdcReaderTest,Success)57 TEST_F(SdcReaderTest, Success) {
58   ASSERT_TRUE(WriteStringToFile(
59       "sdm-timestamp-ns=987654321000000003\napex-versions=/12345678/12345679\n", test_file_));
60 
61   std::string error_msg;
62   std::unique_ptr<SdcReader> reader = SdcReader::Load(test_file_, &error_msg);
63   ASSERT_NE(reader, nullptr) << error_msg;
64 
65   EXPECT_EQ(reader->GetApexVersions(), "/12345678/12345679");
66   EXPECT_EQ(reader->GetSdmTimestampNs(), INT64_C(987654321000000003));
67 }
68 
TEST_F(SdcReaderTest,NotFound)69 TEST_F(SdcReaderTest, NotFound) {
70   std::string error_msg;
71   std::unique_ptr<SdcReader> reader = SdcReader::Load(test_file_, &error_msg);
72   ASSERT_EQ(reader, nullptr);
73 
74   EXPECT_THAT(error_msg, StartsWith("Failed to load sdc file"));
75 }
76 
TEST_F(SdcReaderTest,MissingApexVersions)77 TEST_F(SdcReaderTest, MissingApexVersions) {
78   ASSERT_TRUE(WriteStringToFile("sdm-timestamp-ns=987654321\n", test_file_));
79 
80   std::string error_msg;
81   std::unique_ptr<SdcReader> reader = SdcReader::Load(test_file_, &error_msg);
82   ASSERT_EQ(reader, nullptr);
83 
84   EXPECT_THAT(error_msg, StartsWith("Missing key 'apex-versions' in sdc file"));
85 }
86 
TEST_F(SdcReaderTest,InvalidSdmTimestamp)87 TEST_F(SdcReaderTest, InvalidSdmTimestamp) {
88   ASSERT_TRUE(
89       WriteStringToFile("sdm-timestamp-ns=0\napex-versions=/12345678/12345679\n", test_file_));
90 
91   std::string error_msg;
92   std::unique_ptr<SdcReader> reader = SdcReader::Load(test_file_, &error_msg);
93   ASSERT_EQ(reader, nullptr);
94 
95   EXPECT_THAT(error_msg, HasSubstr("Invalid 'sdm-timestamp-ns'"));
96 }
97 
TEST_F(SdcReaderTest,InvalidApexVersions)98 TEST_F(SdcReaderTest, InvalidApexVersions) {
99   ASSERT_TRUE(WriteStringToFile("sdm-timestamp-ns=987654321\napex-versions=abc\n", test_file_));
100 
101   std::string error_msg;
102   std::unique_ptr<SdcReader> reader = SdcReader::Load(test_file_, &error_msg);
103   ASSERT_EQ(reader, nullptr);
104 
105   EXPECT_THAT(error_msg, HasSubstr("Invalid 'apex-versions'"));
106 }
107 
TEST_F(SdcReaderTest,UnrecognizedKey)108 TEST_F(SdcReaderTest, UnrecognizedKey) {
109   ASSERT_TRUE(WriteStringToFile(
110       "sdm-timestamp-ns=987654321\napex-versions=/12345678/12345679\nwrong-key=12345678\n",
111       test_file_));
112 
113   std::string error_msg;
114   std::unique_ptr<SdcReader> reader = SdcReader::Load(test_file_, &error_msg);
115   ASSERT_EQ(reader, nullptr);
116 
117   EXPECT_THAT(error_msg, HasSubstr("Unrecognized keys"));
118 }
119 
120 class SdcWriterTest : public SdcFileTestBase {};
121 
TEST_F(SdcWriterTest,Success)122 TEST_F(SdcWriterTest, Success) {
123   std::unique_ptr<File> file(OS::CreateEmptyFileWriteOnly(test_file_.c_str()));
124   ASSERT_NE(file, nullptr);
125   SdcWriter writer(std::move(*file));
126 
127   writer.SetApexVersions("/12345678/12345679");
128   writer.SetSdmTimestampNs(987654321l);
129 
130   std::string error_msg;
131   ASSERT_TRUE(writer.Save(&error_msg)) << error_msg;
132 
133   std::string content;
134   ASSERT_TRUE(ReadFileToString(test_file_, &content));
135 
136   EXPECT_EQ(content, "sdm-timestamp-ns=987654321\napex-versions=/12345678/12345679\n");
137 }
138 
TEST_F(SdcWriterTest,SaveFailed)139 TEST_F(SdcWriterTest, SaveFailed) {
140   ASSERT_TRUE(WriteStringToFile("", test_file_));
141 
142   std::unique_ptr<File> file(OS::OpenFileForReading(test_file_.c_str()));
143   ASSERT_NE(file, nullptr);
144   SdcWriter writer(
145       File(file->Release(), file->GetPath(), /*check_usage=*/false, /*read_only_mode=*/false));
146 
147   writer.SetApexVersions("/12345678/12345679");
148   writer.SetSdmTimestampNs(987654321l);
149 
150   std::string error_msg;
151   EXPECT_FALSE(writer.Save(&error_msg));
152 
153   EXPECT_THAT(error_msg, StartsWith("Failed to write sdc file"));
154 }
155 
TEST_F(SdcWriterTest,InvalidSdmTimestamp)156 TEST_F(SdcWriterTest, InvalidSdmTimestamp) {
157   std::unique_ptr<File> file(OS::CreateEmptyFileWriteOnly(test_file_.c_str()));
158   ASSERT_NE(file, nullptr);
159   SdcWriter writer(std::move(*file));
160 
161   writer.SetApexVersions("/12345678/12345679");
162 
163   std::string error_msg;
164   EXPECT_FALSE(writer.Save(&error_msg));
165 
166   EXPECT_THAT(error_msg, StartsWith("Invalid 'sdm-timestamp-ns'"));
167 }
168 
169 }  // namespace art
170