• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 "apexd_session.h"
18 
19 #include <android-base/file.h>
20 #include <android-base/result-gmock.h>
21 #include <android-base/result.h>
22 #include <android-base/scopeguard.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <errno.h>
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 
29 #include <algorithm>
30 #include <filesystem>
31 #include <fstream>
32 #include <string>
33 
34 #include "apexd_test_utils.h"
35 #include "apexd_utils.h"
36 #include "session_state.pb.h"
37 
38 namespace android {
39 namespace apex {
40 namespace {
41 
42 using android::base::Join;
43 using android::base::make_scope_guard;
44 using android::base::testing::Ok;
45 using ::apex::proto::SessionState;
46 using ::testing::Not;
47 using ::testing::UnorderedElementsAre;
48 
49 // TODO(b/170329726): add unit tests for apexd_sessions.h
50 
TEST(ApexSessionManagerTest,CreateSession)51 TEST(ApexSessionManagerTest, CreateSession) {
52   TemporaryDir td;
53   auto manager = ApexSessionManager::Create(std::string(td.path));
54 
55   auto session = manager->CreateSession(239);
56   ASSERT_RESULT_OK(session);
57   ASSERT_EQ(239, session->GetId());
58   std::string session_dir = std::string(td.path) + "/239";
59   ASSERT_EQ(session_dir, session->GetSessionDir());
60 }
61 
TEST(ApexSessionManagerTest,GetSessionsNoSessionReturnsError)62 TEST(ApexSessionManagerTest, GetSessionsNoSessionReturnsError) {
63   TemporaryDir td;
64   auto manager = ApexSessionManager::Create(std::string(td.path));
65 
66   ASSERT_THAT(manager->GetSession(37), Not(Ok()));
67 }
68 
TEST(ApexSessionManagerTest,GetSessionsReturnsErrorSessionNotCommitted)69 TEST(ApexSessionManagerTest, GetSessionsReturnsErrorSessionNotCommitted) {
70   TemporaryDir td;
71   auto manager = ApexSessionManager::Create(std::string(td.path));
72 
73   auto session = manager->CreateSession(73);
74   ASSERT_RESULT_OK(session);
75   ASSERT_THAT(manager->GetSession(73), Not(Ok()));
76 }
77 
TEST(ApexSessionManagerTest,CreateCommitGetSession)78 TEST(ApexSessionManagerTest, CreateCommitGetSession) {
79   TemporaryDir td;
80   auto manager = ApexSessionManager::Create(std::string(td.path));
81 
82   auto session = manager->CreateSession(23);
83   ASSERT_RESULT_OK(session);
84   session->SetErrorMessage("error");
85   ASSERT_RESULT_OK(session->UpdateStateAndCommit(SessionState::STAGED));
86 
87   auto same_session = manager->GetSession(23);
88   ASSERT_RESULT_OK(same_session);
89   ASSERT_EQ(23, same_session->GetId());
90   ASSERT_EQ("error", same_session->GetErrorMessage());
91   ASSERT_EQ(SessionState::STAGED, same_session->GetState());
92 }
93 
TEST(ApexSessionManagerTest,GetSessionsNoSessionsCommitted)94 TEST(ApexSessionManagerTest, GetSessionsNoSessionsCommitted) {
95   TemporaryDir td;
96   auto manager = ApexSessionManager::Create(std::string(td.path));
97 
98   ASSERT_RESULT_OK(manager->CreateSession(3));
99 
100   auto sessions = manager->GetSessions();
101   ASSERT_EQ(0u, sessions.size());
102 }
103 
TEST(ApexSessionManager,GetSessionsCommittedSessions)104 TEST(ApexSessionManager, GetSessionsCommittedSessions) {
105   TemporaryDir td;
106   auto manager = ApexSessionManager::Create(std::string(td.path));
107 
108   auto session1 = manager->CreateSession(1543);
109   ASSERT_RESULT_OK(session1);
110   ASSERT_RESULT_OK(session1->UpdateStateAndCommit(SessionState::ACTIVATED));
111 
112   auto session2 = manager->CreateSession(179);
113   ASSERT_RESULT_OK(session2);
114   ASSERT_RESULT_OK(session2->UpdateStateAndCommit(SessionState::SUCCESS));
115 
116   // This sessions is not committed, it won't be returned in GetSessions.
117   ASSERT_RESULT_OK(manager->CreateSession(101));
118 
119   auto sessions = manager->GetSessions();
120   std::sort(
121       sessions.begin(), sessions.end(),
122       [](const auto& s1, const auto& s2) { return s1.GetId() < s2.GetId(); });
123 
124   ASSERT_EQ(2u, sessions.size());
125 
126   ASSERT_EQ(179, sessions[0].GetId());
127   ASSERT_EQ(SessionState::SUCCESS, sessions[0].GetState());
128 
129   ASSERT_EQ(1543, sessions[1].GetId());
130   ASSERT_EQ(SessionState::ACTIVATED, sessions[1].GetState());
131 }
132 
TEST(ApexSessionManager,GetSessionsInState)133 TEST(ApexSessionManager, GetSessionsInState) {
134   TemporaryDir td;
135   auto manager = ApexSessionManager::Create(std::string(td.path));
136 
137   auto session1 = manager->CreateSession(43);
138   ASSERT_RESULT_OK(session1);
139   ASSERT_RESULT_OK(session1->UpdateStateAndCommit(SessionState::ACTIVATED));
140 
141   auto session2 = manager->CreateSession(41);
142   ASSERT_RESULT_OK(session2);
143   ASSERT_RESULT_OK(session2->UpdateStateAndCommit(SessionState::SUCCESS));
144 
145   auto session3 = manager->CreateSession(23);
146   ASSERT_RESULT_OK(session3);
147   ASSERT_RESULT_OK(session3->UpdateStateAndCommit(SessionState::SUCCESS));
148 
149   auto sessions = manager->GetSessionsInState(SessionState::SUCCESS);
150   std::sort(
151       sessions.begin(), sessions.end(),
152       [](const auto& s1, const auto& s2) { return s1.GetId() < s2.GetId(); });
153 
154   ASSERT_EQ(2u, sessions.size());
155 
156   ASSERT_EQ(23, sessions[0].GetId());
157   ASSERT_EQ(SessionState::SUCCESS, sessions[0].GetState());
158 
159   ASSERT_EQ(41, sessions[1].GetId());
160   ASSERT_EQ(SessionState::SUCCESS, sessions[1].GetState());
161 }
162 
TEST(ApexSessionManagerTest,GetStagedApexDirsSelf)163 TEST(ApexSessionManagerTest, GetStagedApexDirsSelf) {
164   TemporaryDir td;
165   auto manager = ApexSessionManager::Create(std::string(td.path));
166 
167   auto session = manager->CreateSession(239);
168   ASSERT_RESULT_OK(session);
169 
170   ASSERT_THAT(session->GetStagedApexDirs("/path/to/staged_session_dir"),
171               UnorderedElementsAre("/path/to/staged_session_dir/session_239"));
172 }
173 
TEST(ApexSessionManagerTest,GetStagedApexDirsChildren)174 TEST(ApexSessionManagerTest, GetStagedApexDirsChildren) {
175   TemporaryDir td;
176   auto manager = ApexSessionManager::Create(std::string(td.path));
177 
178   auto session = manager->CreateSession(239);
179   ASSERT_RESULT_OK(session);
180   auto child_session_1 = manager->CreateSession(240);
181   ASSERT_RESULT_OK(child_session_1);
182   auto child_session_2 = manager->CreateSession(241);
183   ASSERT_RESULT_OK(child_session_2);
184   session->SetChildSessionIds({240, 241});
185 
186   ASSERT_THAT(session->GetStagedApexDirs("/path/to/staged_session_dir"),
187               UnorderedElementsAre("/path/to/staged_session_dir/session_240",
188                                    "/path/to/staged_session_dir/session_241"));
189 }
190 
191 }  // namespace
192 }  // namespace apex
193 }  // namespace android
194