AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
AssetManagerTest.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef AVS_CAPABILITIES_ASSETMANAGER_ACSDKASSETMANAGER_TEST_ASSETMANAGERTEST_H_
17 #define AVS_CAPABILITIES_ASSETMANAGER_ACSDKASSETMANAGER_TEST_ASSETMANAGERTEST_H_
18 
19 #include "ArtifactUnderTest.h"
20 #include "AuthDelegateMock.h"
22 #include "RequesterMetadata.h"
23 #include "TestUtil.h"
25 #include "archive.h"
26 #include "archive_entry.h"
27 
31 using namespace alexaClientSDK::acsdkAssets::davs;
34 using namespace alexaClientSDK::avsCommon::utils;
35 
36 class AssetManagerTest : public Test {
37 public:
38  void SetUp() override {
39  TMP_DIR = createTmpDir("AssetManager");
40  TESTING_DIRECTORY = TMP_DIR + "/davs_testing";
41  BASE_DIR = TMP_DIR + "/davs";
42  DAVS_TMP = TMP_DIR + "/davstmp";
43  DAVS_RESOURCES_DIR = BASE_DIR + "/resources";
44  DAVS_REQUESTS_DIR = BASE_DIR + "/requests";
45  URL_RESOURCES_DIR = "/tmp/urlResources";
46  filesystem::makeDirectory(URL_RESOURCES_DIR);
47 
48  commsHandler = InMemoryAmdCommunicationHandler::create();
49 
50  tarArtifact->commsHandler = commsHandler;
51  unavailableArtifact->commsHandler = commsHandler;
52  tarUrlArtifact->commsHandler = commsHandler;
53  unavailableUrlArtifact->commsHandler = commsHandler;
54  httpUrlArtifact->commsHandler = commsHandler;
55  nonApprovedUrlArtifact->commsHandler = commsHandler;
56 
57  authDelegateMock = AuthDelegateMock::create();
58  // clang-format off
59  allowUrlList = UrlAllowListWrapper::create(
60  {"https://s3.amazonaws.com/alexareminderservice.prod.usamazon.reminder.earcons/echo_system_alerts_reminder_start_v",
61  "https://tinytts.amazon.com/",
62  "https://tinytts-eu-west-1.amazon.com/",
63  "https://tinytts-us-west-2.amazon.com/",
64  "test://"});
65  // clang-format on
66 
69 
70  uploadArtifactFromRequest(tarArtifact->request);
71  uploadArtifactFromRequest(tarUrlArtifact->request, 100);
72 
73  wifiMonitorMock = InternetConnectionMonitorMock::create();
74  davsEndpointHandler = DavsEndpointHandlerV3::create("123");
75  startAssetManager();
76  }
77 
78  void TearDown() override {
79  shutdownAssetManager();
80  filesystem::removeAll(URL_RESOURCES_DIR);
81  filesystem::removeAll(TMP_DIR);
82  }
83 
85  if (assetManager != nullptr) {
86  return;
87  }
88  davsClient = DavsClient::create(DAVS_TMP, authDelegateMock, wifiMonitorMock, davsEndpointHandler);
89  assetManager = AssetManager::create(commsHandler, davsClient, BASE_DIR, authDelegateMock, allowUrlList);
90  ASSERT_NE(assetManager, nullptr);
91  assetManager->onIdleChanged(1);
92  }
93 
95  if (assetManager == nullptr) {
96  return;
97  }
98  assetManager.reset();
99  }
100 
102  const shared_ptr<ArtifactRequest>& request,
103  size_t size = 1,
104  const string& id = "",
105  milliseconds ttlDelta = minutes(60)) {
106  filesystem::makeDirectory(TESTING_DIRECTORY);
107  auto metadata = RequesterMetadata::create(request);
108  auto type = metadata->getRequest()->getRequestType();
109  if (type == Type::DAVS) {
110  auto davsRequest = static_pointer_cast<DavsRequest>(request);
111  if (davsRequest != nullptr) {
112  service.uploadBinaryArtifact(
113  davsRequest->getType(),
114  davsRequest->getKey(),
115  davsRequest->getFilters(),
116  createTarFile(TESTING_DIRECTORY, "target", size),
117  ttlDelta,
118  id);
119  }
120  } else if (type == Type::URL) {
121  auto urlRequest = static_pointer_cast<UrlRequest>(request);
122  if (urlRequest != nullptr) {
123  createTarFile(URL_RESOURCES_DIR, "urlTarget", 1);
124  }
125  }
126  }
127 
128  static string createTarFile(const string& dir, const string& filename, size_t size = 1) {
129  auto tarPath = dir + "/" + filename + ".tar.gz";
130  auto data = string(size, 'a');
131 
132  auto a = archive_write_new();
133  archive_write_add_filter_gzip(a);
134  archive_write_set_format_pax_restricted(a);
135  archive_write_open_filename(a, tarPath.c_str());
136 
137  auto entry = archive_entry_new();
138  archive_entry_set_pathname(entry, filename.c_str());
139  archive_entry_set_size(entry, static_cast<la_int64_t>(data.size()));
140  archive_entry_set_filetype(entry, AE_IFREG);
141  archive_entry_set_perm(entry, 0644);
142  archive_write_header(a, entry);
143  archive_write_data(a, data.c_str(), data.size());
144  archive_entry_free(entry);
145 
146  archive_write_close(a);
147  archive_write_free(a);
148  return tarPath;
149  }
150 
151  string TMP_DIR;
153  string BASE_DIR;
154  string DAVS_TMP;
159 
161  shared_ptr<DavsClient> davsClient;
162  shared_ptr<AssetManager> assetManager;
163  shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegateMock;
164  shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> wifiMonitorMock;
165  shared_ptr<DavsEndpointHandlerV3> davsEndpointHandler;
166  shared_ptr<UrlAllowListWrapper> allowUrlList;
167  shared_ptr<AmdCommunicationInterface> commsHandler;
168 
169  // clang-format off
170  shared_ptr<ArtifactUnderTest> tarArtifact = make_shared<ArtifactUnderTest>( nullptr, DavsRequest::create("test", "tar", {{"filter1", {"value1"}}, {"filter2", {"value2"}}}, Region::NA, ArtifactRequest::UNPACK));
171  shared_ptr<ArtifactUnderTest> unavailableArtifact = make_shared<ArtifactUnderTest>( nullptr, DavsRequest::create("test", "not_found", {{"filter1", {"value1"}}}, Region::NA, ArtifactRequest::UNPACK));
172  shared_ptr<ArtifactUnderTest> tarUrlArtifact = make_shared<ArtifactUnderTest>(nullptr, UrlRequest::create("test:///tmp/urlResources/urlTarget.tar.gz", "urlArtifact", ArtifactRequest::UNPACK));
173  shared_ptr<ArtifactUnderTest> unavailableUrlArtifact = make_shared<ArtifactUnderTest>( nullptr, UrlRequest::create("test:///unavailableUrlArtifact", "unavailableUrlArtifact"));
174  shared_ptr<ArtifactUnderTest> httpUrlArtifact = make_shared<ArtifactUnderTest>(nullptr, UrlRequest::create("http://tinytts.amazon.com/", "httpUrlArtifact"));
175  shared_ptr<ArtifactUnderTest> nonApprovedUrlArtifact = make_shared<ArtifactUnderTest>(nullptr, UrlRequest::create("https://evil.com/", "nonApprovedUrlArtifact"));
176  // clang-format on
177 };
178 
179 #endif // AVS_CAPABILITIES_ASSETMANAGER_ACSDKASSETMANAGER_TEST_ASSETMANAGERTEST_H_
string DAVS_RESOURCES_DIR
Definition: AssetManagerTest.h:155
void SetUp() override
Definition: AssetManagerTest.h:38
shared_ptr< AmdCommunicationInterface > commsHandler
Definition: AssetManagerTest.h:167
static bool useDavsService
Definition: CurlWrapperMock.h:31
void startAssetManager()
Definition: AssetManagerTest.h:84
#define ASSERT_NE(val1, val2)
Definition: gtest.h:1960
Definition: AssetManagerTest.h:36
static bool downloadShallFail
Definition: CurlWrapperMock.h:32
::std::string string
Definition: gtest-port.h:1097
string TMP_DIR
Definition: AssetManagerTest.h:151
Definition: AssetManager.h:32
string DAVS_TMP
Definition: AssetManagerTest.h:154
DavsServiceMock service
Definition: AssetManagerTest.h:160
shared_ptr< DavsEndpointHandlerV3 > davsEndpointHandler
Definition: AssetManagerTest.h:165
void uploadArtifactFromRequest(const shared_ptr< ArtifactRequest > &request, size_t size=1, const string &id="", milliseconds ttlDelta=minutes(60))
Definition: AssetManagerTest.h:101
string BASE_DIR
Definition: AssetManagerTest.h:153
void shutdownAssetManager()
Definition: AssetManagerTest.h:94
Definition: ArtifactRequest.h:27
shared_ptr< AssetManager > assetManager
Definition: AssetManagerTest.h:162
shared_ptr< DavsClient > davsClient
Definition: AssetManagerTest.h:161
Definition: MockHTTP2Connection.cpp:20
shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface > authDelegateMock
Definition: AssetManagerTest.h:163
std::string createTmpDir(const std::string &prefix="test")
Definition: AmdMetricWrapper.h:31
Definition: DavsClient.h:28
string URL_RESOURCES_DIR
Definition: AssetManagerTest.h:157
string DAVS_REQUESTS_DIR
Definition: AssetManagerTest.h:156
static std::shared_ptr< AuthDelegateMock > create()
Definition: AuthDelegateMock.cpp:32
static string createTarFile(const string &dir, const string &filename, size_t size=1)
Definition: AssetManagerTest.h:128
void TearDown() override
Definition: AssetManagerTest.h:78
type
Definition: upload.py:443
static std::shared_ptr< InternetConnectionMonitorMock > create()
Definition: InternetConnectionMonitorMock.cpp:32
string URL_WORKING_DIR
Definition: AssetManagerTest.h:158
Definition: ArtifactHandlerInterface.h:24
shared_ptr< UrlAllowListWrapper > allowUrlList
Definition: AssetManagerTest.h:166
shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::InternetConnectionMonitorInterface > wifiMonitorMock
Definition: AssetManagerTest.h:164
string TESTING_DIRECTORY
Definition: AssetManagerTest.h:152

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0