Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++
20.4 KB
Raw Blame
#include "homebrew/artwork_backend.hpp"
#include "platform/managed_directory.h"
#include "platform/ps5/title_allow_list.h"

#include <atomic>
#include <cassert>
#include <cstdio>
#include <cstdint>
#include <ctime>
#include <filesystem>
#include <functional>
#include <future>
#include <string>
#include <thread>
#include <vector>
#include <sys/time.h>
#include <unistd.h>
#include "steam_id.h"

using namespace playden::homebrew;

namespace {

std::vector<std::uint8_t> SmallPng() {
  return {
      0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
      0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
      0x08, 0x02, 0x00, 0x00, 0x00, 0x7b, 0x40, 0xe8, 0xdd, 0x00, 0x00, 0x00,
      0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
      0x00, 0x44, 0x65, 0x58, 0x49, 0x66, 0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00,
      0x00, 0x08, 0x00, 0x01, 0x87, 0x69, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
      0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01,
      0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xa0, 0x02,
      0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0xa0, 0x03,
      0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x00, 0xaa, 0xb8, 0xc6, 0x7a, 0x00, 0x00, 0x00, 0x0f, 0x49, 0x44,
      0x41, 0x54, 0x08, 0x1d, 0x63, 0xf8, 0xcf, 0xc0, 0xc0, 0xf0, 0x9f, 0x01,
      0x00, 0x07, 0xff, 0x01, 0xff, 0x19, 0x5f, 0x5d, 0x9f, 0x00, 0x00, 0x00,
      0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
  };
}

std::vector<std::uint8_t> SmallJpeg() {
  return {
      0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
      0x01, 0x00, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xe1, 0x00, 0x4c,
      0x45, 0x78, 0x69, 0x66, 0x00, 0x00, 0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00,
      0x00, 0x08, 0x00, 0x01, 0x87, 0x69, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
      0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01,
      0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xa0, 0x02,
      0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0xa0, 0x03,
      0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x00, 0xff, 0xed, 0x00, 0x38, 0x50, 0x68, 0x6f, 0x74, 0x6f, 0x73,
      0x68, 0x6f, 0x70, 0x20, 0x33, 0x2e, 0x30, 0x00, 0x38, 0x42, 0x49, 0x4d,
      0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x42, 0x49, 0x4d,
      0x04, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xd4, 0x1d, 0x8c, 0xd9,
      0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
      0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x02, 0x03, 0x01, 0x22,
      0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x1f, 0x00,
      0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0a, 0x0b, 0xff, 0xc4, 0x00, 0xb5, 0x10, 0x00, 0x02, 0x01,
      0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01,
      0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41,
      0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1,
      0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
      0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27,
      0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44,
      0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74,
      0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
      0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
      0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5,
      0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
      0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1,
      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
      0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc4, 0x00, 0x1f, 0x01,
      0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0a, 0x0b, 0xff, 0xc4, 0x00, 0xb5, 0x11, 0x00, 0x02, 0x01,
      0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02,
      0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12,
      0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42,
      0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72,
      0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a,
      0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43,
      0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57,
      0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73,
      0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86,
      0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
      0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3,
      0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
      0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
      0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3,
      0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xdb, 0x00, 0x43, 0x00,
      0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x03, 0x05, 0x03,
      0x03, 0x03, 0x05, 0x06, 0x05, 0x05, 0x05, 0x05, 0x06, 0x08, 0x06, 0x06,
      0x06, 0x06, 0x06, 0x08, 0x0a, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a,
      0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
      0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
      0x0f, 0x0f, 0x0f, 0x0f, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x02, 0x02, 0x02,
      0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x07, 0x10, 0x0b, 0x09, 0x0b, 0x10,
      0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
      0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
      0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
      0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
      0x10, 0xff, 0xdd, 0x00, 0x04, 0x00, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
      0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00, 0xec, 0xfc, 0x19,
      0xff, 0x00, 0x22, 0x7e, 0x85, 0xff, 0x00, 0x5e, 0x16, 0xbf, 0xfa, 0x29,
      0x6b, 0xa5, 0xae, 0x6b, 0xc1, 0x9f, 0xf2, 0x27, 0xe8, 0x5f, 0xf5, 0xe1,
      0x6b, 0xff, 0x00, 0xa2, 0x96, 0xba, 0x5a, 0xfe, 0x03, 0x3e, 0xa0, 0xff,
      0xd9,
  };
}

void DecodesPngAndJpegToRgba() {
  const auto png = DecodeArtwork(SmallPng());
  assert(png && png->width == 2 && png->height == 1 && png->rgba.size() == 8);
  assert(png->rgba[0] > 200 && png->rgba[1] < 30 && png->rgba[4] < 30 && png->rgba[5] > 200);

  const auto jpeg = DecodeArtwork(SmallJpeg());
  assert(jpeg && jpeg->width == 2 && jpeg->height == 1 && jpeg->rgba.size() == 8);
  assert(jpeg->rgba[3] == 255 && jpeg->rgba[7] == 255);
}

// A solid red 700x1 RGB PNG: wider than a card, so its framed banner is a single row.
std::vector<std::uint8_t> RedBannerPng() {
  return {
      0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
      0x44, 0x52, 0x00, 0x00, 0x02, 0xbc, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00,
      0x00, 0xb3, 0x13, 0xea, 0x96, 0x00, 0x00, 0x00, 0x1b, 0x49, 0x44, 0x41, 0x54, 0x78,
      0xda, 0x63, 0xf8, 0xcf, 0xc0, 0x30, 0x8a, 0x46, 0xd1, 0x28, 0x1a, 0x45, 0xa3, 0x68,
      0x14, 0x8d, 0xa2, 0x51, 0x44, 0x10, 0x01, 0x00, 0xa6, 0xd3, 0xb9, 0x63, 0x4a, 0x81,
      0x15, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
  };
}

void DecodeAllocatorRejectsBudgetExhaustion() {
  assert(!artwork_internal::DecodeArtworkWithAllocationBudget(SmallPng(), 1));
  assert(artwork_internal::DecodeArtworkWithAllocationBudget(
      SmallPng(), kMaxArtworkDecodeAllocationBytes));
}

void RejectsEmptyCorruptAndTransferOverflow() {
  assert(!DecodeArtwork({}));
  assert(!DecodeArtwork({1, 2, 3, 4}));
  assert(!DecodeArtwork(std::vector<std::uint8_t>(kMaxArtworkTransferBytes + 1, 0)));
}

void RejectsOversizedPngDimensionsBeforePixelAllocation() {
  // Complete, valid RGBA PNGs that differ only in width. The 1920-wide image
  // decodes, so rejecting the 1921-wide one is the dimension limit at work.
  const std::vector<std::uint8_t> limit = {
      0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
      0x44, 0x52, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
      0x00, 0x3f, 0xe9, 0xb3, 0x35, 0x00, 0x00, 0x00, 0x1e, 0x49, 0x44, 0x41, 0x54, 0x78,
      0xda, 0xed, 0xc1, 0x01, 0x0d, 0x00, 0x00, 0x00, 0xc2, 0xa0, 0xf7, 0x4f, 0x6d, 0x0f,
      0x07, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x06, 0x1e, 0x01, 0x00,
      0x01, 0x3b, 0x3d, 0xd9, 0x44, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
      0x42, 0x60, 0x82};
  const std::vector<std::uint8_t> oversized = {
      0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
      0x44, 0x52, 0x00, 0x00, 0x07, 0x81, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
      0x00, 0xd0, 0x2b, 0xd8, 0x0b, 0x00, 0x00, 0x00, 0x1e, 0x49, 0x44, 0x41, 0x54, 0x78,
      0xda, 0xed, 0xc1, 0x01, 0x0d, 0x00, 0x00, 0x00, 0xc2, 0xa0, 0xf7, 0x4f, 0x6d, 0x0f,
      0x07, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x37, 0x06, 0x1e, 0x05, 0x00,
      0x01, 0x3b, 0x98, 0x74, 0xae, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
      0x42, 0x60, 0x82};
  const auto decoded = DecodeArtwork(limit);
  assert(decoded && decoded->width == 1920 && decoded->height == 1);
  assert(!DecodeArtwork(oversized));
}

void InvalidHttpIdentityIsRejectedWithoutTransport() {
  HttpArtworkBackend backend("unused-ca.pem");
  std::atomic<bool> cancel{false};
  assert(!backend.Load({1, SteamId(42), "http://cdn.cloudflare.steamstatic.com/steam/apps/42/library_600x900.jpg"}, cancel));
  assert(!backend.Load({1, SteamId(42), "https://cdn.cloudflare.steamstatic.com/steam/apps/41/library_600x900.jpg"}, cancel));
  assert(!backend.Load({1, SteamId(42), "https://cdn.cloudflare.steamstatic.com/steam/apps/42/library_600x900.jpg?x=1"}, cancel));
  // Hero and logo are allowed for the same game only; other CDN files are not.
  assert(!backend.Load({1, SteamId(42), "https://cdn.cloudflare.steamstatic.com/steam/apps/41/library_hero.jpg"}, cancel));
  assert(!backend.Load({1, SteamId(42), "https://cdn.cloudflare.steamstatic.com/steam/apps/41/logo.png"}, cancel));
  assert(!backend.Load({1, SteamId(42), "https://cdn.cloudflare.steamstatic.com/steam/apps/42/header.jpg"}, cancel));
}

// A private cache folder under a temporary root, removed on exit.
struct CacheFolder {
  std::filesystem::path root;
  std::filesystem::path art;
  CacheFolder() {
    char pattern[] = "/tmp/playden-art-cache-XXXXXX";
    assert(::mkdtemp(pattern));
    root = std::filesystem::canonical(pattern);
    art = root / "art";
  }
  ~CacheFolder() { std::filesystem::remove_all(root); }

