|
| 1 | +// |
| 2 | +// Copyright (C) 2026 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 | +#include "cuttlefish/host/libs/web/android_build_api.h" |
| 17 | + |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "gmock/gmock.h" |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +#include "cuttlefish/host/libs/web/android_build.h" |
| 24 | +#include "cuttlefish/host/libs/web/android_build_url.h" |
| 25 | +#include "cuttlefish/host/libs/web/http_client/fake_http_client.h" |
| 26 | +#include "cuttlefish/host/libs/web/http_client/http_client.h" |
| 27 | +#include "cuttlefish/host/libs/zip/libzip_cc/seekable_source.h" |
| 28 | +#include "cuttlefish/result/result.h" |
| 29 | +#include "cuttlefish/result/result_matchers.h" |
| 30 | + |
| 31 | +namespace cuttlefish { |
| 32 | +namespace { |
| 33 | + |
| 34 | +TEST(AndroidBuildApiTest, FileReader) { |
| 35 | + FakeHttpClient http_client; |
| 36 | + AndroidBuildUrl build_url("https://androidbuild-pa.googleapis.com/v4", "", |
| 37 | + ""); |
| 38 | + AndroidBuildApi api(http_client, build_url); |
| 39 | + |
| 40 | + http_client.SetResponse("{\"signedUrl\": \"http://zip-url\"}", "/url"); |
| 41 | + |
| 42 | + HttpResponse<std::string> res = { |
| 43 | + .data = "abc", |
| 44 | + .http_code = 200, |
| 45 | + .headers = |
| 46 | + { |
| 47 | + {"accept-ranges", "bytes"}, |
| 48 | + {"content-length", "3"}, |
| 49 | + }, |
| 50 | + }; |
| 51 | + http_client.SetResponse(res, "http://zip-url"); |
| 52 | + |
| 53 | + Build build = DeviceBuild{.id = "123", .target = "test"}; |
| 54 | + Result<SeekableZipSource> source = api.FileReader(build, "a.zip"); |
| 55 | + ASSERT_THAT(source, IsOk()); |
| 56 | + |
| 57 | + Result<SeekingZipSourceReader> reader = source->Reader(); |
| 58 | + ASSERT_THAT(reader, IsOk()); |
| 59 | + |
| 60 | + char buf[4] = {}; |
| 61 | + ASSERT_THAT(reader->Read(buf, 3), IsOkAndValue(3)); |
| 62 | + EXPECT_STREQ(buf, "abc"); |
| 63 | + |
| 64 | + EXPECT_TRUE(http_client.RequestMade( |
| 65 | + "https://androidbuild-pa.googleapis.com/v4/builds/123/test/attempts/" |
| 66 | + "latest/artifacts/a.zip/url")); |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace |
| 70 | +} // namespace cuttlefish |
0 commit comments