  void Write(const std::string& name, const std::vector<std::uint8_t>& bytes, bool expired) {
    auto parent = playden::platform::ManagedDirectory::Open(root, playden::platform::DirectoryPolicy::kApprovedTree);
    assert(parent);
    auto managed = parent->OpenChild("art", playden::platform::DirectoryPolicy::kPrivate, true, 0700);
    assert(managed);
    const int fd = managed->CreateExclusiveFile(name, 0600);
    assert(fd >= 0);
    assert(::write(fd, bytes.data(), bytes.size()) == static_cast<ssize_t>(bytes.size()));
    assert(::close(fd) == 0);
    if (expired) {
      const std::time_t old = std::time(nullptr) - 15 * 24 * 3600;
      const struct timeval times[2] = {{old, 0}, {old, 0}};
      assert(::utimes((art / name).c_str(), times) == 0);
    }
  }

  std::vector<std::uint8_t> Read(const std::string& name) const {
    std::vector<std::uint8_t> bytes;
    FILE* file = std::fopen((art / name).c_str(), "rb");
    if (!file) return bytes;
    std::uint8_t buffer[4096];
    std::size_t count = 0;
    while ((count = std::fread(buffer, 1, sizeof(buffer), file)) > 0) bytes.insert(bytes.end(), buffer, buffer + count);
    std::fclose(file);
    return bytes;
  }
};

bool EndsWith(const std::string& url, const std::string& name) {
  return url.size() >= name.size() && url.compare(url.size() - name.size(), name.size(), name) == 0;
}

// Answers by URL and records every URL asked for.
struct FakeCdn {
  std::vector<std::string> urls;
  std::function<playden::steam::HttpResponse(const std::string&)> answer;
  HttpArtworkBackend::Perform perform() {
    return [this](const playden::steam::HttpRequest& request, const std::atomic<bool>&) {
      urls.push_back(request.url);
      return answer(request.url);
    };
  }
  bool asked(const std::string& name) const {
    for (const auto& url : urls)
      if (EndsWith(url, name)) return true;
    return false;
  }
};

playden::steam::HttpResponse Status(long code, std::vector<std::uint8_t> body = {}) {
  playden::steam::HttpResponse response;
  response.status_code = code;
  response.body = std::move(body);
  return response;
}

playden::steam::HttpResponse Timeout() {
  playden::steam::HttpResponse response;
  response.error = playden::steam::HttpError::kTimeout;
  return response;
}

const char kCover[] = "https://cdn.cloudflare.steamstatic.com/steam/apps/42/library_600x900.jpg";

void FreshCacheIsUsedWithoutTheNetwork() {
  CacheFolder folder;
  folder.Write("42_600x900.jpg", SmallJpeg(), false);
  FakeCdn cdn;
  cdn.answer = [](const std::string&) { return Status(500); };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  assert(backend.Load({1, SteamId(42), kCover}, cancel));
  assert(cdn.urls.empty());
}

void MissingCoverFallsBackToFramedHeader() {
  CacheFolder folder;
  FakeCdn cdn;
  cdn.answer = [](const std::string& url) {
    return EndsWith(url, "/header.jpg") ? Status(200, SmallPng()) : Status(404);
  };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  const auto image = backend.Load({1, SteamId(42), kCover}, cancel);
  // The 2x1 banner is framed into a 600x900 portrait card.
  assert(image && image->width == 600 && image->height == 900);
  assert(image->rgba.size() == 600u * 900u * 4u);
  // The banner fills the middle third undimmed; the margins fade to black at the edges.
  const auto banner = DecodeArtwork(SmallPng());
  assert(banner);
  const auto pixel = [](const ArtworkImage& picture, std::uint32_t x, std::uint32_t y) {
    const auto* p = &picture.rgba[(y * picture.width + x) * 4];
    return std::vector<std::uint8_t>{p[0], p[1], p[2], p[3]};
  };
  auto opaque = pixel(*banner, 0, 0);
  opaque[3] = 255;
  assert(pixel(*image, 0, 450) == opaque);
  assert((pixel(*image, 0, 0) == std::vector<std::uint8_t>{0, 0, 0, 255}));
  assert((pixel(*image, 0, 899) == std::vector<std::uint8_t>{0, 0, 0, 255}));
  assert(cdn.asked("/library_600x900.jpg") && cdn.asked("/header.jpg"));
  assert(folder.Read("42_600x900.jpg") == SmallPng());

  // The next start reads the cached banner and frames it the same way.
  cdn.urls.clear();
  HttpArtworkBackend again(cdn.perform(), folder.art.string());
  const auto cached = again.Load({1, SteamId(42), kCover}, cancel);
  assert(cached && cached->width == 600 && cached->height == 900 && cdn.urls.empty());
}

void VeryWideBannerKeepsARow() {
  CacheFolder folder;
  FakeCdn cdn;
  cdn.answer = [](const std::string& url) {
    return EndsWith(url, "/header.jpg") ? Status(200, RedBannerPng()) : Status(404);
  };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  const auto image = backend.Load({1, SteamId(42), kCover}, cancel);
  assert(image && image->width == 600 && image->height == 900);
  // One row in the middle is the banner itself, not a dimmed margin.
  bool banner_row = false;
  for (std::uint32_t y = 0; y < 900; ++y) {
    const auto* p = &image->rgba[(y * 600u) * 4];
    banner_row = banner_row || (p[0] == 255 && p[1] == 0 && p[2] == 0);
  }
  assert(banner_row);
}

void SaveIsSkippedWhileTheEffectiveUidIsHeld() {
  CacheFolder folder;
  FakeCdn cdn;
  cdn.answer = [](const std::string&) { return Status(200, SmallJpeg()); };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  std::promise<void> held, done;
  std::thread holder([&] {
    const auto uid = playden::ps5::HoldEffectiveUid();
    held.set_value();
    done.get_future().wait();
  });
  held.get_future().wait();
  // The cover still shows; it just isn't cached, and no folder is created.
  assert(backend.Load({1, SteamId(42), kCover}, cancel));
  assert(!std::filesystem::exists(folder.art));
  done.set_value();
  holder.join();
  assert(backend.Load({1, SteamId(42), kCover}, cancel));
  assert(folder.Read("42_600x900.jpg") == SmallJpeg());
}

void HeroHasNoHeaderFallback() {
  CacheFolder folder;
  FakeCdn cdn;
  cdn.answer = [](const std::string&) { return Status(404); };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  assert(!backend.Load({1, SteamId(42), "https://cdn.cloudflare.steamstatic.com/steam/apps/42/library_hero.jpg"}, cancel));
  assert(cdn.urls.size() == 1 && !cdn.asked("/header.jpg"));
}

void OtherFailuresKeepTheCachedCover() {
  for (const bool timeout : {false, true}) {
    CacheFolder folder;
    folder.Write("42_600x900.jpg", SmallJpeg(), true);
    FakeCdn cdn;
    cdn.answer = [timeout](const std::string&) { return timeout ? Timeout() : Status(503); };
    HttpArtworkBackend backend(cdn.perform(), folder.art.string());
    std::atomic<bool> cancel{false};
    assert(backend.Load({1, SteamId(42), kCover}, cancel));
    // No banner is fetched on a failure that is not a 404, and the cached cover stays.
    assert(cdn.urls.size() == 1 && !cdn.asked("/header.jpg"));
    assert(folder.Read("42_600x900.jpg") == SmallJpeg());
  }

  // With nothing cached, a failure that is not a 404 still never fetches the banner.
  CacheFolder folder;
  FakeCdn cdn;
  cdn.answer = [](const std::string&) { return Timeout(); };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  assert(!backend.Load({1, SteamId(42), kCover}, cancel));
  assert(cdn.urls.size() == 1);
}

void BadDownloadKeepsTheCachedCover() {
  CacheFolder folder;
  folder.Write("42_600x900.jpg", SmallJpeg(), true);
  FakeCdn cdn;
  cdn.answer = [](const std::string&) { return Status(200, {1, 2, 3, 4}); };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  assert(backend.Load({1, SteamId(42), kCover}, cancel));
  assert(cdn.urls.size() == 1);
  assert(folder.Read("42_600x900.jpg") == SmallJpeg());
  for (const auto& entry : std::filesystem::directory_iterator(folder.art))
    assert(entry.path().filename() == "42_600x900.jpg");  // no temporary file left behind
}

void ExpiredCacheIsReplacedByANewCover() {
  CacheFolder folder;
  folder.Write("42_600x900.jpg", SmallJpeg(), true);
  FakeCdn cdn;
  cdn.answer = [](const std::string&) { return Status(200, SmallPng()); };
  HttpArtworkBackend backend(cdn.perform(), folder.art.string());
  std::atomic<bool> cancel{false};
  assert(backend.Load({1, SteamId(42), kCover}, cancel));
  assert(folder.Read("42_600x900.jpg") == SmallPng());
}

}  // namespace

int main(int argc, char** argv) {
  if (argc == 3 && std::string(argv[1]) == "--live-smoke") {
    HttpArtworkBackend backend(argv[2]);
    std::atomic<bool> cancel{false};
    const auto image = backend.Load(
        {1, SteamId(440),
         "https://cdn.cloudflare.steamstatic.com/steam/apps/440/library_600x900.jpg"},
        cancel);
    assert(image && !image->rgba.empty());
    std::printf("artwork %ux%u %zu bytes\n", image->width, image->height,
                image->rgba.size());
    return 0;
  }
  assert(argc == 1);
  DecodesPngAndJpegToRgba();
  DecodeAllocatorRejectsBudgetExhaustion();
  RejectsEmptyCorruptAndTransferOverflow();
  RejectsOversizedPngDimensionsBeforePixelAllocation();
  InvalidHttpIdentityIsRejectedWithoutTransport();
  FreshCacheIsUsedWithoutTheNetwork();
  MissingCoverFallsBackToFramedHeader();
  VeryWideBannerKeepsARow();
  SaveIsSkippedWhileTheEffectiveUidIsHeld();
  HeroHasNoHeaderFallback();
  OtherFailuresKeepTheCachedCover();
  BadDownloadKeepsTheCachedCover();
  ExpiredCacheIsReplacedByANewCover();
}