Target Android SDK 37 for Play Store review

Integrate current main while preserving the original SDK migration history.
Update the Android toolchain, runtime permissions, foreground service lifecycle, storage access, native execution and platform API compatibility. Complete translations and regression coverage.

Validation: 3,260 JVM tests, 1,620 Python tests (eight skips), 171 Rust tests, six native regression programs and release lint with zero errors. Signed APK upgrade installed on the requested device. Signed app bundle validated with bundletool.

Review candidate: physical before/after feature checks, performance parity and native 16 KiB audio compatibility remain pending. Play Console acceptance has not been verified.
This commit is contained in:
maxjivi05 committed 2026-11-02 08:17:51 -05:00
commit b14587baeb
1553 files changed
+124616 -8790

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
#include <assert.h>
#include <stdio.h>
#include "cursor_constants.inc"
#include "cursor_scene_positions.inc"
#include "cursor_clip_to_eye.inc"
int main(void) {
int out[2];
int dx, sx, dw;
assert(cursor_scene_positions(1920, CARDBOARD_SOURCE_SCREEN, 100, 0, out) == 1);
assert(out[0] == 100);
assert(cursor_scene_positions(1920, CARDBOARD_SOURCE_FLAT, 100, 0, out) == 1);
assert(out[0] == 100);
assert(cursor_scene_positions(1, CARDBOARD_SOURCE_SBS, 0, 0, out) == 1);
assert(cursor_scene_positions(3840, CARDBOARD_SOURCE_SBS, 100, 0, out) == 2);
assert(out[0] == 100 && out[1] == 2020);
assert(cursor_scene_positions(3840, CARDBOARD_SOURCE_SBS, 2000, 0, out) == 2);
assert(out[0] == 2000 && out[1] == 80);
assert(cursor_scene_positions(3840, CARDBOARD_SOURCE_SBS, 1919, 0, out) == 2);
assert(out[0] == 1919 && out[1] == 3839);
assert(cursor_scene_positions(3840, CARDBOARD_SOURCE_SBS, 1920, 0, out) == 2);
assert(out[0] == 1920 && out[1] == 0);
assert(cursor_scene_positions(1920, CARDBOARD_SOURCE_SBS, 960, 32, out) == 2);
assert(out[0] == 928 && out[1] == -32);
assert(cursor_scene_positions(1920, CARDBOARD_SOURCE_SBS, 959, 32, out) == 2);
assert(out[0] == 927 && out[1] == 1887);
assert(cursor_clip_to_eye(1920, -1, 100, 32, &dx, &sx, &dw) == 1);
assert(dx == 100 && sx == 0 && dw == 32);
assert(cursor_clip_to_eye(1920, 0, 100, 32, &dx, &sx, &dw) == 1);
assert(dx == 100 && sx == 0 && dw == 32);
assert(cursor_clip_to_eye(1920, 0, 950, 32, &dx, &sx, &dw) == 1);
assert(dx == 950 && sx == 0 && dw == 10);
assert(cursor_clip_to_eye(1920, 1, 950, 32, &dx, &sx, &dw) == 1);
assert(dx == 960 && sx == 10 && dw == 22);
assert(cursor_clip_to_eye(1920, 1, 960, 32, &dx, &sx, &dw) == 1);
assert(dx == 960 && sx == 0 && dw == 32);
assert(cursor_clip_to_eye(1920, 0, 928, 32, &dx, &sx, &dw) == 1);
assert(dx == 928 && sx == 0 && dw == 32);
assert(cursor_clip_to_eye(1920, 1, 928, 32, &dx, &sx, &dw) == 0);
assert(cursor_clip_to_eye(1920, 0, -32, 32, &dx, &sx, &dw) == 0);
puts("PASS: cursor stereo placement");
return 0;
}
+359
View File
@@ -0,0 +1,359 @@
#include "framegen.hpp"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <vector>
namespace {
constexpr int levels = 7;
constexpr VkImageUsageFlags flow_usage =
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
enum Pipeline { LUMA, DOWNSAMPLE, FLOW3, FLOW4, EXPAND3, EXPAND4, GLOBAL_MOTION, PREWARP, FLOW_REG, SYNTH };
struct Image { VkExtent3D extent; VkFormat format; VkImageUsageFlags usage; };
struct Allocation { VkDeviceSize size; std::unique_ptr<uint8_t[]> data; };
struct Binding { VkDescriptorType type; VkImageView view; VkBuffer buffer; VkDeviceSize range; };
struct Dispatch { unsigned pipeline; uint32_t x, y, z; };
std::map<VkImage, Image> images;
std::map<VkImageView, VkImage> views;
std::map<VkBuffer, VkBufferCreateInfo> buffers;
std::map<VkDeviceMemory, Allocation> allocations;
std::map<VkBuffer, VkDeviceMemory> buffer_memory;
std::map<VkDescriptorSet, std::map<uint32_t, Binding>> descriptors;
std::map<VkPipeline, unsigned> pipelines;
std::map<VkImage, unsigned> image_ids;
std::map<VkBuffer, unsigned> buffer_ids;
std::vector<Dispatch> dispatches;
uintptr_t serial = 100;
unsigned buffer_creates, set_creates;
VkPipeline bound_pipeline;
VkDescriptorSet bound_set;
uint64_t command_hash = 14695981039346656037ull;
template<class T> T handle() { return reinterpret_cast<T>(++serial); }
void hash(uint64_t value) {
for (unsigned i = 0; i < 8; i++) {
command_hash = (command_hash ^ (value & 255)) * 1099511628211ull;
value >>= 8;
}
}
void hash_image(VkImage image) {
assert(images.count(image));
if (!image_ids.count(image)) image_ids[image] = static_cast<unsigned>(image_ids.size() + 1);
const auto &info = images.at(image);
hash(image_ids.at(image));
hash(info.extent.width); hash(info.extent.height); hash(info.format); hash(info.usage);
}
template<class Info, class Out>
VkResult VKAPI_CALL create(VkDevice, const Info *, const VkAllocationCallbacks *, Out *out) {
*out = handle<Out>();
return VK_SUCCESS;
}
template<class T> void VKAPI_CALL destroy(VkDevice, T, const VkAllocationCallbacks *) {}
VkResult VKAPI_CALL create_image(VkDevice, const VkImageCreateInfo *info,
const VkAllocationCallbacks *, VkImage *out) {
*out = handle<VkImage>();
images[*out] = {info->extent, info->format, info->usage};
return VK_SUCCESS;
}
VkResult VKAPI_CALL create_view(VkDevice, const VkImageViewCreateInfo *info,
const VkAllocationCallbacks *, VkImageView *out) {
assert(images.count(info->image));
*out = handle<VkImageView>();
views[*out] = info->image;
return VK_SUCCESS;
}
void VKAPI_CALL destroy_image(VkDevice, VkImage image, const VkAllocationCallbacks *) {
assert(images.erase(image) == 1);
}
void VKAPI_CALL destroy_view(VkDevice, VkImageView view, const VkAllocationCallbacks *) {
assert(views.erase(view) == 1);
}
VkResult VKAPI_CALL create_buffer(VkDevice, const VkBufferCreateInfo *info,
const VkAllocationCallbacks *, VkBuffer *out) {
*out = handle<VkBuffer>();
buffers[*out] = *info;
buffer_creates++;
return VK_SUCCESS;
}
void VKAPI_CALL destroy_buffer(VkDevice, VkBuffer buffer, const VkAllocationCallbacks *) {
assert(buffers.erase(buffer) == 1);
buffer_memory.erase(buffer);
}
void VKAPI_CALL image_requirements(VkDevice, VkImage image, VkMemoryRequirements *out) {
const auto &info = images.at(image);
const uint64_t bytes = info.format == VK_FORMAT_R32_SFLOAT ? 4 : 8;
*out = {uint64_t(info.extent.width) * info.extent.height * bytes, 1, 1};
}
void VKAPI_CALL buffer_requirements(VkDevice, VkBuffer buffer, VkMemoryRequirements *out) {
*out = {buffers.at(buffer).size, 1, 1};
}
VkResult VKAPI_CALL allocate(VkDevice, const VkMemoryAllocateInfo *info,
const VkAllocationCallbacks *, VkDeviceMemory *out) {
*out = handle<VkDeviceMemory>();
allocations[*out].size = info->allocationSize;
return VK_SUCCESS;
}
void VKAPI_CALL free_memory(VkDevice, VkDeviceMemory memory, const VkAllocationCallbacks *) {
assert(allocations.erase(memory) == 1);
}
VkResult VKAPI_CALL bind_image(VkDevice, VkImage image, VkDeviceMemory memory, VkDeviceSize) {
assert(images.count(image) && allocations.count(memory));
return VK_SUCCESS;
}
VkResult VKAPI_CALL bind_buffer(VkDevice, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize) {
assert(buffers.count(buffer) && allocations.count(memory));
buffer_memory[buffer] = memory;
return VK_SUCCESS;
}
VkResult VKAPI_CALL map_memory(VkDevice, VkDeviceMemory memory, VkDeviceSize offset,
VkDeviceSize size, VkMemoryMapFlags, void **out) {
auto &allocation = allocations.at(memory);
assert(offset == 0 && size <= allocation.size);
allocation.data = std::make_unique<uint8_t[]>(allocation.size);
*out = allocation.data.get();
return VK_SUCCESS;
}
void VKAPI_CALL unmap(VkDevice, VkDeviceMemory memory) { assert(allocations.count(memory)); }
VkResult VKAPI_CALL idle(VkDevice) { return VK_SUCCESS; }
void VKAPI_CALL memory_properties(VkPhysicalDevice, VkPhysicalDeviceMemoryProperties *out) {
*out = {};
out->memoryTypeCount = 1;
out->memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
}
VkResult VKAPI_CALL allocate_sets(VkDevice, const VkDescriptorSetAllocateInfo *info, VkDescriptorSet *out) {
for (uint32_t i = 0; i < info->descriptorSetCount; i++) {
out[i] = handle<VkDescriptorSet>();
descriptors[out[i]] = {};
set_creates++;
}
return VK_SUCCESS;
}
VkResult VKAPI_CALL reset_pool(VkDevice, VkDescriptorPool, VkDescriptorPoolResetFlags) {
descriptors.clear();
return VK_SUCCESS;
}
void VKAPI_CALL update_sets(VkDevice, uint32_t count, const VkWriteDescriptorSet *writes,
uint32_t, const VkCopyDescriptorSet *) {
for (uint32_t i = 0; i < count; i++) {
const auto &write = writes[i];
assert(descriptors.count(write.dstSet) && write.descriptorCount == 1);
Binding binding{write.descriptorType, VK_NULL_HANDLE, VK_NULL_HANDLE, 0};
if (write.pImageInfo) {
binding.view = write.pImageInfo->imageView;
assert(views.count(binding.view));
} else {
binding.buffer = write.pBufferInfo->buffer;
binding.range = write.pBufferInfo->range;
assert(buffers.count(binding.buffer));
}
descriptors.at(write.dstSet)[write.dstBinding] = binding;
}
}
VkResult VKAPI_CALL create_pipelines(VkDevice, VkPipelineCache, uint32_t count,
const VkComputePipelineCreateInfo *, const VkAllocationCallbacks *,
VkPipeline *out) {
for (uint32_t i = 0; i < count; i++) {
const auto index = static_cast<unsigned>(pipelines.size());
out[i] = handle<VkPipeline>();
pipelines[out[i]] = index;
}
return VK_SUCCESS;
}
void VKAPI_CALL bind_pipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline pipeline) {
assert(pipelines.count(pipeline));
bound_pipeline = pipeline;
}
void VKAPI_CALL bind_sets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout,
uint32_t, uint32_t count, const VkDescriptorSet *sets,
uint32_t, const uint32_t *) {
assert(count == 1 && descriptors.count(sets[0]));
bound_set = sets[0];
}
void VKAPI_CALL dispatch(VkCommandBuffer, uint32_t x, uint32_t y, uint32_t z) {
const unsigned pipeline = pipelines.at(bound_pipeline);
dispatches.push_back({pipeline, x, y, z});
hash(1); hash(pipeline); hash(x); hash(y); hash(z);
for (const auto &[slot, binding] : descriptors.at(bound_set)) {
hash(slot); hash(binding.type);
if (binding.view) {
hash_image(views.at(binding.view));
} else {
if (!buffer_ids.count(binding.buffer))
buffer_ids[binding.buffer] = static_cast<unsigned>(buffer_ids.size() + 1);
const auto &allocation = allocations.at(buffer_memory.at(binding.buffer));
const auto size = binding.range == VK_WHOLE_SIZE ? allocation.size : binding.range;
assert(allocation.data && size <= allocation.size);
hash(buffer_ids.at(binding.buffer)); hash(size);
for (VkDeviceSize i = 0; i < size; i++) hash(allocation.data[i]);
}
}
}
void VKAPI_CALL barrier(VkCommandBuffer, VkPipelineStageFlags src, VkPipelineStageFlags dst,
VkDependencyFlags flags, uint32_t nm, const VkMemoryBarrier *memory,
uint32_t nb, const VkBufferMemoryBarrier *, uint32_t ni,
const VkImageMemoryBarrier *image) {
assert(nb == 0);
hash(2); hash(src); hash(dst); hash(flags); hash(nm); hash(ni);
for (uint32_t i = 0; i < nm; i++) {
hash(memory[i].srcAccessMask); hash(memory[i].dstAccessMask);
}
for (uint32_t i = 0; i < ni; i++) {
hash_image(image[i].image);
hash(image[i].oldLayout); hash(image[i].newLayout);
hash(image[i].srcAccessMask); hash(image[i].dstAccessMask);
hash(image[i].srcQueueFamilyIndex); hash(image[i].dstQueueFamilyIndex);
}
}
void VKAPI_CALL clear_image(VkCommandBuffer, VkImage image, VkImageLayout layout,
const VkClearColorValue *color, uint32_t count, const VkImageSubresourceRange *) {
assert(count == 1);
hash(3); hash_image(image); hash(layout);
for (unsigned i = 0; i < 4; i++) hash(color->uint32[i]);
}
void VKAPI_CALL copy_image(VkCommandBuffer, VkImage src, VkImageLayout sl, VkImage dst,
VkImageLayout dl, uint32_t count, const VkImageCopy *region) {
assert(count == 1);
hash(4); hash_image(src); hash(sl); hash_image(dst); hash(dl);
hash(region->extent.width); hash(region->extent.height); hash(region->extent.depth);
}
uint32_t dimension(uint32_t full, int level) { return std::max(full >> level, 1u); }
void check_images(uint32_t width, uint32_t height, int finest) {
assert(images.size() == static_cast<size_t>(32 - 2 * finest));
for (int level = finest; level < levels; level++) {
const auto w = dimension(width, level), h = dimension(height, level);
assert(std::any_of(images.begin(), images.end(), [=](const auto &entry) {
const auto &image = entry.second;
return image.format == VK_FORMAT_R16G16B16A16_SFLOAT && image.usage == flow_usage &&
image.extent.width == w && image.extent.height == h;
}));
}
}
VkImageView external_image(uint32_t width, uint32_t height) {
VkImage image = handle<VkImage>();
VkImageView view = handle<VkImageView>();
images[image] = {{width, height, 1}, VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT};
views[view] = image;
return view;
}
void record(ddfg::FrameGen &fg, int model, uint32_t width, uint32_t height, unsigned generations) {
const auto previous = external_image(width, height), current = external_image(width, height);
const auto first = dispatches.size();
for (unsigned i = 0; i < generations; i++)
fg.record(reinterpret_cast<VkCommandBuffer>(1), previous, current, external_image(width, height),
float(i + 1) / float(generations + 1), 0, i, i != 0);
std::vector<Dispatch> expected;
auto add = [&](unsigned pipeline, int level) {
expected.push_back({pipeline, (dimension(width, level) + 15) / 16,
(dimension(height, level) + 15) / 16, 1});
};
add(LUMA, 0); add(LUMA, 0);
for (int level = 1; level < levels; level++) { add(DOWNSAMPLE, level); add(DOWNSAMPLE, level); }
expected.push_back({GLOBAL_MOTION, 8, 1, 1});
for (int level = fg.flowFinest(); level < levels; level++) add(PREWARP, level);
for (int level = levels - 1; level >= fg.flowFinest(); level--) add(model == 4 ? FLOW4 : FLOW3, level);
for (int i = 0; i < 4; i++) add(FLOW_REG, fg.flowFinest());
add(model == 4 ? EXPAND4 : EXPAND3, 0);
for (unsigned i = 0; i < generations; i++) add(SYNTH, 0);
assert(dispatches.size() - first == expected.size());
for (size_t i = 0; i < expected.size(); i++) {
const auto &actual = dispatches[first + i], &want = expected[i];
assert(actual.pipeline == want.pipeline && actual.x == want.x && actual.y == want.y && actual.z == want.z);
}
for (auto it = views.begin(); it != views.end();) {
if (images.at(it->second).format == VK_FORMAT_R8G8B8A8_UNORM) {
images.erase(it->second);
it = views.erase(it);
} else ++it;
}
}
}
int main(int argc, char **argv) {
assert(argc == 8);
const std::string mode = argv[1];
const int model = std::atoi(argv[2]), preset = std::atoi(argv[3]);
uint32_t width = static_cast<uint32_t>(std::atoi(argv[4]));
uint32_t height = static_cast<uint32_t>(std::atoi(argv[5]));
const unsigned generations = static_cast<unsigned>(std::atoi(argv[6]));
const unsigned repeats = static_cast<unsigned>(std::atoi(argv[7]));
assert((model == 3 || model == 4) && preset >= 0 && preset <= 2);
assert(width && height && generations >= 1 && generations <= 3 && repeats > 0);
ddfg::DeviceDispatch dd;
ddfg::InstanceDispatch id;
id.GetPhysicalDeviceMemoryProperties = memory_properties;
dd.CreateImage = create_image; dd.DestroyImage = destroy_image;
dd.CreateImageView = create_view; dd.DestroyImageView = destroy_view;
dd.CreateBuffer = create_buffer; dd.DestroyBuffer = destroy_buffer;
dd.GetImageMemoryRequirements = image_requirements; dd.GetBufferMemoryRequirements = buffer_requirements;
dd.AllocateMemory = allocate; dd.FreeMemory = free_memory;
dd.BindImageMemory = bind_image; dd.BindBufferMemory = bind_buffer;
dd.MapMemory = map_memory; dd.UnmapMemory = unmap; dd.DeviceWaitIdle = idle;
dd.CreateSampler = create<VkSamplerCreateInfo, VkSampler>; dd.DestroySampler = destroy<VkSampler>;
dd.CreateShaderModule = create<VkShaderModuleCreateInfo, VkShaderModule>;
dd.DestroyShaderModule = destroy<VkShaderModule>;
dd.CreateDescriptorSetLayout = create<VkDescriptorSetLayoutCreateInfo, VkDescriptorSetLayout>;
dd.DestroyDescriptorSetLayout = destroy<VkDescriptorSetLayout>;
dd.CreatePipelineLayout = create<VkPipelineLayoutCreateInfo, VkPipelineLayout>;
dd.DestroyPipelineLayout = destroy<VkPipelineLayout>;
dd.CreateDescriptorPool = create<VkDescriptorPoolCreateInfo, VkDescriptorPool>;
dd.DestroyDescriptorPool = destroy<VkDescriptorPool>; dd.ResetDescriptorPool = reset_pool;
dd.AllocateDescriptorSets = allocate_sets; dd.UpdateDescriptorSets = update_sets;
dd.CreateComputePipelines = create_pipelines; dd.DestroyPipeline = destroy<VkPipeline>;
dd.CmdBindPipeline = bind_pipeline; dd.CmdBindDescriptorSets = bind_sets; dd.CmdDispatch = dispatch;
dd.CmdPipelineBarrier = barrier; dd.CmdClearColorImage = clear_image; dd.CmdCopyImage = copy_image;
ddfg::FrameGen fg;
assert(fg.init(&dd, &id, reinterpret_cast<VkPhysicalDevice>(1), reinterpret_cast<VkDevice>(2),
0, reinterpret_cast<VkQueue>(3)));
ddfg::Config cfg;
cfg.enabled = true; cfg.model = model; cfg.perfPreset = preset; cfg.flowScale = 0.6f;
fg.configure(cfg);
assert(fg.onResize({width, height}, VK_FORMAT_R8G8B8A8_UNORM));
assert(mode == "resources" || mode == "record" || mode == "resize" || mode == "preset");
for (unsigned repeat = 0; repeat < repeats; repeat++) {
const auto old_buffers = buffer_creates, old_sets = set_creates;
if (mode != "record") check_images(width, height, fg.flowFinest());
record(fg, model, width, height, generations);
if (mode != "record") {
assert(buffer_creates - old_buffers == static_cast<unsigned>(15 - fg.flowFinest()));
assert(set_creates - old_sets == static_cast<unsigned>(54 - 4 * fg.flowFinest()));
}
if (repeat + 1 == repeats) break;
if (mode == "resize") {
width += 17; height += 9;
assert(fg.onResize({width, height}, VK_FORMAT_R8G8B8A8_UNORM));
} else if (mode == "preset") {
cfg.perfPreset = (cfg.perfPreset + 1) % 3;
fg.configure(cfg);
} else {
assert(mode == "record");
}
}
uint64_t bytes = 0;
for (const auto &[image, info] : images) {
(void)image;
bytes += uint64_t(info.extent.width) * info.extent.height *
(info.format == VK_FORMAT_R32_SFLOAT ? 4 : 8);
}
std::printf("PASS images=%zu bytes=%llu buffers=%u sets=%u dispatches=%zu trace=%016llx\n",
images.size(), static_cast<unsigned long long>(bytes), buffer_creates, set_creates,
dispatches.size(), static_cast<unsigned long long>(command_hash));
fg.destroy();
assert(images.empty() && views.empty());
}
+56
View File
@@ -0,0 +1,56 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "kgsl_busy.h"
#define ENTRIES 819
static uint32_t memstore[ENTRIES * KGSL_BUSY_ENTRY_WORDS];
static void stamp(int entry, uint32_t sop, uint32_t eop) {
memstore[entry * KGSL_BUSY_ENTRY_WORDS + KGSL_BUSY_SOP] = sop;
memstore[entry * KGSL_BUSY_ENTRY_WORDS + KGSL_BUSY_EOP] = eop;
}
int main(void) {
assert(kgsl_busy_first_slot(8 * 4096) == 813);
assert(kgsl_busy_first_slot(8 * 16384) == 3270);
assert(kgsl_busy_first_slot(7 * 40) == -1);
int first = kgsl_busy_first_slot(sizeof(memstore));
uint32_t last[KGSL_BUSY_SLOTS] = {0};
int moved = 0;
stamp(first, 100, 100);
stamp(first + 2, 7, 7);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 1 && last[0] == 100 && last[2] == 7);
moved = 0;
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 0);
stamp(first + 2, 8, 7);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 1);
assert(moved == 1);
moved = 0;
stamp(first + 2, 8, 8);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 0);
stamp(first + 4, 3, 2);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 1);
memset(memstore, 0, sizeof(memstore));
stamp(first - 1, 5, 4);
stamp(first + KGSL_BUSY_SLOTS, 9, 8);
memset(last, 0, sizeof(last));
moved = 0;
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 0);
puts("PASS: kgsl memstore busy");
return 0;
}
-2
View File
@@ -23,8 +23,6 @@ static void wl_pointer_send_button(struct wl_resource *r, uint32_t serial,
r->held &= ~(1u << (button - 0x110));
r->releases++;
}
/* The old dispatcher calls this even for releases, emitting stale coordinates.
* Recording this boundary detects that unwanted motion/focus routing path. */
static void pointer_input(double x, double y, int relative, uint32_t button, int pressed) {
assert(x == 0 && y == 0 && relative == 1 && button >= 0x110 && button <= 0x112);
assert(pressed == 1);
+109
View File
@@ -0,0 +1,109 @@
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
@unittest.skipUnless(shutil.which("cc"), "C compiler required")
class AndroidVolumeTest(unittest.TestCase):
def test_missing_or_invalid_initial_state_recovers_without_restarting_the_sink(self):
header = Path(__file__).resolve().parents[1] / "aaudio-sink/android-volume.h"
harness = r'''
#define _GNU_SOURCE
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#define PA_VOLUME_NORM 65536U
#define PA_USEC_PER_MSEC 1000
#define PA_SINK_INIT 0
#define PA_SUBSCRIPTION_EVENT_SINK 1
#define PA_SUBSCRIPTION_EVENT_CHANGE 2
#define PA_MIN(a,b) ((a)<(b)?(a):(b))
#define pa_xnew0(type,n) ((type*)calloc(n,sizeof(type)))
#define pa_xstrdup strdup
#define pa_xfree free
#define pa_log_warn(...) ((void)0)
typedef struct { unsigned value; } pa_cvolume;
typedef struct pa_time_event { int value; } pa_time_event;
typedef struct pa_mainloop_api { void (*time_free)(pa_time_event*); } pa_mainloop_api;
typedef struct { pa_mainloop_api *mainloop; } pa_core;
typedef struct pa_sink {
void *userdata; int state; bool muted; unsigned n_volume_steps, index;
struct { unsigned channels; } sample_spec;
pa_cvolume real_volume, reference_volume, soft_volume;
pa_core *core;
void (*set_volume)(struct pa_sink*), (*set_mute)(struct pa_sink*);
} pa_sink;
struct android_volume;
struct userdata { struct android_volume *android_volume; };
static int restarts, frees;
static uint64_t last_delay;
static char *pa_sprintf_malloc(const char *format, ...) {
va_list ap; va_start(ap,format); char *result; assert(vasprintf(&result,format,ap)>=0); va_end(ap); return result;
}
static void pa_cvolume_set(pa_cvolume *volume, unsigned channels, unsigned value) { volume->value=value; }
static unsigned pa_cvolume_max(const pa_cvolume *volume) { return volume->value; }
static void pa_cvolume_reset(pa_cvolume *volume, unsigned channels) { volume->value=PA_VOLUME_NORM; }
static bool pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) { return a->value==b->value; }
static void pa_sink_volume_changed(pa_sink *sink, const pa_cvolume *volume) { sink->real_volume=*volume; }
static void pa_sink_mute_changed(pa_sink *sink, bool muted) { sink->muted=muted; }
static void pa_sink_set_set_volume_callback(pa_sink *sink, void (*callback)(pa_sink*)) { sink->set_volume=callback; }
static void pa_sink_set_set_mute_callback(pa_sink *sink, void (*callback)(pa_sink*)) { sink->set_mute=callback; }
static void pa_subscription_post(pa_core *core, int event, unsigned index) {}
static uint64_t pa_rtclock_now(void) { return 0; }
static void pa_core_rttime_restart(pa_core *core, pa_time_event *event, uint64_t time) { restarts++; last_delay=time; }
static pa_time_event *pa_core_rttime_new(pa_core *core, uint64_t time,
void (*callback)(pa_mainloop_api*,pa_time_event*,const struct timeval*,void*), void *userdata) {
return calloc(1,sizeof(pa_time_event));
}
static void free_timer(pa_time_event *event) { frees++; free(event); }
#include "android-volume.h"
static void state(const char *directory, const char *text) {
char *path=pa_sprintf_malloc("%s/volume.state",directory);
FILE *f=fopen(path,"w"); assert(f); fputs(text,f); fclose(f); free(path);
}
int main(int argc, char **argv) {
pa_mainloop_api api={.time_free=free_timer}; pa_core core={.mainloop=&api};
struct userdata userdata={0};
pa_sink sink={.userdata=&userdata,.core=&core,.state=1,.sample_spec={.channels=2},.real_volume={PA_VOLUME_NORM}};
userdata.android_volume=android_volume_new(&sink,argv[1]);
assert(userdata.android_volume && userdata.android_volume->timer && sink.set_volume && sink.set_mute);
state(argv[1],"invalid\n");
android_volume_refresh(&api,userdata.android_volume->timer,NULL,userdata.android_volume);
assert(sink.real_volume.value==PA_VOLUME_NORM && restarts==1 && last_delay==100*PA_USEC_PER_MSEC);
state(argv[1],"3 15 1\n");
android_volume_refresh(&api,userdata.android_volume->timer,NULL,userdata.android_volume);
assert(sink.real_volume.value==3*PA_VOLUME_NORM/15 && sink.muted && sink.n_volume_steps==16);
assert(sink.soft_volume.value==PA_VOLUME_NORM && restarts==2);
assert(last_delay==16*PA_USEC_PER_MSEC);
state(argv[1],"4 15 1\n");
android_volume_refresh(&api,userdata.android_volume->timer,NULL,userdata.android_volume);
assert(sink.real_volume.value==4*PA_VOLUME_NORM/15 && last_delay==16*PA_USEC_PER_MSEC);
for (int i = 0; i < ANDROID_VOLUME_ACTIVE_TICKS + 1; i++)
android_volume_refresh(&api,userdata.android_volume->timer,NULL,userdata.android_volume);
assert(last_delay==100*PA_USEC_PER_MSEC);
sink.real_volume.value=2*PA_VOLUME_NORM/3; sink.set_volume(&sink);
char *request=pa_sprintf_malloc("%s/volume.request",argv[1]);
FILE *f=fopen(request,"r"); unsigned level; assert(f && fscanf(f,"%u",&level)==1 && level==10); fclose(f); free(request);
android_volume_free(userdata.android_volume); assert(frees==1);
return 0;
}
'''
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "pulsecore").mkdir()
for name in ("core-rtclock.h", "core-subscribe.h"):
(root / "pulsecore" / name).touch()
source = root / "test.c"
source.write_text(harness)
binary = root / "test"
subprocess.run(["cc", "-std=c11", "-I", str(root), "-I", str(header.parent),
str(source), "-o", str(binary)], check=True, capture_output=True)
subprocess.run([str(binary), str(root)], check=True, capture_output=True)
+74
View File
@@ -0,0 +1,74 @@
import hashlib
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
REMEMBER = Path(__file__).resolve().parents[1] / "ubuntu/overlay-runtime/usr/share/droiddeck/apt/remember"
class AptRememberTest(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.top = Path(temp.name)
self.dir = self.top / "share/apt"
self.state = self.top / "state"
self.etc = self.top / "etc/apt"
self.keys = self.top / "keyrings"
self.autostart = self.top / "autostart"
for d in (self.dir, self.etc / "sources.list.d", self.keys, self.autostart, self.top / "bin"):
d.mkdir(parents=True)
(self.dir / "system-manual.txt").write_text("base\n")
(self.dir / "runtime-id").write_text("new\n")
system = self.keys / "ubuntu-archive.gpg"
system.write_text("system key")
(self.dir / "system-sources.txt").write_text("%s %s\n" % (system, hashlib.sha256(b"system key").hexdigest()))
apt_mark = self.top / "bin/apt-mark"
apt_mark.write_text("#!/bin/sh\nprintf 'base\\nvlc\\n'\n")
apt_mark.chmod(0o755)
script = REMEMBER.read_text()
script = script.replace("/usr/share/droiddeck/apt", str(self.dir))
script = script.replace("/root/.local/share/droiddeck/apt", str(self.state))
script = script.replace("/etc/xdg/autostart", str(self.autostart))
script = script.replace("/usr/share/keyrings", str(self.keys))
script = script.replace("/etc/apt", str(self.etc))
self.script = self.top / "remember"
self.script.write_text(script)
def run_remember(self):
env = dict(os.environ, PATH="%s:%s" % (self.top / "bin", os.environ["PATH"]))
env.pop("DROIDDECK_APT_RESTORING", None)
subprocess.run(["bash", str(self.script)], env=env, check=True, timeout=30)
def test_user_sources_and_keys_outside_etc_apt_are_kept(self):
(self.etc / "sources.list").write_text("deb http://example.invalid stable main\n")
(self.keys / "example.gpg").write_text("user key")
self.run_remember()
self.assertEqual("vlc\n", (self.state / "packages.txt").read_text())
self.assertTrue((self.state / "sources/sources.list").is_file())
self.assertEqual("user key", (self.state / "keyrings" / ("%s_example.gpg" % str(self.keys).lstrip("/").replace("/", "_"))).read_text())
self.assertFalse(any("ubuntu-archive" in p.name for p in (self.state / "keyrings").iterdir()))
def test_the_previous_runtime_snapshot_waits_for_the_desktop_restore(self):
(self.autostart / "droiddeck-apt-restore.desktop").write_text("")
self.state.mkdir()
(self.state / "packages.txt").write_text("old-app\n")
(self.state / "restored").write_text("old\n")
self.run_remember()
self.assertEqual("old-app\n", (self.state / "packages.txt").read_text())
(self.state / "attempted").write_text("new\n")
self.run_remember()
self.assertEqual("vlc\n", (self.state / "packages.txt").read_text())
def test_without_the_desktop_the_snapshot_follows_the_runtime(self):
self.state.mkdir()
(self.state / "packages.txt").write_text("old-app\n")
(self.state / "restored").write_text("old\n")
self.run_remember()
self.assertEqual("vlc\n", (self.state / "packages.txt").read_text())
if __name__ == "__main__":
unittest.main()
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -60,7 +60,7 @@ class BinfmtPreloadTest(unittest.TestCase):
cls.library = cls.tmp / "libbinfmt.so"
subprocess.run(["gcc", "-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror", "-DBINFMT_TEST",
'-DBINFMT_HANDLER="%s"' % cls.handler, "-o", str(cls.library),
"-pthread"] + [str(PRELOAD / name) for name in ("binfmt.c", "net.c", "udevmon.c", "pathcache.c", "webhelper.c")] + ["-ldl"],
"-pthread"] + [str(PRELOAD / name) for name in ("binfmt.c", "net.c", "udevmon.c", "pathcache.c", "perms.c", "webhelper.c")] + ["-ldl"],
check=True)
source = cls.tmp / "program.c"
source.write_text(PROGRAM)
+98
View File
@@ -0,0 +1,98 @@
import os
import runpy
import shutil
import struct
import tempfile
import unittest
from pathlib import Path
BWRAP = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-bwrap"
def elf(path, machine):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(b"\x7fELF\x02\x01\x01" + bytes(11) + struct.pack("<H", machine) + bytes(64))
path.chmod(0o755)
class BwrapFexTest(unittest.TestCase):
def setUp(self):
self.dir = Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.dir, True)
self.shim = runpy.run_path(str(BWRAP))
self.fex = self.dir / "fex"
for name in ("bin/FEX", "bin/FEXServer", "lib/libxxhash.so.0"):
elf(self.fex / name, 183)
self.shim["through_fex"].__globals__["OWN_FEX"] = str(self.fex)
self.runtime = self.dir / "runtime"
self.app = self.dir / "app"
elf(self.runtime / "bin/ldconfig", 62)
elf(self.runtime / "bin/bash", 62)
os.symlink("bash", self.runtime / "bin/sh")
elf(self.runtime / "bin/arm-tool", 183)
(self.app / "bin").mkdir(parents=True)
(self.app / "bin/r2modman").write_text("#!/bin/sh\nexec /app/r2modman \"$@\"\n")
def spec(self, command, path="/app/bin:/usr/bin"):
s = self.shim["Spec"]()
s.env = {"PATH": path, "XDG_CACHE_HOME": "/home/user/.var/app/x/cache", "FEX_ROOTFS": "/sniper"}
s.ops = [
{"t": "bind", "src": str(self.runtime), "dest": "/usr", "try": False, "ro": True},
{"t": "bind", "src": str(self.app), "dest": "/app", "try": False, "ro": True},
{"t": "symlink", "target": "usr/bin", "dest": "/bin"},
]
s.command = command
return s
def test_an_x86_64_program_found_on_path_runs_through_fex(self):
s = self.spec(["ldconfig", "-X", "-C", "/run/cache"])
self.assertTrue(self.shim["through_fex"](s))
self.assertEqual(["/run/droiddeck-fex/bin/FEX", "/usr/bin/ldconfig", "-X", "-C", "/run/cache"], s.command)
dests = {op["dest"]: op["src"] for op in s.ops if op["t"] == "bind"}
self.assertEqual(str(self.fex / "bin/FEX"), dests["/run/droiddeck-fex/bin/FEX"])
self.assertEqual(str(self.fex / "lib/libxxhash.so.0"), dests["/run/droiddeck-fex/lib/libxxhash.so.0"])
self.assertIn("/usr/lib/aarch64-linux-gnu", dests)
self.assertIn("/usr/lib/ld-linux-aarch64.so.1", dests)
self.assertNotIn("FEX_ROOTFS", s.env)
self.assertEqual("/home/user/.var/app/x/cache/droiddeck-fex/", s.env["FEX_APP_DATA_LOCATION"])
def test_a_script_whose_interpreter_is_x86_64_runs_through_fex(self):
s = self.spec(["r2modman", "--flag"])
self.assertTrue(self.shim["through_fex"](s))
self.assertEqual(["/run/droiddeck-fex/bin/FEX", "/app/bin/r2modman", "--flag"], s.command)
def test_a_program_reached_through_a_sandbox_symlink_is_checked(self):
s = self.spec(["/bin/sh", "-c", "true"])
self.assertTrue(self.shim["through_fex"](s))
self.assertEqual("/bin/sh", s.command[1])
def test_a_bind_whose_source_is_a_symlink_resolves_from_the_source(self):
real = self.dir / "store" / "tool-real"
elf(real, 62)
os.symlink("tool-real", self.dir / "store" / "tool-link")
s = self.spec(["/opt/tool"])
s.ops.append({"t": "bind", "src": str(self.dir / "store" / "tool-link"), "dest": "/opt/tool", "try": False,
"ro": True})
self.assertTrue(self.shim["through_fex"](s))
self.assertEqual("/opt/tool", s.command[1])
def test_a_relative_path_entry_is_searched_from_the_working_directory(self):
s = self.spec(["r2modman"], path="bin")
s.cwd = "/app"
self.assertTrue(self.shim["through_fex"](s))
self.assertEqual("/app/bin/r2modman", s.command[1])
def test_an_arm64_program_runs_as_it_is(self):
s = self.spec(["arm-tool"])
self.assertFalse(self.shim["through_fex"](s))
self.assertEqual(["arm-tool"], s.command)
self.assertEqual(3, len(s.ops))
def test_a_missing_program_is_left_alone(self):
s = self.spec(["nothing-here"])
self.assertFalse(self.shim["through_fex"](s))
self.assertEqual(["nothing-here"], s.command)
if __name__ == "__main__":
unittest.main()
+107
View File
@@ -0,0 +1,107 @@
import json
import os
import runpy
import socket
import struct
import subprocess
import sys
import tempfile
import threading
import unittest
from pathlib import Path
BWRAP = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-bwrap"
PROBE = """
import fcntl, os, sys
fd = os.open(sys.argv[1], os.O_RDWR)
try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
print("held")
else:
print("free")
"""
class BwrapLockFileTest(unittest.TestCase):
def setUp(self):
self.shim = runpy.run_path(str(BWRAP))
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.instance = Path(self.temp.name) / "instance"
self.instance.mkdir()
(self.instance / ".ref").write_text("")
def spec(self, *extra):
argv = ["--ro-bind", str(self.instance), "/run/user/1000/.flatpak/7", *extra, "--", "true"]
return self.shim["parse"](argv)
def probe(self):
return subprocess.run([sys.executable, "-c", PROBE, str(self.instance / ".ref")],
capture_output=True, text=True, timeout=30, check=True).stdout.strip()
def test_sandbox_paths_resolve_through_the_binds(self):
spec = self.spec()
self.assertEqual(str(self.instance / ".ref"), self.shim["host_path"](spec, "/run/user/1000/.flatpak/7/.ref"))
self.assertEqual("/etc/hosts", self.shim["host_path"](spec, "/etc/hosts"))
def test_the_instance_lock_is_held_while_the_sandbox_runs(self):
spec = self.spec("--lock-file", "/run/user/1000/.flatpak/7/.ref")
self.assertEqual("free", self.probe())
self.shim["hold_locks"](spec)
self.assertEqual("held", self.probe())
def test_without_a_lock_file_nothing_is_locked(self):
self.shim["hold_locks"](self.spec())
self.assertEqual("free", self.probe())
def test_the_lock_survives_exec(self):
script = (
"import runpy, sys; shim = runpy.run_path(sys.argv[1]); "
"spec = shim['parse'](['--ro-bind', sys.argv[2], '/run/user/1000/.flatpak/7', "
"'--lock-file', '/run/user/1000/.flatpak/7/.ref', '--', 'true']); "
"shim['hold_locks'](spec); "
"import os; os.execv(sys.executable, [sys.executable, '-c', "
"'import subprocess, sys; print(subprocess.run([sys.executable, \"-c\", sys.argv[1], sys.argv[2]], "
"capture_output=True, text=True).stdout.strip())', sys.argv[3], sys.argv[4]])"
)
out = subprocess.run([sys.executable, "-c", script, str(BWRAP), str(self.instance), PROBE,
str(self.instance / ".ref")], capture_output=True, text=True, timeout=30, check=True)
self.assertEqual("held", out.stdout.strip())
class BwrapReportedPidTest(unittest.TestCase):
def test_flatpak_is_given_the_wrapper_so_killing_it_ends_the_whole_sandbox(self):
name = "droiddeck-bwrap-test-%d" % os.getpid()
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind("\0" + name)
server.listen(1)
self.addCleanup(server.close)
def answer():
conn, _ = server.accept()
with conn:
head = conn.recv(5)
size = struct.unpack(">I", head[1:])[0]
while size > 0:
size -= len(conn.recv(size))
for kind, payload in ((b"P", b"424242"), (b"X", b"0")):
conn.sendall(kind + struct.pack(">I", len(payload)) + payload)
thread = threading.Thread(target=answer, daemon=True)
thread.start()
read_end, write_end = os.pipe()
env = dict(os.environ, DROIDDECK_BWRAP_SOCKET=name)
proc = subprocess.Popen([sys.executable, str(BWRAP), "--ro-bind", "/", "/elsewhere", "--info-fd",
str(write_end), "--", "true"], env=env, pass_fds=[write_end])
os.close(write_end)
info = os.read(read_end, 4096)
os.close(read_end)
self.assertEqual(0, proc.wait(30))
thread.join(5)
self.assertEqual({"child-pid": proc.pid}, json.loads(info))
if __name__ == "__main__":
unittest.main()
+95
View File
@@ -0,0 +1,95 @@
import itertools
import json
import os
from pathlib import Path
import shlex
import shutil
import subprocess
import tempfile
import unittest
import yaml
ROOT = Path(__file__).resolve().parents[2]
class CiChecksTest(unittest.TestCase):
def test_pr_cache_writes_and_source_timestamp_policy(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
cache = next(step for step in jobs['build']['steps']
if step.get('uses') == 'gradle/actions/setup-gradle@v4')['with']
self.assertEqual(cache['cache-read-only'],
"${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}")
gradle = (ROOT / 'app/build.gradle').read_text()
self.assertIn("gitValue(['show', '-s', '--format=%ct', '--end-of-options', sourceCommit])", gradle)
def test_report_comment_lookup_ignores_human_marker(self):
if not shutil.which('jq'):
self.skipTest('jq required to execute comment selection')
script = (ROOT / 'tools/release/report-build.sh').read_text()
line = next(line for line in script.splitlines() if line.startswith('ID='))
arguments = shlex.split(line[len('ID=$('):].split(' | head', 1)[0])
query = arguments[arguments.index('--jq') + 1].replace('$MARK', '<!-- build -->')
comments = [dict(id=1, user=dict(login='someone'), body='<!-- build --> forged'),
dict(id=2, user=dict(login='github-actions[bot]'), body='<!-- build --> result')]
result = subprocess.check_output(['jq', '-r', query], input=json.dumps(comments), text=True)
self.assertEqual(result.strip(), '2')
def test_build_and_tests_use_identical_generated_metadata(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
steps = {step.get('id'): step for step in jobs['build']['steps']}
self.assertEqual(steps['build']['env'], steps['unit-tests']['env'])
self.assertIn('DROIDDECK_BUILD_LABEL', steps['unit-tests']['env'])
self.assertIn('SGDB_API_KEY', steps['unit-tests']['env'])
gradle = (ROOT / 'app/build.gradle').read_text()
self.assertIn("outputs.doNotCacheIf('CI executes the test suite on every run') { true }", gradle)
self.assertIn('outputs.upToDateWhen { false }', gradle)
def test_independent_jobs_wait_only_for_the_pinned_source(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
for name in ('xbox', 'components', 'python-tests'):
self.assertEqual(jobs[name]['needs'], 'source')
self.assertEqual(set(jobs['build']['needs']), {'source', 'components', 'xbox'})
def test_dependency_update_errors_block_installation(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
sudo = root / 'sudo'
sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\nexit "${APT_RESULT:-0}"\n')
sudo.chmod(0o755)
query = root / 'dpkg-query'
query.write_text('#!/bin/sh\nexit 1\n')
query.chmod(0o755)
log = root / 'apt.log'
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'], APT_LOG=str(log))
installer = ROOT / 'tools/ci/install-ubuntu-packages.sh'
result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17'))
self.assertEqual(result.returncode, 17)
self.assertEqual(len(log.read_text().splitlines()), 2)
log.unlink()
subprocess.run(['bash', str(installer), 'qemu-user'], env=env, check=True)
update, install = log.read_text().splitlines()
for command in (update, install):
self.assertIn('Dir::Etc::sourceparts=-', command)
self.assertIn('Acquire::https::Timeout=15', command)
self.assertIn('Acquire::Retries=1', command)
self.assertIn('APT::Update::Error-Mode=any', update)
self.assertIn('install -y -q --no-install-recommends qemu-user', install)
def test_required_status_rejects_failed_cancelled_or_skipped_test_jobs(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
gate = jobs['checks']
self.assertEqual(gate['name'], 'Compile and test APK')
self.assertEqual(set(gate['needs']), {'build', 'python-tests'})
self.assertIn('!cancelled()', gate['if'])
for build, python in itertools.product(('success', 'failure', 'cancelled', 'skipped'), repeat=2):
with self.subTest(build=build, python=python):
result = subprocess.run(
['bash', '-c', gate['steps'][0]['run']],
env=dict(os.environ, BUILD_RESULT=build, PYTHON_RESULT=python),
capture_output=True)
self.assertEqual(result.returncode == 0, build == python == 'success')
for publisher in ('sign', 'packs'):
self.assertIn('checks', jobs[publisher]['needs'])
+27
View File
@@ -0,0 +1,27 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
class CiNdkTest(unittest.TestCase):
def test_installed_ndk_must_match_the_declared_version(self):
version = (ROOT / 'tools/ci/toolchains.env').read_text().strip().split('=', 1)[1]
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
ndk = root / 'ndk' / version
ndk.mkdir(parents=True)
properties = ndk / 'source.properties'
properties.write_text('Pkg.Revision = ' + version + '\n')
output = root / 'environment'
env = dict(os.environ, ANDROID_HOME=str(root), GITHUB_ENV=str(output))
script = ROOT / 'tools/ci/resolve-ndk.sh'
subprocess.run(['bash', str(script)], env=env, check=True)
self.assertIn('NDK_VERSION=' + version, output.read_text())
self.assertIn('ANDROID_NDK_HOME=' + str(ndk), output.read_text())
properties.write_text('Pkg.Revision = unexpected\n')
self.assertNotEqual(subprocess.run(['bash', str(script)], env=env).returncode, 0)
-1
View File
@@ -1,4 +1,3 @@
"""Single-core Steam affinity is rechecked even if its thread count stayed the same."""
import subprocess
import tempfile
import unittest
+228
View File
@@ -0,0 +1,228 @@
from pathlib import Path
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session'
def function_source(name):
source = SCRIPT.read_text()
start = source.index('%s() {' % name)
end = source.index('\n }', start)
return source[start:end + len('\n }')]
def run_bash(script, env=None):
return subprocess.run(['bash', '-c', script], capture_output=True, text=True, timeout=10, env=env)
class ClientZramFocusTest(unittest.TestCase):
def test_the_hidden_epic_launcher_is_compressed_with_the_client(self):
with tempfile.TemporaryDirectory() as tmp:
proc = Path(tmp)
def task(pid, argv, env=()):
folder = proc / str(pid)
folder.mkdir()
(folder / 'cmdline').write_bytes(b''.join(arg.encode() + b'\0' for arg in argv))
(folder / 'environ').write_bytes(b''.join(item.encode() + b'\0' for item in env))
fex = '/s/FEX-Emu/usr/bin/FEX'
task(5000010, ['/s/steamrtarm64/steam', '-gamepadui'])
task(5000011, [fex, '/e/usr/bin/EpicGamesLauncher'], ['HOME=/root', 'DROIDDECK_EPIC_HIDDEN=1'])
task(5000012, [fex, './/lib/webkit2gtk-4.1/WebKitWebProcess', '4'], ['DROIDDECK_EPIC_HIDDEN=1', 'LANG=C'])
task(5000013, [fex, '/e/usr/bin/EpicGamesLauncher'], ['HOME=/root'])
task(5000014, ['/e/WebKitWebProcess', '4'], ['DROIDDECK_EPIC_HIDDEN=1'])
task(5000015, [fex, '/e/usr/bin/EpicGamesLauncher'], ['DROIDDECK_EPIC_HIDDEN=10'])
task(5000016, ['/s/reaper', 'SteamLaunch', 'AppId=413150', '--', 'game'])
source = function_source('bl_zram_scan').replace('/proc/[0-9]*', tmp + '/[0-9]*')
result = run_bash(source + '\nbl_zram_scan\necho "${_bl_pids[*]}|${_bl_others[*]}|$_bl_game|$_bl_apps"')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual('5000010 5000011 5000012|5000013 5000014 5000015|1| 413150', result.stdout.strip())
def scan_fake(self, tasks):
with tempfile.TemporaryDirectory() as tmp:
for pid, argv in tasks:
self.write_cmd(Path(tmp), pid, argv)
source = function_source('bl_zram_scan').replace('/proc/[0-9]*', tmp + '/[0-9]*')
return run_bash(source + '\nbl_zram_scan\necho "${_bl_pids[*]}|${_bl_others[*]}|$_bl_game"')
def test_battlenet_is_compressed_with_the_client_while_one_of_its_games_runs(self):
launcher = [
(6000010, ['/s/steamrtarm64/steam', '-gamepadui']),
(6000011, ['/s/reaper', 'SteamLaunch', 'AppId=2899116412', '--', 'droiddeck-battlenet']),
(6000012, ['C:\\Program Files (x86)\\Battle.net\\Battle.net.exe', '--exec=launch S1']),
(6000013, ['C:\\Program Files (x86)\\Battle.net\\Battle.net.15511\\Battle.net Helper.exe', '--type=renderer']),
(6000014, ['C:\\ProgramData\\Battle.net\\Agent\\Agent.9324\\Agent.exe', '--internalclienttools']),
(6000015, ['C:\\windows\\system32\\services.exe']),
(6000016, ['C:\\windows\\system32\\winedevice.exe', '-uid']),
]
game = [(6000017, ['S:\\Games\\StarCraft\\x86\\StarCraft.exe', '-launch', '-uid', 's1'])]
before = self.scan_fake(launcher)
during = self.scan_fake(launcher + game)
for result in (before, during):
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual('6000010|6000015 6000016 6000012 6000013 6000014|1', before.stdout.strip())
self.assertEqual('6000010 6000012 6000013 6000014|6000015 6000016 6000017|1', during.stdout.strip())
def test_a_game_without_a_battlenet_uid_leaves_battlenet_alone(self):
result = self.scan_fake([
(6000020, ['C:\\Program Files (x86)\\Battle.net\\Battle.net.exe']),
(6000021, ['D:\\Games\\Other\\Game.exe', '-windowed']),
(6000022, ['/usr/bin/wine', 'game.exe', '-uid']),
])
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('|6000021 6000022 6000020|0', result.stdout.strip())
def test_refocus_restarts_the_delay_and_game_exit_restores_a_packed_client(self):
source = SCRIPT.read_text()
start = source.index(' if [ "$_bl_game" = 1 ] && bl_game_focused; then')
stop = source.index(' nap 5', start)
body = source[start:stop]
harness = '''
_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_apps=" 413150"
_bl_pids=(123)
bl_game_focused() { [ "$focused" = 1 ]; }
bl_over_cap() { return 1; }
bl_zram_send() { echo "send:$1"; }
tick() {
SECONDS=$1 _bl_game=$2 focused=$3
''' + body + '''
}
tick 0 1 1
tick 5 1 0
tick 30 1 1
echo "refocus:$_bl_packed"
tick 49 1 1
tick 50 1 1
tick 55 1 0
tick 60 0 0
'''
result = run_bash(harness)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['refocus:-1', 'send:1', 'send:0'], result.stdout.splitlines())
def monitor_harness(self):
source = SCRIPT.read_text()
start = source.index(' bl_zram_scan\n if bl_cap_due; then')
stop = source.index(' nap 5', start)
return '''
_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30
_bl_apps=" 413150"
_bl_pids=(123)
step() { :; }
bl_zram_scan() { :; }
bl_game_focused() { [ "$focused" = 1 ]; }
bl_cap_due() { [ "$cap_due" = 1 ]; }
bl_over_cap() { [ "$over_cap" = 1 ]; }
bl_zram_send() { echo "send:$1:$3"; }
tick() {
SECONDS=$1 _bl_game=$2 focused=$3 over_cap=$4 cap_due=$5
''' + source[start:stop] + '\n}\n'
def test_client_toggle_still_disables_automatic_compression(self):
result = run_bash(self.monitor_harness() + '''
BL_ZRAM_CLIENT=0
tick 0 1 1 0 0
tick 25 1 1 0 0
tick 30 0 0 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual('', result.stdout)
def test_a_new_game_restarts_the_compression_delay(self):
result = run_bash(self.monitor_harness() + '''
tick 0 1 1 0 0
_bl_apps=" 413150 1245620"
tick 15 1 1 0 0
tick 20 1 1 0 0
echo "starting:$_bl_packed"
tick 34 1 1 0 0
tick 35 1 1 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['starting:-1', 'send:1:client'], result.stdout.splitlines())
def test_ram_cap_can_still_compress_an_unfocused_client(self):
result = run_bash(self.monitor_harness() + '''
BL_ZRAM_CLIENT=0 BL_RAM_CAP_MB=1024
bl_zram_usage() { _bl_rss=$((768 * 1024)); }
tick 0 1 0 1 1
echo "cap:$_bl_packed:$_bl_since:$_bl_backoff"
tick 5 0 0 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'send:1:session', 'cap:0:-1:60', 'send:0:client'], result.stdout.splitlines())
def test_game_exit_defers_restore_until_the_session_is_under_the_ram_cap(self):
result = run_bash(self.monitor_harness() + '''
tick 0 1 1 0 0
tick 20 1 1 0 0
tick 25 1 0 0 0
tick 30 0 0 1 0
echo "deferred:$_bl_unpack:$_bl_packed"
tick 35 0 0 0 1
echo "restored:$_bl_unpack"
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'deferred:1:-1', 'send:0:client', 'restored:0'], result.stdout.splitlines())
def test_scan_keeps_the_running_game_and_ignores_steams_own_reaper(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
root = Path(temp.name)
self.write_cmd(root, 10, ['/usr/lib/steam/reaper', 'SteamLaunch', 'AppId=769', '--'])
self.write_cmd(root, 20, ['/usr/bin/FEX', '/usr/lib/steam/reaper', 'SteamLaunch', 'AppId=1245620', '--'])
self.write_cmd(root, 30, ['/home/deck/.local/share/Steam/steamrtarm64/steam'])
self.write_cmd(root, 40, ['/usr/bin/wine', 'game.exe'])
self.write_cmd(root, 50, ['/usr/bin/FEXInterpreter', '/usr/bin/steamwebhelper'])
scan = function_source('bl_zram_scan').replace('for _d in /proc/[0-9]*', 'for _d in "$FAKE"/[0-9]*')
result = run_bash('FAKE=%s\n%s\nbl_zram_scan\necho "game=$_bl_game"\necho "apps=${_bl_apps# }"\necho "pids=${_bl_pids[*]}"\necho "others=${_bl_others[*]}"' % (root, scan))
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['game=1', 'apps=1245620', 'pids=30 50', 'others=40'], result.stdout.splitlines())
def test_focus_must_be_the_running_game(self):
focus = function_source('bl_game_focused')
script = '''
%s
check() {
BL_DESKTOP=$1 BL_LAUNCH_DIR=$2 _bl_apps=$3
if bl_game_focused; then echo yes; else echo no; fi
}
check "" "$dir" " 413150"
check "" "$dir" " 1245620"
printf '%%s\\n' '{"focusedApp": 999}' > "$dir/agent/focus.json"
check "" "$dir" " 413150"
printf '%%s\\n' '{"focusedApp": 769}' > "$dir/agent/focus.json"
check "" "$dir" " 413150"
rm -f "$dir/agent/focus.json"
check 1 "$dir" " 413150"
check "" "" " 413150"
''' % focus
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
launch = Path(temp.name)
(launch / 'agent').mkdir()
(launch / 'agent' / 'focus.json').write_text('{"t": 1, "focusedApp": 413150}\n')
result = run_bash('dir=%s\n%s' % (launch, script))
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['yes', 'no', 'no', 'no', 'yes', 'yes'], result.stdout.splitlines())
@staticmethod
def write_cmd(root, pid, args):
proc = root / str(pid)
proc.mkdir()
(proc / 'cmdline').write_bytes(b'\0'.join(arg.encode() for arg in args) + b'\0')
if __name__ == '__main__':
unittest.main()
+137
View File
@@ -0,0 +1,137 @@
import os
from pathlib import Path
import shutil
import struct
import subprocess
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
PROGRAM = r"""
#define _GNU_SOURCE
#include <linux/futex.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
static int64_t ns(clockid_t clock) {
struct timespec ts;
clock_gettime(clock, &ts);
return (int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
static struct timespec in(clockid_t clock, int64_t delay) {
int64_t t = ns(clock) + delay;
struct timespec ts = {(time_t)(t / 1000000000LL), (long)(t % 1000000000LL)};
return ts;
}
static int64_t start;
static void begin(void) { start = ns(CLOCK_BOOTTIME); }
static void end(const char *name) { printf("%s %lld\n", name, (long long)((ns(CLOCK_BOOTTIME) - start) / 1000000LL)); }
int main(void) {
const int64_t wait = 60000000LL;
printf("gap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ns(CLOCK_MONOTONIC)) / 1000000LL));
struct timespec raw;
syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &raw);
printf("rawgap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ((int64_t)raw.tv_sec * 1000000000LL + raw.tv_nsec)) / 1000000LL));
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_condattr_t attr;
pthread_cond_t mono, real;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&mono, &attr);
pthread_cond_init(&real, NULL);
struct timespec ts;
pthread_mutex_lock(&mutex);
ts = in(CLOCK_MONOTONIC, wait); begin(); pthread_cond_timedwait(&mono, &mutex, &ts); end("cond_mono");
ts = in(CLOCK_REALTIME, wait); begin(); pthread_cond_timedwait(&real, &mutex, &ts); end("cond_real");
ts = in(CLOCK_MONOTONIC, wait); begin(); pthread_cond_clockwait(&real, &mutex, CLOCK_MONOTONIC, &ts); end("clockwait");
pthread_mutex_unlock(&mutex);
sem_t sem;
sem_init(&sem, 0, 0);
ts = in(CLOCK_MONOTONIC, wait); begin(); sem_clockwait(&sem, CLOCK_MONOTONIC, &ts); end("sem");
ts = in(CLOCK_MONOTONIC, wait); begin(); clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL); end("nanosleep");
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
struct itimerspec it = {{0, 0}, in(CLOCK_MONOTONIC, wait)};
uint64_t ticks;
timerfd_settime(fd, TFD_TIMER_ABSTIME, &it, NULL);
begin(); if (read(fd, &ticks, sizeof(ticks)) < 0) ticks = 0; end("timerfd");
static uint32_t word;
ts = in(CLOCK_MONOTONIC, wait); begin();
syscall(SYS_futex, &word, FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG, 0, &ts, NULL, FUTEX_BITSET_MATCH_ANY);
end("futex");
return 0;
}
"""
SHIFT_MS = 10000
@unittest.skipUnless(shutil.which("cc"), "needs a C compiler")
class ClockPausePreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory()
cls.dir = Path(cls.temp.name)
cls.control = cls.dir / "clock"
build = cls.dir / "build"
build.mkdir()
(build / "program.c").write_text(PROGRAM)
cls.preload = build / "libblsession.so"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-DBL_CLOCK_ANY_ARCH",
'-DCP_CTL="%s"' % cls.control, "-o", str(cls.preload),
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
cls.shell = build / "plasmashell"
subprocess.run(["cc", "-O2", "-pthread", "-o", str(cls.shell), str(build / "program.c")], check=True)
cls.other = build / "kwin_wayland"
shutil.copy(cls.shell, cls.other)
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
def run_program(self, program, shift_ms, preload=True):
self.control.write_bytes(struct.pack("<q", shift_ms * 1000000))
env = dict(os.environ)
if preload:
env["LD_PRELOAD"] = str(self.preload)
result = subprocess.run([str(program)], env=env, capture_output=True, text=True, check=True, timeout=30)
return {name: int(value) for name, value in (line.split() for line in result.stdout.splitlines())}
def test_the_shell_sees_monotonic_time_stopped_for_the_pause(self):
plain = self.run_program(self.shell, 0)
paused = self.run_program(self.shell, SHIFT_MS)
self.assertAlmostEqual(paused["gap"] - plain["gap"], SHIFT_MS, delta=50)
self.assertAlmostEqual(paused["rawgap"] - plain["rawgap"], SHIFT_MS, delta=50)
def test_timed_waits_last_as_long_as_asked_after_a_pause(self):
paused = self.run_program(self.shell, SHIFT_MS)
for name in ("cond_mono", "cond_real", "clockwait", "sem", "nanosleep", "timerfd", "futex"):
self.assertGreaterEqual(paused[name], 50, name)
self.assertLess(paused[name], 1000, name)
def test_other_programs_keep_the_real_clock(self):
plain = self.run_program(self.other, 0, preload=False)
paused = self.run_program(self.other, SHIFT_MS)
self.assertAlmostEqual(paused["gap"], plain["gap"], delta=50)
for name in ("cond_mono", "clockwait", "sem", "nanosleep", "timerfd", "futex"):
self.assertGreaterEqual(paused[name], 50, name)
self.assertLess(paused[name], 1000, name)
if __name__ == "__main__":
unittest.main()
+112
View File
@@ -0,0 +1,112 @@
import errno
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
SHIM = r"""
#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
int close(int fd) {
char link[64], path[PATH_MAX];
snprintf(link, sizeof link, "/proc/self/fd/%d", fd);
ssize_t n = readlink(link, path, sizeof path - 1);
path[n > 0 ? n : 0] = 0;
long r = syscall(SYS_close, fd);
if (r == 0 && n > 4 && !strcmp(path + n - 4, ".eio")) {
errno = EIO;
return -1;
}
return (int)r;
}
"""
PROGRAM = r"""
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
static void check(const char *name, const char *path, int flags) {
int fd = open(path, flags);
if (fd < 0) { printf("%s open-failed\n", name); return; }
char b[4];
if (read(fd, b, sizeof b) < 0) {}
errno = 0;
int r = close(fd);
int error = r ? errno : 0;
int still_open = fcntl(fd, F_GETFD) >= 0;
printf("%s %d %d %d\n", name, r, error, still_open);
}
int main(int argc, char **argv) {
check("readonly", argv[1], O_RDONLY);
check("readwrite", argv[1], O_RDWR);
check("plain", argv[2], O_RDONLY);
errno = 0;
int r = close(-1);
printf("badfd %d %d\n", r, errno == EBADF);
return 0;
}
"""
@unittest.skipUnless(shutil.which("cc"), "needs a C compiler")
class CloseEioPreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory()
cls.dir = Path(cls.temp.name)
(cls.dir / "shim.c").write_text(SHIM)
(cls.dir / "program.c").write_text(PROGRAM)
cls.preload = cls.dir / "libblsession.so"
cls.shim = cls.dir / "libshim.so"
cls.program = cls.dir / "program"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-DBL_CLOCK_ANY_ARCH",
'-DCP_CTL="%s"' % (cls.dir / "clock"), "-o", str(cls.preload),
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.shim), str(cls.dir / "shim.c")], check=True)
subprocess.run(["cc", "-O2", "-o", str(cls.program), str(cls.dir / "program.c")], check=True)
(cls.dir / "data.eio").write_bytes(b"data")
(cls.dir / "plain").write_bytes(b"data")
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
def run_program(self, preloads):
env = dict(os.environ, LD_PRELOAD=":".join(map(str, preloads)))
result = subprocess.run([str(self.program), str(self.dir / "data.eio"), str(self.dir / "plain")],
env=env, capture_output=True, text=True, check=True, timeout=30)
return {line.split()[0]: line.split()[1:] for line in result.stdout.splitlines()}
def test_a_flush_error_on_a_read_only_file_is_not_a_close_failure(self):
out = self.run_program([self.preload, self.shim])
self.assertEqual(["0", "0", "0"], out["readonly"])
def test_a_flush_error_after_writing_access_is_still_reported(self):
out = self.run_program([self.preload, self.shim])
self.assertEqual(["-1", str(errno.EIO), "0"], out["readwrite"])
def test_other_closes_are_unchanged(self):
out = self.run_program([self.preload, self.shim])
self.assertEqual(["0", "0", "0"], out["plain"])
self.assertEqual(["-1", "1"], out["badfd"])
def test_without_the_session_library_the_error_reaches_the_program(self):
out = self.run_program([self.shim])
self.assertEqual("-1", out["readonly"][0])
if __name__ == "__main__":
unittest.main()
+106
View File
@@ -0,0 +1,106 @@
import importlib.util
import io
import json
import os
from pathlib import Path
import subprocess
import tarfile
import tempfile
import unittest
from unittest.mock import patch
SPEC = importlib.util.spec_from_file_location('component_prepare', Path(__file__).resolve().parents[1] / 'components/prepare.py')
components = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(components)
class ComponentPrepareTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
self.fixture(self.root)
def fixture(self, root):
for name in components.TREES:
(root / name).mkdir(parents=True)
(root / 'tools/components/recipes').mkdir(parents=True)
(root / 'input.h').write_text('#define VALUE 1\n')
(root / 'tools/components/recipes/fake.sh').write_text('printf binary > app/src/main/assets/linuxfs/demo.so\n')
spec = dict(script='tools/components/recipes/fake.sh', inputs=['input.h', 'tools/components/recipes/fake.sh'],
outputs=['app/src/main/assets/linuxfs/demo.so'])
(root / components.REGISTRY).write_text(json.dumps(dict(fake=spec)))
for name in components.LIBRARIES:
(root / name).parent.mkdir(parents=True, exist_ok=True)
(root / name).write_bytes(b'library')
subprocess.run(['git', 'init', '-q', str(root)], check=True)
subprocess.run(['git', '-C', str(root), 'config', 'core.autocrlf', 'false'], check=True)
subprocess.run(['git', '-C', str(root), 'add', '.'], check=True)
subprocess.run(['git', '-C', str(root), '-c', 'user.name=Test Developer', '-c', 'user.email=test@example.test',
'commit', '-qm', 'baseline'], check=True)
def test_recipe_failure_or_missing_output_does_not_succeed(self):
script = self.root / 'tools/components/recipes/fake.sh'
script.write_text('exit 7\n')
with self.assertRaises(subprocess.CalledProcessError):
components.prepare('fake', self.root)
script.write_text('exit 0\n')
with self.assertRaisesRegex(ValueError, 'required outputs'):
components.prepare('fake', self.root)
def test_identity_covers_source_and_recipe_changes(self):
original = components.identity('fake', self.root)
(self.root / 'input.h').write_text('#define VALUE 2\n')
changed = components.identity('fake', self.root)
self.assertNotEqual(original, changed)
script = self.root / 'tools/components/recipes/fake.sh'
script.write_text(script.read_text() + '\nprintf changed\n')
self.assertNotEqual(changed, components.identity('fake', self.root))
def test_tool_requirements_are_installed_in_one_union(self):
definitions = dict(one=dict(packages=['gcc', 'cmake']), two=dict(packages=['gcc', 'qemu-user']))
with patch.object(components, 'registry', return_value=definitions), patch.object(components.subprocess, 'run') as run:
components.prepare_tools(['one', 'two'], self.root)
run.assert_called_once_with(['bash', 'tools/ci/install-ubuntu-packages.sh', 'cmake', 'gcc', 'qemu-user'],
cwd=self.root, check=True)
def test_prepared_handoff_verifies_every_file_and_source_tree(self):
components.prepare('fake', self.root)
archive = self.root / 'prepared.tar.gz'
components.pack(archive, self.root)
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=self.root, text=True).strip()
target = self.root / 'target'
self.fixture(target)
components.unpack(archive, tree, target)
self.assertEqual((target / 'app/src/main/assets/linuxfs/demo.so').read_bytes(), b'binary')
with self.assertRaisesRegex(ValueError, 'pinned source'):
components.unpack(archive, 'wrong-tree', target)
def test_tampered_component_bytes_fail_handoff_verification(self):
components.prepare('fake', self.root)
archive = self.root / 'prepared.tar.gz'
components.pack(archive, self.root)
corrupt = self.root / 'corrupt.tar.gz'
with tarfile.open(archive) as original, tarfile.open(corrupt, 'w:gz') as output:
for entry in original:
content = original.extractfile(entry).read()
if entry.name.endswith('demo.so'):
content = b'tampered'
entry.size = len(content)
output.addfile(entry, io.BytesIO(content))
target = self.root / 'target'
self.fixture(target)
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=self.root, text=True).strip()
with self.assertRaisesRegex(ValueError, 'file hashes'):
components.unpack(corrupt, tree, target)
def test_archive_cannot_overwrite_source_through_traversal(self):
archive = self.root / 'bad.tar.gz'
with tarfile.open(archive, mode='w:gz') as tar:
name = 'app/src/main/assets/linuxfs/../../../../../input.h'
entry = tarfile.TarInfo(name)
entry.size = 3
tar.addfile(entry, io.BytesIO(b'bad'))
with self.assertRaisesRegex(ValueError, 'Unsafe'):
components.unpack(archive, 'tree', self.root)
self.assertEqual((self.root / 'input.h').read_text(), '#define VALUE 1\n')
+53
View File
@@ -0,0 +1,53 @@
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[2]
class CursorCompositeTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
raise unittest.SkipTest('A host C compiler is required')
header = (REPO / 'app/src/main/cpp/waylandcomp/src/cardboard.h').read_text()
sources = dict(re.findall(r'CARDBOARD_SOURCE_(SCREEN|SBS|FLAT)\s*=\s*(\d+)', header))
for name in ('SCREEN', 'SBS', 'FLAT'):
if name not in sources:
raise AssertionError(f'cardboard.h no longer defines CARDBOARD_SOURCE_{name}')
if sources['SBS'] != '1':
raise AssertionError(f'CARDBOARD_SOURCE_SBS is {sources["SBS"]}, expected 1')
source = (REPO / 'app/src/main/cpp/waylandcomp/src/compositor.c').read_text()
start = source.index('static int cursor_scene_positions(int scene_w, int source, int ptr_x, int hx, int out[2]) {')
end = source.index('\n}\n', start) + 3
cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-cursor-composite-')
cls.addClassCleanup(cls.temp.cleanup)
folder = Path(cls.temp.name)
(folder / 'cursor_scene_positions.inc').write_text(source[start:end])
start = source.index('static int cursor_clip_to_eye(int scene_w, int eye, int x0, int w, int *dx, int *sx, int *dw) {')
end = source.index('\n}\n', start) + 3
(folder / 'cursor_clip_to_eye.inc').write_text(source[start:end])
(folder / 'cursor_constants.inc').write_text(
f'#define CARDBOARD_SOURCE_SCREEN {sources["SCREEN"]}\n'
f'#define CARDBOARD_SOURCE_SBS {sources["SBS"]}\n'
f'#define CARDBOARD_SOURCE_FLAT {sources["FLAT"]}\n')
cls.binary = folder / 'cursor-composite-test'
subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror', '-I', str(folder),
str(REPO / 'tools/tests/cursor_composite_harness.c'),
'-o', str(cls.binary)], check=True, capture_output=True, text=True)
def test_cursor_is_drawn_once_for_one_eye_and_mirrored_for_sbs(self):
result = subprocess.run([str(self.binary)], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn('PASS: cursor stereo placement', result.stdout)
if __name__ == '__main__':
unittest.main()
+102
View File
@@ -0,0 +1,102 @@
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
CHAIN = ROOT / "app/src/main/cpp/framegen/ddfg"
class DdfgFlowLevelsTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = shutil.which(os.environ.get("CXX", "c++"))
if compiler is None:
raise unittest.SkipTest("A host C++ compiler is required")
candidates = [Path("/usr/include/vulkan"), Path("/opt/homebrew/include/vulkan")]
for key in ("ANDROID_NDK_HOME", "ANDROID_NDK_ROOT", "NDK_HOME"):
if os.environ.get(key):
candidates.extend(Path(os.environ[key]).glob("toolchains/llvm/prebuilt/*/sysroot/usr/include/vulkan"))
for sdk in {Path(os.environ.get("ANDROID_HOME", "/nonexistent")),
Path(os.environ.get("ANDROID_SDK_ROOT", "/nonexistent")),
Path.home() / "Library/Android/sdk", Path.home() / "Android/Sdk"}:
candidates.extend(sdk.glob("ndk/*/toolchains/llvm/prebuilt/*/sysroot/usr/include/vulkan"))
headers = next((p for p in candidates if (p / "vulkan.h").is_file()), None)
if headers is None:
raise unittest.SkipTest("Vulkan headers from the Android NDK or system SDK are required")
cls.temp = tempfile.TemporaryDirectory(prefix="droiddeck-ddfg-levels-")
cls.addClassCleanup(cls.temp.cleanup)
work = Path(cls.temp.name)
shutil.copytree(headers, work / "include/vulkan", ignore=shutil.ignore_patterns("*.hpp"))
if (headers.parent / "vk_video").is_dir():
shutil.copytree(headers.parent / "vk_video", work / "include/vk_video")
cls.binary = work / "ddfg-flow-levels"
build = subprocess.run([
compiler, "-std=c++17", "-O2", "-Wall", "-Wextra",
"-Wno-missing-field-initializers", "-Wno-misleading-indentation",
"-I" + str(work / "include"), "-I" + str(CHAIN),
str(ROOT / "tools/tests/ddfg_flow_levels_harness.cpp"), str(CHAIN / "framegen.cpp"),
"-o", str(cls.binary),
], capture_output=True, text=True, timeout=120)
if build.returncode:
raise RuntimeError(build.stdout + build.stderr)
def run_case(self, mode="resources", model=4, preset=2, width=1280, height=720, generations=1, repeats=1):
result = subprocess.run([
str(self.binary), mode, str(model), str(preset), str(width), str(height),
str(generations), str(repeats),
], capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertTrue(result.stdout.startswith("PASS "), result.stdout)
return dict(re.findall(r"(\w+)=(\w+)", result.stdout))
def test_each_model_and_preset_allocates_only_solved_flow_levels(self):
for model in (3, 4):
for preset, finest in enumerate((1, 2, 3)):
with self.subTest(model=model, preset=preset):
values = self.run_case(model=model, preset=preset)
self.assertEqual(int(values["images"]), 32 - 2 * finest)
self.assertEqual(int(values["buffers"]), 18 - finest)
self.assertEqual(int(values["sets"]), 54 - 4 * finest)
def test_default_preset_saves_the_exact_high_resolution_flow_payload(self):
for width, height in ((1280, 720), (1920, 1080), (2560, 1440)):
with self.subTest(width=width, height=height):
values = self.run_case(width=width, height=height)
pixels = lambda level: max(width >> level, 1) * max(height >> level, 1)
luma_bytes = 4 * (2 * sum(pixels(l) for l in range(7)) +
sum(pixels(l) for l in range(3, 7)))
flow_bytes = 8 * (sum(pixels(l) for l in range(3, 7)) +
2 * width * height + 2 * pixels(3))
self.assertEqual(int(values["bytes"]), luma_bytes + flow_bytes)
def test_small_and_odd_extents_keep_valid_bindings_and_dispatch_sizes(self):
for width, height in ((1, 1), (17, 9), (135, 73)):
for preset in range(3):
with self.subTest(width=width, height=height, preset=preset):
self.run_case(width=width, height=height, preset=preset)
def test_two_three_and_four_times_generation_keep_the_same_dispatch_schedule(self):
for model in (3, 4):
for preset in range(3):
for generations in (1, 2, 3):
with self.subTest(model=model, preset=preset, generations=generations):
self.run_case("record", model=model, preset=preset, generations=generations, repeats=3)
def test_resize_rebuilds_every_solved_level(self):
for preset in range(3):
with self.subTest(preset=preset):
self.run_case("resize", preset=preset, generations=3, repeats=3)
def test_live_preset_changes_rebuild_the_level_range(self):
for model in (3, 4):
with self.subTest(model=model):
self.run_case("preset", model=model, generations=3, repeats=4)
if __name__ == "__main__":
unittest.main()
+278
View File
@@ -0,0 +1,278 @@
import hashlib
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile, ZipInfo
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import deploy_fast
def make_apk(path, entries):
with ZipFile(path, 'w') as package:
for name, (data, method) in entries.items():
info = ZipInfo(name)
info.extra = b'\0' * 6
package.writestr(info, data, compress_type=method)
class ClassifyTest(unittest.TestCase):
agent = 'tools/linuxfs/overlay/usr/local/bin/droiddeck-agent'
targets = {agent: 'assets/linuxfs/usr/local/bin/droiddeck-agent',
'tools/linuxfs/overlay/usr/local/bin/droiddeck-new': 'assets/linuxfs/usr/local/bin/droiddeck-new'}
entries = {'assets/linuxfs/usr/local/bin/droiddeck-agent'}
def classify(self, changes):
return deploy_fast.classify(changes, self.targets, self.entries)
def test_helpers_graft_and_tests_or_docs_are_ignored(self):
grafts, ignored, blocked = self.classify({
self.agent: 'M', 'tools/tests/test_x.py': 'M', 'docs/a/b.txt': 'A', 'README.md': 'M'})
self.assertEqual(grafts, {self.agent: 'assets/linuxfs/usr/local/bin/droiddeck-agent'})
self.assertEqual(sorted(ignored), ['README.md', 'docs/a/b.txt', 'tools/tests/test_x.py'])
self.assertEqual(blocked, {})
def test_everything_else_fails_closed(self):
changes = {'app/src/main/java/com/droiddeck/launcher/Foo.kt': 'M', 'tools/proot/build.sh': 'M',
'tools/linuxfs/overlay/usr/local/bin/droiddeck-new': 'A'}
self.assertEqual(set(self.classify(changes)[2]), set(changes))
self.assertEqual(set(self.classify({self.agent: 'D'})[2]), {self.agent})
def test_real_graft_targets_exist_in_the_overlay(self):
for path in deploy_fast.graft_targets():
self.assertTrue((deploy_fast.ROOT / path).is_file(), path)
class GraftTest(unittest.TestCase):
def test_replaces_entries_keeps_the_rest_and_drops_the_signature(self):
with tempfile.TemporaryDirectory() as directory:
donor, output = Path(directory) / 'donor.apk', Path(directory) / 'out.apk'
make_apk(donor, {'classes.dex': (b'dex', ZIP_DEFLATED), 'lib/arm64-v8a/libx.so': (b'elf', ZIP_STORED),
'assets/a': (b'old', ZIP_DEFLATED), 'META-INF/MANIFEST.MF': (b'm', ZIP_DEFLATED),
'META-INF/CERT.SF': (b's', ZIP_DEFLATED), 'META-INF/CERT.RSA': (b'r', ZIP_DEFLATED),
'META-INF/keep.version': (b'v', ZIP_DEFLATED)})
deploy_fast.graft(donor, {'assets/a': b'new'}, output)
with ZipFile(output) as package:
self.assertEqual(package.read('assets/a'), b'new')
self.assertEqual(package.read('classes.dex'), b'dex')
self.assertEqual(package.getinfo('lib/arm64-v8a/libx.so').compress_type, ZIP_STORED)
self.assertEqual(package.getinfo('classes.dex').compress_type, ZIP_DEFLATED)
self.assertEqual(package.getinfo('classes.dex').extra, b'')
self.assertEqual([n for n in package.namelist() if n.startswith('META-INF/')],
['META-INF/keep.version'])
def test_unknown_replacement_entry_is_refused(self):
with tempfile.TemporaryDirectory() as directory:
donor = Path(directory) / 'donor.apk'
make_apk(donor, {'assets/a': (b'old', ZIP_DEFLATED)})
with self.assertRaises(deploy_fast.Refused):
deploy_fast.graft(donor, {'assets/missing': b'x'}, Path(directory) / 'out.apk')
class RepoTest(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.addCleanup(self.directory.cleanup)
self.root = Path(self.directory.name)
patch = mock.patch.object(deploy_fast, 'ROOT', self.root)
patch.start()
self.addCleanup(patch.stop)
self.git('init', '-q', '-b', 'trunk')
self.git('config', 'user.email', 'a@b.c')
self.git('config', 'user.name', 'a')
(self.root / 'kept').write_text('1')
(self.root / 'gone').write_text('1')
self.git('add', '.')
self.git('commit', '-q', '-m', 'base')
self.base = self.git('rev-parse', 'HEAD').strip()
def git(self, *args):
return subprocess.run(['git', '-C', str(self.root), *args], capture_output=True, text=True, check=True).stdout
def test_changed_paths_cover_staged_untracked_and_deleted_files(self):
(self.root / 'kept').write_text('2')
(self.root / 'gone').unlink()
(self.root / 'fresh').write_text('x')
self.git('add', 'kept')
self.assertEqual(deploy_fast.changed_paths(self.base), {'kept': 'M', 'gone': 'D', 'fresh': 'A'})
def test_helper_bytes_normalize_text_but_not_binaries(self):
(self.root / 'script').write_bytes(b'a\r\nb\r\n')
(self.root / 'binary').write_bytes(b'\0a\r\n')
self.assertEqual(deploy_fast.helper_bytes('script'), b'a\nb\n')
self.assertEqual(deploy_fast.helper_bytes('binary'), b'\0a\r\n')
def test_a_donor_ahead_of_the_checkout_is_not_an_ancestor(self):
self.git('checkout', '-q', '-b', 'side')
(self.root / 'kept').write_text('3')
self.git('commit', '-qam', 'side')
side = self.git('rev-parse', 'HEAD').strip()
self.git('checkout', '-q', 'trunk')
self.assertTrue(deploy_fast.is_ancestor(self.base))
self.assertFalse(deploy_fast.is_ancestor(side))
class DonorTest(unittest.TestCase):
def test_pick_tag_takes_the_newest_ancestor(self):
commits = {'main-new': 'n', 'main-mid': 'm', 'main-old': 'o'}
with mock.patch.object(deploy_fast, 'release_tags', return_value=list(commits)), \
mock.patch.object(deploy_fast, 'donor_commit', side_effect=lambda tag: commits[tag]), \
mock.patch.object(deploy_fast, 'is_ancestor', side_effect=lambda commit: commit != 'n'):
self.assertEqual(deploy_fast.pick_tag(None), 'main-mid')
self.assertEqual(deploy_fast.pick_tag('main-new'), 'main-new')
def test_pick_tag_without_an_ancestor_is_refused(self):
with mock.patch.object(deploy_fast, 'release_tags', return_value=['main-a']), \
mock.patch.object(deploy_fast, 'donor_commit', return_value='a'), \
mock.patch.object(deploy_fast, 'is_ancestor', return_value=False):
with self.assertRaises(deploy_fast.Refused):
deploy_fast.pick_tag(None)
def test_checksum_mismatch_discards_the_cached_donor(self):
with tempfile.TemporaryDirectory() as directory:
folder = Path(directory) / 'main-x'
folder.mkdir()
apk = folder / 'DroidDeck-main-x.apk'
sums = folder / 'SHA256SUMS.txt'
apk.write_bytes(b'apk')
sums.write_text('0' * 64 + ' DroidDeck-main-x.apk\n')
with mock.patch.object(deploy_fast, 'CACHE', Path(directory)):
with self.assertRaises(deploy_fast.Refused):
deploy_fast.fetch_donor('main-x')
self.assertFalse(apk.exists())
apk.write_bytes(b'apk')
sums.write_text(hashlib.sha256(b'apk').hexdigest() + ' DroidDeck-main-x.apk\n')
self.assertEqual(deploy_fast.fetch_donor('main-x'), apk)
def test_a_cached_apk_without_checksums_is_downloaded_again(self):
with tempfile.TemporaryDirectory() as directory:
folder = Path(directory) / 'main-x'
folder.mkdir()
apk = folder / 'DroidDeck-main-x.apk'
apk.write_bytes(b'truncated')
calls = []
sums = hashlib.sha256(b'apk').hexdigest() + ' ' + apk.name + '\n'
def download(command, timeout, **options):
pattern = command[command.index('-p') + 1]
calls.append(pattern)
(folder / pattern).write_bytes(b'apk' if pattern.endswith('.apk') else sums.encode())
return mock.Mock(returncode=0, stderr='')
with mock.patch.object(deploy_fast, 'CACHE', Path(directory)):
with mock.patch.object(deploy_fast, 'run_bounded', download):
self.assertEqual(deploy_fast.fetch_donor('main-x'), apk)
self.assertEqual(calls, [apk.name, 'SHA256SUMS.txt'])
def test_a_failed_download_removes_the_apk_and_a_timeout_is_refused(self):
with tempfile.TemporaryDirectory() as directory:
folder = Path(directory) / 'main-x'
folder.mkdir()
apk = folder / 'DroidDeck-main-x.apk'
apk.write_bytes(b'partial')
failed = mock.Mock(returncode=1, stderr='boom')
with mock.patch.object(deploy_fast, 'CACHE', Path(directory)):
with mock.patch.object(deploy_fast, 'run_bounded', return_value=failed):
with self.assertRaises(deploy_fast.Refused):
deploy_fast.fetch_donor('main-x')
self.assertFalse(apk.exists())
timeout = deploy_fast.subprocess.TimeoutExpired('git', 1)
with mock.patch.object(deploy_fast.subprocess, 'run', side_effect=timeout):
with self.assertRaises(deploy_fast.Refused):
deploy_fast.git('status')
class SignTest(unittest.TestCase):
def test_paths_with_apostrophes_are_escaped_for_powershell(self):
seen = []
def run(command, **options):
seen.append(command)
return mock.Mock(returncode=1, stdout='', stderr='')
with mock.patch.object(deploy_fast, 'pwsh', return_value='pwsh'), \
mock.patch.object(deploy_fast, 'signing_env', return_value=None):
with mock.patch.object(deploy_fast, 'SIGNER', Path("C:/O'Brien/sign.ps1")):
with mock.patch.object(deploy_fast.subprocess, 'run', side_effect=run):
with self.assertRaises(deploy_fast.Refused):
deploy_fast.sign(Path("C:/a'b/in.apk"), Path("C:/a'b/out.apk"))
script = seen[0][-1]
self.assertIn("O''Brien", script)
self.assertIn("a''b", script)
self.assertNotIn("a'b", script.replace("a''b", ''))
self.assertNotIn("O'Brien", script.replace("O''Brien", ''))
def test_signing_env_prefers_the_override_then_the_checkout_then_the_main_checkout(self):
with tempfile.TemporaryDirectory() as directory:
base = Path(directory)
(base / 'main').mkdir()
(base / 'checkout').mkdir()
base = base.resolve()
override, local, shared = base / 'override.env', base / 'checkout/.signing.env', base / 'main/.signing.env'
common = (str(base / 'main/.git') + '\n').encode()
with mock.patch.object(deploy_fast, 'ROOT', base / 'checkout'), \
mock.patch.object(deploy_fast, 'git', return_value=common), \
mock.patch.dict(deploy_fast.os.environ, {'DROIDDECK_SIGNING_ENV': str(override)}):
self.assertIsNone(deploy_fast.signing_env())
shared.write_text('x')
self.assertEqual(deploy_fast.signing_env(), shared)
local.write_text('x')
self.assertEqual(deploy_fast.signing_env(), local)
override.write_text('x')
self.assertEqual(deploy_fast.signing_env(), override)
def test_the_env_signer_is_used_when_the_windows_signer_is_missing(self):
with tempfile.TemporaryDirectory() as directory:
base = Path(directory)
(base / 'build-tools/35.0.0').mkdir(parents=True)
for tool in ('aapt', 'zipalign', 'apksigner'):
(base / 'build-tools/35.0.0' / tool).write_text('')
signed = base / 'out.apk'
def run(command, **options):
signed.write_text('apk')
return mock.Mock(returncode=0, stdout='', stderr='')
with mock.patch.object(deploy_fast, 'SIGNER', base / 'missing.ps1'), \
mock.patch.object(deploy_fast, 'signing_env', return_value=base / '.signing.env'), \
mock.patch.object(deploy_fast, 'sdk_root', return_value=base), \
mock.patch.object(deploy_fast.subprocess, 'run', side_effect=run) as run_:
deploy_fast.sign(base / 'in.apk', signed)
self.assertEqual(run_.call_args[0][0][-4:], [str(deploy_fast.ROOT / 'tools/release/sign-apk.sh'), str(base / 'in.apk'), str(signed), str((base / 'build-tools/35.0.0').resolve())])
failed = mock.Mock(returncode=1, stdout='', stderr='boom')
signed.unlink()
with mock.patch.object(deploy_fast, 'SIGNER', base / 'missing.ps1'), \
mock.patch.object(deploy_fast, 'signing_env', return_value=base / '.signing.env'), \
mock.patch.object(deploy_fast, 'sdk_root', return_value=base), \
mock.patch.object(deploy_fast.subprocess, 'run', return_value=failed):
with self.assertRaises(deploy_fast.Refused):
deploy_fast.sign(base / 'in.apk', signed)
def test_the_newest_build_tools_win_numerically_and_the_env_file_cannot_replace_them(self):
with tempfile.TemporaryDirectory() as directory:
base = Path(directory).resolve()
for version in ('9.0.0', '35.0.0', '100.0.0'):
(base / 'build-tools' / version).mkdir(parents=True)
for tool in ('aapt', 'zipalign', 'apksigner'):
(base / 'build-tools' / version / tool).write_text('')
(base / 'build-tools/200.0.0').mkdir()
(base / 'build-tools/200.0.0/aapt').write_text('')
root = base / 'checkout'
(root / 'tools/release').mkdir(parents=True)
fake = root / 'tools/release/sign-apk.sh'
fake.write_text('#!/bin/bash\necho "$BUILD_TOOLS" > "$3"\n')
fake.chmod(0o755)
env_file = base / 'keys/.signing.env'
env_file.parent.mkdir()
env_file.write_text('BUILD_TOOLS=/wrong\n')
signed = base / 'out.apk'
with mock.patch.object(deploy_fast, 'ROOT', root), mock.patch.object(deploy_fast, 'sdk_root', return_value=base):
deploy_fast.sign_with_env(env_file, base / 'in.apk', signed)
self.assertEqual(signed.read_text().strip(), str(base / 'build-tools/100.0.0'))
if __name__ == '__main__':
unittest.main()
+88
View File
@@ -0,0 +1,88 @@
import os
from pathlib import Path
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/desktop/droiddeck-desktop-gpu"
PREFIX = "env -u WAYLAND_DISPLAY XDG_SESSION_TYPE=x11 ELECTRON_DISABLE_SANDBOX=1 "
FLAGS = " --no-sandbox --ozone-platform=x11"
def load():
source = SCRIPT.read_text()
assert source.rstrip().endswith("main()")
scope = {"__name__": "droiddeck_desktop_gpu"}
exec(compile(source.rstrip()[:-len("main()")], str(SCRIPT), "exec"), scope)
return scope
class DesktopChromiumAppsTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.gpu = load()
self.electron = self.app("lib/chat/chat", sandbox=True)
self.plain = self.app("lib/editor/editor", sandbox=False)
bin_dir = self.root / "bin"
bin_dir.mkdir()
(bin_dir / "chat").symlink_to(self.electron)
old_path = os.environ.get("PATH", "")
os.environ["PATH"] = "%s:%s" % (bin_dir, old_path)
self.addCleanup(os.environ.__setitem__, "PATH", old_path)
def app(self, relative, sandbox):
path = self.root / relative
path.parent.mkdir(parents=True)
path.write_text("#!/bin/sh\n")
path.chmod(0o755)
if sandbox:
(path.parent / "chrome-sandbox").write_text("")
return path
def entry(self, exec_line, categories="Network;", actions=""):
text = "[Desktop Entry]\nType=Application\nName=App\nExec=%s\nCategories=%s\n" % (exec_line, categories)
if actions:
text += "Actions=new;\n\n[Desktop Action new]\nName=New\nExec=%s\n" % actions
return text
def exec_lines(self, text):
return [l[5:] for l in text.splitlines() if l.startswith("Exec=")]
def test_an_electron_app_starts_without_the_sandbox_on_x11(self):
out = self.gpu["wrap"](self.entry("%s %%U" % self.electron, actions="%s --new" % self.electron))
self.assertEqual(self.exec_lines(out), [PREFIX + str(self.electron) + FLAGS + " %U",
PREFIX + str(self.electron) + FLAGS + " --new"])
self.assertIn("X-DroidDeck-GPU=1", out)
self.assertIn("Actions=new;", out)
self.assertNotIn("droiddeck-window", out)
def test_a_command_on_the_path_is_followed_to_its_install_folder(self):
out = self.gpu["wrap"](self.entry("chat %U"))
self.assertEqual(self.exec_lines(out), [PREFIX + "chat" + FLAGS + " %U"])
def test_a_quoted_program_keeps_its_quotes(self):
out = self.gpu["wrap"](self.entry('"%s" %%U' % self.electron))
self.assertEqual(self.exec_lines(out), [PREFIX + '"%s"' % self.electron + FLAGS + " %U"])
def test_other_apps_are_left_alone(self):
self.assertIsNone(self.gpu["wrap"](self.entry("%s %%F" % self.plain)))
self.assertIsNone(self.gpu["wrap"](self.entry("missing-program %F")))
def test_an_electron_game_goes_to_the_gpu_and_keeps_a_window_action(self):
out = self.gpu["wrap"](self.entry(str(self.electron), categories="Game;"))
fixed = PREFIX + str(self.electron) + FLAGS
self.assertEqual(self.exec_lines(out), ["droiddeck-gpu " + fixed, fixed])
def test_with_the_gpu_off_electron_apps_are_still_fixed(self):
out = self.gpu["wrap"](self.entry(str(self.electron), categories="Game;"), False)
self.assertEqual(self.exec_lines(out), [PREFIX + str(self.electron) + FLAGS])
self.assertIsNone(self.gpu["wrap"](self.entry(str(self.plain), categories="Game;"), False))
def test_games_that_are_not_chromium_are_unchanged(self):
out = self.gpu["wrap"](self.entry(str(self.plain), categories="Game;"))
self.assertEqual(self.exec_lines(out), ["droiddeck-gpu " + str(self.plain), str(self.plain)])
if __name__ == "__main__":
unittest.main()
+74
View File
@@ -0,0 +1,74 @@
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
TOOLS = Path(__file__).resolve().parents[1]
GPU = TOOLS / "linuxfs/desktop/droiddeck-gpu"
WINDOW = TOOLS / "linuxfs/overlay/usr/local/bin/droiddeck-gpu-window"
KEYS = ("MESA_VK_WSI_DEBUG", "SDL_VIDEODRIVER", "WAYLAND_DISPLAY", "BL_GPU_CHILD_WSI", "BL_DESKTOP_GPU_CHILD")
class DesktopGpuWindowTest(unittest.TestCase):
def setUp(self):
self.tmp = Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.tmp, True)
self.out = self.tmp / "gamescope.json"
fake = self.tmp / "gamescope"
fake.write_text("#!%s\nimport json, os, sys\njson.dump({'args': sys.argv[1:], 'env': {k: os.environ.get(k) for k in %r}}, open(%r, 'w'))\n"
% (sys.executable, KEYS, str(self.out)))
fake.chmod(0o755)
def run_gpu(self, **env):
base = {k: v for k, v in os.environ.items() if k not in KEYS + ("BL_DEBUG_DIR", "BL_DESKTOP_GPU_WINDOW")}
base.update(PATH="%s:%s" % (self.tmp, os.environ["PATH"]), BL_WIDTH="1280", BL_HEIGHT="720", **env)
subprocess.run(["bash", str(GPU), "game.exe"], env=base, check=True, capture_output=True)
return json.loads(self.out.read_text())
def test_on_the_desktop_the_game_gets_a_window_of_its_own(self):
run = self.run_gpu(WAYLAND_DISPLAY="wayland-1", MESA_VK_WSI_DEBUG="sw,linear")
self.assertEqual(run["args"][:2], ["--backend", "sdl"])
self.assertNotIn("-f", run["args"])
self.assertEqual(run["args"][-2:], ["/usr/local/bin/droiddeck-gpu-window", "game.exe"])
self.assertEqual(run["env"]["WAYLAND_DISPLAY"], "wayland-1")
self.assertEqual(run["env"]["SDL_VIDEODRIVER"], "wayland")
self.assertEqual(run["env"]["MESA_VK_WSI_DEBUG"], "sw")
self.assertEqual(run["env"]["BL_GPU_CHILD_WSI"], "linear")
self.assertEqual(run["env"]["BL_DESKTOP_GPU_CHILD"], "1")
def test_full_screen_stays_available(self):
for env in ({"WAYLAND_DISPLAY": "wayland-1", "BL_DESKTOP_GPU_WINDOW": "0"}, {"WAYLAND_DISPLAY": "wayland-0"}, {}):
run = self.run_gpu(**env)
self.assertEqual(run["args"][:2], ["--backend", "wayland"])
self.assertIn("-f", run["args"])
self.assertEqual(run["env"]["WAYLAND_DISPLAY"], "wayland-0")
self.assertIsNone(run["env"]["MESA_VK_WSI_DEBUG"])
self.assertIsNone(run["env"]["BL_GPU_CHILD_WSI"])
def test_the_game_inside_the_window_gets_its_own_settings_back(self):
script = "import json, os; print(json.dumps({k: os.environ.get(k) for k in %r}))" % (KEYS,)
for saved, expected in (("linear", "linear"), ("", None)):
env = {k: v for k, v in os.environ.items() if k not in KEYS + ("DISPLAY",)}
env.update(BL_GPU_CHILD_WSI=saved, MESA_VK_WSI_DEBUG="sw", SDL_VIDEODRIVER="wayland")
out = subprocess.run([sys.executable, str(WINDOW), sys.executable, "-c", script], env=env,
check=True, capture_output=True, text=True).stdout
child = json.loads(out)
self.assertEqual(child["MESA_VK_WSI_DEBUG"], expected)
self.assertEqual(child["SDL_VIDEODRIVER"], "x11")
self.assertIsNone(child["BL_GPU_CHILD_WSI"])
def test_outside_a_window_the_game_keeps_its_settings(self):
script = "import os; print(os.environ.get('MESA_VK_WSI_DEBUG'), os.environ.get('SDL_VIDEODRIVER'))"
env = {k: v for k, v in os.environ.items() if k not in KEYS + ("DISPLAY",)}
env.update(MESA_VK_WSI_DEBUG="linear", SDL_VIDEODRIVER="x11")
out = subprocess.run([sys.executable, str(WINDOW), sys.executable, "-c", script], env=env,
check=True, capture_output=True, text=True).stdout.split()
self.assertEqual(out, ["linear", "x11"])
if __name__ == "__main__":
unittest.main()
+104
View File
@@ -0,0 +1,104 @@
import json
import os
from pathlib import Path
import socket
import struct
import subprocess
import tempfile
import time
import unittest
IME = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-ime'
def message(obj, opcode, payload=b''):
return struct.pack('<II', obj, ((8 + len(payload)) << 16) | opcode) + payload
def wl_string(value):
data = value.encode() + b'\0'
return struct.pack('<I', len(data)) + data + b'\0' * (-len(data) % 4)
class DesktopImeTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.session = Path(self.temp.name)
self.server, client = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
self.addCleanup(self.server.close)
env = dict(os.environ, BL_LAUNCH_DIR=str(self.session), WAYLAND_SOCKET=str(client.fileno()))
self.proc = subprocess.Popen(['python3', str(IME)], env=env, pass_fds=[client.fileno()],
stderr=subprocess.PIPE, text=True)
client.close()
self.addCleanup(self.stop)
self.server.settimeout(5)
self.assertEqual(self.read(12), message(1, 1, struct.pack('<I', 2)))
self.server.sendall(message(2, 0, struct.pack('<I', 7) + wl_string('zwp_input_method_v1') + struct.pack('<I', 1)))
bind = self.read(8 + 4 + len(wl_string('zwp_input_method_v1')) + 8)
self.assertEqual(bind, message(2, 0, struct.pack('<I', 7) + wl_string('zwp_input_method_v1') + struct.pack('<II', 1, 3)))
def stop(self):
if self.proc.poll() is None:
self.proc.kill()
self.proc.wait(5)
self.proc.stderr.close()
def read(self, size):
data = b''
while len(data) < size:
chunk = self.server.recv(size - len(data))
if not chunk:
break
data += chunk
return data
def state(self, seq, timeout=5):
path = self.session / 'agent/ime.json'
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
snapshot = json.loads(path.read_text())
except (OSError, ValueError):
snapshot = None
if snapshot and snapshot['seq'] >= seq:
return snapshot
time.sleep(0.02)
self.fail('ime.json never reached seq %d' % seq)
def test_focus_and_blur_are_reported(self):
self.server.sendall(message(3, 0, struct.pack('<I', 100)))
self.assertTrue(self.state(1)['textField'])
self.server.sendall(message(100, 2, struct.pack('<II', 0, 9)))
snapshot = self.state(2)
self.assertEqual((snapshot['textField'], snapshot['purpose']), (True, 9))
self.server.sendall(message(3, 1, struct.pack('<I', 100)))
self.assertEqual(self.read(8), message(100, 0))
self.assertFalse(self.state(3)['textField'])
self.assertEqual(oct((self.session / 'agent/ime.json').stat().st_mode & 0o777), '0o600')
def test_switching_fields_does_not_flicker(self):
self.server.sendall(message(3, 0, struct.pack('<I', 100)))
self.assertTrue(self.state(1)['textField'])
self.server.sendall(message(3, 1, struct.pack('<I', 100)) + message(3, 0, struct.pack('<I', 101)))
self.assertEqual(self.read(8), message(100, 0))
time.sleep(0.4)
self.assertEqual(self.state(1), {'textField': True, 'hint': 0, 'purpose': 0, 'seq': 1})
def test_compositor_exit_clears_the_field(self):
self.server.sendall(message(3, 0, struct.pack('<I', 100)))
self.assertTrue(self.state(1)['textField'])
self.server.close()
self.assertEqual(self.proc.wait(5), 0)
self.assertFalse(self.state(2)['textField'])
def test_a_protocol_error_clears_the_field(self):
self.server.sendall(message(3, 0, struct.pack('<I', 100)))
self.assertTrue(self.state(1)['textField'])
self.server.sendall(message(1, 0, struct.pack('<II', 3, 1) + wl_string('bad request')))
self.assertEqual(self.proc.wait(5), 1)
self.assertFalse(self.state(2)['textField'])
if __name__ == '__main__':
unittest.main()
+97 -1
View File
@@ -316,7 +316,6 @@ class GameFolderTest(Temp):
def appinfo(apps):
"""appinfo.vdf as the client writes it (format 29: keys in a string table)."""
strings = []
def key(name):
@@ -456,6 +455,15 @@ class ProtonRunTest(Temp):
("0", "0", str(appid << 32 | 0x02000000), "1"))
self.assertEqual(env["STEAM_COMPAT_DATA_PATH"], str(self.root / ("steamapps/compatdata/%d" % appid)))
def test_a_window_draws_with_the_desktops_software_gl(self):
target = RUN["Target"]("2596113170", "/c", "/i", str(self.root), "fixed")
session = {"MESA_LOADER_DRIVER_OVERRIDE": "zink", "GALLIUM_DRIVER": "zink", "LIBGL_KOPPER_DRI2": "true"}
env = RUN["environment"](session, target, "/tool", str(self.root), window=True)
self.assertEqual((env["LIBGL_ALWAYS_SOFTWARE"], env["GALLIUM_DRIVER"]), ("1", "llvmpipe"))
self.assertNotIn("MESA_LOADER_DRIVER_OVERRIDE", env)
self.assertNotIn("LIBGL_KOPPER_DRI2", env)
self.assertEqual(RUN["environment"](session, target, "/tool", str(self.root))["GALLIUM_DRIVER"], "zink")
def test_a_steam_game_keeps_its_own_id(self):
target = RUN["Target"]("413150", "/c", "/i", str(self.root), "steam")
env = RUN["environment"]({"PROTON_USE_WINED3D": "0"}, target, "/tool", str(self.root), window=True)
@@ -465,6 +473,13 @@ class ProtonRunTest(Temp):
self.assertEqual(RUN["arguments"]("/root/x/setup.msi", ["/qn"]), ["msiexec", "/i", "Z:\\root\\x\\setup.msi", "/qn"])
self.assertEqual(RUN["arguments"]("/root/x/a.exe", ["-w"]), ["/root/x/a.exe", "-w"])
def test_a_virtual_desktop_runs_the_program_by_its_windows_path(self):
pfx = "/root/Steam/steamapps/compatdata/2596113170"
self.assertEqual(RUN["arguments"](pfx + "/pfx/drive_c/Program Files (x86)/Battle.net/Battle.net Launcher.exe", [], "1280x720", pfx),
["explorer.exe", "/desktop=droiddeck,1280x720", "C:\\Program Files (x86)\\Battle.net\\Battle.net Launcher.exe"])
self.assertEqual(RUN["arguments"]("/root/.cache/setup.exe", ["--lang=enUS"], "1920x1080", pfx),
["explorer.exe", "/desktop=droiddeck,1920x1080", "Z:\\root\\.cache\\setup.exe", "--lang=enUS"])
def test_a_games_own_program_is_left_to_steam(self):
self.write("Steam/appcache/appinfo.vdf", appinfo({413150: STARDEW}))
self.assertEqual(RUN["compat"]()["launch_programs"](STARDEW["config"]),
@@ -554,6 +569,50 @@ class ProtonRunTest(Temp):
self.game("70", "Linux Game", "run.sh")
self.assertEqual(self.run_main(["--app", "70"])[0], 2)
def test_without_steam_a_shortcut_reuses_a_signed_prefix_folder(self):
bat = self.write("Games/Epic/Game/.droiddeck-launch.bat", b"@echo off")
start = self.tmp / "Games/Epic/Game/Binaries"
start.mkdir()
signed = self.root / "steamapps/compatdata/-1561598272"
signed.mkdir(parents=True)
code, calls, dirs = self.run_main(["--app", "2733369024", "--dir", str(start), "--", str(bat)])
self.assertIsNone(code)
env = calls[0][2]
self.assertEqual(env["STEAM_COMPAT_DATA_PATH"], str(signed))
self.assertFalse((self.root / "steamapps/compatdata/2733369024").exists())
def test_prefix_folder_prefers_initialized_unsigned_over_empty_signed(self):
signed = self.root / "steamapps/compatdata/-1561598272"
signed.mkdir(parents=True)
initialized = self.root / "steamapps/compatdata/2733369024/pfx"
initialized.mkdir(parents=True)
self.assertEqual(RUN["prefix_folder"](str(self.root), 2733369024), str(initialized.parent))
def test_prefix_folder_prefers_initialized_signed_over_empty_unsigned(self):
unsigned = self.root / "steamapps/compatdata/2733369024"
unsigned.mkdir(parents=True)
initialized = self.root / "steamapps/compatdata/-1561598272/pfx"
initialized.mkdir(parents=True)
self.assertEqual(RUN["prefix_folder"](str(self.root), 2733369024), str(initialized.parent))
def test_prefix_folder_prefers_unsigned_when_initialization_ties(self):
unsigned = self.root / "steamapps/compatdata/2733369024/pfx"
unsigned.mkdir(parents=True)
signed = self.root / "steamapps/compatdata/-1561598272/pfx"
signed.mkdir(parents=True)
self.assertEqual(RUN["prefix_folder"](str(self.root), 2733369024), str(unsigned.parent))
def test_prefix_folder_skips_numeric_names_too_long_to_convert(self):
from unittest.mock import patch
folder = self.root / "steamapps/compatdata"
folder.mkdir(parents=True)
with patch.object(RUN["os"], "listdir", return_value=["9" * 5000]):
self.assertEqual(RUN["prefix_folder"](str(self.root), 2733369024),
str(folder / "2733369024"))
def test_without_steam_a_shortcut_keeps_its_prefix_folder_and_launch_options(self):
bat = self.write("Games/Epic/Game/.droiddeck-launch.bat", b"@echo off")
start = self.tmp / "Games/Epic/Game/Binaries"
@@ -568,6 +627,43 @@ class ProtonRunTest(Temp):
self.assertTrue((self.root / "steamapps/compatdata/2733369024").is_dir())
self.assertEqual(dirs, [str(start)])
def write_account(self):
self.write("Steam/config/loginusers.vdf",
'"users"\n{\n\t"76561198352563669"\n\t{\n\t\t"MostRecent"\t\t"1"\n\t}\n}\n')
return "Steam/userdata/392297941/config/"
def test_without_steam_and_options_a_game_takes_its_steam_launch_options(self):
config = self.write_account()
self.write(config + "localconfig.vdf", '"UserLocalConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n'
'\t\t\t"Steam"\n\t\t\t{\n\t\t\t\t"apps"\n\t\t\t\t{\n\t\t\t\t\t"413150"\n\t\t\t\t\t{\n'
'\t\t\t\t\t\t"cloud"\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t"LaunchOptions"\t\t"-wrong"\n\t\t\t\t\t\t}\n'
'\t\t\t\t\t\t"LaunchOptions"\t\t"DXVK_HUD=1 %command% -d3d9 \\"-name=Two Words\\""\n'
'\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n')
shortcuts = runpy.run_path(str(BIN / "droiddeck-steam-shortcuts"))
self.write(config + "shortcuts.vdf", shortcuts["write"]({"shortcuts": {
"0": {"appid": -1561598272, "AppName": "Batman", "LaunchOptions": "-d3d9"},
"1": {"appid": -1, "AppName": "Other", "LaunchOptions": "-x"},
}}))
root = str(self.root)
self.assertEqual(RUN["steam_options"]("413150", root), 'DXVK_HUD=1 %command% -d3d9 "-name=Two Words"')
self.assertEqual(RUN["steam_options"]("2733369024", root), "-d3d9")
self.assertEqual(RUN["steam_options"]("70", root), "")
self.assertEqual(RUN["steam_options"]("2733369025", root), "")
info = {"common": {"type": "Game"}, "config": {"launch": {
"0": {"executable": "Game.exe", "type": "default", "config": {"oslist": "windows"}}}}}
self.write("Steam/appcache/appinfo.vdf", appinfo({413150: info}))
program = self.game("413150", "Some Game", "Game.exe")
code, calls, _ = self.run_main(["--app", "413150"])
self.assertIsNone(code)
_, argv, env = calls[0]
self.assertEqual(argv[1:], ["waitforexitandrun", str(program), "-d3d9", "-name=Two Words"])
self.assertEqual(env["DXVK_HUD"], "1")
bat = self.write("Games/GOG/Batman/Batman.exe", b"MZ")
code, calls, _ = self.run_main(["--app", "2733369024", "--", str(bat)])
self.assertEqual(calls[0][1][1:], ["waitforexitandrun", str(bat), "-d3d9"])
code, calls, _ = self.run_main(["--app", "2733369024", "--options", "", "--", str(bat)])
self.assertEqual(calls[0][1][1:], ["waitforexitandrun", str(bat)])
def test_launch_options_split_like_steam_does(self):
split = RUN["split_options"]
self.assertEqual(split(""), ({}, []))
+209
View File
@@ -0,0 +1,209 @@
import json
from pathlib import Path
import runpy
import subprocess
import tempfile
import unittest
from unittest import mock
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
LIVE = runpy.run_path(str(BIN / "droiddeck-steam-compat"))
class DeviceControlsTest(unittest.TestCase):
def test_sync_controls_queued_notifications_are_processed_without_another_frame(self):
with tempfile.TemporaryDirectory() as directory:
Path(directory, "state.json").write_text(json.dumps({"brightness": 0.5, "revision": 1}))
helper = LIVE["Helper"]("/steam", 0)
helper.controls_installed = True
page = mock.Mock(events=[])
page.ws.pending.return_value = False
helper.page = page
payload = json.dumps({"id": 1, "options": {"strTitle": "Pick a file"}})
def evaluate(expression):
for name, value in [(LIVE["BRIGHTNESS_BINDING"], "0.4"), (LIVE["FILE_BINDING"], payload)]:
page.events.append({"method": "Runtime.bindingCalled", "params": {"name": name, "payload": value}})
return True
def drain():
events, page.events = page.events, []
return events
page.evaluate.side_effect = evaluate
page.drain.side_effect = drain
helper.reconcile = mock.Mock()
helper.sync_frame_limit = mock.Mock()
helper.ensure_tools = mock.Mock(side_effect=[None, StopIteration])
helper.open_chooser = mock.Mock()
watcher = mock.Mock()
watcher.fileno.return_value = -1
watcher.changed.return_value = False
globals_ = LIVE["Helper"].serve.__globals__
with mock.patch.dict(globals_, {"CONTROLS_DIR": directory,
"brightness_request": lambda value: LIVE["brightness_request"](value, directory)}), \
mock.patch.dict(globals_["COMPAT"], {"state_path": lambda name: str(Path(directory, name))}), \
mock.patch.object(globals_["select"], "select", return_value=([], [], [])) as select_:
with self.assertRaises(StopIteration):
helper.serve(watcher)
self.assertEqual(float(Path(directory, "brightness.request").read_text()), 0.4)
helper.open_chooser.assert_called_once_with(payload)
self.assertEqual(page.events, [])
select_.assert_not_called()
def test_native_brightness_notifications_do_not_echo_android_readback(self):
harness = r"""
const assert = require('assert');
globalThis.window = globalThis;
const requests = [], callbacks = [];
const timers = [];
let now = 0;
Object.defineProperty(globalThis, 'performance', { value: { now: () => now }, configurable: true });
globalThis.setTimeout = callback => timers.push(callback);
let current = 1;
window.droiddeckBrightnessChanged = value => requests.push(Number(value));
globalThis.SteamClient = { System: { Display: {
RegisterForBrightnessChanges(cb) { callbacks.push(cb); cb({ flBrightness: 1 }); return {}; },
SetBrightness(value) {
if (current === value) return;
current = value;
callbacks.forEach(cb => cb({ flBrightness: value }));
}
} } };
const script = require('fs').readFileSync(process.argv[2], 'utf8');
assert.strictEqual(eval(script), 'installed');
assert.strictEqual(eval(script), 'present');
const bridge = window.__droiddeckBrightness;
now = 10000;
bridge.set(0.5);
bridge.set(0.5);
assert.deepStrictEqual(requests, []);
callbacks.forEach(cb => cb({ flBrightness: 0.8 }));
assert.deepStrictEqual(requests, [0.8]);
now += 1000;
bridge.set(0.7992126);
assert.deepStrictEqual(requests, [0.8]);
for (const value of [NaN, Infinity, -1, 2, '0.5']) callbacks.forEach(cb => cb({ flBrightness: value }));
assert.deepStrictEqual(requests, [0.8]);
assert.strictEqual(callbacks.length, 1);
now += 1000;
SteamClient.System.Display.SetBrightness = value => callbacks.forEach(cb => cb({ flBrightness: value + 0.1 }));
bridge.set(0.3);
assert.deepStrictEqual(requests, [0.8]);
callbacks.forEach(cb => cb({ flBrightness: 0.6 }));
assert.deepStrictEqual(requests, [0.8, 0.6]);
SteamClient.System.Display.SetBrightness = () => {};
bridge.set(0.65);
timers.forEach(callback => callback());
callbacks.forEach(cb => cb({ flBrightness: 0.7 }));
assert.deepStrictEqual(requests, [0.8, 0.6, 0.7]);
// A held stick: Android's lagging readbacks must not pull Steam back while it keeps stepping.
let applied = [];
SteamClient.System.Display.SetBrightness = value => applied.push(value);
for (const value of [0.71, 0.72, 0.73]) { now += 50; callbacks.forEach(cb => cb({ flBrightness: value })); }
assert.strictEqual(bridge.set(0.71), 'held');
now += 100;
assert.strictEqual(bridge.set(0.72), 'held');
assert.deepStrictEqual(applied, []);
now += 800;
assert.strictEqual(bridge.set(0.3), true);
assert.deepStrictEqual(applied, [0.3]);
"""
with tempfile.TemporaryDirectory() as directory:
script = Path(directory) / "bridge.js"
script.write_text(LIVE["BRIGHTNESS_JS"])
test = Path(directory) / "test.js"
test.write_text(harness)
result = subprocess.run(["node", str(test), str(script)], capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
def test_requests_reject_nonfinite_and_out_of_range_values(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "brightness.request"
for value in ["NaN", "Infinity", "-0.1", "1.1", "invalid", None]:
LIVE["brightness_request"](value, directory)
self.assertFalse(path.exists())
LIVE["brightness_request"]("0.4", directory)
self.assertEqual(float(path.read_text()), 0.4)
def test_failed_request_revision_republishes_the_actual_brightness(self):
with tempfile.TemporaryDirectory() as directory:
globals_ = LIVE["Helper"].sync_controls.__globals__
previous = globals_["CONTROLS_DIR"]
try:
globals_["CONTROLS_DIR"] = directory
helper = LIVE["Helper"]("/steam", 0)
calls = []
helper.controls_installed = True
helper.page = type("Page", (), {"evaluate": lambda self, expression: calls.append(expression) or True})()
path = Path(directory) / "state.json"
path.write_text(json.dumps({"brightness": 0.5, "revision": 1}))
helper.sync_controls()
helper.sync_controls()
self.assertEqual(len(calls), 1)
path.write_text(json.dumps({"brightness": 0.5, "revision": 2}))
helper.sync_controls()
self.assertEqual(len(calls), 2)
finally:
globals_["CONTROLS_DIR"] = previous
def test_a_readback_held_by_the_bridge_is_retried_instead_of_cached(self):
with tempfile.TemporaryDirectory() as directory:
globals_ = LIVE["Helper"].sync_controls.__globals__
previous = globals_["CONTROLS_DIR"]
try:
globals_["CONTROLS_DIR"] = directory
Path(directory, "state.json").write_text(json.dumps({"brightness": 0.5, "revision": 1}))
results = ["held", "held", True]
helper = LIVE["Helper"]("/steam", 0)
helper.controls_installed = True
helper.page = type("Page", (), {"evaluate": lambda self, expression: results.pop(0)})()
helper.sync_controls()
helper.sync_controls()
self.assertTrue(helper.controls_installed)
self.assertIsNone(helper.controls_state)
helper.sync_controls()
self.assertEqual(helper.controls_state["revision"], 1)
self.assertEqual(results, [])
finally:
globals_["CONTROLS_DIR"] = previous
def test_display_bridge_retries_binding_and_install_before_caching_state(self):
with tempfile.TemporaryDirectory() as directory:
globals_ = LIVE["Helper"].sync_controls.__globals__
previous = globals_["CONTROLS_DIR"]
try:
globals_["CONTROLS_DIR"] = directory
Path(directory, "state.json").write_text(json.dumps({"brightness": 0.5, "revision": 1}))
calls, installs, writes = [], ["unavailable", "installed", "present"], []
class Page:
present = True
def call(self, method, params):
calls.append((method, params))
def evaluate(self, expression):
if expression == LIVE["BRIGHTNESS_JS"]:
return installs.pop(0)
writes.append(expression)
return self.present
helper = LIVE["Helper"]("/steam", 0)
helper.page = Page()
helper.sync_controls()
self.assertIsNone(helper.controls_state)
self.assertFalse(writes)
helper.sync_controls()
helper.sync_controls()
self.assertEqual(len(calls), 2)
self.assertEqual(len(writes), 1)
self.assertEqual(calls[1], ("Runtime.addBinding", {"name": LIVE["BRIGHTNESS_BINDING"]}))
helper.page.present = False
Path(directory, "state.json").write_text(json.dumps({"brightness": 0.7, "revision": 2}))
helper.sync_controls()
self.assertIsNone(helper.controls_state)
self.assertFalse(helper.controls_installed)
helper.page.present = True
helper.sync_controls()
self.assertEqual(len(calls), 3)
self.assertEqual(helper.controls_state["brightness"], 0.7)
finally:
globals_["CONTROLS_DIR"] = previous
if __name__ == "__main__":
unittest.main()
+12
View File
@@ -47,6 +47,8 @@ elif a[:1] == ["shell"]:
s.setdefault("log_commands", []).append(a)
p.write_text(json.dumps(s))
if "ls" in a: print(s["session"])
elif a[-1].endswith("/device.txt"): print(s.get("report", ""))
elif a[-1].endswith("/app.log"): print(s.get("app_log", ""))
else: print("package not debuggable")
'''
@@ -112,6 +114,16 @@ class DeviceSmokeTest(unittest.TestCase):
self.assertEqual(1, result.returncode, result.stdout)
self.assertIn("could not launch", result.stdout)
def test_fast_path_requires_a_session_activation_log(self):
for active in (False, True):
with self.subTest(active=active):
result, _ = self.run_smoke(
session="session", report="proot fast path (eligible) true",
app_log="proot: fast path on (66 binds)" if active else "proot: fast path off - too many binds",
)
self.assertEqual(0, result.returncode, result.stdout + result.stderr)
self.assertEqual(active, "PASS proot fast path was on" in result.stdout)
def test_session_name_remains_one_remote_argument(self):
session = "x';id;#"
result, state = self.run_smoke(session=session)
+216 -3
View File
@@ -1,4 +1,3 @@
"""The guest agent: its request files, exec, errors and the DevTools client, without a session."""
import base64
import hashlib
import importlib.machinery
@@ -26,7 +25,6 @@ loader.exec_module(agent)
class FakeDevTools(threading.Thread):
"""GET /json lists one target; its WebSocket answers Runtime.evaluate the way Chromium does."""
def __init__(self, answer):
super().__init__(daemon=True)
@@ -196,6 +194,63 @@ class AgentTest(unittest.TestCase):
answer = self.ask({"kind": "vr", "action": "status"})
self.assertEqual("UNAVAILABLE", answer["error"]["code"])
def fake_proc(self, pid, parent, comm, env):
root = self.dir / "proc" / str(pid)
root.mkdir(parents=True)
(root / "comm").write_text(comm + "\n")
(root / "stat").write_text("%d (%s) S %d 0 0" % (pid, comm, parent))
(root / "environ").write_bytes(b"\0".join(b"%s=%s" % (k.encode(), v.encode()) for k, v in env.items()) + b"\0")
def tag(self, wins, arch="arm64"):
snap = {"displays": {":0": [], ":1": wins}}
with patch.dict(os.environ, {"DISPLAY": ":0", "BL_STEAM_CLIENT_ARCH": arch}), \
patch.object(agent.subprocess, "run") as run, patch.object(agent, "log"):
run.return_value.returncode = 0
tagged = agent.tag_game_windows(snap, proc=self.dir / "proc")
return tagged, [call.args[0][-1] for call in run.call_args_list]
def test_a_sandboxed_flatpak_window_is_tagged_with_its_shortcut(self):
self.fake_proc(40, 1, "gjs", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "2166984148"})
win = {"window": 7, "map": "viewable", "class": "com.github.tchx84.Flatseal", "pid": None}
self.assertEqual(self.tag([win]), (True, ["2166984148"]))
def test_a_flatpak_window_named_after_the_app_is_matched(self):
self.fake_proc(40, 1, "vacuumtube", {"FLATPAK_ID": "rocks.shy.VacuumTube", "SteamAppId": "3000000001"})
win = {"window": 7, "map": "viewable", "class": "VacuumTube", "pid": None}
self.assertEqual(self.tag([win]), (True, ["3000000001"]))
def test_a_window_whose_process_has_no_app_id_falls_back_to_its_flatpak(self):
self.fake_proc(40, 1, "bwrap", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "2166984148"})
self.fake_proc(41, 1, "gjs", {})
win = {"window": 7, "map": "viewable", "class": "com.github.tchx84.Flatseal", "pid": 41}
self.assertEqual(self.tag([win]), (True, ["2166984148"]))
def test_a_flatpak_shared_by_two_shortcuts_is_not_guessed(self):
self.fake_proc(40, 1, "gjs", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "2166984148"})
self.fake_proc(42, 1, "gjs", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "3000000003"})
win = {"window": 7, "map": "viewable", "class": "com.github.tchx84.Flatseal", "pid": None}
self.assertEqual(self.tag([win]), (False, []))
def test_a_window_outside_the_reaper_takes_its_process_app_id(self):
self.fake_proc(30, 1, "proot", {})
self.fake_proc(31, 30, "electron", {"SteamAppId": "3000000002"})
win = {"window": 8, "map": "viewable", "class": "electron", "pid": 31}
self.assertEqual(self.tag([win]), (True, ["3000000002"]))
def test_windows_gamescope_already_resolves_are_left_alone(self):
self.fake_proc(20, 1, "reaper", {})
self.fake_proc(21, 20, "game", {"SteamAppId": "413150"})
wins = [{"window": 9, "map": "viewable", "class": "game", "pid": 21},
{"window": 10, "map": "viewable", "class": "tagged", "pid": None, "appId": 5},
{"window": 11, "map": "viewable", "class": "other", "pid": None},
{"window": 12, "map": "unmapped", "class": "game", "pid": 21},
{"window": 13, "map": "viewable", "class": "steam_app_550", "pid": None}]
self.assertEqual(self.tag(wins), (False, []))
def test_the_x86_client_still_tags_its_game_classes(self):
win = {"window": 13, "map": "viewable", "class": "steam_app_550", "pid": None}
self.assertEqual(self.tag([win], arch="x86_64"), (True, ["550"]))
def test_exec_output_and_status(self):
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "echo out; echo err >&2; exit 3"], "env": {"X": "1"}})
self.assertTrue(answer["ok"])
@@ -578,6 +633,165 @@ class SteamKeyboardTest(unittest.TestCase):
self.assertFalse(watcher.attached)
self.assertEqual(3, log.call_count)
def test_requests_and_route_files(self):
watcher = agent.KeyboardWatcher(self.dir)
watcher.request()
watcher.request()
self.assertEqual({"seq": 2}, json.loads((self.dir / "keyboard-request.json").read_text()))
self.assertFalse(watcher.wanted_route())
(self.dir / "keyboard-route").write_text("bottom")
self.assertTrue(watcher.wanted_route())
(self.dir / "keyboard-route").write_text("top")
self.assertFalse(watcher.wanted_route())
watcher.event({"method": "Runtime.bindingCalled", "params": {"name": agent.KEYBOARD_BINDING, "payload": "request"}})
self.assertEqual({"seq": 3}, json.loads((self.dir / "keyboard-request.json").read_text()))
self.assertIsNone(self.state())
def test_dismissals_are_written_for_the_app(self):
watcher = agent.KeyboardWatcher(self.dir)
watcher.event({"method": "Runtime.bindingCalled", "params": {"name": agent.KEYBOARD_BINDING, "payload": "dismiss"}})
watcher.event({"method": "Runtime.bindingCalled", "params": {"name": agent.KEYBOARD_BINDING, "payload": "dismiss"}})
self.assertEqual({"seq": 2}, json.loads((self.dir / "keyboard-dismiss.json").read_text()))
self.assertFalse((self.dir / "keyboard-request.json").exists())
@unittest.skipUnless(shutil.which("node"), "node is not installed")
def test_page_script_reports_when_the_routed_field_loses_focus(self):
harness = r"""
const assert = require('assert');
globalThis.window = globalThis;
const fs = require('fs');
const script = fs.readFileSync(process.argv[2], 'utf8');
const route = fs.readFileSync(process.argv[3], 'utf8');
const calls = [];
window.droiddeckSteamKeyboard = value => calls.push(value);
const open = { m_currentValue: false };
const vkm = {
m_currentVirtualKeyboardRef: null,
IsShowingVirtualKeyboard: { m_currentValue: false },
m_bIsInlineVirtualKeyboardOpen: open,
m_bIsVirtualKeyboardModal: { m_currentValue: false },
BUseVRKeyboard: () => false,
m_Instance: { IsDesktopOverlayWindow: () => false },
SetActiveVirtualKeyboardTarget(ref, props) { this.m_currentVirtualKeyboardRef = ref; },
SetVirtualKeyboardHidden() { open.m_currentValue = false; this.m_currentVirtualKeyboardRef = null; },
ShowVirtualKeyboard(ref, props, modal) { this.m_currentVirtualKeyboardRef = ref; open.m_currentValue = true; },
};
globalThis.SteamUIStore = { WindowStore: { SteamUIWindows: [{ VirtualKeyboardManager: vkm }] } };
eval(script);
vkm.ShowVirtualKeyboard('a', {}, false);
eval(route.replace('__ROUTE__', 'true'));
assert.strictEqual(open.m_currentValue, false);
assert.strictEqual(vkm.m_currentVirtualKeyboardRef, 'a');
const wait = ms => new Promise(done => setTimeout(done, ms));
(async () => {
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), []);
vkm.m_currentVirtualKeyboardRef = null;
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss']);
vkm.ShowVirtualKeyboard('b', {}, false);
assert.strictEqual(open.m_currentValue, false);
vkm.m_currentVirtualKeyboardRef = null;
await wait(160);
vkm.ShowVirtualKeyboard('c', {}, false);
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss']);
vkm.m_currentVirtualKeyboardRef = null;
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss', 'dismiss']);
let focused = false;
vkm.ShowVirtualKeyboard({ BIsElementValidForInput: () => focused }, {}, false);
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss', 'dismiss']);
focused = true;
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss', 'dismiss']);
focused = false;
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss', 'dismiss', 'dismiss']);
const doc = { activeElement: { tagName: 'INPUT', type: 'text' } };
vkm.m_ownerWindow = { document: doc };
vkm.ShowVirtualKeyboard({ BIsElementValidForInput: () => false }, {}, false);
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss', 'dismiss', 'dismiss']);
doc.activeElement = { tagName: 'DIV' };
await wait(500);
assert.deepStrictEqual(calls.filter(c => c === 'dismiss'), ['dismiss', 'dismiss', 'dismiss', 'dismiss']);
clearInterval(window.__droiddeckKeyboard.timer);
})().catch(e => { console.error(e); process.exit(1); });
"""
(self.dir / "keyboard.js").write_text(agent.KEYBOARD_JS)
(self.dir / "route.js").write_text(agent.KEYBOARD_ROUTE_JS)
(self.dir / "harness.js").write_text(harness)
result = subprocess.run(["node", str(self.dir / "harness.js"), str(self.dir / "keyboard.js"), str(self.dir / "route.js")],
capture_output=True, text=True, timeout=20)
self.assertEqual(0, result.returncode, result.stderr)
def test_route_is_applied_once_per_change(self):
watcher = agent.KeyboardWatcher(self.dir)
sent = []
with patch.object(watcher, "call", side_effect=lambda ws, method, params: sent.append(params["expression"]) or {}):
watcher.apply_route(None)
watcher.apply_route(None)
(self.dir / "keyboard-route").write_text("bottom")
watcher.apply_route(None)
watcher.apply_route(None, force=True)
self.assertEqual(3, len(sent))
self.assertTrue(sent[0].rstrip().endswith("(false)"))
self.assertTrue(sent[1].rstrip().endswith("(true)"))
self.assertNotIn("__ROUTE__", sent[2])
self.assertNotIn("__BINDING__", sent[2])
@unittest.skipUnless(shutil.which("node"), "node is not installed")
def test_page_script_routes_the_keyboard_to_the_app(self):
harness = r"""
const assert = require('assert');
globalThis.window = globalThis;
const fs = require('fs');
const script = fs.readFileSync(process.argv[2], 'utf8');
const route = fs.readFileSync(process.argv[3], 'utf8');
const calls = [];
window.droiddeckSteamKeyboard = value => calls.push(value);
const shown = [], targets = [];
const open = { m_currentValue: false };
const vkm = {
IsShowingVirtualKeyboard: { m_currentValue: false },
m_bIsInlineVirtualKeyboardOpen: open,
m_bIsVirtualKeyboardModal: { m_currentValue: false },
BUseVRKeyboard: () => false,
m_Instance: { IsDesktopOverlayWindow: () => false },
SetActiveVirtualKeyboardTarget(ref, props) { targets.push(ref); },
SetVirtualKeyboardHidden() { open.m_currentValue = false; },
ShowVirtualKeyboard(ref, props, modal) { shown.push(ref); open.m_currentValue = true; },
};
globalThis.SteamUIStore = { WindowStore: { SteamUIWindows: [{ VirtualKeyboardManager: vkm }] } };
eval(script);
vkm.ShowVirtualKeyboard('a', {}, false);
assert.deepStrictEqual(shown, ['a']);
assert.strictEqual(eval(route.replace('__ROUTE__', 'true')), true);
assert.strictEqual(open.m_currentValue, false);
assert.deepStrictEqual(calls.filter(c => c === 'request'), ['request']);
vkm.ShowVirtualKeyboard('b', {}, false);
assert.deepStrictEqual(shown, ['a']);
assert.deepStrictEqual(targets, ['b']);
assert.deepStrictEqual(calls.filter(c => c === 'request'), ['request', 'request']);
vkm.ShowVirtualKeyboard('c', {}, true);
assert.deepStrictEqual(shown, ['a', 'c']);
eval(script);
vkm.ShowVirtualKeyboard('d', {}, false);
assert.deepStrictEqual(targets, ['b', 'd']);
eval(route.replace('__ROUTE__', 'false'));
vkm.ShowVirtualKeyboard('e', {}, false);
assert.deepStrictEqual(shown, ['a', 'c', 'e']);
clearInterval(window.__droiddeckKeyboard.timer);
"""
(self.dir / "keyboard.js").write_text(agent.KEYBOARD_JS)
(self.dir / "route.js").write_text(agent.KEYBOARD_ROUTE_JS)
(self.dir / "harness.js").write_text(harness)
result = subprocess.run(["node", str(self.dir / "harness.js"), str(self.dir / "keyboard.js"), str(self.dir / "route.js")],
capture_output=True, text=True, timeout=20)
self.assertEqual(0, result.returncode, result.stderr)
def test_no_shared_context_yet(self):
with patch.object(agent, "cdp_targets", return_value=[{"title": "Steam", "webSocketDebuggerUrl": "ws://127.0.0.1:1/x"}]):
with self.assertRaises(agent.AgentError) as caught:
@@ -641,7 +855,6 @@ class VrFocusTest(unittest.TestCase):
self.assertEqual(focus.order([42, 9, 7], [7, 42], None), [9, 7])
self.assertIsNone(focus.order([9, 7], [7], 42))
self.assertEqual(focus.order([9, 7], [7, 42], 42), [42, 9, 7])
# Do not overwrite a different focus order installed after our last write.
self.assertIsNone(focus.order([123, 7], [7, 42], None))
def test_scene_requires_live_server_and_game_environment(self):
+23
View File
@@ -326,6 +326,15 @@ class SignInHandoffTest(EpicTestCase):
self.assertEqual((None, None, "unix:abstract=a11y"),
(carried.get("NO_AT_BRIDGE"), carried.get("GTK_A11Y"), carried.get("AT_SPI_BUS_ADDRESS")))
def test_only_the_hidden_launcher_skips_the_store_and_fab_pages(self):
with mock.patch.dict(G, {"fex_environment": lambda fex, files, base: {}}):
visible = G["client_environment"]({}, None, None, ":0", "bus", "broker")
hidden = G["client_environment"]({}, None, None, ":2", "bus", "broker", hidden=True)
self.assertNotIn("DROIDDECK_EPIC_HIDDEN", visible)
self.assertNotIn("EGL_DISABLE_STORE_PREWARM", visible)
self.assertEqual(("1", "1"), (hidden.get("DROIDDECK_EPIC_HIDDEN"), hidden.get("EGL_DISABLE_STORE_PREWARM")))
self.assertEqual(visible["WEBKIT_SKIA_ENABLE_CPU_RENDERING"], hidden["WEBKIT_SKIA_ENABLE_CPU_RENDERING"])
def session(self):
session = G["Session"]("visible")
session.carried = True
@@ -906,6 +915,20 @@ class BrokerTest(EpicTestCase):
self.assertNotIn(G["STEAM_PRELOAD"], envs[0])
self.assertNotIn("LD_PRELOAD", envs[1])
def test_a_game_started_by_the_hidden_launcher_does_not_inherit_its_page_switches(self):
folder = self.tmp / "Fig"
folder.mkdir()
envs = []
def popen(command, env=None, cwd=None, start_new_session=False):
envs.append(env)
return mock.Mock(wait=mock.Mock(return_value=0))
with mock.patch.object(subprocess, "Popen", popen):
G["run_game"]({"exe": str(folder / "Fig.exe"), "args": []}, dict(G["HIDDEN_ENV"], PATH="/usr/bin"))
self.assertFalse(set(G["HIDDEN_ENV"]) & set(envs[0]))
self.assertEqual("/usr/bin", envs[0]["PATH"])
def test_an_owner_gone_ends_a_game_the_open_launcher_handed_to_steam(self):
broker, _ = self.broker("visible")
session = G["Session"]("visible")
+100 -2
View File
@@ -48,6 +48,8 @@ class FexTestCase(unittest.TestCase):
"HOST_PRELOAD": str(self.tmp / "ld.so.preload"),
"DEVICE_FONTS": str(self.tmp / "android-fonts"),
"FONT_RULES": str(self.tmp / "66-droiddeck-device-fonts.conf"),
"OWN_FEX": str(self.tmp / "own"),
"OWN_FEX_TAG": str(self.tmp / "own/share/fex-emu/.droiddeck-tag"),
}
patcher = mock.patch.dict(G, patches)
patcher.start()
@@ -202,6 +204,75 @@ class LocateTest(FexTestCase):
self.assertEqual((fex["bin"], fex["server"], fex["portable"]), (str(tool / "usr/bin/FEX"), str(tool / "usr/bin/FEXServer"), True))
self.assertTrue(fex["host_thunks"].endswith("HostThunks") and fex["guest_thunks"].endswith("GuestThunks") and fex["thunks_db"])
def own_fex(self, tag, loads=True):
own = self.tmp / "own"
self.write(own / "bin/FEX", mode=0o755)
self.write(own / "bin/FEXServer", mode=0o755)
self.write(own / "share/fex-emu/.droiddeck-tag", tag + "\n")
listing = b"libc.so.6 => /usr/lib/libc.so.6\n" if loads else b"libxxhash.so.0 => not found\n"
patcher = mock.patch.object(G["subprocess"], "run", lambda args, **kw: subprocess.CompletedProcess(args, 0, listing))
patcher.start()
self.addCleanup(patcher.stop)
return own
def test_an_own_build_that_no_longer_loads_is_installed_again_and_skipped(self):
tool = self.fex_tool(self.steam)
self.own_fex("fex-2610-r7", loads=False)
calls = []
with mock.patch.dict(os.environ, {"BL_FEX_TAG": "fex-2610-r7"}), \
mock.patch.object(G["subprocess"], "call", lambda args, **kw: calls.append(args) or 1):
FEX["update_own_fex"]()
self.assertEqual([[str(BIN / "droiddeck-fex-update")]], calls)
self.assertEqual(FEX["fex_install"]()["bin"], str(tool / "usr/bin/FEX"))
def test_droiddeck_s_own_build_comes_before_the_steam_tool(self):
self.fex_tool(self.steam)
own = self.own_fex("fex-2610-r7")
fex = FEX["fex_install"]()
self.assertEqual((fex["bin"], fex["server"], fex["portable"]), (str(own / "bin/FEX"), str(own / "bin/FEXServer"), False))
def test_the_build_the_session_names_is_installed_once(self):
self.own_fex("fex-2610-r6")
calls = []
with mock.patch.dict(os.environ, {"BL_FEX_TAG": "fex-2610-r7"}), \
mock.patch.object(G["subprocess"], "call", lambda args, **kw: calls.append(args) or 0):
FEX["update_own_fex"]()
self.write(self.tmp / "own/share/fex-emu/.droiddeck-tag", "fex-2610-r7\n")
FEX["update_own_fex"]()
self.assertEqual([[str(BIN / "droiddeck-fex-update")]], calls)
def test_a_failed_build_waits_an_hour_unless_the_session_names_another(self):
self.own_fex("fex-2610-r6")
Path(G["STATE"]).mkdir(parents=True, exist_ok=True)
calls = []
tag = {"BL_FEX_TAG": "fex-2610-r7"}
with mock.patch.dict(os.environ, tag), \
mock.patch.object(G["subprocess"], "call", lambda args, **kw: calls.append(os.environ["BL_FEX_TAG"]) or 1):
FEX["update_own_fex"]()
FEX["update_own_fex"]()
failed = Path(G["STATE"]) / "update-failed"
os.utime(failed, (failed.stat().st_atime, failed.stat().st_mtime - 3601))
FEX["update_own_fex"]()
os.environ["BL_FEX_TAG"] = "fex-2610-r8"
FEX["update_own_fex"]()
self.assertEqual(["fex-2610-r7", "fex-2610-r7", "fex-2610-r8"], calls)
def test_a_missing_updater_falls_back_to_the_fex_already_here(self):
self.fex_tool(self.steam)
Path(G["STATE"]).mkdir(parents=True, exist_ok=True)
with mock.patch.dict(os.environ, {"BL_FEX_TAG": "fex-2610-r7"}), \
mock.patch.object(G["subprocess"], "call", mock.Mock(side_effect=FileNotFoundError)):
FEX["update_own_fex"]()
self.assertEqual(FEX["fex_install"]()["bin"], str(self.steam / "steamapps/common/FEX-Emu/usr/bin/FEX"))
def test_no_build_is_installed_without_one_from_the_session(self):
calls = []
with mock.patch.dict(os.environ, {}, clear=False), \
mock.patch.object(G["subprocess"], "call", lambda args, **kw: calls.append(args) or 0):
os.environ.pop("BL_FEX_TAG", None)
FEX["update_own_fex"]()
self.assertEqual([], calls)
def test_library_folders_are_read(self):
other = self.tmp / "other"
(other / "steamapps/common").mkdir(parents=True)
@@ -549,11 +620,12 @@ class LaunchTest(FexTestCase):
def test_the_guest_environment(self):
arm = self.write(self.tmp / "arm.so", elf(183))
x86 = self.write(self.tmp / "x86.so", elf(62))
env = {"LD_PRELOAD": "%s:%s %s" % (arm, x86, self.tmp / "gone.so"),
env = {"LD_PRELOAD": "%s:%s %s libMelonLoader.Bootstrap.so" % (arm, x86, self.tmp / "gone.so"),
"LD_LIBRARY_PATH": "/steam/steamrtarm64:/usr/lib/aarch64-linux-gnu:/game/lib:/steam/linuxarm64",
"FEX_ROOTFS": "/usr/share/guestos/fex-mesa", "FEX_TSOENABLED": "1"}
guest = FEX["guest_environment"]({"portable": True}, env)
self.assertEqual((guest["LD_PRELOAD"], guest["LD_LIBRARY_PATH"], guest["FEX_PORTABLE"]), (str(x86), "/game/lib", "1"))
self.assertEqual((guest["LD_PRELOAD"], guest["LD_LIBRARY_PATH"], guest["FEX_PORTABLE"]),
("%s:libMelonLoader.Bootstrap.so" % x86, "/game/lib", "1"))
self.assertNotIn("FEX_ROOTFS", guest)
self.assertEqual(guest["FEX_TSOENABLED"], "1")
self.assertTrue(guest["FEX_APP_CONFIG_LOCATION"].endswith("/config/"))
@@ -690,6 +762,32 @@ class LaunchTest(FexTestCase):
command, env, mode = G["launch"].call_args[0]
self.assertEqual((command, mode), (["/game/run.sh", "-a"], "auto"))
def test_steam_games_get_their_settings_and_linux_unity_keeps_tso(self):
config = self.home / ".config/droiddeck/game-environment.json"
unity = self.write(self.tmp / "Megabonk/Megabonk.x86_64", elf(62), 0o755)
self.write(self.tmp / "Megabonk/UnityPlayer.so", elf(62))
self.write(self.tmp / "Megabonk/GameAssembly.so", elf(62))
other = self.write(self.tmp / "Other/game.x86_64", elf(62), 0o755)
self.write(config, json.dumps({"version": 1, "shared": {"FEX_TSOENABLED": "0", "FEX_MULTIBLOCK": "1"},
"games": {"20": {"FEX_TSOENABLED": "0"}}}))
def started(program, app):
with mock.patch.dict(G, {"launch": mock.Mock(return_value=0), "GAME_ENVIRONMENT": str(config)}), \
mock.patch.dict(os.environ, {"SteamAppId": app}, clear=True):
_, said = self.quiet(FEX["main"], ["steam", "waitforexitandrun", str(program), "-arg"])
command, env, _ = G["launch"].call_args[0]
self.assertEqual(command, [str(program), "-arg"])
return env.get("FEX_TSOENABLED"), env.get("FEX_MULTIBLOCK"), said
tso, multiblock, said = started(unity, "10")
self.assertEqual((tso, multiblock), ("1", "1"))
self.assertIn("Unity: FEX with TSO", said)
self.assertEqual(started(other, "10")[:2], ("0", "1"))
self.assertEqual(started(unity, "20")[:2], ("0", "1"))
config.unlink()
self.assertEqual(started(unity, "10")[:2], ("1", None))
self.assertEqual(started(other, "10")[:2], (None, None))
class AppInfoTest(unittest.TestCase):
COMPAT = runpy.run_path(str(BIN / "steam-compatibility"))
@@ -0,0 +1,95 @@
import json
import runpy
import tempfile
import unittest
from pathlib import Path
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
COMPAT = runpy.run_path(str(BIN / "steam-compatibility"))
TOOL = COMPAT["TOOL"]
OLD = {"name": "DroidDeck-Proton-11-11.2", "display": "DroidDeck-Proton 11-11.2", "dir": "DroidDeck-Proton 11-11.2", "valve": False}
NEW = {"name": "DroidDeck-Proton-11-11.4", "display": "DroidDeck-Proton 11-11.4", "dir": "DroidDeck-Proton 11-11.4", "valve": False}
VALVE = {"name": TOOL, "display": "Proton Experimental ARM64", "dir": "Proton - Experimental (ARM64)", "valve": True}
GE = {"name": "GE-Proton11-7", "display": "GE-Proton11-7", "dir": "GE-Proton11-7", "valve": False}
def config_text(entries):
mapping = "".join('"%s" { "name" "%s" "config" "" "priority" "%s" }' % (app, name, "75" if app == "0" else "250") for app, name in entries)
return '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }' % mapping
class DroidDeckProtonDefaultTest(unittest.TestCase):
def run_default(self, state, mapping, catalog, apps=(), request=None, settle=True):
with tempfile.TemporaryDirectory() as tmp:
directory = Path(tmp)
(directory / "state.json").write_text(json.dumps(state))
if request is not None:
(directory / "request.json").write_text(json.dumps(request))
config = directory / "config.vdf"
config.write_text(config_text(mapping))
default = COMPAT["register_default"](str(config), list(apps), catalog=catalog, directory=str(directory), settle=settle)
return default, COMPAT["read_config_mapping"](str(config)), json.loads((directory / "state.json").read_text())
def test_a_replacement_seen_while_steam_runs_still_moves_the_default_later(self):
_, mapping, state = self.run_default(
{"default": OLD["name"], "own_proton": True, "tools": [VALVE, OLD]}, [("0", OLD["name"])], [VALVE, NEW], settle=False)
self.assertEqual(state["tools"], [VALVE, NEW])
self.assertIn(OLD["name"], state["own_builds"])
default, mapping, state = self.run_default(state, [("0", OLD["name"])], [VALVE, NEW])
self.assertEqual(default, NEW["name"])
self.assertEqual(mapping, {"0": NEW["name"]})
def test_a_valve_default_picked_in_steam_after_a_replacement_is_kept(self):
default, mapping, _ = self.run_default(
{"default": OLD["name"], "own_proton": True, "tools": [VALVE, OLD]}, [("0", TOOL)], [VALVE, NEW])
self.assertEqual(default, TOOL)
self.assertEqual(mapping, {"0": TOOL})
def test_the_first_start_with_droiddeck_proton_makes_it_the_default_for_following_titles(self):
default, mapping, state = self.run_default(
{"default": TOOL, "auto": {"42": TOOL}}, [("0", TOOL), ("42", TOOL), ("43", TOOL)], [VALVE, NEW], ["42", "43"])
self.assertEqual(default, NEW["name"])
self.assertEqual(mapping, {"0": NEW["name"], "42": NEW["name"], "43": TOOL})
self.assertEqual(state["default"], NEW["name"])
self.assertTrue(state["own_proton"])
def test_a_default_picked_after_the_switch_is_kept(self):
default, mapping, state = self.run_default(
{"default": NEW["name"], "own_proton": True, "applied": 1, "tools": [VALVE, NEW]},
[("0", NEW["name"])], [VALVE, NEW], request={"seq": 2, "valve": True, "dir": VALVE["dir"]})
self.assertEqual(default, TOOL)
self.assertEqual(mapping, {"0": TOOL})
default, mapping, state = self.run_default(
{"default": TOOL, "own_proton": True, "tools": [VALVE, NEW]}, [("0", TOOL)], [VALVE, NEW])
self.assertEqual(default, TOOL)
self.assertEqual(mapping, {"0": TOOL})
def test_a_replaced_build_moves_its_default_and_titles_to_the_newest(self):
default, mapping, state = self.run_default(
{"default": OLD["name"], "own_proton": True, "auto": {"42": OLD["name"]}, "tools": [VALVE, OLD, GE]},
[("0", OLD["name"]), ("42", OLD["name"]), ("43", OLD["name"]), ("44", GE["name"]), ("45", TOOL)],
[VALVE, NEW, GE], ["42", "43", "44", "45"])
self.assertEqual(default, NEW["name"])
self.assertEqual(mapping, {"0": NEW["name"], "42": NEW["name"], "43": NEW["name"], "44": GE["name"], "45": TOOL})
self.assertEqual(state["auto"], {"42": NEW["name"]})
def test_a_replaced_build_does_not_override_a_valve_default(self):
default, mapping, _ = self.run_default(
{"default": TOOL, "own_proton": True, "tools": [VALVE, OLD]}, [("0", TOOL), ("43", OLD["name"])], [VALVE, NEW], ["43"])
self.assertEqual(default, TOOL)
self.assertEqual(mapping, {"0": TOOL, "43": NEW["name"]})
def test_nothing_changes_without_droiddeck_proton(self):
default, mapping, state = self.run_default({"default": TOOL}, [("0", TOOL)], [VALVE, GE])
self.assertEqual(default, TOOL)
self.assertEqual(mapping, {"0": TOOL})
self.assertNotIn("own_proton", state)
def test_the_newest_build_is_found_by_version(self):
newest, gone = COMPAT["own_proton_moves"]({"tools": [OLD]}, [dict(NEW, name="a", dir="DroidDeck-Proton 11-9.10"), dict(NEW, name="b", dir="DroidDeck-Proton 11-11.1")])
self.assertEqual(newest, "b")
self.assertEqual(gone, {OLD["name"]})
if __name__ == "__main__":
unittest.main()
+16 -5
View File
@@ -1,4 +1,3 @@
"""droiddeckctl and droiddeck-scenario against a fake adb: the JSON contract and exit codes."""
import base64
import contextlib
import importlib.machinery
@@ -88,8 +87,6 @@ def timestats_record(name, frames, histogram, hz):
f"present2present histogram is as below:\n{bins}\nlatch2present histogram is as below:\n8ms={frames}\n")
# A capture where the panel went from 60 to 120 Hz: the game layer is split in two records. The
# activity's own window and an unrelated app draw too, and must not count.
TIMESTATS_SPLIT = "SurfaceFlinger TimeStats:\ntotalFrames = 2000\n" + "".join([
timestats_record("droiddeck_wayland_game#6110", 900, {16: 700, 33: 199}, 60),
timestats_record("droiddeck_wayland_game#6110", 900, {16: 850, 25: 49}, 120),
@@ -99,7 +96,6 @@ TIMESTATS_SPLIT = "SurfaceFlinger TimeStats:\ntotalFrames = 2000\n" + "".join([
timestats_record("StatusBar#106", 1, {}, 60),
])
# A build that presents the game on the session's SurfaceView instead of a layer of its own.
TIMESTATS_SURFACEVIEW = "SurfaceFlinger TimeStats:\n" + "".join([
timestats_record("a1b2c3 SurfaceView[com.droiddeck.launcher/com.droiddeck.launcher.SessionActivity]#77",
1800, {16: 1796, 33: 3}, 120),
@@ -155,7 +151,7 @@ class DroiddeckctlTest(FakeAdbTest):
with unittest.mock.patch.dict(os.environ, {"DROIDDECK_PACKAGE": "com.antutu.benchmark.full"}):
variant = load("droiddeckctl")
self.assertEqual("content://com.antutu.benchmark.full.agent", variant.AUTHORITY)
with self.assertRaises(variant.ControlError): # the fake adb does not answer `am start`
with self.assertRaises(variant.ControlError):
variant.start_agent_activity(str(self.adb), "fake-1", {"mode": "steam"})
started = [e["args"] for e in self.calls() if e["args"][:3] == ["shell", "am", "start"]]
self.assertIn("com.antutu.benchmark.full/com.droiddeck.launcher.agent.AgentStartActivity", started[0])
@@ -413,6 +409,21 @@ class DroiddeckctlTest(FakeAdbTest):
self.assertEqual("Awake", out["wakefulness"])
self.assertIn(["shell", "input", "keyevent", "KEYCODE_WAKEUP"], [c["args"] for c in self.calls()])
def test_proot_profile_queues_only_the_next_session(self):
self.configure({"env": {"ok": True}})
code, out = self.run_ctl("proot-profile", "enable", "--slow-us", "250")
self.assertEqual(0, code, out)
self.assertEqual({"op": "set", "lines": ["PROOT_PROFILE=1", "PROOT_PROFILE_SLOW_US=250"], "persistent": False},
self.calls("env")[0]["request"])
def test_proot_profile_report_needs_no_adb_or_device(self):
path = self.dir / "session.log"
path.write_text('ordinary log')
with unittest.mock.patch.object(ctl, "adb_path", side_effect=AssertionError("must stay offline")):
code, out = self.run_ctl("proot-profile", "report", str(path))
self.assertEqual(0, code, out)
self.assertFalse(out["hasCounters"])
def test_override_pushes_to_the_inbox_first(self):
self.configure({
"access": {"ok": True, "commands": True, "inbox": "/sdcard/Android/data/x/files/agent-inbox"},
+12 -6
View File
@@ -1085,7 +1085,9 @@ class LauncherTest(SyncTestCase):
self.assertEqual([call[:2] for call in calls], [(str(dist / "proton"), [str(dist / "proton")] + command[1:]),
(str(tool / "proton"), command)])
self.assertEqual((calls[0][2]["WINEESYNC"], calls[0][2]["PROTON_NO_ESYNC"], calls[0][2]["BL_SYNC_PACK"]), ("0", "1", pack["id"]))
self.assertEqual(calls[1][2], self.env(STEAM_COMPAT_APP_ID="2630", PROTON_NO_NTSYNC="1", BL_SYNC="0", WINEESYNC="1"))
self.assertEqual(calls[1][2], self.env(STEAM_COMPAT_APP_ID="2630", PROTON_NO_NTSYNC="1", BL_SYNC="0", WINEESYNC="1",
vblank_mode="0", GAMESCOPE_WSI_MIN_IMAGE_COUNT="8",
GAMESCOPE_WSI_ENSURE_MIN_IMAGE_COUNT="1"))
self.assertIn("could not start (", said.getvalue())
def test_generated_launchers_run_the_dist_proton(self):
@@ -1160,16 +1162,20 @@ class BundleTest(unittest.TestCase):
except ImportError:
self.skipTest("PyYAML is not installed")
apk = yaml.safe_load((self.ROOT / ".github/workflows/build.yml").read_text())
steps = {step.get("name"): step for step in apk["jobs"]["build"]["steps"]}
bundle = steps["Bundle the droiddeck-esync packs"]
steps = {step.get("name"): step for step in apk["jobs"]["components"]["steps"]}
import json
recipes = json.loads((self.ROOT / "tools/components/recipes.json").read_text())
bundle = dict(steps["Bundle the droiddeck-esync packs"])
self.assertIn("prepare.py esync", bundle["run"])
bundle["run"] = (self.ROOT / recipes["esync"]["script"]).read_text()
self.assertNotIn("if", bundle)
self.assertIn("tools/droiddeck-esync/release.env", bundle["run"])
self.assertIn('release_url="https://github.com/${SYNC_BUNDLE_REPO}/releases/download/${SYNC_BUNDLE_TAG}"', bundle["run"])
self.assertIn('curl -fsSL --retry 3 -o sync-bundle.tzst "$release_url/$SYNC_BUNDLE_ASSET"', bundle["run"])
self.assertIn('curl -fsSL --retry 3 -o sync-index.json "$release_url/index.json"', bundle["run"])
self.assertIn('curl -fsSL --retry 5 --retry-all-errors --connect-timeout 20 -o sync-bundle.tzst "$release_url/$SYNC_BUNDLE_ASSET"', bundle["run"])
self.assertIn('curl -fsSL --retry 5 --retry-all-errors --connect-timeout 20 -o sync-index.json "$release_url/index.json"', bundle["run"])
self.assertIn("sha256sum -c -", bundle["run"])
self.assertNotIn("revoked.txt", bundle["run"])
stage = next(step for step in apk["jobs"]["build"]["steps"] if "overlay/usr/local/bin/droiddeck-*" in step.get("run", ""))
stage = {"run": (self.ROOT / recipes["runtime"]["script"]).read_text()}
self.assertIn("tools/linuxfs/overlay/usr/local/bin/droiddeck-* ", stage["run"])
local = (self.ROOT / "tools/build_local.sh").read_text()
self.assertIn("tools/linuxfs/overlay/usr/local/bin/droiddeck-* ", local)
+290
View File
@@ -0,0 +1,290 @@
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
HEADER = Path(__file__).resolve().parents[1] / "proot/fastpath/svc-patch.h"
class FastPathSvcEncodingTest(unittest.TestCase):
def compile_check(self, source, header):
if shutil.which("cc") is None:
self.skipTest("cc is not available")
with tempfile.TemporaryDirectory() as directory:
program = Path(directory) / "check.c"
program.write_text(source.replace('"HEADER"', '"' + (HEADER.parent / header).as_posix() + '"'))
output = Path(directory) / "check"
subprocess.run(["cc", "-O2", "-Wall", "-Wextra", "-Werror", str(program), "-o", str(output)], check=True)
subprocess.run([str(output)], check=True, timeout=10)
def test_benchmark_checks_execute_with_ndebug(self):
self.compile_check(r'''
#define NDEBUG
#include "HEADER"
int main(void) {
int calls = 0;
assert(++calls == 1);
return calls != 1;
}
''', "../bench/checks.h")
def test_stack_bounds_and_alternate_stack_fallback(self):
self.compile_check(r'''
#define _GNU_SOURCE
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/resource.h>
static rlim_t stack_limit = 1024 * 1024;
static long sc(long n, long a, long b, long c, long d, long e) {
if (n == SYS_prlimit64) {
if (!stack_limit) return -1;
((struct rlimit *)d)->rlim_cur = stack_limit;
return 0;
}
return syscall(n,a,b,c,d,e);
}
#include "HEADER"
int main(void) {
uintptr_t low, high;
assert(svc_stack_line("1000-9000 rw-p 00000000 00:00 0", 0x8000, &low, &high));
assert(low == 0x1000 && high == 0x9000);
assert(!svc_stack_line("1000-9000 r--p", 0x8000, &low, &high));
assert(!svc_stack_line("1000-9000 rw-p", 0x9000, &low, &high));
assert(!svc_stack_line("9000-1000 rw-p", 0x8000, &low, &high));
assert(!svc_stack_line("fffffffffffffffff-9000 rw-p", 0x8000, &low, &high));
assert(!svc_stack_line("", 0x8000, &low, &high));
char local;
assert(svc_stack_find((uintptr_t)&local));
assert((uintptr_t)&local >= svc_stack_low && (uintptr_t)&local < svc_stack_high);
svc_stack_main = 0; svc_stack_low = 0x1000; svc_stack_high = 0x100000;
assert(!svc_stack_room(0x1000 + 96 * 1024 - 1));
assert(svc_stack_room(0x1000 + 96 * 1024));
svc_stack_main = 1; svc_stack_low = 0xf0000; svc_stack_gap = 0x1000;
assert(svc_stack_room(0xff000));
stack_limit = 64 * 1024;
assert(!svc_stack_room(0xff000));
stack_limit = 0;
assert(!svc_stack_room(0xff000));
return 0;
}
''', "svc-stack.h")
def test_patch_publication_failure_and_partial_success(self):
self.compile_check(r'''
#define _GNU_SOURCE
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
static struct svc_site { uint32_t *site; uint32_t branch; } svc_plan[3];
static size_t svc_planned = 3;
static unsigned svc_sites, svc_pages;
static int fail_alloc, fail_emit, fail_rx, fail_write, writes, live, rx;
static uint32_t *svc_relay_page(uintptr_t site, size_t page) {
(void)site;
if (fail_alloc) return NULL;
live++; rx = 0;
return calloc(1, page);
}
static int svc_emit(uint32_t *code, uint32_t *site, uint32_t *branch) {
(void)code; (void)site;
*branch = 0x14000001;
return !fail_emit;
}
static int mock_rx(void *p, size_t size, int prot) {
(void)p; (void)size;
assert(prot == (PROT_READ | PROT_EXEC));
if (fail_rx) return -1;
rx = 1;
return 0;
}
static int mock_unmap(void *p, size_t size) { (void)size; free(p); live--; return 0; }
static long sc(long nr, long fd, long buf, long len, long at, long unused) {
(void)fd; (void)unused;
assert(nr == SYS_pwrite64 && len == 4 && rx);
if (++writes == fail_write) return -1;
memcpy((void *)at, (void *)buf, len);
return len;
}
#define mprotect mock_rx
#define munmap mock_unmap
#include "HEADER"
int main(void) {
static uint32_t sites[3];
for (int mode = 0; mode < 5; mode++) {
for (int i=0; i<3; i++) { sites[i]=0xd4000001; svc_plan[i].site=&sites[i]; }
svc_sites=svc_pages=writes=live=rx=0;
fail_alloc=mode==0; fail_emit=mode==1; fail_rx=mode==2; fail_write=mode==3 ? 2 : 0;
svc_publish(7,4096);
if (mode<3) assert(!svc_sites && !svc_pages && !writes && !live);
else assert(svc_sites==(mode==3 ? 2U : 3U) && svc_pages==1 && live==1);
for (int i=0; i<3; i++) assert(sites[i]==(mode<3 || (mode==3 && i==1) ? 0xd4000001 : 0x14000001));
}
return 0;
}
''', "svc-publish.h")
def test_instruction_decoding_and_branch_limits(self):
if shutil.which("cc") is None:
self.skipTest("cc is not available")
source = r'''
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#define FP_SVC_ENCODINGS_ONLY
#include "HEADER"
int main(void) {
assert(svc_number(0xd2800708) == 56);
assert(svc_number(0x528009e8) == 79);
assert(svc_number(0xd2800709) == -1);
assert(svc_number(0xd2a00708) == -1);
assert(svc_number(0xf2800708) == -1);
assert(svc_number(0xaa0003e8) == -1);
const uint32_t create[] = {0xd2800708, 0x29047fe0, 0x92800c60, 0xd4000001};
const uint32_t create_at[] = {0xd2800708, 0x29047fe3, 0xb9404be3, 0xd4000001};
const uint32_t xstat[] = {0xd28009e8, 0xaa0203e1, 0xaa0303e2, 0x93407c83, 0xd4000001};
const uint32_t overwritten[] = {0xd2800708, 0xaa0003e8, 0xd4000001};
const uint32_t loaded[] = {0xd2800708, 0xf94003e8, 0xd4000001};
const uint32_t branch[] = {0xd2800708, 0x14000001, 0xd4000001};
const uint32_t call[] = {0xd2800708, 0x94000001, 0xd4000001};
const uint32_t shift[] = {0xd2800708, 0xd2a00708, 0xd4000001};
const uint32_t robust[] = {0xd2800c68, 0xf9000260, 0xa9000261, 0xf9400260, 0x9100e260, 0xd4000001};
const uint32_t writeback[] = {0xd2800708, 0xa9bf07e0, 0xd4000001};
const uint32_t added[] = {0xd2800708, 0x91000408, 0xd4000001};
assert(svc_site_number(create, 3) == 56);
assert(svc_site_number(create_at, 3) == 56);
assert(svc_site_number(xstat, 4) == 79);
assert(svc_site_number(overwritten, 2) == -1);
assert(svc_site_number(loaded, 2) == -1);
assert(svc_site_number(branch, 2) == -1);
assert(svc_site_number(call, 2) == -1);
assert(svc_site_number(shift, 2) == -1);
assert(svc_site_number(robust, 5) == 99);
assert(svc_site_number(writeback, 2) == -1);
assert(svc_site_number(added, 2) == -1);
uint32_t out;
uintptr_t pc = 0x10000000;
assert(svc_branch(pc, pc + 4, &out) && out == 0x14000001);
assert(svc_branch(pc, pc - 4, &out) && out == 0x17ffffff);
assert(svc_branch(pc, pc + 0x7fffffc, &out) && out == 0x15ffffff);
assert(svc_branch(pc, pc - 0x8000000, &out) && out == 0x16000000);
assert(!svc_branch(pc, pc + 0x8000000, &out));
assert(!svc_branch(pc, pc - 0x8000004, &out));
assert(!svc_branch(pc, pc + 2, &out));
return 0;
}
'''
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
program = work / "check.c"
program.write_text(source.replace("HEADER", HEADER.as_posix()))
output = work / "check"
subprocess.run(["cc", "-O2", "-Wall", "-Wextra", "-Werror", str(program), "-o", str(output)], check=True)
subprocess.run([str(output)], check=True, timeout=10)
def test_cache_revalidates_image_offsets_and_untrusted_files(self):
if shutil.which("cc") is None:
self.skipTest("cc is not available")
source = r'''
#define _GNU_SOURCE
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <link.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#define FP_SVC_ENCODINGS_ONLY
#include "HEADER"
static int svc_android_api = 33;
static struct { uint32_t *site; } svc_plan[256];
static size_t svc_planned;
static int svc_supported(int nr) { return nr == 56 || nr == 79; }
#define sc(n,a,b,c,d,e) syscall(n,a,b,c,d,e)
static long fp_open(int d, const char *p, int f, mode_t m) { return openat(d,p,f,m); }
static long fp_rename(int d, const char *p, int e, const char *q, int f) { (void)f; return renameat(d,p,e,q); }
static long fp_unlink(int d, const char *p, int f) { return unlinkat(d,p,f); }
#include "CACHE"
static void store(const char *path, struct svc_cached_sites *table) {
int fd = open(path, O_WRONLY | O_TRUNC);
assert(fd >= 0 && write(fd, table, sizeof *table) == sizeof *table && close(fd) == 0);
}
int main(void) {
uint32_t image[32] = {0xd2800708, 0xd4000001, 0xd28009e8, 0xd4000001};
ElfW(Phdr) ph[] = {{.p_type=PT_LOAD, .p_flags=PF_R|PF_X, .p_memsz=sizeof image},
{.p_type=PT_NOTE, .p_vaddr=64, .p_memsz=24}};
struct dl_phdr_info info = {.dlpi_addr=(uintptr_t)image, .dlpi_phdr=ph, .dlpi_phnum=2};
ElfW(Nhdr) note = {.n_type=NT_GNU_BUILD_ID, .n_namesz=4, .n_descsz=8};
memcpy((char *)image+64, &note, sizeof note);
memcpy((char *)image+76, "GNU", 4);
uint64_t id = getpid();
memcpy((char *)image+80, &id, 8);
char path[256], other[256];
assert(svc_cache_name(&info, path, sizeof path));
svc_android_api = 35;
assert(svc_cache_name(&info, other, sizeof other) && strcmp(path, other));
svc_android_api = 33;
setenv("PROOT_FP_SVC_CACHE", "0", 1);
assert(!svc_cache_name(&info, other, sizeof other));
unsetenv("PROOT_FP_SVC_CACHE");
ph[1].p_memsz = sizeof image;
assert(!svc_cache_name(&info, other, sizeof other));
ph[1].p_memsz = 24;
svc_plan[0].site = image+1; svc_plan[1].site = image+3; svc_planned=2;
svc_cache_write(&info, path, 0);
svc_planned=0;
assert(svc_cache_read(&info, path) && svc_planned==2 && svc_plan[1].site==image+3);
struct svc_cached_sites valid, table;
int fd = open(path, O_RDONLY);
assert(fd >= 0 && read(fd, &valid, sizeof valid)==sizeof valid && close(fd)==0);
#define REJECT(change) do { table=valid; change; store(path,&table); svc_planned=0; \
assert(!svc_cache_read(&info,path) && svc_planned==0); } while(0)
REJECT(table.magic++);
REJECT(table.api=35);
REJECT(table.count=0);
REJECT(table.count=257);
REJECT(table.offset[0]=3);
REJECT(table.offset[0]=sizeof image);
REJECT(table.offset[0]=UINT64_MAX-3);
REJECT(table.offset[1]=table.offset[0]);
REJECT(table.offset[0]=12; table.offset[1]=4);
store(path,&valid);
image[1]=0x14000001;
assert(!svc_cache_read(&info,path));
image[1]=0xd4000001; image[0]=0xd2800008;
assert(!svc_cache_read(&info,path));
image[0]=0xd2800708;
assert(chmod(path,0644)==0 && !svc_cache_read(&info,path));
assert(chmod(path,0600)==0 && truncate(path,8)==0 && !svc_cache_read(&info,path));
assert(unlink(path)==0 && symlink("/dev/zero",path)==0 && !svc_cache_read(&info,path));
assert(unlink(path)==0 && mkdir(path,0700)==0 && !svc_cache_read(&info,path));
assert(rmdir(path)==0);
svc_planned=0;
svc_cache_write(&info,path,0);
assert(access(path,F_OK)<0);
(void)svc_cache_hits; (void)svc_cache_misses;
return 0;
}
'''
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
program = work / "cache.c"
program.write_text(source.replace('"HEADER"', '"' + HEADER.as_posix() + '"')
.replace('"CACHE"', '"' + HEADER.with_name("svc-cache.h").as_posix() + '"'))
output = work / "cache"
subprocess.run(["cc", "-O2", "-Wall", "-Wextra", "-Werror", str(program), "-o", str(output)], check=True)
subprocess.run([str(output)], check=True, timeout=10)
+1 -1
View File
@@ -6,7 +6,7 @@ import tempfile
import textwrap
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-steam-x64"
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-fex-update"
SOURCE = SCRIPT.read_text()
START = SOURCE.index("fex_install() {")
END = SOURCE.index("\n}\n", START) + 3
+3 -3
View File
@@ -5,9 +5,9 @@ import tempfile
import textwrap
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-steam-x64"
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-fex-update"
SOURCE = SCRIPT.read_text()
START = SOURCE.index("x64_fex() {")
START = SOURCE.index("fex_ready() {")
END = SOURCE.index("\n}\n", START) + 3
X64_FEX = SOURCE[START:END]
@@ -35,7 +35,7 @@ class FexSelectTest(unittest.TestCase):
fex.chmod(0o755)
body = (X64_FEX.replace("/usr/local/share/fex-emu/.droiddeck-tag", "$ROOT/tag")
.replace("/usr/local/bin/FEX", "$ROOT/FEX"))
script = HARNESS + body + 'if x64_fex; then echo "NOW $FEX_NOW|$FEX_FROM"; else echo FAILED; fi\n'
script = HARNESS + body + 'if fex_ready; then echo "NOW $FEX_NOW|$FEX_FROM"; else echo FAILED; fi\n'
env = dict(os.environ, ROOT=root, LOADS="1" if loads else "0", INSTALLABLE=" ".join(installable))
env.pop("BL_FEX_TAG", None)
if catalog:
+136
View File
@@ -0,0 +1,136 @@
import importlib.machinery
import importlib.util
import io
import json
from pathlib import Path
import sys
import types
import unittest
from contextlib import redirect_stdout
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-flatpak"
class FakeRef:
def __init__(self, arch):
self.arch = arch
def get_name(self):
return "io.github.ebkr.r2modman"
def get_arch(self):
return self.arch
def load(bundle_arch, installs):
flatpak = types.SimpleNamespace(
get_default_arch=lambda: "aarch64",
BundleRef=types.SimpleNamespace(new=lambda bundle: FakeRef(bundle_arch)),
)
repository = types.ModuleType("gi.repository")
repository.Flatpak = flatpak
repository.Gio = types.SimpleNamespace(File=types.SimpleNamespace(new_for_path=lambda path: path))
repository.GLib = types.SimpleNamespace(Error=RuntimeError)
gi = types.ModuleType("gi")
gi.require_version = lambda *args: None
gi.repository = repository
saved = {name: sys.modules.get(name) for name in ("gi", "gi.repository")}
sys.modules["gi"] = gi
sys.modules["gi.repository"] = repository
try:
loader = importlib.machinery.SourceFileLoader("droiddeck_flatpak", str(SCRIPT))
module = importlib.util.module_from_spec(importlib.util.spec_from_loader(loader.name, loader))
loader.exec_module(module)
finally:
for name, value in saved.items():
if value is None:
sys.modules.pop(name, None)
else:
sys.modules[name] = value
module.installation = lambda: object()
module.app_ref = lambda inst, app_id: None
def transaction(inst):
installs.append("bundle")
raise AssertionError("the bundle reached a transaction")
module.transaction = transaction
return module
class FlatpakBundleArchTest(unittest.TestCase):
def run_install(self, arch):
installs = []
module = load(arch, installs)
out = io.StringIO()
with redirect_stdout(out):
try:
rc = module.install_bundle("/tmp/r2modman.flatpak")
except AssertionError:
rc = None
return rc, [json.loads(line) for line in out.getvalue().splitlines()], installs
def test_a_bundle_for_an_architecture_fex_cannot_run_is_refused_before_installing(self):
rc, events, installs = self.run_install("riscv64")
self.assertEqual(rc, 1)
self.assertEqual(installs, [])
self.assertEqual(events[0]["e"], "error")
self.assertEqual(events[0]["code"], "arch")
self.assertEqual(events[0]["arch"], "riscv64")
self.assertEqual(events[0]["id"], "io.github.ebkr.r2modman")
def test_a_bundle_for_this_device_goes_on_to_install(self):
rc, events, installs = self.run_install("aarch64")
self.assertEqual(installs, ["bundle"])
self.assertEqual(events, [])
def test_an_x86_64_bundle_goes_on_to_install_for_fex(self):
rc, events, installs = self.run_install("x86_64")
self.assertEqual(installs, ["bundle"])
self.assertEqual(events, [])
def test_flathub_falls_back_to_the_x86_64_build(self):
module = load("aarch64", [])
asked = []
class Remote:
def __init__(self, branch):
self.branch = branch
def get_branch(self):
return self.branch
class Installation:
def fetch_remote_ref_sync(self, remote, kind, app_id, arch, branch, cancellable):
asked.append((arch, branch))
if arch == "x86_64" and branch == "stable":
return Remote("stable")
raise RuntimeError("not found")
added = []
class Transaction:
def add_install(self, remote, ref, subpaths):
added.append(ref)
module.Flatpak.RefKind = types.SimpleNamespace(APP=0)
module.installation = lambda: Installation()
installed = []
module.app_ref = lambda inst, app_id: (installed[0] if installed else None)
module.transaction = lambda inst: (Transaction(), {})
def run(t, state, report_done=True):
installed.append(object())
return 0
module.run = run
out = io.StringIO()
with redirect_stdout(out):
rc = module.install("io.github.ebkr.r2modman")
self.assertEqual(rc, 0)
self.assertEqual(asked, [("aarch64", "stable"), ("aarch64", "master"), ("x86_64", "stable")])
self.assertEqual(added, ["app/io.github.ebkr.r2modman/x86_64/stable"])
if __name__ == "__main__":
unittest.main()
+67
View File
@@ -0,0 +1,67 @@
import contextlib
import fcntl
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-flatpak-setup"
class FlatpakSetupTest(unittest.TestCase):
def run_setup(self, uid, pacman, locked=False, busy=False):
with tempfile.TemporaryDirectory() as directory, contextlib.ExitStack() as held:
root = Path(directory)
if locked:
(root / "work/db").mkdir(parents=True)
(root / "work/db/db.lck").write_text("")
if busy:
holder = held.enter_context(open(root / "work/setup.lock", "w"))
fcntl.flock(holder, fcntl.LOCK_EX | fcntl.LOCK_NB)
bin_dir = root / "bin"
bin_dir.mkdir()
stubs = {"id": "echo %d" % uid, "curl": "exit 7", "pacman": pacman, "pacman-key": "exit 0"}
for name, body in stubs.items():
(bin_dir / name).write_text("#!/bin/sh\n%s\n" % body)
(bin_dir / name).chmod(0o755)
script = root / "setup"
script.write_text(SCRIPT.read_text().replace("WORK=/var/cache/droiddeck-flatpak", "WORK=%s/work" % root)
.replace("/etc/pacman.d/gnupg/pubring.gpg", str(root / "pubring.gpg"))
.replace(" /etc/pacman.conf >", " %s >" % (root / "pacman.conf")))
(root / "pacman.conf").write_text("[options]\n[core]\nInclude = /etc/pacman.d/mirrorlist\n")
(root / "pubring.gpg").write_text("")
env = dict(os.environ, PATH="%s:%s" % (bin_dir, os.environ["PATH"]))
result = subprocess.run(["bash", str(script)], env=env, capture_output=True, text=True, timeout=30)
return result.returncode, result.stdout
def test_a_manual_run_without_root_says_so(self):
code, out = self.run_setup(1000, "exit 0")
self.assertEqual(code, 1)
self.assertIn("== FAIL needs root", out)
self.assertNotIn("Reading the package lists", out)
def test_an_unreachable_mirror_reports_pacmans_error(self):
code, out = self.run_setup(0, "echo \"error: failed retrieving file 'core.db' : Could not resolve host\" >&2; exit 1")
self.assertEqual(code, 1)
self.assertIn("== FAIL could not reach the Arch Linux ARM mirrors (error: failed retrieving file 'core.db' : "
"Could not resolve host)", out)
SYNC = ("db=; while [ $# -gt 0 ]; do [ \"$1\" = --dbpath ] && db=$2; shift; done\n"
"[ -e \"$db/db.lck\" ] && { echo 'error: failed to synchronize all databases (unable to lock database)' >&2; exit 1; }\n"
"mkdir -p \"$db/sync\" && touch \"$db/sync/extra.db\"")
def test_a_lock_left_by_an_interrupted_run_is_cleared(self):
code, out = self.run_setup(0, self.SYNC, locked=True)
self.assertNotIn("unable to lock database", out)
self.assertIn("== STEP Downloading Flatpak", out)
def test_a_setup_that_is_still_running_keeps_its_lock(self):
code, out = self.run_setup(0, self.SYNC, locked=True, busy=True)
self.assertEqual(code, 1)
self.assertIn("== FAIL another Flatpak setup is still running", out)
self.assertNotIn("Reading the package lists", out)
if __name__ == "__main__":
unittest.main()
+199
View File
@@ -0,0 +1,199 @@
import importlib.machinery
import importlib.util
import os
from pathlib import Path
import socket
import sys
import tempfile
import types
import unittest
from unittest import mock
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-flatpak"
def load():
repository = types.ModuleType("gi.repository")
repository.Flatpak = types.SimpleNamespace()
repository.Gio = types.SimpleNamespace()
repository.GLib = types.SimpleNamespace(Error=RuntimeError)
gi = types.ModuleType("gi")
gi.require_version = lambda *args: None
gi.repository = repository
saved = {name: sys.modules.get(name) for name in ("gi", "gi.repository")}
sys.modules["gi"] = gi
sys.modules["gi.repository"] = repository
try:
loader = importlib.machinery.SourceFileLoader("droiddeck_flatpak_bus", str(SCRIPT))
module = importlib.util.module_from_spec(importlib.util.spec_from_loader(loader.name, loader))
loader.exec_module(module)
finally:
for name, value in saved.items():
if value is None:
sys.modules.pop(name, None)
else:
sys.modules[name] = value
return module
class FakeDaemon:
started = []
def __init__(self, argv, **kwargs):
conf = next(a for a in argv if a.startswith("--config-file=")).split("=", 1)[1]
text = open(conf).read()
self.path = text.split("<listen>unix:path=", 1)[1].split("</listen>", 1)[0]
self.server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.server.bind(self.path)
self.server.listen(4)
self.running = True
FakeDaemon.started.append(self)
def poll(self):
return None if self.running else 0
def terminate(self):
self.running = False
self.server.close()
def wait(self, timeout=None):
return 0
def kill(self):
self.terminate()
class FlatpakSystemBusTest(unittest.TestCase):
def setUp(self):
self.module = load()
self.tmp = tempfile.mkdtemp()
self.env = mock.patch.dict(os.environ, {"DBUS_SYSTEM_BUS_ADDRESS": "unix:path=" + os.path.join(self.tmp, "missing")})
self.env.start()
FakeDaemon.started = []
def tearDown(self):
self.env.stop()
os.rmdir(self.tmp)
def test_a_reachable_system_bus_is_used_as_it_is(self):
path = os.path.join(self.tmp, "system")
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(path)
server.listen(1)
try:
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:path=" + path
with mock.patch.object(self.module.subprocess, "Popen", FakeDaemon):
self.assertIsNone(self.module.private_system_bus())
self.assertEqual([], FakeDaemon.started)
finally:
server.close()
os.unlink(path)
def test_without_a_system_bus_a_private_one_runs_for_the_command_and_is_removed_after(self):
with mock.patch.object(self.module.shutil, "which", return_value="/usr/bin/dbus-daemon"), \
mock.patch.object(self.module.subprocess, "Popen", FakeDaemon):
bus = self.module.private_system_bus()
self.assertIsNotNone(bus)
_, work, conf, path = bus
self.assertEqual("unix:path=" + path, os.environ["DBUS_SYSTEM_BUS_ADDRESS"])
self.assertTrue(self.module.bus_reachable(path))
self.module.stop_system_bus(bus)
self.assertFalse(os.path.exists(work))
def test_any_reachable_address_in_a_list_counts_as_the_system_bus(self):
path = os.path.join(self.tmp, "system")
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(path)
server.listen(1)
try:
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:path=%s;unix:guid=1,path=%s" % (os.path.join(self.tmp, "missing"), path)
with mock.patch.object(self.module.subprocess, "Popen", FakeDaemon):
self.assertIsNone(self.module.private_system_bus())
self.assertEqual([], FakeDaemon.started)
finally:
server.close()
os.unlink(path)
def test_escaped_and_abstract_addresses_are_probed_as_dbus_reads_them(self):
path = os.path.join(self.tmp, "sys-bus")
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(path)
server.listen(1)
abstract = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
abstract.bind(b"\0droiddeck-flatpak-test-%d\xff" % os.getpid())
abstract.listen(1)
try:
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:path=" + path.replace("-", "%2D")
self.assertTrue(self.module.system_bus_reachable())
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:path=%s;unix:abstract=droiddeck-flatpak-test-%d%%FF" % (os.path.join(self.tmp, "missing"), os.getpid())
self.assertTrue(self.module.system_bus_reachable())
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:abstract=droiddeck-flatpak-missing-%d" % os.getpid()
self.assertFalse(self.module.system_bus_reachable())
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.bind(("127.0.0.1", 0))
listener.listen(8)
port = listener.getsockname()[1]
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:abstract=droiddeck-flatpak-missing-%d;tcp:host=127.0.0.1,port=%d" % (os.getpid(), port)
self.assertTrue(self.module.system_bus_reachable())
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "tcp:host=127.0.0.1,port=" + "".join("%%%02X" % ord(c) for c in str(port))
self.assertTrue(self.module.system_bus_reachable())
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "tcp:host=127.0.0.1,port=000%d" % port
self.assertTrue(self.module.system_bus_reachable())
listener.close()
self.assertFalse(self.module.system_bus_reachable())
for bad in ("%C2%B2", "65536", "0", "000", "x", "9" * 5000):
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "tcp:host=127.0.0.1,port=" + bad
self.assertEqual([], self.module.system_bus_paths())
self.assertFalse(self.module.system_bus_reachable())
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "launchd:env=DBUS_LAUNCHD_SESSION_BUS_SOCKET"
self.assertTrue(self.module.system_bus_reachable())
finally:
abstract.close()
server.close()
os.unlink(path)
def test_a_daemon_that_cannot_launch_leaves_no_files_and_warns(self):
made = []
real = tempfile.mkdtemp
def mkdtemp(**kwargs):
made.append(real(**kwargs))
return made[-1]
with mock.patch.object(self.module.shutil, "which", return_value="/usr/bin/dbus-daemon"), \
mock.patch.object(self.module.tempfile, "mkdtemp", mkdtemp), \
mock.patch.object(self.module.subprocess, "Popen", side_effect=OSError("exec failed")), \
mock.patch.object(self.module, "emit") as emit:
self.assertIsNone(self.module.private_system_bus())
emit.assert_called_once()
self.assertFalse(os.path.exists(made[0]))
def test_without_dbus_daemon_the_command_still_runs_and_warns(self):
with mock.patch.object(self.module.shutil, "which", return_value=None), \
mock.patch.object(self.module, "emit") as emit:
self.assertIsNone(self.module.private_system_bus())
emit.assert_called_once()
def test_every_command_that_changes_apps_gets_a_system_bus(self):
calls = []
self.module.private_system_bus = lambda: calls.append("start") or "bus"
self.module.stop_system_bus = lambda bus: calls.append("stop:%s" % bus)
self.module.prepare_rootfs = lambda: None
self.module.install = lambda app_id: calls.append("install") or 0
self.module.updates = lambda: calls.append("updates") or 0
with mock.patch.object(sys, "argv", ["droiddeck-flatpak", "install", "org.example.App"]), \
mock.patch.object(self.module.signal, "signal") as handler:
self.assertEqual(0, self.module.main())
self.assertEqual(["start", "install", "stop:bus"], calls)
self.assertEqual(self.module.signal.SIGTERM, handler.call_args[0][0])
with self.assertRaises(SystemExit):
handler.call_args[0][1](self.module.signal.SIGTERM, None)
calls.clear()
with mock.patch.object(sys, "argv", ["droiddeck-flatpak", "updates"]):
self.assertEqual(0, self.module.main())
self.assertNotIn("start", calls)
self.assertIn("updates", calls)
if __name__ == "__main__":
unittest.main()
+138
View File
@@ -0,0 +1,138 @@
from pathlib import Path
import runpy
import subprocess
import tempfile
import unittest
from unittest import mock
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
LIVE = runpy.run_path(str(BIN / "droiddeck-steam-compat"))
HARNESS = r"""
const assert = require('assert');
globalThis.window = globalThis;
globalThis.btoa = text => Buffer.from(text, 'binary').toString('base64');
let timers = new Map(), nextTimer = 0;
globalThis.setTimeout = (callback, ms) => { timers.set(++nextTimer, callback); return nextTimer; };
globalThis.clearTimeout = id => timers.delete(id);
globalThis.setInterval = () => 0;
const fire = async () => {
const pending = [...timers.values()]; timers = new Map();
for (const callback of pending) await callback();
};
let rates = [36, 48, 72, 144];
const notify = [];
const service = {
GetState: async () => ({ BSuccess: () => true,
Body: () => ({ toObject: () => ({ state: { active_display_info: { supported_frame_rates: rates } } }) }) }),
RegisterForNotifyStateChanged(callback) { notify.push(callback); return {}; },
SetBlurParams() {},
};
const writes = [];
globalThis.settingsStore = { clientSettings: {
gamescope_enable_app_target_framerate: true, gamescope_app_target_framerate: 120, gamescope_disable_framelimit: false } };
const varints = bytes => { const out = []; let value = 0, shift = 0;
for (const b of bytes) { value += (b & 127) * 2 ** shift; shift += 7; if (b < 128) { out.push(value); value = 0; shift = 0; } }
return out; };
globalThis.SteamClient = { Settings: { SetSetting(encoded) {
const [key, value] = varints([...Buffer.from(encoded, 'base64')]);
assert.strictEqual(key, 21002 * 8);
writes.push(value);
settingsStore.clientSettings.gamescope_app_target_framerate = value;
} } };
const modules = {
1: { source: () => 'RegisterForNotifyStateChanged only', exports: { other: { GetState() {} } } },
2: { source: () => 'n.MX.SetBlurParams(); n.MX.RegisterForNotifyStateChanged()', exports: { hooks: () => {} } },
3: { source: () => 'SetBlurParams RegisterForNotifyStateChanged GetState', exports: { MX: service } },
};
const webpack = modules => { const require = id => modules[id].exports;
require.m = Object.fromEntries(Object.entries(modules).map(([id, m]) => [id, { toString: m.source }]));
return require; };
globalThis.webpackChunksteamui = { push([, , callback]) { callback(webpack(modules)); } };
const script = require('fs').readFileSync(process.argv[2], 'utf8');
const change = async next => { rates = next; notify.forEach(callback => callback()); };
(async () => {
assert.strictEqual(eval(script), 'installed');
assert.strictEqual(eval(script), 'present');
await fire();
assert.deepStrictEqual(writes, [144], 'a remembered limit the panel does not offer moves to the highest value');
await fire();
assert.deepStrictEqual(writes, [144], 'unchanged choices are not written again');
settingsStore.clientSettings.gamescope_app_target_framerate = 72;
await change([24, 30, 40, 60, 120]);
await fire();
assert.deepStrictEqual(writes, [144, 120], '72 is not offered at 120 Hz');
settingsStore.clientSettings.gamescope_app_target_framerate = 60;
await change([12, 15, 20, 30, 60]);
await fire();
assert.deepStrictEqual(writes, [144, 120], '60 is still offered at 60 Hz and is kept');
await change([18, 24, 36, 72]);
await change([24, 30, 40, 60, 120]);
await fire();
assert.deepStrictEqual(writes, [144, 120], 'a passing rate during a mode switch is not acted on');
settingsStore.clientSettings.gamescope_enable_app_target_framerate = false;
settingsStore.clientSettings.gamescope_app_target_framerate = 72;
await change([36, 48, 144]);
await fire();
assert.deepStrictEqual(writes, [144, 120], 'nothing changes while Steam has the frame limit turned off');
settingsStore.clientSettings.gamescope_enable_app_target_framerate = true;
window.__droiddeckFrameLimit.schedule();
await fire();
assert.deepStrictEqual(writes, [144, 120, 144], 'turning the limit on after a rate change moves an unoffered value');
settingsStore.clientSettings.gamescope_app_target_framerate = 72;
const setSetting = SteamClient.Settings.SetSetting;
SteamClient.Settings.SetSetting = () => { throw new Error('busy'); };
window.__droiddeckFrameLimit.schedule();
await fire();
SteamClient.Settings.SetSetting = setSetting;
window.__droiddeckFrameLimit.schedule();
await fire();
assert.deepStrictEqual(writes, [144, 120, 144, 144], 'a failed write is tried again on the next check');
settingsStore.clientSettings.gamescope_app_target_framerate = 72;
SteamClient.Settings.SetSetting = () => Promise.reject(new Error('busy'));
window.__droiddeckFrameLimit.schedule();
await fire();
SteamClient.Settings.SetSetting = setSetting;
window.__droiddeckFrameLimit.schedule();
await fire();
assert.deepStrictEqual(writes, [144, 120, 144, 144, 144], 'a rejected write is caught and tried again');
delete window.__droiddeckFrameLimit;
webpackChunksteamui.push = ([, , callback]) => callback(webpack({ 1: modules[1], 2: modules[2] }));
assert.strictEqual(eval(script), 'unavailable');
})().catch(error => { console.error(error); process.exit(1); });
"""
class FrameLimitSyncTest(unittest.TestCase):
def test_limit_is_kept_when_offered_and_moved_to_the_highest_value_otherwise(self):
with tempfile.TemporaryDirectory() as directory:
script = Path(directory) / "frame_limit.js"
script.write_text(LIVE["FRAME_LIMIT_JS"])
test = Path(directory) / "test.js"
test.write_text(HARNESS)
result = subprocess.run(["node", str(test), str(script)], capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
def test_install_is_retried_until_steam_is_ready_and_reported_once_per_state(self):
helper = LIVE["Helper"]("/steam", 0)
page = mock.Mock()
page.evaluate.side_effect = ["unavailable", "unavailable", "installed"]
helper.page = page
said = []
clock = [100.0]
globals_ = LIVE["Helper"].sync_frame_limit.__globals__
with mock.patch.dict(globals_, {"say": said.append}), mock.patch.object(globals_["time"], "monotonic", lambda: clock[0]):
helper.sync_frame_limit()
helper.sync_frame_limit()
clock[0] += 10
helper.sync_frame_limit()
clock[0] += 10
helper.sync_frame_limit()
clock[0] += 10
helper.sync_frame_limit()
self.assertEqual(page.evaluate.call_count, 3)
self.assertEqual(said, ["frame limit sync: unavailable", "frame limit sync: installed"])
if __name__ == "__main__":
unittest.main()
+1 -1
View File
@@ -319,7 +319,7 @@ class FsyncPreloadTest(unittest.TestCase):
source = base / "fsync-test.c"
source.write_text(PROGRAM)
subprocess.run(["cc", "-O2", "-Wall", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib),
str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), str(PRELOAD / "clockpause.c"), "-ldl"], check=True)
subprocess.run(["cc", "-O2", "-Wall", "-pthread", "-o", str(cls.exe), str(source)], check=True)
@classmethod
+884 -7
View File
@@ -1,4 +1,5 @@
import hashlib
import io
import json
import os
import shutil
@@ -8,6 +9,7 @@ import shlex
import subprocess
import sys
import tempfile
from unittest import mock
import unittest
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
@@ -27,6 +29,442 @@ def config_text(entries):
return '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }' % mapping
class DirtTelemetryFixTest(unittest.TestCase):
SHOWDOWN_ON = '<x><motion enabled="true" ip="127.0.0.1" port="20777" delay="1" extradata="3" /></x>'
SHOWDOWN_DEFAULT = '<x><motion enabled="true" ip="dbox" port="20777" delay="1" extradata="0" /></x>'
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
root = Path(self.tmp.name)
self.prefix = root / "compatdata/2848447563"
self.proton = root / "Proton/proton"
self.own = root / "Proton/files/lib/wine/i386-windows/ws2_32.dll"
self.own.parent.mkdir(parents=True)
self.own.write_bytes(b"MZ wine ws2_32")
self.proton.write_text("")
self.target = self.prefix / "pfx/drive_c/windows/syswow64/ws2_32.dll"
self.target.parent.mkdir(parents=True)
self.target.symlink_to(self.own)
self.stand_in = root / "droiddeck-dirt/ws2_32.dll"
self.stand_in.parent.mkdir()
self.stand_in.write_bytes(b"MZ droiddeck-dirt-ws2_32 stand-in")
self.patch = mock.patch.dict(MODULE["dirt_ws2_wanted"].__globals__, {"DIRT_WS2": str(self.stand_in)})
self.patch.start()
def tearDown(self):
self.patch.stop()
self.tmp.cleanup()
def config(self, folder, text):
path = self.prefix / "pfx/drive_c/users/steamuser/Documents/My Games" / folder / "hardwaresettings/hardware_settings_config.xml"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text)
def command(self, exe="showdown.exe"):
return [str(self.proton), "waitforexitandrun", "/root/Storage/games/DiRT Showdown/" + exe]
def env(self):
return {"STEAM_COMPAT_DATA_PATH": str(self.prefix)}
def test_telemetry_counts_as_on_when_the_game_sends_somewhere(self):
self.config("DiRT Showdown", self.SHOWDOWN_DEFAULT)
self.assertFalse(MODULE["dirt_telemetry_on"](self.prefix, "dirtshowdown"))
self.config("DiRT Showdown", self.SHOWDOWN_ON)
self.assertTrue(MODULE["dirt_telemetry_on"](self.prefix, "dirtshowdown"))
self.config("DiRT Showdown", self.SHOWDOWN_ON.replace('enabled="true"', 'enabled="false"'))
self.assertFalse(MODULE["dirt_telemetry_on"](self.prefix, "dirtshowdown"))
self.config("DiRT3", '<x><motion enabled="true" ip="192.168.1.20" port="20777" /></x>')
self.assertTrue(MODULE["dirt_telemetry_on"](self.prefix, "dirt3"))
def test_stand_in_is_wanted_only_for_the_dirt_games_with_telemetry_on_and_a_proton_without_the_fix(self):
wanted = MODULE["dirt_ws2_wanted"]
self.config("DiRT Showdown", self.SHOWDOWN_DEFAULT)
self.assertIsNone(wanted(self.command(), self.env()))
self.config("DiRT Showdown", self.SHOWDOWN_ON)
self.assertEqual(wanted(self.command(), self.env()), str(self.prefix))
self.assertEqual(wanted(self.command("Showdown_AVX.exe"), self.env()), str(self.prefix))
self.assertIsNone(wanted(self.command("DiRTRally2.exe"), self.env()))
self.own.write_bytes(b"MZ wine ws2_32 " + "showdown_avx.exe".encode("utf-16-le"))
self.assertIsNone(wanted(self.command(), self.env()))
def test_swap_keeps_the_prefix_link_and_restore_puts_it_back(self):
MODULE["swap_dirt_ws2"](self.prefix)
saved = self.target.with_name("ws2_32.dll.droiddeck-original")
self.assertFalse(self.target.is_symlink())
self.assertEqual(self.target.read_bytes(), self.stand_in.read_bytes())
self.assertEqual(os.readlink(saved), str(self.own))
self.assertEqual(self.own.read_bytes(), b"MZ wine ws2_32")
MODULE["swap_dirt_ws2"](self.prefix)
self.assertEqual(os.readlink(saved), str(self.own))
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, self.proton))
self.assertEqual(os.readlink(self.target), str(self.own))
self.assertFalse(os.path.lexists(saved))
self.assertFalse(MODULE["restore_dirt_ws2"](self.prefix, self.proton))
def test_restore_keeps_a_file_proton_put_back_meanwhile(self):
MODULE["swap_dirt_ws2"](self.prefix)
newer = self.own.with_name("ws2_32-newer.dll")
newer.write_bytes(b"MZ newer wine ws2_32")
self.target.unlink()
self.target.symlink_to(newer)
self.assertFalse(MODULE["restore_dirt_ws2"](self.prefix, self.proton))
self.assertEqual(os.readlink(self.target), str(newer))
self.assertFalse(os.path.lexists(self.target.with_name("ws2_32.dll.droiddeck-original")))
def test_after_a_proton_switch_the_running_protons_file_goes_back(self):
MODULE["swap_dirt_ws2"](self.prefix)
other = Path(self.tmp.name) / "GE-Proton/proton"
other_own = other.parent / "files/lib/wine/i386-windows/ws2_32.dll"
other_own.parent.mkdir(parents=True)
other_own.write_bytes(b"MZ GE wine ws2_32")
other.write_text("")
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, other))
self.assertEqual(os.readlink(self.target), str(other_own))
self.assertFalse(os.path.lexists(self.target.with_name("ws2_32.dll.droiddeck-original")))
def test_restore_follows_a_proton_reached_through_a_linked_folder(self):
MODULE["swap_dirt_ws2"](self.prefix)
pack = Path(self.tmp.name) / "pack"
(pack / "files/lib/wine").mkdir(parents=True)
(pack / "files/lib/wine/i386-windows").symlink_to(self.own.parent)
(pack / "proton").write_text("")
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, pack / "proton"))
self.assertEqual(os.readlink(self.target), str(self.own))
def test_a_stand_in_left_without_the_saved_link_is_replaced_by_protons_own_file(self):
self.target.unlink()
self.target.write_bytes(self.stand_in.read_bytes())
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, self.proton))
self.assertEqual(os.readlink(self.target), str(self.own))
def test_launch_swaps_the_stand_in_in_for_the_game_and_back_afterwards(self):
self.config("DiRT Showdown", self.SHOWDOWN_ON)
self.proton.write_text("#!" + sys.executable + "\nimport os, sys\n"
"target = os.path.join(os.environ['STEAM_COMPAT_DATA_PATH'], 'pfx/drive_c/windows/syswow64/ws2_32.dll')\n"
"print(os.environ.get('WINEDLLOVERRIDES', ''))\n"
"print(os.path.islink(target), b'droiddeck-dirt-ws2_32' in open(target, 'rb').read())\n")
self.proton.chmod(0o755)
runner = Path(self.tmp.name) / "run.py"
runner.write_text("import runpy, sys\nmain = runpy.run_path(sys.argv[1])['main']\nmain.__globals__['DIRT_WS2'] = sys.argv[2]\n"
"sys.argv = [sys.argv[1]] + sys.argv[3:]\nsys.exit(main())\n")
home = Path(self.tmp.name) / "home"
home.mkdir()
result = subprocess.run([sys.executable, str(runner), str(BIN / "droiddeck-game-env"), str(self.stand_in), *self.command()],
env={**os.environ, "HOME": str(home), "STEAM_COMPAT_DATA_PATH": str(self.prefix)}, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
overrides, state = result.stdout.splitlines()[-2:]
self.assertIn("ws2_32=n,b", overrides.split(";"))
self.assertEqual(state, "False True")
self.assertIn("DiRT telemetry fix: ws2_32 stand-in", result.stderr)
self.assertEqual(os.readlink(self.target), str(self.own))
self.assertFalse(os.path.lexists(self.target.with_name("ws2_32.dll.droiddeck-original")))
def test_a_proton_that_cannot_start_leaves_the_prefix_as_it_was(self):
self.config("DiRT Showdown", self.SHOWDOWN_ON)
self.proton.write_text("not a program")
self.proton.chmod(0o644)
runner = Path(self.tmp.name) / "run.py"
runner.write_text("import runpy, sys\nmain = runpy.run_path(sys.argv[1])['main']\nmain.__globals__['DIRT_WS2'] = sys.argv[2]\n"
"sys.argv = [sys.argv[1]] + sys.argv[3:]\nsys.exit(main())\n")
home = Path(self.tmp.name) / "home"
home.mkdir()
result = subprocess.run([sys.executable, str(runner), str(BIN / "droiddeck-game-env"), str(self.stand_in), *self.command()],
env={**os.environ, "HOME": str(home), "STEAM_COMPAT_DATA_PATH": str(self.prefix)}, text=True, capture_output=True)
self.assertNotEqual(result.returncode, 0)
self.assertIn("DiRT telemetry fix: ws2_32 stand-in", result.stderr)
self.assertEqual(os.readlink(self.target), str(self.own))
self.assertFalse(os.path.lexists(self.target.with_name("ws2_32.dll.droiddeck-original")))
def test_launch_with_telemetry_off_leaves_the_prefix_alone(self):
self.config("DiRT Showdown", self.SHOWDOWN_DEFAULT)
self.proton.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ.get('WINEDLLOVERRIDES', ''))\n")
self.proton.chmod(0o755)
home = Path(self.tmp.name) / "home"
home.mkdir()
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), *self.command()],
env={**os.environ, "HOME": str(home), "STEAM_COMPAT_DATA_PATH": str(self.prefix)}, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertNotIn("ws2_32=n,b", result.stdout)
self.assertEqual(os.readlink(self.target), str(self.own))
def test_a_failed_put_back_does_not_end_the_launch_with_an_error(self):
def fail():
raise OSError(28, "No space left on device")
stderr = io.StringIO()
with mock.patch("sys.stderr", stderr):
code = MODULE["start"]([sys.executable, "-c", "raise SystemExit(0)"], dict(os.environ), None, fail)
self.assertEqual(code, 0)
self.assertIn("not put back", stderr.getvalue())
def test_a_half_done_swap_is_put_back(self):
saved = self.target.with_name("ws2_32.dll.droiddeck-original")
os.replace(self.target, saved)
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, self.proton))
self.assertEqual(os.readlink(self.target), str(self.own))
self.assertFalse(os.path.lexists(saved))
def test_a_native_ws2_32_goes_back_as_it_was_after_a_proton_switch(self):
self.target.unlink()
self.target.write_bytes(b"MZ a mod's own ws2_32")
MODULE["swap_dirt_ws2"](self.prefix)
other = Path(self.tmp.name) / "GE-Proton/proton"
(other.parent / "files/lib/wine/i386-windows").mkdir(parents=True)
(other.parent / "files/lib/wine/i386-windows/ws2_32.dll").write_bytes(b"MZ GE wine ws2_32")
other.write_text("")
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, other))
self.assertFalse(self.target.is_symlink())
self.assertEqual(self.target.read_bytes(), b"MZ a mod's own ws2_32")
def test_a_link_to_a_mods_ws2_32_goes_back_as_it_was(self):
mod = Path(self.tmp.name) / "mods/ws2_32.dll"
mod.parent.mkdir()
mod.write_bytes(b"MZ a mod's ws2_32")
self.target.unlink()
self.target.symlink_to(mod)
MODULE["swap_dirt_ws2"](self.prefix)
other = Path(self.tmp.name) / "GE-Proton/proton"
(other.parent / "files/lib/wine/i386-windows").mkdir(parents=True)
(other.parent / "files/lib/wine/i386-windows/ws2_32.dll").write_bytes(b"MZ GE wine ws2_32")
other.write_text("")
self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, other))
self.assertEqual(os.readlink(self.target), str(mod))
def test_start_gives_the_signal_handlers_back(self):
import signal
before = {s: signal.getsignal(s) for s in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP)}
MODULE["start"]([sys.executable, "-c", "pass"], dict(os.environ), None, lambda: None)
self.assertEqual(before, {s: signal.getsignal(s) for s in before})
def test_start_puts_the_file_back_after_the_game_exits(self):
calls = []
code = MODULE["start"]([sys.executable, "-c", "raise SystemExit(3)"], dict(os.environ), None, lambda: calls.append("restored"))
self.assertEqual(code, 3)
self.assertEqual(calls, ["restored"])
with self.assertRaises(OSError):
MODULE["start"]([str(Path(self.tmp.name) / "missing")], dict(os.environ), None, lambda: calls.append("again"))
self.assertEqual(calls, ["restored"])
class EgoCorePlanTest(unittest.TestCase):
@staticmethod
def rule(op, value, worker_map):
return (' <restrict_if name="cores" op="%s" value="%d">\r\n <restrict_add>\r\n'
' <threadStrategy parallelUpdateRender="true" workerMapFile="system/%s" updateProcessor="2" />\r\n'
' </restrict_add>\r\n </restrict_if>\r\n' % (op, value, worker_map))
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.system = Path(self.tmp.name) / "DiRT 3/system"
self.system.mkdir(parents=True)
cpu = "".join(self.rule("int_eq", n, "workerMap%dCore.xml" % n) for n in (2, 3, 4, 6)) + self.rule("int_ge", 8, "workerMap8Core.xml")
self.shipped = ('<?xml version="1.0" encoding="UTF-8" ?>\r\n<hardware_settings_restrictions>\r\n <cpu>\r\n'
' <restrict_info>\r\n' + cpu +
' <restrict_else>\r\n <restrict_add>\r\n'
' <threadStrategy parallelUpdateRender="false" workerMapFile="system/workerMap.xml" />\r\n'
' </restrict_add>\r\n </restrict_else>\r\n </restrict_info>\r\n </cpu>\r\n'
' <audio_card>\r\n' + self.rule("int_eq", 5, "audio.xml") + ' </audio_card>\r\n'
'</hardware_settings_restrictions>').encode("latin-1")
self.path = self.system / "hardware_settings_restrictions.xml"
self.path.write_bytes(self.shipped)
for name in ("workerMap.xml", "workerMap2Core.xml", "workerMap3Core.xml", "workerMap4Core.xml", "workerMap6Core.xml", "workerMap8Core.xml"):
(self.system / name).write_text("<workerMap />")
self.command = ["/proton", "waitforexitandrun", str(self.system.parent / "dirt3_game.exe")]
self.saved = self.path.with_name(self.path.name + ".droiddeck-original")
def plan(self, topology, command=None):
env = {} if topology is None else {"WINE_CPU_TOPOLOGY": topology}
return MODULE["ego_core_plan"](command or self.command, env)
def test_five_cores_get_a_copy_of_the_four_core_plan(self):
note = self.plan("5:7,3,4,5,6")
self.assertIn("5 cores", note)
self.assertIn("4-core plan", note)
text = self.path.read_bytes()
four = self.rule("int_eq", 4, "workerMap4Core.xml").encode("latin-1")
five = self.rule("int_eq", 5, "workerMap4Core.xml").encode("latin-1")
self.assertEqual(text, self.shipped.replace(four, four + five))
self.assertNotIn(b"\n", text.replace(b"\r\n", b""))
self.assertEqual(self.saved.read_bytes(), self.shipped)
self.assertEqual(sorted(p.name for p in self.system.glob("*droiddeck-tmp*")), [])
self.assertIsNone(self.plan("5:7,3,4,5,6"))
self.assertEqual(self.path.read_bytes(), text)
self.assertEqual(self.saved.read_bytes(), self.shipped)
def test_seven_cores_get_the_six_core_plan(self):
self.assertIn("6-core plan", self.plan("7:7,1,2,3,4,5,6"))
self.assertIn(b'value="7">\r\n <restrict_add>\r\n <threadStrategy parallelUpdateRender="true" '
b'workerMapFile="system/workerMap6Core.xml"', self.path.read_bytes())
def test_counts_with_a_plan_are_left_alone(self):
for topology in ("2:7,6", "4:7,3,4,5", "6:7,3,4,5,6,2", "8:7,2,3,4,5,6,0,1", "12:0,1,2,3,4,5,6,7,8,9,10,11"):
self.assertIsNone(self.plan(topology), topology)
self.assertEqual(self.path.read_bytes(), self.shipped)
self.assertFalse(os.path.lexists(self.saved))
def test_nothing_changes_without_a_core_count_a_restrictions_file_a_smaller_plan_or_its_worker_map(self):
self.assertIsNone(self.plan(None))
self.assertIsNone(self.plan(""))
self.assertIsNone(self.plan("x:1"))
self.assertIsNone(self.plan("\u00b2:7"))
self.assertIsNone(self.plan("1:7"))
self.assertIsNone(self.plan("5:7,3,4,5,6", ["/proton", "waitforexitandrun"]))
self.assertIsNone(self.plan("5:7,3,4,5,6", ["/proton", "waitforexitandrun", "/elsewhere/game.exe"]))
(self.system / "workerMap4Core.xml").unlink()
self.assertIsNone(self.plan("5:7,3,4,5,6"))
self.assertEqual(self.path.read_bytes(), self.shipped)
self.assertFalse(os.path.lexists(self.saved))
def test_an_unknown_comparison_counts_as_a_plan(self):
self.path.write_bytes(self.shipped.replace(b'op="int_ge" value="8"', b'op="int_between" value="8"'))
self.assertIsNone(self.plan("5:7,3,4,5,6"))
def test_main_adds_the_plan_before_the_game_starts(self):
with tempfile.TemporaryDirectory() as tmp:
probe = Path(tmp) / "probe"
probe.write_text('#!/bin/sh\ngrep -c \'value="5"\' "%s"\n' % self.path)
probe.chmod(0o755)
env = {k: v for k, v in os.environ.items() if not k.startswith(("WINE", "PROTON", "DROIDDECK", "BL_", "STEAM"))}
env.update(HOME=tmp, STEAM_COMPAT_DATA_PATH="/compatdata/3791535942", WINE_CPU_TOPOLOGY="5:7,3,4,5,6")
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "waitforexitandrun", self.command[2]],
env=env, text=True, capture_output=True, check=True)
self.assertEqual(result.stdout.strip(), "2", result.stderr)
self.assertIn("added a copy of its 4-core plan", result.stderr)
class GameDebugLoggingTest(unittest.TestCase):
def test_maintenance_precision_survives_run_with_performance_game_settings(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
config = home / ".config/droiddeck/game-environment.json"
config.parent.mkdir(parents=True)
config.write_text(json.dumps({"version": 1, "shared": {"FEX_X87REDUCEDPRECISION": "1"},
"games": {"42": {"FEX_X87REDUCEDPRECISION": "1"}}}))
probe = home / "proton"
probe.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ['FEX_X87REDUCEDPRECISION'])\n")
probe.chmod(0o755)
base = {**os.environ, "HOME": tmp, "STEAM_COMPAT_DATA_PATH": str(home / "compatdata/42")}
maintenance = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "run"],
env={**base, "DROIDDECK_GOG_MAINTENANCE": "1", "FEX_X87REDUCEDPRECISION": "0"}, text=True, capture_output=True, check=True)
game = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "waitforexitandrun"],
env={**base, "FEX_X87REDUCEDPRECISION": "1"}, text=True, capture_output=True, check=True)
self.assertEqual("0", maintenance.stdout.strip())
self.assertEqual("1", game.stdout.strip())
def test_real_game_launch_without_config_file_keeps_the_base_environment(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
probe = home / "proton"
probe.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ['PROTON_LOG'])\n")
probe.chmod(0o755)
result = subprocess.run(
[sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "waitforexitandrun"],
env={**os.environ, "HOME": tmp, "STEAM_COMPAT_DATA_PATH": str(home / "compatdata/42"),
"BL_DEBUG_DIR": tmp, "PROTON_LOG": "0"},
text=True, capture_output=True, check=True,
)
self.assertEqual(result.stdout.strip(), "0")
def test_game_debug_logs_override_only_diagnostic_values_and_keep_raw_settings(self):
with tempfile.TemporaryDirectory(prefix="game logs '") as debug_dir:
inherited = {"BL_DEBUG_DIR": debug_dir, "PROTON_LOG": "0", "WINEDEBUG": "-all,+relay",
"PROTON_LOG_DIR": "/old", "PROTON_CRASH_REPORT_DIR": "/old-crashes",
"DXVK_LOG_PATH": "/old-dxvk", "DXVK_LOG_LEVEL": "none", "VKD3D_DEBUG": "none",
"CUSTOM": "$(touch never); 'quoted' = literal", "DXVK_CONFIG": "dxvk.tearFree = True"}
config = {"version": 1, "gameDebugLogs": True,
"shared": {"PROTON_LOG": "0", "BL_DEBUG_DIR": "/profile-redirect"},
"games": {"42": {"PROTON_LOG_DIR": None, "DXVK_LOG_PATH": "/game-redirect"}}}
normal = MODULE["apply_config"](inherited, config, "42")
debug = MODULE["apply_game_debug_logs"](normal, config, inherited["BL_DEBUG_DIR"])
self.assertEqual(debug["PROTON_LOG"], "1")
for key in ("PROTON_LOG_DIR", "PROTON_CRASH_REPORT_DIR", "DXVK_LOG_PATH"):
self.assertEqual(debug[key], debug_dir)
self.assertEqual(debug["DXVK_LOG_LEVEL"], "debug")
self.assertEqual(debug["VKD3D_DEBUG"], "info")
self.assertIn("+relay", debug["WINEDEBUG"].split(","))
self.assertTrue(debug["WINEDEBUG"].endswith("+loaddll,+mscoree"))
for key in ("CUSTOM", "DXVK_CONFIG", "BL_DEBUG_DIR"):
self.assertEqual(debug[key], normal[key])
self.assertEqual(normal["PROTON_LOG"], "0")
self.assertEqual(inherited["PROTON_LOG_DIR"], "/old")
self.assertIs(MODULE["apply_game_debug_logs"](normal, {**config, "gameDebugLogs": False}, debug_dir), normal)
self.assertEqual(MODULE["apply_game_debug_logs"](debug, config, debug_dir), debug)
def test_wine_debug_channels_are_formatted_deduplicated_and_diagnostics_win(self):
formatted = MODULE["game_debug_channels"](
" +relay, trace-vulkan, -all, -seh, +relay, trace+vulkan, garbage, $(touch never) ",
"+seh,warn-foo,warn+foo,+relay",
)
self.assertEqual(formatted, "-all,trace+vulkan,warn+foo,+relay," +
"+timestamp,+pid,+tid,+seh,+unwind,+threadname,+debugstr,+loaddll,+mscoree")
self.assertEqual(MODULE["game_debug_channels"](formatted, "1"), formatted)
self.assertNotIn("-seh", formatted)
for value in ("x\0,+relay", "+" + "x" * 8192, 1, None):
self.assertEqual(MODULE["game_debug_channels"](value), MODULE["game_debug_channels"]())
oversized = ",".join("+channel_" + str(i) for i in range(550))
other = ",".join("+other_" + str(i) for i in range(550))
self.assertLessEqual(len(MODULE["game_debug_channels"](oversized, other)), 8192)
self.assertTrue(MODULE["game_debug_channels"](oversized, other).endswith("+loaddll,+mscoree"))
def test_game_debug_logs_require_opt_in_and_a_real_session_log_directory(self):
env = {"PROTON_LOG": "user", "CUSTOM": "keep"}
for config in ({}, {"version": 1}, {"version": 1, "gameDebugLogs": False},
{"version": 1, "gameDebugLogs": "true"}, {"version": 2, "gameDebugLogs": True}):
self.assertIs(MODULE["apply_game_debug_logs"](env, config, "/"), env)
with tempfile.TemporaryDirectory() as tmp:
not_a_directory = Path(tmp) / "file"
not_a_directory.write_text("file")
for path in (None, "", "relative", "bad\0path", str(not_a_directory), str(Path(tmp) / "missing")):
with self.subTest(path=path):
self.assertIs(MODULE["apply_game_debug_logs"](env, {"version": 1, "gameDebugLogs": True}, path), env)
def test_game_debug_toggle_is_live_preserves_command_and_excludes_probes(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
config_path = home / ".config/droiddeck/game-environment.json"
config_path.parent.mkdir(parents=True)
debug_dir = home / "session logs '$literal; directory'"
debug_dir.mkdir()
probe = home / "fake proton"
probe.write_text("#!" + sys.executable + "\nimport json, os, sys\n"
"print(json.dumps({'env': dict(os.environ), 'argv': sys.argv[1:]}))\n")
probe.chmod(0o755)
literal = "$(touch " + str(home / "never-created") + "); 'literal'=value"
config = {"version": 1, "gameDebugLogs": True,
"shared": {"PROTON_LOG": "0", "WINEDEBUG": "-all,+relay", "CUSTOM": literal,
"BL_DEBUG_DIR": "/profile-must-not-redirect"}}
env = {**os.environ, "HOME": tmp, "STEAM_COMPAT_DATA_PATH": str(home / "compatdata/42"),
"BL_DEBUG_DIR": str(debug_dir), "PROTON_LOG": "0", "WINEDEBUG": "-all"}
command = [str(probe), "waitforexitandrun", "game 'with quotes'.exe", "argument with spaces", "$(literal)", "%command%"]
def launch(arguments, environment):
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), *arguments],
env=environment, text=True, capture_output=True, check=True)
return json.loads(result.stdout)
for enabled in (True, False, True):
config_path.write_text(json.dumps({**config, "gameDebugLogs": enabled}))
result = launch(command, env)
self.assertEqual(result["argv"], command[1:])
self.assertEqual(result["env"]["CUSTOM"], literal)
self.assertEqual(result["env"]["PROTON_LOG"], "1" if enabled else "0")
if enabled:
self.assertEqual(result["env"]["PROTON_LOG_DIR"], str(debug_dir))
self.assertIn("+relay", result["env"]["WINEDEBUG"].split(","))
self.assertIn("+seh", result["env"]["WINEDEBUG"].split(","))
else:
self.assertEqual(result["env"]["WINEDEBUG"], "-all,+relay")
for verb, prefix in (("run", "42"), ("waitforexitandrun", "0"), ("waitforexitandrun", "not-a-game")):
result = launch([str(probe), verb], {**env, "STEAM_COMPAT_DATA_PATH": str(home / "compatdata" / prefix)})
self.assertEqual(result["env"]["PROTON_LOG"], "0")
self.assertEqual(result["env"]["WINEDEBUG"], "-all")
result = launch(command, {**env, "STEAM_COMPAT_DATA_PATH": str(home / "compatdata/-1")})
self.assertEqual(result["env"]["PROTON_LOG"], "1")
self.assertFalse((home / "never-created").exists())
self.assertEqual(json.loads(config_path.read_text())["shared"], config["shared"])
class ReadConfigMappingTest(unittest.TestCase):
def test_missing_parent_or_unbalanced_vdf_is_not_treated_as_absent_choices(self):
with tempfile.TemporaryDirectory() as tmp:
@@ -59,23 +497,72 @@ class GameEnvironmentTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
probe = home / "probe"
probe.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ.get('BANNER_AUDIO_DIRECT_MIC', 'unset'))\n")
probe.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ.get('AUDIO_DIRECT_MIC', 'unset'))\n")
probe.chmod(0o755)
config = home / ".config/droiddeck/game-environment.json"
config.parent.mkdir(parents=True)
for enabled, override, expected in (("1", None, "1"), ("0", "1", "0"), ("1", "0", "0")):
with self.subTest(enabled=enabled, override=override):
profile = {} if override is None else {
"BANNER_AUDIO_DIRECT_MIC": override, "BL_MIC_ENABLED": override,
"AUDIO_DIRECT_MIC": override, "BL_MIC_ENABLED": override,
}
config.write_text(json.dumps({"version": 1, "games": {"42": profile}}))
env = dict(os.environ, HOME=tmp, BL_MIC_ENABLED=enabled,
BANNER_AUDIO_DIRECT_MIC="1", STEAM_COMPAT_DATA_PATH="/compatdata/42")
AUDIO_DIRECT_MIC="1", STEAM_COMPAT_DATA_PATH="/compatdata/42")
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"),
str(probe), "waitforexitandrun"],
env=env, text=True, capture_output=True, check=True)
self.assertEqual(result.stdout.strip(), expected)
def test_game_profile_directaudio_settings_reach_the_driver(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
probe = home / "probe"
probe.write_text("#!" + sys.executable + "\nimport os\nprint(' '.join(sorted(k + '=' + v for k, v in os.environ.items() if k.startswith('BANNER_AUDIO_DIRECT_'))))\n")
probe.chmod(0o755)
config = home / ".config/droiddeck/game-environment.json"
config.parent.mkdir(parents=True)
config.write_text(json.dumps({"version": 1, "games": {"42": {"AUDIO_DIRECT_MS": "80"}}}))
base = {key: value for key, value in os.environ.items() if "AUDIO_DIRECT_" not in key}
base.update(HOME=tmp, BL_MIC_ENABLED="0", STEAM_COMPAT_DATA_PATH="/compatdata/42",
AUDIO_DIRECT_MS="60", AUDIO_DIRECT_PERIOD_MS="20", AUDIO_DIRECT_MIC="1",
BANNER_AUDIO_DIRECT_MS="60", BANNER_AUDIO_DIRECT_MIC="1")
for driver, expected in (("directaudio,pulse", "BANNER_AUDIO_DIRECT_MIC=0 BANNER_AUDIO_DIRECT_MS=80 BANNER_AUDIO_DIRECT_PERIOD_MS=20"),
("pulse", "BANNER_AUDIO_DIRECT_MIC=1 BANNER_AUDIO_DIRECT_MS=60")):
with self.subTest(driver=driver):
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "waitforexitandrun"],
env=dict(base, WINE_AUDIO_DRIVER=driver), text=True, capture_output=True, check=True)
self.assertEqual(result.stdout.strip(), expected)
def test_desktop_games_restart_on_the_gpu_with_steams_preloads_handed_back(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
gpu = home / "gpu"
gpu.write_text("#!/bin/bash\necho \"gpu LD_PRELOAD=${LD_PRELOAD-unset}\"\nexport BL_DESKTOP_GPU_CHILD=1\nexec \"$@\"\n")
gpu.chmod(0o755)
launcher = home / "launcher"
setup = COMPAT["BL_DESKTOP_GPU_SETUP"].replace("/usr/local/bin/droiddeck-gpu", str(gpu))
launcher.write_text("#!/bin/bash\nverb=$1; shift\n" + setup + "echo \"game $verb $* LD_PRELOAD=${LD_PRELOAD-unset} saved=${BL_GPU_SAVED_LD_PRELOAD-unset}\"\n")
launcher.chmod(0o755)
base = {key: value for key, value in os.environ.items() if not key.startswith("BL_")}
base["LD_PRELOAD"] = "overlay.so"
for extra, args, expected in (
({"BL_DESKTOP": "1"}, ["waitforexitandrun", "game.exe"],
["gpu LD_PRELOAD=unset", "game waitforexitandrun game.exe LD_PRELOAD=overlay.so saved=unset"]),
({"BL_DESKTOP": "1", "BL_DESKTOP_GPU": "Off"}, ["waitforexitandrun", "game.exe"],
["game waitforexitandrun game.exe LD_PRELOAD=overlay.so saved=unset"]),
({"BL_DESKTOP": "1"}, ["run", "setup.exe"],
["game run setup.exe LD_PRELOAD=overlay.so saved=unset"]),
({"BL_DESKTOP": "1"}, ["waitforexitandrun", "iscriptevaluator.exe"],
["game waitforexitandrun iscriptevaluator.exe LD_PRELOAD=overlay.so saved=unset"]),
({}, ["waitforexitandrun", "game.exe"],
["game waitforexitandrun game.exe LD_PRELOAD=overlay.so saved=unset"]),
):
with self.subTest(extra=extra, args=args):
result = subprocess.run([str(launcher)] + args, env=dict(base, **extra),
text=True, capture_output=True, check=True)
self.assertEqual(result.stdout.splitlines(), expected)
def test_the_install_script_evaluator_gets_its_scripts_marked_first(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
@@ -180,6 +667,14 @@ class GameEnvironmentTest(unittest.TestCase):
self.assertEqual(MODULE["apply_config"](env, config, "42")["TU_DEBUG"], "sysmem")
self.assertNotIn("TU_DEBUG", MODULE["apply_config"](env, config, "43"))
def test_the_vram_cap_comes_before_the_games_own_dxvk_settings(self):
cap = MODULE["vram_cap"]
self.assertEqual(cap({"BL_VRAM_MB": "4096"})["DXVK_CONFIG"], "dxgi.maxDeviceMemory = 4096")
self.assertEqual(cap({"BL_VRAM_MB": "2048", "DXVK_CONFIG": "dxvk.maxFrameRate = 60;"})["DXVK_CONFIG"],
"dxgi.maxDeviceMemory = 2048; dxvk.maxFrameRate = 60")
for off in ({}, {"BL_VRAM_MB": "0"}, {"BL_VRAM_MB": "lots"}, {"BL_VRAM_MB": "²"}, {"BL_VRAM_MB": "-1"}):
self.assertNotIn("DXVK_CONFIG", cap(off))
def test_a_games_wine_sync_choice_also_reaches_its_setup_runs(self):
env = {"BL_SYNC": "1", "BL_SYNC_FALLBACK": "1", "BL_FSYNC_FIRST": "0", "OTHER": "kept"}
config = {"version": 1, "games": {"42": {"BL_SYNC": "0", "BL_FSYNC_FIRST": "1", "PROTON_NO_ESYNC": "1", "FEX_TSOENABLED": "1"}}}
@@ -260,10 +755,17 @@ class GameEnvironmentTest(unittest.TestCase):
(game / name).touch()
env, extra, notes = MODULE["engine_fixes"](str(game / "Game.exe"), [])
self.assertEqual(env["FEX_TSOENABLED"], "1")
self.assertEqual(env["FEX_MULTIBLOCK"], "0")
self.assertNotIn("FEX_MULTIBLOCK", env)
self.assertEqual(env["WINEDLLOVERRIDES"], "libsentry.windows.release.x86_64=d")
self.assertEqual(extra, ["--rendering-driver", "vulkan"])
self.assertEqual(MODULE["engine_fixes"](str(game / "Game.exe"), ["--rendering-driver", "opengl3"])[1], [])
script = Path(tmp) / "GDScript Game"
script.mkdir()
for name in ("Game.exe", "Game.pck", "libsentry.windows.release.x86_64.dll"):
(script / name).touch()
env, extra, notes = MODULE["engine_fixes"](str(script / "Game.exe"), [])
self.assertNotIn("WINEDLLOVERRIDES", env)
self.assertEqual(extra, ["--rendering-driver", "vulkan"])
other = Path(tmp) / "Other"
other.mkdir()
(other / "Other.exe").touch()
@@ -357,6 +859,290 @@ class GameEnvironmentTest(unittest.TestCase):
(asylum / name).touch()
self.assertEqual(MODULE["engine_fixes"](str(asylum / "BmLauncher.exe"), []), ({}, [], []))
def test_d3dgear_capture_hook_beside_the_game_is_turned_off(self):
with tempfile.TemporaryDirectory() as tmp:
game = Path(tmp) / "The Crew 2"
game.mkdir()
for name in ("TheCrew2.exe", "TheCrew2_BE.exe", "d3dgear64.dll"):
(game / name).touch()
env, extra, notes = MODULE["engine_fixes"](str(game / "TheCrew2.exe"), [])
self.assertEqual((env, extra), ({"WINEDLLOVERRIDES": "d3dgear64=d"}, []))
self.assertTrue(notes)
(game / "d3dgear64.dll").rename(game / "D3DGear64.DLL")
self.assertEqual(MODULE["engine_fixes"](str(game / "TheCrew2.exe"), [])[0], {"WINEDLLOVERRIDES": "d3dgear64=d"})
(game / "D3DGear64.DLL").unlink()
self.assertEqual(MODULE["engine_fixes"](str(game / "TheCrew2.exe"), []), ({}, [], []))
def test_dll_overrides_from_several_fixes_are_all_kept(self):
with tempfile.TemporaryDirectory() as tmp:
for name in ("Game.exe", "Game.pck", "GodotSharp.dll", "libsentry.windows.release.x86_64.dll", "d3dgear64.dll"):
(Path(tmp) / name).touch()
env = MODULE["engine_fixes"](str(Path(tmp) / "Game.exe"), [])[0]
self.assertEqual(env["WINEDLLOVERRIDES"], "libsentry.windows.release.x86_64=d;d3dgear64=d")
def test_black_ops_iii_and_dark_souls_remastered_skip_protonfixes(self):
for exe in ("/game/Call of Duty Black Ops III/BlackOps3.exe", "/game/DARK SOULS REMASTERED/DarkSoulsRemastered.exe"):
env, extra, notes = MODULE["engine_fixes"](exe, [])
self.assertEqual((env, extra), ({"PROTONFIXES_DISABLE": "1"}, []))
self.assertTrue(notes)
self.assertEqual(MODULE["engine_fixes"]("/game/Game/DarkSoulsII.exe", []), ({}, [], []))
def test_little_nightmares_keeps_its_render_thread(self):
env, extra, notes = MODULE["engine_fixes"]("/game/Little Nightmares/Atlas/Binaries/Win64/LittleNightmares.exe", [])
self.assertEqual((env, extra), ({"PROTONFIXES_DISABLE": "1"}, []))
self.assertTrue(notes)
self.assertEqual(MODULE["engine_fixes"]("/game/Little Nightmares II/Moonlight/Binaries/Win64/LittleNightmaresII.exe", []), ({}, [], []))
def test_game_cores_are_parsed_from_lists_and_ranges(self):
parse = MODULE["parse_cores"]
self.assertEqual(parse("0-3,6"), {0, 1, 2, 3, 6})
self.assertEqual(parse(" 4, 5 "), {4, 5})
for bad in ("", "a", "3-1", "1,,2", "-1", "1-"):
self.assertIsNone(parse(bad), bad)
def test_a_profiles_game_cores_pin_the_game_within_the_online_cores(self):
calls = []
os_mod = MODULE["os"]
with mock.patch.dict(MODULE["game_cores"].__globals__, {"online_cores": lambda: {0, 1, 2, 3, 4, 5, 6, 7}}), \
mock.patch.object(os_mod, "sched_setaffinity", side_effect=lambda pid, cores: calls.append(set(cores)), create=True):
MODULE["game_cores"]({"DROIDDECK_GAME_CORES": "0-2,9"})
MODULE["game_cores"]({"DROIDDECK_GAME_CORES": "junk"})
MODULE["game_cores"]({"DROIDDECK_GAME_CORES": "12-13"})
MODULE["game_cores"]({})
self.assertEqual(calls, [{0, 1, 2}])
self.assertEqual(MODULE["parse_cores"]("0,2-7"), {0, 2, 3, 4, 5, 6, 7})
def test_windows_games_see_only_the_cores_they_are_pinned_to(self):
os_mod = MODULE["os"]
online = {"online_cores": lambda: {0, 1, 2, 3, 4, 5, 6, 7}}
cases = [
({"BL_GAME_CPUS": "6,7"}, {6, 7}, "2:6,7", "2:6,7"),
({"DROIDDECK_GAME_CORES": "0-2"}, {0, 1, 2}, "3:0,1,2", "3:0,1,2"),
({"BL_GAME_CPUS": "0-7"}, set(range(8)), None, None),
({}, {6, 7}, None, None),
({"BL_GAME_CPUS": "6,7", "WINE_CPU_TOPOLOGY": "4:0,1,2,3"}, {6, 7}, "4:0,1,2,3", "4:0,1,2,3"),
({"WINE_CPU_TOPOLOGY": "2:0,1"}, set(range(8)), "2:0,1", "2:0,1"),
({"BL_GAME_CPUS": "6,7", "WINE_CPU_TOPOLOGY": ""}, {6, 7}, "", None),
({"BL_GAME_CPUS": "6,7", "PROTON_CPU_TOPOLOGY": "1:7"}, {6, 7}, None, "1:7"),
({"BL_GAME_CPUS": "9"}, {9}, None, None),
]
for env, affinity, expected, proton in cases:
env["DROIDDECK_FASTEST_CORES"] = "0"
with mock.patch.dict(MODULE["cpu_topology"].__globals__, online), \
mock.patch.object(os_mod, "sched_getaffinity", return_value=affinity, create=True):
MODULE["cpu_topology"](env)
self.assertEqual(env.get("WINE_CPU_TOPOLOGY"), expected, env)
self.assertEqual(env.get("PROTON_CPU_TOPOLOGY"), proton, env)
def test_only_games_get_the_larger_swapchain(self):
game = {"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/945360"}
self.assertTrue(MODULE["game_images"](game))
self.assertEqual(game["GAMESCOPE_WSI_MIN_IMAGE_COUNT"], "8")
self.assertEqual(game["GAMESCOPE_WSI_ENSURE_MIN_IMAGE_COUNT"], "1")
own = {"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/945360", "GAMESCOPE_WSI_MIN_IMAGE_COUNT": "3"}
MODULE["game_images"](own)
self.assertEqual(own["GAMESCOPE_WSI_MIN_IMAGE_COUNT"], "3")
for env in ({}, {"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/0"}):
self.assertFalse(MODULE["game_images"](env))
self.assertNotIn("GAMESCOPE_WSI_MIN_IMAGE_COUNT", env)
def test_opengl_games_do_not_wait_for_the_screen_refresh(self):
cases = [
({}, "0"),
({"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/-686078602"}, "0"),
({"vblank_mode": "2"}, "2"),
({"vblank_mode": ""}, ""),
({"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/0"}, None),
({"STEAM_COMPAT_DATA_PATH": ""}, None),
]
for env, expected in cases:
env.setdefault("STEAM_COMPAT_DATA_PATH", "/steam/compatdata/3608888694")
added = expected == "0" and "vblank_mode" not in env
self.assertEqual(MODULE["gl_vsync"](env), added, env)
self.assertEqual(env.get("vblank_mode"), expected, env)
said = "OpenGL does not wait for the screen refresh"
with tempfile.TemporaryDirectory() as tmp:
probe = Path(tmp) / "probe"
probe.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ.get('vblank_mode', 'unset'))\n")
probe.chmod(0o755)
config = Path(tmp) / ".config/droiddeck/game-environment.json"
config.parent.mkdir(parents=True)
for inherited, edits, expected in (({}, None, "0"), ({"vblank_mode": "3"}, None, "3"),
({}, {"vblank_mode": None}, "unset"), ({}, {"vblank_mode": "2"}, "2"),
({"vblank_mode": "3"}, {"vblank_mode": None}, "unset")):
with self.subTest(inherited=inherited, edits=edits):
config.write_text(json.dumps({"version": 1, "games": {"42": edits or {}}}))
env = {name: value for name, value in os.environ.items() if name != "vblank_mode"}
env.update(HOME=tmp, STEAM_COMPAT_DATA_PATH="/compatdata/42", **inherited)
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "waitforexitandrun"],
env=env, text=True, capture_output=True, check=True)
self.assertEqual(result.stdout.strip(), expected)
self.assertEqual(said in result.stderr, expected == "0" and not inherited, result.stderr)
def test_teardown_keeps_its_own_vsync_unless_the_player_chose(self):
with tempfile.TemporaryDirectory() as tmp:
probe = Path(tmp) / "probe"
probe.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ.get('vblank_mode', 'unset'))\n")
probe.chmod(0o755)
game = Path(tmp) / "Teardown/teardown.exe"
game.parent.mkdir()
game.write_bytes(b"MZ")
config = Path(tmp) / ".config/droiddeck/game-environment.json"
config.parent.mkdir(parents=True)
for inherited, edits, shared, expected in (({}, None, None, "2"), ({"vblank_mode": "0"}, None, None, "0"),
({}, {"vblank_mode": "1"}, None, "1"),
({}, None, {"vblank_mode": "1"}, "1")):
with self.subTest(inherited=inherited, edits=edits, shared=shared):
config.write_text(json.dumps({"version": 1, "shared": shared or {}, "games": {"1167630": edits or {}}}))
env = {name: value for name, value in os.environ.items() if name != "vblank_mode"}
env.update(HOME=tmp, STEAM_COMPAT_DATA_PATH="/compatdata/1167630", **inherited)
result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(probe), "waitforexitandrun", str(game)],
env=env, text=True, capture_output=True, check=True)
self.assertEqual(result.stdout.strip(), expected, result.stderr)
def test_windows_cpu_0_is_the_fastest_core(self):
os_mod = MODULE["os"]
pocket_fit = {0: 379, 1: 379, 2: 923, 3: 923, 4: 923, 5: 867, 6: 867, 7: 1024}
every = set(range(8))
with tempfile.TemporaryDirectory() as tmp:
proton = Path(tmp) / "proton"
proton.write_text('default_cpu_limit = {\n "19900" : "16", # Far Cry 2\n'
' "2229830" : "1", # Command & Conquer\n "11440" : "4", # DiRT\n'
' "1038250" : "P", # DIRT 5\n "618970" : 4, # Outcast, written as Proton 10 does\n'
'}\n\nother = {\n "620" : "2",\n}\n')
command = [str(proton), "waitforexitandrun", "/game/game.exe"]
cases = [
({}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"),
({"BL_GAME_CPUS": "6,7"}, {6, 7}, pocket_fit, "2:7,6", "2:7,6"),
({"DROIDDECK_GAME_CORES": "0-2"}, {0, 1, 2}, pocket_fit, "3:2,0,1", "3:2,0,1"),
({"BL_GAME_CPUS": "0-7"}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"),
({"WINE_CPU_TOPOLOGY": "2:0,1"}, every, pocket_fit, "2:0,1", "2:0,1"),
({"WINE_CPU_TOPOLOGY": ""}, every, pocket_fit, "", None),
({"PROTON_CPU_TOPOLOGY": "1:0"}, every, pocket_fit, None, "1:0"),
({"DROIDDECK_FASTEST_CORES": "0"}, every, pocket_fit, None, None),
({"SteamGameId": "2229830"}, every, pocket_fit, "1:7", "1:7"),
({"SteamGameId": "11440"}, every, pocket_fit, "4:7,2,3,4", "4:7,2,3,4"),
({"SteamGameId": "19900"}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"),
({"SteamGameId": "1038250"}, every, pocket_fit, None, None),
({"SteamGameId": "618970"}, every, pocket_fit, "4:7,2,3,4", "4:7,2,3,4"),
({"SteamGameId": "620"}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"),
({"SteamGameId": "2229830", "BL_GAME_CPUS": "0,1"}, {0, 1}, pocket_fit, "2:0,1", "2:0,1"),
({"SteamGameId": "2229830", "DROIDDECK_GAME_CORES": "12-13"}, every, pocket_fit, "1:7", "1:7"),
({"SteamGameId": "11440", "BL_GAME_CPUS": "0-7"}, every, pocket_fit, "4:7,2,3,4", "4:7,2,3,4"),
({}, every, {core: 100 for core in every}, None, None),
({}, every, None, None, None),
({"SteamGameId": "11440"}, every, None, "4:0,1,2,3", "4:0,1,2,3"),
({"BL_GAME_CPUS": "9"}, {9}, pocket_fit, None, None),
]
cases += [
({"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/0"}, every, pocket_fit, None, None),
({"STEAM_COMPAT_DATA_PATH": ""}, every, pocket_fit, None, None),
({"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/0", "BL_GAME_CPUS": "6,7"}, {6, 7}, pocket_fit, "2:6,7", "2:6,7"),
]
for env, affinity, speeds, expected, proton_topology in cases:
env.setdefault("STEAM_COMPAT_DATA_PATH", "/steam/compatdata/3608888694")
with mock.patch.dict(MODULE["cpu_topology"].__globals__, {"online_cores": lambda: every}), \
mock.patch.dict(MODULE["fastest_first"].__globals__, {"core_speeds": lambda cores, speeds=speeds: speeds}), \
mock.patch.object(os_mod, "sched_getaffinity", return_value=affinity, create=True):
MODULE["cpu_topology"](env, command)
self.assertEqual(env.get("WINE_CPU_TOPOLOGY"), expected, env)
self.assertEqual(env.get("PROTON_CPU_TOPOLOGY"), proton_topology, env)
self.assertIsNone(MODULE["proton_cpu_limit"](["/missing/proton"], {"SteamGameId": "11440"}))
self.assertIsNone(MODULE["proton_cpu_limit"](command, {}))
self.assertEqual(MODULE["proton_cpu_limit"](command, {"SteamGameId": "1038250"}), "P")
self.assertEqual(MODULE["proton_cpu_limit"](command, {"SteamGameId": "618970"}), 4)
def test_core_speeds_read_capacity_then_top_clock(self):
with tempfile.TemporaryDirectory() as tmp:
for core, capacity, clock in ((0, "379", "2265600"), (1, None, "3302400")):
folder = Path(tmp) / ("cpu%d" % core) / "cpufreq"
folder.mkdir(parents=True)
(folder / "cpuinfo_max_freq").write_text(clock + "\n")
if capacity:
(folder.parent / "cpu_capacity").write_text(capacity + "\n")
speeds = MODULE["core_speeds"]
self.assertEqual(speeds([0], tmp), {0: 379})
self.assertEqual(speeds([0, 1], tmp), {0: 2265600, 1: 3302400})
self.assertIsNone(speeds([0, 2], tmp))
def test_reskate_runs_at_feature_level_12_1(self):
with tempfile.TemporaryDirectory() as tmp:
reskate = Path(tmp) / "ReSkate"
reskate.mkdir()
for name in ("Skate.exe", "ReSkate.dll", "ReSkateLauncher.exe", "EAAntiCheat.GameServiceLauncher.exe"):
(reskate / name).touch()
for exe in ("ReSkateLauncher.exe", "Skate.exe"):
env, extra, notes = MODULE["engine_fixes"](str(reskate / exe), [])
self.assertEqual((env, extra), ({"VKD3D_FEATURE_LEVEL": "12_1"}, []), exe)
self.assertTrue(notes)
config = {"version": 1, "shared": {"VKD3D_FEATURE_LEVEL": "12_2"}}
self.assertEqual(MODULE["apply_config"]({}, config, "3354750", env)["VKD3D_FEATURE_LEVEL"], "12_1")
config["games"] = {"3354750": {"VKD3D_FEATURE_LEVEL": "12_2"}}
self.assertEqual(MODULE["apply_config"]({}, config, "3354750", env)["VKD3D_FEATURE_LEVEL"], "12_2")
skate = Path(tmp) / "Skate"
skate.mkdir()
for name in ("Skate.exe", "EAAntiCheat.GameServiceLauncher.exe"):
(skate / name).touch()
self.assertEqual(MODULE["engine_fixes"](str(skate / "EAAntiCheat.GameServiceLauncher.exe"), [])[:2], ({}, []))
def test_caravan_sandwitch_robocop_and_cyberpunk_run_at_feature_level_12_1(self):
with tempfile.TemporaryDirectory() as tmp:
caravan = Path(tmp) / "Caravan SandWitch"
shipping = caravan / "CaravanSandWitch/Binaries/Win64"
shipping.mkdir(parents=True)
for name in ("CaravanSandWitch.exe", ".droiddeck-launch.bat"):
(caravan / name).touch()
(shipping / "CaravanSandWitch-Win64-Shipping.exe").touch()
cyberpunk = Path(tmp) / "Cyberpunk 2077"
(cyberpunk / "bin/x64").mkdir(parents=True)
for name in ("REDprelauncher.exe", "bin/x64/Cyberpunk2077.exe"):
(cyberpunk / name).touch()
robocop = Path(tmp) / "RoboCop Rogue City"
(robocop / "RoboCop/Binaries/Win64").mkdir(parents=True)
for name in ("RoboCop.exe", "RoboCop/Binaries/Win64/RoboCop-Win64-Shipping.exe"):
(robocop / name).touch()
games = [caravan / "CaravanSandWitch.exe", caravan / ".droiddeck-launch.bat",
shipping / "CaravanSandWitch-Win64-Shipping.exe",
robocop / "RoboCop.exe", robocop / "RoboCop/Binaries/Win64/RoboCop-Win64-Shipping.exe",
cyberpunk / "REDprelauncher.exe", cyberpunk / "bin/x64/Cyberpunk2077.exe"]
for exe in games:
env, extra, notes = MODULE["engine_fixes"](str(exe), [])
self.assertEqual((env, extra), ({"VKD3D_FEATURE_LEVEL": "12_1"}, []), exe)
self.assertTrue(notes)
config = {"version": 1, "shared": {"VKD3D_FEATURE_LEVEL": "12_2"}}
self.assertEqual(MODULE["apply_config"]({}, config, "1582650", env)["VKD3D_FEATURE_LEVEL"], "12_1")
config["games"] = {"1582650": {"VKD3D_FEATURE_LEVEL": "12_2"}}
self.assertEqual(MODULE["apply_config"]({}, config, "1582650", env)["VKD3D_FEATURE_LEVEL"], "12_2")
other = Path(tmp) / "Other/Binaries/Win64"
other.mkdir(parents=True)
(other / "Other-Win64-Shipping.exe").touch()
self.assertEqual(MODULE["engine_fixes"](str(other / "Other-Win64-Shipping.exe"), []), ({}, [], []))
witcher = Path(tmp) / "The Witcher 3"
witcher.mkdir()
(witcher / "REDprelauncher.exe").touch()
self.assertNotIn("VKD3D_FEATURE_LEVEL", MODULE["engine_fixes"](str(witcher / "REDprelauncher.exe"), [])[0])
def test_unity_players_keep_tso_over_the_preset(self):
with tempfile.TemporaryDirectory() as tmp:
def game(*names):
for name in names:
(Path(tmp) / name).parent.mkdir(parents=True, exist_ok=True)
(Path(tmp) / name).touch()
return str(Path(tmp) / names[0])
il2cpp = game("Megabonk/Megabonk.x86_64", "Megabonk/UnityPlayer.so", "Megabonk/GameAssembly.so")
mono = game("Mono/Game.x86_64", "Mono/UnityPlayer.so")
windows = game("Crab Game/Crab Game.exe", "Crab Game/UnityPlayer.dll", "Crab Game/GameAssembly.dll")
windows_mono = game("RoR2/Risk of Rain 2.exe", "RoR2/UnityPlayer.dll", "RoR2/MonoBleedingEdge/mono-2.0-bdwgc.dll")
for exe in (il2cpp, mono, windows, windows_mono):
env, extra, notes = MODULE["engine_fixes"](exe, [])
self.assertEqual((env, extra, notes), ({"FEX_TSOENABLED": "1"}, [], ["Unity: FEX with TSO"]))
performance = {"version": 1, "shared": {"FEX_TSOENABLED": "0", "FEX_VECTORTSOENABLED": "0"}}
result = MODULE["apply_config"]({}, performance, "3405340", env)
self.assertEqual((result["FEX_TSOENABLED"], result["FEX_VECTORTSOENABLED"]), ("1", "0"))
performance["games"] = {"3405340": {"FEX_TSOENABLED": "0"}}
self.assertEqual(MODULE["apply_config"]({}, performance, "3405340", env)["FEX_TSOENABLED"], "0")
def test_a_new_fix_only_needs_registering(self):
with tempfile.TemporaryDirectory() as tmp:
(Path(tmp) / "Game.exe").touch()
@@ -599,6 +1385,36 @@ class GameEnvironmentTest(unittest.TestCase):
subprocess.run(["bash", "-n"], input=script, text=True, check=True)
self.assertIn('bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', script)
def test_both_proton_wrappers_answer_steams_driver_query_without_wine(self):
scripts = (COMPAT["launcher_sh"](), COMPAT["extra_wrapper_sh"]("proton"))
query = "/root/.local/share/Steam/steamrtarm64/../bin/d3ddriverquery64.exe"
probe = "/root/.local/share/Steam/steamapps/compatdata/0"
for script in scripts:
subprocess.run(["bash", "-n"], input=script, text=True, check=True)
self.assertLess(script.index('case "$verb:${1##*/}:'), script.index("bl_components "))
for args, prefix in (([query, "-d3d12"], probe), ([query], probe + "/"),
([query.replace("64.exe", ".exe")], probe)):
result = subprocess.run(["bash", "-s", "run"] + args, input=script, text=True, capture_output=True,
env={"PATH": os.defpath, "HOME": "/nonexistent", "STEAM_COMPAT_DATA_PATH": prefix},
timeout=10)
self.assertEqual(result.returncode, 1, result.stderr)
self.assertEqual(result.stdout, "")
self.assertIn("D3D driver query is not run", result.stderr)
skip = COMPAT["BL_DRIVER_QUERY_SKIP"]
for verb, args, prefix in (
("getcompatpath", [query], probe),
("run", [query], "/root/.local/share/Steam/steamapps/compatdata/2276548447"),
("run", [query], None),
("waitforexitandrun", ["/games/AIO/AIO-Graphics-Test-64bit.exe", "--cube", "dx11"], probe),
("run", ["/games/Tool/tool.exe", query], probe),
("run", ["/games/d3ddriverquery64.exe.bak"], probe)):
env = {"PATH": os.defpath}
if prefix is not None:
env["STEAM_COMPAT_DATA_PATH"] = prefix
out = subprocess.run(["bash", "-c", "verb=" + verb + "\n" + skip + "echo continued", "_"] + args,
env=env, capture_output=True, text=True, check=True).stdout
self.assertEqual(out, "continued\n", (verb, args, prefix))
def test_wrappers_preload_the_session_library_before_the_input_shim(self):
overlay = "/root/.local/share/Steam/ubuntu12_64/gameoverlayrenderer.so"
with tempfile.TemporaryDirectory() as tmp:
@@ -1010,11 +1826,11 @@ class DirectAudioPrefixTest(unittest.TestCase):
(self.windows / folder).mkdir(parents=True)
self.reg = self.compat / "pfx/user.reg"
def run_setup(self, verb="waitforexitandrun", target="game.exe", cwd=None):
script = COMPAT["BL_DIRECTAUDIO_SETUP"] + '\nverb=$2\nbl_directaudio "$1" "$3"\necho "WINEDLLPATH=$WINEDLLPATH"\necho "WINE_AUDIO_DRIVER=${WINE_AUDIO_DRIVER:-}"\n'
def run_setup(self, verb="waitforexitandrun", target="game.exe", cwd=None, extra=None):
script = COMPAT["BL_DIRECTAUDIO_SETUP"] + '\nverb=$2\nbl_directaudio "$1" "$3"\necho "WINEDLLPATH=$WINEDLLPATH"\necho "WINE_AUDIO_DRIVER=${WINE_AUDIO_DRIVER:-}"\nenv | grep "^BANNER_AUDIO_DIRECT_" | sort\n'
steam = self.compat.parent / "Steam"
env = dict(os.environ, BL_DIRECTAUDIO=str(self.sets), STEAM_COMPAT_DATA_PATH=str(self.compat),
STEAM_COMPAT_CLIENT_INSTALL_PATH=str(steam))
STEAM_COMPAT_CLIENT_INSTALL_PATH=str(steam), **(extra or {}))
env.pop("WINEDLLPATH", None)
env.pop("WINE_AUDIO_DRIVER", None)
return subprocess.run(["bash", "-c", script, "bash", str(self.tool), verb, target], cwd=cwd, env=env,
@@ -1062,6 +1878,21 @@ class DirectAudioPrefixTest(unittest.TestCase):
self.assertIn("DirectAudio selected", result.stderr)
self.assertEqual(self.link("system32"), str(self.audio / "lib/wine/aarch64-windows/winedirectaudio.drv"))
self.assertEqual(self.audio_values(), [("[Software\\\\Wine\\\\Drivers]", '"Audio"="directaudio,pulse"')])
def test_audio_direct_settings_reach_the_driver_under_its_own_names(self):
self.reg.write_text("WINE REGISTRY Version 2\n\n#arch=win64\n")
result = self.run_setup(extra={"AUDIO_DIRECT_MS": "60", "AUDIO_DIRECT_PERIOD_MS": "20",
"AUDIO_DIRECT_RELAY": "/run/relay.sock", "OTHER_AUDIO_DIRECT_MS": "5"})
lines = [line for line in result.stdout.splitlines() if line.startswith("BANNER_")]
self.assertEqual(lines, ["BANNER_AUDIO_DIRECT_MS=60", "BANNER_AUDIO_DIRECT_PERIOD_MS=20",
"BANNER_AUDIO_DIRECT_RELAY=/run/relay.sock"])
def test_audio_direct_settings_are_not_handed_on_when_directaudio_stays_off(self):
legacycompat = self.compat.parent / "Steam/legacycompat"
legacycompat.mkdir(parents=True)
(legacycompat / "iscriptevaluator.exe").write_bytes(b"Steam install-script evaluator")
result = self.run_setup(target="iscriptevaluator.exe", cwd=legacycompat, extra={"AUDIO_DIRECT_MS": "60"})
self.assertNotIn("BANNER_AUDIO_DIRECT_MS", result.stdout)
def test_non_launch_verbs_preserve_audio_configuration(self):
for verb in ("getcompatpath", "getnativepath", "destroyprefix"):
with self.subTest(verb=verb):
@@ -1163,6 +1994,52 @@ class DirectAudioPrefixTest(unittest.TestCase):
self.assertIsNone(self.link("system32"))
class UbisoftOverlayTest(unittest.TestCase):
SETTINGS = (
"language:\n code: en-US\n"
"overlay:\n enabled: true\n forceunhookgame: false\n fps_enabled: false\n warning_enabled: true\n"
"patchTrigger:\n source: USER_RESTART\n"
)
def test_the_overlay_and_its_prompt_are_turned_off_and_the_rest_is_kept(self):
got = MODULE["overlay_off"](self.SETTINGS)
self.assertEqual(got, self.SETTINGS.replace("enabled: true", "enabled: false"))
self.assertEqual(MODULE["overlay_off"](got), got)
def test_missing_keys_or_section_are_added(self):
self.assertEqual(MODULE["overlay_off"]("overlay:\n fps_enabled: true\nmisc:\n a: 1\n"),
"overlay:\n fps_enabled: true\n enabled: false\n warning_enabled: false\nmisc:\n a: 1\n")
self.assertEqual(MODULE["overlay_off"](""), "overlay:\n enabled: false\n warning_enabled: false\n")
def test_blank_lines_inside_the_overlay_section_do_not_end_it(self):
text = "overlay:\n enabled: true\n\n warning_enabled: true\n\nmisc:\n a: 1\n"
self.assertEqual(MODULE["overlay_off"](text), text.replace("true", "false"))
self.assertEqual(MODULE["overlay_off"]("overlay:\n fps_enabled: true\n\nmisc:\n a: 1\n"),
"overlay:\n fps_enabled: true\n enabled: false\n warning_enabled: false\n\nmisc:\n a: 1\n")
def test_an_annotated_overlay_header_is_the_same_section(self):
self.assertEqual(MODULE["overlay_off"]("overlay: # in-game\n enabled: true\n"),
"overlay: # in-game\n enabled: false\n warning_enabled: false\n")
def test_only_prefixes_with_ubisoft_connect_are_touched(self):
with tempfile.TemporaryDirectory() as tmp:
prefix = Path(tmp) / "447040/pfx"
env = {"STEAM_COMPAT_DATA_PATH": str(prefix.parent)}
settings = prefix / MODULE["UBISOFT_SETTINGS"]
prefix.mkdir(parents=True)
MODULE["ubisoft_overlay_off"](env)
self.assertFalse(settings.exists())
(prefix / MODULE["UBISOFT_LAUNCHER"]).mkdir(parents=True)
MODULE["ubisoft_overlay_off"](env)
self.assertEqual(settings.read_text(), "overlay:\n enabled: false\n warning_enabled: false\n")
settings.write_text(self.SETTINGS)
settings.chmod(0o644)
MODULE["ubisoft_overlay_off"](env)
self.assertEqual(settings.read_text(), self.SETTINGS.replace("enabled: true", "enabled: false"))
self.assertEqual(settings.stat().st_mode & 0o777, 0o644)
self.assertEqual(sorted(p.name for p in settings.parent.iterdir()), ["settings.yaml"])
if __name__ == "__main__":
unittest.main()
-3
View File
@@ -106,9 +106,6 @@ def main():
surface = alloc()
send(comp, 0, U(surface))
if not args.unassigned_control:
# wlroots 0.20.2 assigns the cursor role before forwarding this serial
# to Gamescope, so zero avoids needing pointer focus but still tests the
# role guard in xwayland_surface_commit.
send(pointer, 0, U(0, surface, 0, 0))
roundtrip()
before = count()
+216
View File
@@ -0,0 +1,216 @@
import contextlib
import concurrent.futures
import errno
import json
import os
from pathlib import Path
import runpy
import subprocess
import sys
import stat
import tempfile
import unittest
from unittest import mock
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
G = runpy.run_path(str(BIN / "droiddeck-gog-maintenance"))["main"].__globals__
class GogMaintenance(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.folder = Path(self.temp.name)
self.control = G["control"](self.folder)
(self.folder / ".droiddeck-store.json").write_text(json.dumps({"store": "gog", "id": "1"}))
def test_only_downloaded_windows_executables_can_be_patches(self):
download = self.control / "downloads"
download.mkdir()
patch = download / "patch.exe"
patch.write_bytes(b"MZtest")
self.assertEqual(patch, G["validate_installer"](self.folder, patch))
outside = self.folder / "outside.exe"
outside.write_bytes(b"MZtest")
with self.assertRaises(ValueError):
G["validate_installer"](self.folder, outside)
patch.write_bytes(b"not PE")
with self.assertRaises(ValueError):
G["validate_installer"](self.folder, patch)
def test_overlapping_metadata_writes_publish_complete_documents(self):
target = self.control / "operation.json"
def publish(value):
G["write"](target, {"value": value, "payload": str(value) * 10000})
result = G["read"](target)
self.assertEqual(str(result["value"]) * 10000, result["payload"])
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
list(pool.map(publish, range(32)))
self.assertEqual([], list(self.control.glob("*.new")))
def test_directory_sync_unsupported_by_external_storage_keeps_the_record(self):
original = os.fsync
def fsync(fd):
if stat.S_ISDIR(os.fstat(fd).st_mode):
raise OSError(errno.EINVAL, "directory sync unsupported")
return original(fd)
target = self.control / "operation.json"
with mock.patch.object(G["os"], "fsync", side_effect=fsync):
G["write"](target, {"phase": "download"})
self.assertEqual("download", G["read"](target)["phase"])
def test_unsuccessful_registry_preparation_never_runs_the_installer(self):
download = self.control / "downloads"; download.mkdir()
patch = download / "patch.exe"; patch.write_bytes(b"MZtest")
prefix = self.folder / "prefix/pfx"
(prefix / "drive_c/windows/system32").mkdir(parents=True)
(prefix / "drive_c/windows/system32/reg.exe").write_bytes(b"MZreg")
(prefix / "system.reg").write_text("registry")
for result in ("timeout", "import-failed", "no-proton", "none", "unexpected"):
with self.subTest(result=result):
G["write"](self.control / "operation.json", {"phase": "patch-starting", "before": {"id": "1"}})
def setup_main():
pass
with mock.patch.dict(setup_main.__globals__, {"apply_gog_registry": lambda *a, **k: result}), \
mock.patch.dict(G, {"runner": lambda *a: ("/proton", {}, str(self.folder / "prefix")), "active": lambda *a: []}), \
mock.patch.object(G["runpy"], "run_path", return_value={"main": setup_main}), \
mock.patch.object(G["subprocess"], "Popen") as spawn:
self.assertEqual(1, G["run_patch"](str(self.folder), "1", str(patch), "game.exe"))
spawn.assert_not_called()
self.assertIn("registry preparation failed", G["read"](self.control / "patch-status.json")["reason"])
def test_only_recognized_inno_installers_get_the_gog_ui_compatibility_argument(self):
path = self.folder / "patch.exe"
path.write_bytes(b"MZunknown installer")
self.assertEqual([], G["installer_arguments"](path))
path.write_bytes(b"MZdata Inno Setup Setup Data (6.2.1)")
self.assertEqual(["/NOGUI"], G["installer_arguments"](path))
def test_game_launch_lease_excludes_updater_and_releases_on_owner_exit(self):
import fcntl
owner = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])
self.addCleanup(lambda: owner.poll() is None and owner.kill())
lease = subprocess.Popen([sys.executable, str(BIN / "droiddeck-gog-maintenance"), "lease", str(self.folder), str(owner.pid)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.addCleanup(lease.stdout.close)
self.addCleanup(lease.stderr.close)
self.addCleanup(lambda: lease.poll() is None and lease.kill())
self.assertEqual(b"ready\n", lease.stdout.readline())
with G["lock_file"](self.control) as stream:
with self.assertRaises(BlockingIOError):
fcntl.lockf(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
owner.terminate(); owner.wait()
lease.wait(timeout=5)
with G["lock_file"](self.control) as stream:
fcntl.lockf(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
def test_incomplete_update_cannot_launch(self):
(self.folder / ".droiddeck-store.json").write_text(json.dumps({"store": "gog", "id": "1", "state": "updating"}))
with self.assertRaises(ValueError):
G["lease"](self.folder, os.getpid())
def test_running_game_and_wrong_prefix_are_distinguished(self):
proc = self.folder / "proc"
for pid, prefix, name in [(10, "/prefix/pfx", "game.exe"), (11, "/other/pfx", "other.exe"), (12, "/prefix/pfx", "services.exe")]:
row = proc / str(pid); row.mkdir(parents=True)
(row / "environ").write_bytes(b"WINEPREFIX=" + prefix.encode() + b"\0")
(row / "cmdline").write_bytes(name.encode() + b"\0")
self.assertEqual([10], G["active"]("/prefix", proc))
def test_desktop_installer_inherits_the_nested_display_not_the_bridge_display(self):
proc = self.folder / "proc"
row = proc / "10"; row.mkdir(parents=True)
(row / "cmdline").write_bytes(b"/usr/bin/plasmashell\0")
(row / "environ").write_bytes(b"BL_DESKTOP=1\0XDG_RUNTIME_DIR=/run/session\0WAYLAND_DISPLAY=wayland-1\0DISPLAY=:0\0XAUTHORITY=/run/session/auth\0UNRELATED=value\0")
env = G["desktop_environment"]({"DROIDDECK_GOG_DESKTOP": "1", "XDG_RUNTIME_DIR": "/run/session", "WAYLAND_DISPLAY": "wayland-0"}, proc)
self.assertEqual("wayland-1", env["WAYLAND_DISPLAY"])
self.assertEqual(":0", env["DISPLAY"])
self.assertEqual("/run/session/auth", env["XAUTHORITY"])
self.assertEqual("1", env["BL_DESKTOP"])
self.assertNotIn("UNRELATED", env)
def test_maintenance_uses_full_x87_precision_without_changing_game_preferences(self):
target = mock.Mock(compatdata="/selected/prefix", appid="2526420341")
run = {"tools": lambda root: {"selected": ("/selected/proton", "proton")},
"mapping": lambda root: {}, "choose_tool": lambda *args: ("selected", None),
"environment": lambda env, *args, **kwargs: dict(env)}
with mock.patch.dict(G, {"target_for": lambda *args: (run, "/steam", target)}), \
mock.patch.dict(os.environ, {"FEX_X87REDUCEDPRECISION": "1"}):
command, env, prefix = G["runner"]("/game", "2526420341", "/game/game.exe")
self.assertEqual("/selected/proton/proton", command)
self.assertEqual("/selected/prefix", prefix)
self.assertEqual("0", env["FEX_X87REDUCEDPRECISION"])
self.assertEqual("1", os.environ["FEX_X87REDUCEDPRECISION"])
def test_legacy_setup_uses_authentic_timestamp_and_support_layout(self):
program = self.folder / "support/1/setup.exe"
program.parent.mkdir(parents=True); program.write_bytes(b"MZtest")
record = {"id": "1", "build": "3301", "products": ["1"], "language": "en-US", "manifest": {"product": {
"timestamp": 31847977, "support_commands": [{"gameID": "1", "executable": "/setup.exe", "languages": ["English"]}]}}}
G["write"](self.control / "installed.json", {"verified": False})
G["write"](self.control / "operation.json", {"before": record})
G["write"](self.folder / "goggame-1.info", {"language": "English"})
with mock.patch.object(G["subprocess"], "run", return_value=mock.Mock(returncode=0)) as run:
G["prepare_setup"](str(self.folder), "/proton", {})
args = run.call_args.args[0]
self.assertEqual(str(program), args[2])
self.assertIn("/buildId=31847977", args)
self.assertNotIn("/buildId=3301", args)
self.assertIn("/Language=English", args)
def test_interrupted_patch_is_not_started_again(self):
(self.control / "operation.json").write_text(json.dumps({"phase": "patch-applying"}))
with mock.patch.dict(G, {"validate_installer": lambda *a: self.folder / "patch.exe"}):
with self.assertRaises(ValueError):
G["start"](str(self.folder), "2147483666", "patch.exe", "game.exe")
def test_dead_installer_helper_requires_recovery_without_replay(self):
G["write"](self.control / "operation.json", {"phase": "patch-applying"})
G["write"](self.control / "patch-status.json", {"state": "running", "pid": 99999999, "generation": "123"})
with mock.patch.dict(G, {"generation": lambda pid: None}), mock.patch.object(G["subprocess"], "Popen") as spawn:
self.assertEqual(0, G["status"](str(self.folder)))
self.assertFalse(spawn.called)
self.assertEqual("uncertain", G["read"](self.control / "patch-status.json")["state"])
self.assertEqual("patch-uncertain", G["read"](self.control / "operation.json")["phase"])
def test_abandoned_start_and_confirmed_completion_are_distinct(self):
G["write"](self.control / "operation.json", {"phase": "patch-starting"})
G["write"](self.control / "patch-status.json", {"state": "starting", "startedAt": 10})
with mock.patch.object(G["time"], "time", return_value=100):
G["status"](str(self.folder))
self.assertEqual("uncertain", G["read"](self.control / "patch-status.json")["state"])
G["write"](self.control / "patch-status.json", {"state": "done", "exitCode": 0})
G["status"](str(self.folder))
self.assertEqual({"state": "done", "exitCode": 0}, G["read"](self.control / "patch-status.json"))
def test_registry_and_installer_use_same_prefix_without_cloud_launch(self):
download = self.control / "downloads"; download.mkdir()
patch = download / "patch.exe"; patch.write_bytes(b"MZtest")
prefix = self.folder / "prefix/pfx"
(prefix / "drive_c/windows/system32").mkdir(parents=True)
(prefix / "drive_c/windows/system32/reg.exe").write_bytes(b"MZreg")
(prefix / "system.reg").write_text("registry")
G["write"](self.control / "operation.json", {"phase": "patch-starting"})
calls = []
setup = {"main": lambda: None}
def setup_main():
pass
setup_main.__globals__["apply_gog_registry"] = lambda *a, **k: calls.append(("registry", a[1])) or "written=1"
fake = mock.Mock(); fake.wait.return_value = 0
def spawn(argv, **kwargs):
self.assertEqual("run", argv[1])
self.assertEqual("1", kwargs["env"]["DROIDDECK_GOG_MAINTENANCE"])
calls.append(("patch", kwargs["env"]["STEAM_COMPAT_DATA_PATH"]))
return fake
with mock.patch.dict(G, {"runner": lambda *a: ("/proton", {"STEAM_COMPAT_DATA_PATH": str(self.folder / "prefix"), "DROIDDECK_GOG_MAINTENANCE": "1"}, str(self.folder / "prefix")),
"active": lambda *a: []}), \
mock.patch.object(G["runpy"], "run_path", return_value={"main": setup_main}), \
mock.patch.object(G["subprocess"], "Popen", side_effect=spawn):
self.assertEqual(0, G["run_patch"](str(self.folder), "2147483666", str(patch), "game.exe"))
self.assertEqual([("registry", str(self.folder / "prefix")), ("patch", str(self.folder / "prefix"))], calls)
self.assertEqual("done", G["read"](self.control / "patch-status.json")["state"])
self.assertEqual("updating", G["read"](self.folder / ".droiddeck-store.json")["state"])
if __name__ == "__main__":
unittest.main()
+188
View File
@@ -0,0 +1,188 @@
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "../../app/src/main/cpp/adrenotools/src/hook/kgsl.h"
#include "../../app/src/main/cpp/waylandcomp/src/gpu_clock_pin.h"
enum { NONE, OPEN, DEVICE, CONTEXT, CONSTRAINT, MEMORY, MAP, SYNC, SUBMIT, THREAD, WAIT };
static atomic_int failure, descriptors, contexts, memories, mappings, submissions, waits;
static atomic_bool timeout_wait;
static bool legacy_core;
static uint32_t commands[1024];
int __android_log_print(int priority, const char *tag, const char *format, ...) {
(void)priority; (void)tag; (void)format;
return 0;
}
int __wrap_open(const char *path, int flags, ...) {
(void)flags;
assert(!strcmp(path, "/dev/kgsl-3d0"));
if (atomic_load(&failure) == OPEN) { errno = EACCES; return -1; }
assert(atomic_fetch_add(&descriptors, 1) == 0);
return 91;
}
int __wrap_close(int fd) {
assert(fd == 91);
assert(atomic_fetch_sub(&descriptors, 1) == 1);
errno = EBADF;
return 0;
}
void *__wrap_mmap(void *address, size_t size, int protection, int flags, int fd, off_t offset) {
(void)address; (void)protection; (void)flags;
assert(fd == 91 && size == sizeof(commands) && offset == (23 << 12));
if (atomic_load(&failure) == MAP) { errno = EACCES; return MAP_FAILED; }
assert(atomic_fetch_add(&mappings, 1) == 0);
return commands;
}
int __wrap_munmap(void *address, size_t size) {
assert(address == commands && size == sizeof(commands));
assert(atomic_fetch_sub(&mappings, 1) == 1);
return 0;
}
#ifdef __ANDROID__
int __wrap_ioctl(int fd, int request, ...) {
#else
int __wrap_ioctl(int fd, unsigned long request, ...) {
#endif
assert(fd == 91);
va_list args;
va_start(args, request);
void *arg = va_arg(args, void *);
va_end(args);
int step = NONE;
switch ((unsigned int)request) {
case IOCTL_KGSL_DEVICE_GETPROPERTY: step = DEVICE; break;
case IOCTL_KGSL_DRAWCTXT_CREATE: step = CONTEXT; break;
case IOCTL_KGSL_SETPROPERTY: step = CONSTRAINT; break;
case IOCTL_KGSL_GPUMEM_ALLOC_ID: step = MEMORY; break;
case IOCTL_KGSL_GPUMEM_SYNC_CACHE: step = SYNC; break;
case IOCTL_KGSL_GPU_COMMAND: step = SUBMIT; break;
case IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID: step = WAIT; break;
}
if (step && atomic_load(&failure) == step) { errno = EACCES; return -1; }
switch ((unsigned int)request) {
case IOCTL_KGSL_DEVICE_GETPROPERTY: {
struct kgsl_device_getproperty *property = arg;
assert(property->type == KGSL_PROP_DEVICE_INFO);
((struct kgsl_devinfo *)property->value)->chip_id = legacy_core ? 0x04030000 : 0x07030000;
break;
}
case IOCTL_KGSL_DRAWCTXT_CREATE: {
struct kgsl_drawctxt_create *context = arg;
assert(context->flags & KGSL_CONTEXT_PWR_CONSTRAINT);
context->drawctxt_id = 17;
atomic_fetch_add(&contexts, 1);
break;
}
case IOCTL_KGSL_SETPROPERTY: {
struct kgsl_device_getproperty *property = arg;
assert(property->type == KGSL_PROP_PWR_CONSTRAINT);
struct kgsl_device_constraint *constraint = property->value;
assert(constraint->context_id == 17 && constraint->type == KGSL_CONSTRAINT_PWRLEVEL);
assert(((struct kgsl_device_constraint_pwrlevel *)constraint->data)->level == KGSL_CONSTRAINT_PWR_MAX);
break;
}
case IOCTL_KGSL_GPUMEM_ALLOC_ID: {
struct kgsl_gpumem_alloc_id *memory = arg;
memory->id = 23; memory->gpuaddr = 0x100000; memory->mmapsize = sizeof(commands);
atomic_fetch_add(&memories, 1);
break;
}
case IOCTL_KGSL_GPUMEM_SYNC_CACHE:
assert(((struct kgsl_gpumem_sync_cache *)arg)->op == KGSL_GPUMEM_CACHE_CLEAN);
break;
case IOCTL_KGSL_GPU_COMMAND: {
struct kgsl_gpu_command *command = arg;
struct kgsl_command_object *object = (void *)(uintptr_t)command->cmdlist;
assert(command->context_id == 17 && command->numcmds == 1);
assert(command->flags & KGSL_CMDBATCH_PWR_CONSTRAINT);
assert(object->id == 23 && object->gpuaddr == 0x100000 && object->size == 8);
assert(commands[0] == (legacy_core ? 0xc0001000 : 0x70100001) && commands[1] == 0);
command->timestamp = atomic_fetch_add(&submissions, 1) + 1;
break;
}
case IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID:
atomic_fetch_add(&waits, 1);
if (atomic_load(&timeout_wait)) { errno = ETIMEDOUT; return -1; }
break;
case IOCTL_KGSL_DRAWCTXT_DESTROY:
assert(atomic_fetch_sub(&contexts, 1) == 1);
break;
case IOCTL_KGSL_GPUMEM_FREE_ID:
assert(atomic_fetch_sub(&memories, 1) == 1);
break;
default: assert(!"unexpected ioctl");
}
return 0;
}
int __real_pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg) {
if (atomic_load(&failure) == THREAD) return EAGAIN;
return __real_pthread_create(thread, attr, start, arg);
}
static void assert_released(void) {
assert(!atomic_load(&descriptors) && !atomic_load(&contexts));
assert(!atomic_load(&memories) && !atomic_load(&mappings));
}
int main(void) {
assert(gpu_clock_pin_set(false));
assert_released();
legacy_core = true;
assert(gpu_clock_pin_set(true));
assert(gpu_clock_pin_set(false));
assert_released();
legacy_core = false;
for (int step = OPEN; step <= THREAD; ++step) {
atomic_store(&failure, step);
assert(!gpu_clock_pin_set(true));
assert(errno == (step == THREAD ? EAGAIN : EACCES));
assert_released();
}
atomic_store(&failure, NONE);
atomic_store(&submissions, 0);
assert(gpu_clock_pin_set(true));
assert(gpu_clock_pin_set(true));
for (int i = 0; i < 100 && atomic_load(&submissions) < 2; ++i) usleep(10000);
assert(atomic_load(&submissions) > 1);
assert(gpu_clock_pin_set(false));
assert(gpu_clock_pin_set(false));
assert_released();
atomic_store(&timeout_wait, true);
atomic_store(&submissions, 0);
atomic_store(&waits, 0);
assert(gpu_clock_pin_set(true));
for (int i = 0; i < 100 && atomic_load(&waits) < 2; ++i) usleep(10000);
assert(atomic_load(&waits) >= 2);
assert(atomic_load(&submissions) == 1);
assert(gpu_clock_pin_set(false));
assert_released();
atomic_store(&timeout_wait, false);
atomic_store(&failure, WAIT);
assert(gpu_clock_pin_set(true));
for (int i = 0; i < 100 && atomic_load(&descriptors); ++i) usleep(10000);
assert_released();
atomic_store(&failure, NONE);
assert(gpu_clock_pin_set(true));
assert(gpu_clock_pin_set(false));
assert_released();
puts("gpu clock pin: failure cleanup, renewal, bounded queue and restart passed");
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
import pathlib
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[2]
class GpuClockPinTest(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith('linux') and shutil.which('cc'), 'requires a Linux C compiler')
def test_constraint_lifecycle(self):
with tempfile.TemporaryDirectory() as tmp:
directory = pathlib.Path(tmp)
(directory / 'android').mkdir()
(directory / 'android/log.h').write_text(
'#define ANDROID_LOG_WARN 5\n'
'int __android_log_print(int, const char *, const char *, ...);\n')
binary = directory / 'gpu-clock-pin-test'
subprocess.run([
'cc', '-std=c11', '-D_DEFAULT_SOURCE', '-D__user=', '-Wall', '-Wextra', '-Werror',
'-I', str(directory), '-pthread',
str(ROOT / 'tools/tests/test_gpu_clock_pin.c'),
str(ROOT / 'app/src/main/cpp/waylandcomp/src/gpu_clock_pin.c'),
'-Wl,--wrap=open,--wrap=close,--wrap=mmap,--wrap=munmap,--wrap=ioctl,--wrap=pthread_create',
'-o', str(binary),
], check=True)
subprocess.run([str(binary)], check=True, timeout=10)
+136
View File
@@ -0,0 +1,136 @@
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
PATCH = ROOT / "tools/gamescope/patches/0125-droiddeck-hud-frame-times.patch"
READER = ROOT / "app/src/main/cpp/waylandcomp/src/hudframes.c"
def patch_writer():
added = [line[1:] for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")]
text = "\n".join(added)
start = text.index("static constexpr uint32_t k_uDroidDeckHudFramesMagic")
end = text.index("\n}\n", text.index("static void publish_droiddeck_hud_info()", start)) + 3
return text[start:end]
PREAMBLE = r'''
#include <assert.h>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <memory>
#include <mutex>
#include <string>
#include <sys/mman.h>
#include <unistd.h>
#include <unordered_map>
extern "C" {
#include "hudframes.h"
}
struct steamcompmgr_win_t { int appID; };
struct global_focus_t {
uint64_t ulBasePlaneCommitID = 0;
pid_t nFocusWindowPid = 0;
std::shared_ptr<std::string> pFocusWindowEngine;
steamcompmgr_win_t *inputFocusWindow = nullptr;
};
static std::unordered_map<uint64_t, global_focus_t> g_VirtualConnectorFocuses;
static global_focus_t *GetCurrentFocus() { return nullptr; }
static bool window_is_steam(steamcompmgr_win_t *w) { return w && w->appID == 769; }
static uint64_t g_now = 1000000000ull;
static uint64_t get_time_in_nanos() { return g_now; }
static uint32_t g_nOutputWidth = 1280, g_nOutputHeight = 720;
static int g_nOutputRefresh = 144000;
struct Log {
void infof(const char *, ...) {}
void errorf_errno(const char *fmt, ...) { va_list a; va_start(a, fmt); vfprintf(stderr, fmt, a); va_end(a); }
} xwm_log;
'''
MAIN = r'''
static void frame(uint64_t step) { g_now += step; droiddeck_hud_app_frame(g_now); }
int main(int argc, char **argv) {
setenv("DROIDDECK_HUD_FRAMES", argv[1], 1);
assert(droiddeck_hud_frames_wanted());
global_focus_t &focus = g_VirtualConnectorFocuses[0];
focus.nFocusWindowPid = 4242;
focus.pFocusWindowEngine = std::make_shared<std::string>("DXVK");
steamcompmgr_win_t game{ 12345 };
focus.inputFocusWindow = &game;
publish_droiddeck_hud_info();
droiddeck_hud_app_frame(g_now);
struct hud_frames *h = hud_frames_open(argv[1]);
assert(h);
int64_t ft[2048], tm[2048];
assert(hud_frames_poll(h, ft, tm, 2048) == 0);
const uint64_t steps[] = { 2700000, 2800000, 41600000, 6944000 };
for (uint64_t step : steps) frame(step);
int n = hud_frames_poll(h, ft, tm, 2048);
assert(n == 4);
for (int i = 0; i < 4; i++) assert((uint64_t)ft[i] == steps[i]);
assert((uint64_t)tm[3] == g_now);
assert(hud_frames_poll(h, ft, tm, 2048) == 0);
int32_t info[5];
char engine[41];
assert(hud_frames_info(h, info, engine, sizeof(engine)));
assert(info[0] == 4242 && info[1] == 1280 && info[2] == 720 && info[3] == 144000 && info[4] == 0);
assert(strcmp(engine, "DXVK") == 0);
game.appID = 769;
publish_droiddeck_hud_info();
assert(hud_frames_info(h, info, engine, sizeof(engine)) && info[4] == 1);
for (int i = 0; i < 3000; i++) frame(1000000 + i);
n = hud_frames_poll(h, ft, tm, 2048);
assert(n == 1024);
assert(ft[1023] == 1000000 + 2999);
assert(ft[0] == 1000000 + 2999 - 1023);
for (int i = 0; i < 10; i++) frame(2000000);
assert(hud_frames_poll(h, ft, tm, 4) == 4 && ft[3] == 2000000);
hud_frames_close(h);
puts("ok");
return 0;
}
'''
@unittest.skipUnless(shutil.which("cc") and shutil.which("c++"), "needs a C and C++ compiler")
class HudFramesTest(unittest.TestCase):
def test_gamescope_writer_and_app_reader_agree(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
source = tmp / "writer.cpp"
source.write_text(PREAMBLE + patch_writer() + MAIN)
include = READER.parent
subprocess.run(["cc", "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-c", str(READER), "-o", str(tmp / "reader.o")], check=True)
subprocess.run(["c++", "-std=c++20", "-Wall", "-Werror", "-Wno-unused-function", f"-I{include}", str(source), str(tmp / "reader.o"),
"-o", str(tmp / "hudframes")], check=True)
result = subprocess.run([str(tmp / "hudframes"), str(tmp / "frames")], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.strip(), "ok")
self.assertEqual(os.path.getsize(tmp / "frames"), 128 + 1024 * 16)
def test_session_points_gamescope_at_the_file_the_app_reads(self):
session = (ROOT / "tools/linuxfs/overlay/usr/local/bin/droiddeck-session").read_text()
self.assertIn("export DROIDDECK_HUD_FRAMES=/run/droiddeck-hud/frames", session)
app = (ROOT / "app/src/main/java/com/droiddeck/launcher/hud/HudFrames.kt").read_text()
self.assertIn('"run/droiddeck-hud/frames"', app)
if __name__ == "__main__":
unittest.main()
+33
View File
@@ -0,0 +1,33 @@
"""Exercise the HUD's kgsl memstore busy sampling (app/src/main/cpp/gpuclock/kgsl_busy.h)."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[2]
class KgslBusyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
raise unittest.SkipTest('A host C compiler is required')
cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-kgsl-busy-')
cls.addClassCleanup(cls.temp.cleanup)
cls.binary = Path(cls.temp.name) / 'kgsl-busy-test'
subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror',
'-I', str(REPO / 'app/src/main/cpp/gpuclock'),
str(REPO / 'tools/tests/kgsl_busy_harness.c'), '-o', str(cls.binary)],
check=True, capture_output=True, text=True)
def test_ringbuffer_slots_and_busy_rule(self):
result = subprocess.run([str(self.binary)], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn('PASS: kgsl memstore busy', result.stdout)
if __name__ == '__main__':
unittest.main()
-1
View File
@@ -19,7 +19,6 @@ ALL = set(range(8))
class FakeCores:
"""Per-thread masks and priorities, with every change recorded."""
def __init__(self):
self.state = {}
+98
View File
@@ -0,0 +1,98 @@
import os
from pathlib import Path
import stat
import subprocess
import sys
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-library-perms"
class LibraryPermsTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.restore)
self.root = Path(self.temp.name)
self.home = self.root / "home"
self.steam = self.home / ".local/share/Steam"
self.card = self.root / "card"
self.stores = self.root / "stores"
(self.steam / "steamapps").mkdir(parents=True)
(self.steam / "steamapps/libraryfolders.vdf").write_text(
'"libraryfolders"\n{\n\t"0"\n\t{\n\t\t"path"\t\t"%s"\n\t}\n\t"1"\n\t{\n\t\t"path"\t\t"%s"\n\t}\n}\n'
% (self.steam, self.card))
self.locked = []
def restore(self):
for path in self.locked:
if path.exists() and not path.is_symlink():
path.chmod(0o700 if path.is_dir() else 0o600)
for path in sorted(self.root.rglob("*"), key=lambda p: len(p.parts), reverse=True):
if path.is_dir() and not path.is_symlink():
path.chmod(0o700)
self.temp.cleanup()
def file(self, path, mode=0o400):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("x")
path.chmod(mode)
self.locked.append(path)
return path
def folder(self, path, mode=0o500):
path.mkdir(parents=True, exist_ok=True)
self.locked.append(path)
return path
def run_script(self):
env = dict(os.environ, HOME=str(self.home), DROIDDECK_STORES_ROOT=str(self.stores))
for path in reversed(self.locked):
if path.is_dir():
path.chmod(0o500)
return subprocess.run([sys.executable, str(SCRIPT)], env=env, check=True, capture_output=True, text=True).stdout
def mode(self, path):
return stat.S_IMODE(os.lstat(path).st_mode)
def test_read_only_game_files_and_folders_become_writable_in_every_library(self):
ini = self.file(self.steam / "steamapps/compatdata/3712566526/pfx/drive_c/users/steamuser/Documents/My Games/FalloutNV/FALLOUT.INI")
data = self.folder(self.steam / "steamapps/common/Game/Data")
game = self.file(data / "game.esm", 0o444)
card = self.file(self.card / "steamapps/common/Other/settings.ini")
gog = self.file(self.home / "Games/Stores/GOG/Fallout New Vegas/Fallout_default.ini")
store = self.file(self.stores / "abc/Game/config.cfg")
out = self.run_script()
self.assertEqual(self.mode(ini) & 0o600, 0o600)
self.assertEqual(self.mode(game), 0o644)
self.assertEqual(self.mode(data) & 0o700, 0o700)
for path in (card, gog, store):
self.assertEqual(self.mode(path) & 0o600, 0o600, path)
self.assertIn("made writable", out)
def test_proton_and_steam_runtimes_are_left_as_valve_ships_them(self):
proton = self.file(self.steam / "steamapps/common/Proton Experimental (ARM64)/files/lib/wine/a.so", 0o444)
runtime = self.file(self.card / "steamapps/common/SteamLinuxRuntime_sniper/run", 0o555)
nested = self.file(self.steam / "steamapps/common/Game/Proton notes.txt")
self.run_script()
self.assertEqual(self.mode(proton), 0o444)
self.assertEqual(self.mode(runtime), 0o555)
self.assertEqual(self.mode(nested) & 0o600, 0o600)
def test_symlinks_are_not_followed(self):
outside = self.file(self.root / "outside/keep.ini")
link = self.steam / "steamapps/compatdata/1/pfx/dosdevices/z:"
link.parent.mkdir(parents=True)
link.symlink_to(self.root / "outside")
self.run_script()
self.assertEqual(self.mode(outside), 0o400)
def test_writable_files_are_untouched_and_missing_libraries_are_fine(self):
fine = self.file(self.steam / "steamapps/common/Game/ok.ini", 0o640)
out = self.run_script()
self.assertEqual(self.mode(fine), 0o640)
self.assertIn("0 files and folders made writable", out)
if __name__ == "__main__":
unittest.main()
-1
View File
@@ -1,4 +1,3 @@
"""Real login1 sleep handshakes; run with dbus-run-session and python3-gi."""
import os
from pathlib import Path
import subprocess
-1
View File
@@ -10,7 +10,6 @@ COMPONENTS = runpy.run_path(str(BIN / "droiddeck-wincomponents"))
class FakePackage:
"""A package's tables in memory, as msiinfo would give them."""
def __init__(self, tables, summary=None):
self.path = "fake.msi"
-4
View File
@@ -1,7 +1,3 @@
"""NetworkManager contract and real D-Bus handovers (requires python3-gi and dbus).
Run on Linux: dbus-run-session -- python3 -m unittest discover -s tools/tests -p test_netmanager.py
"""
import importlib.machinery
import importlib.util
import json
+42 -5
View File
@@ -1,4 +1,5 @@
import os
import sys
from pathlib import Path
import shutil
import subprocess
@@ -11,6 +12,7 @@ PROGRAM = r"""
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
int main(int argc, char **argv) {
@@ -43,7 +45,7 @@ class NoexecPreloadTest(unittest.TestCase):
cls.tmp = Path(tempfile.mkdtemp())
cls.lib = cls.tmp / "libnoexec.so"
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-DNOEXEC_TEST", "-o", str(cls.lib),
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "writewatch.c"), str(PRELOAD / "clockpause.c"), "-ldl"], check=True)
source = cls.tmp / "program.c"
source.write_text(PROGRAM)
for name in ("FEX", "game"):
@@ -75,6 +77,7 @@ EXECMOD = r"""
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
@@ -97,6 +100,14 @@ static int private_file(void *addr) {
return found;
}
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) {
if ((flags & MAP_ANONYMOUS) && getenv("DROIDDECK_TEST_FAIL_ANON")) {
errno = ENOMEM;
return MAP_FAILED;
}
return (void *)syscall(SYS_mmap, addr, len, prot, flags, fd, off);
}
int mprotect(void *addr, size_t len, int prot) {
if ((prot & PROT_EXEC) && private_file(addr)) {
errno = EACCES;
@@ -110,6 +121,7 @@ CALLER = r"""
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
@@ -127,27 +139,28 @@ int main(int argc, char **argv) {
printf("protect %d\n", result ? errno : 0);
__builtin___clear_cache((char *)code, (char *)code + 4096);
if (!result) printf("call %d\n", ((int (*)(void))(code + 64))());
if (result && getenv("DROIDDECK_TEST_WRITE_AFTER_FAILURE")) code[0] = 9;
printf("data %d\n", code[0]);
char line[512];
FILE *maps = fopen("/proc/self/maps", "r");
while (maps && fgets(line, sizeof line, maps)) {
unsigned long lo, hi;
if (sscanf(line, "%lx-%lx", &lo, &hi) == 2 && (unsigned long)code >= lo && (unsigned long)code < hi)
printf("shared %d\n", strstr(line, ".droiddeck-rx") != NULL);
printf("shared %d\n", strstr(line, getenv("DROIDDECK_NOEXEC_CACHE_DIR")) != NULL);
}
return 0;
}
"""
@unittest.skipUnless(shutil.which("cc") and os.uname().sysname == "Linux", "needs cc on Linux")
@unittest.skipUnless(sys.platform == "linux" and shutil.which("cc"), "needs cc on Linux")
class ExecmodRebakeTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tmp = Path(tempfile.mkdtemp())
cls.lib = cls.tmp / "libnoexec.so"
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-DNOEXEC_TEST", "-o", str(cls.lib),
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), "-ldl"], check=True)
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "clockpause.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
(cls.tmp / "execmod.c").write_text(EXECMOD)
cls.execmod = cls.tmp / "libexecmod.so"
subprocess.run(["cc", "-O2", "-fPIC", "-shared", "-o", str(cls.execmod), str(cls.tmp / "execmod.c")], check=True)
@@ -168,9 +181,10 @@ class ExecmodRebakeTest(unittest.TestCase):
def tearDown(self):
shutil.rmtree(self.tmp / self._testMethodName, ignore_errors=True)
def run_with(self, *libs):
def run_with(self, *libs, extra=None):
env = dict(os.environ, LD_PRELOAD=":".join(str(lib) for lib in libs),
DROIDDECK_NOEXEC_CACHE_DIR=str(self.cache))
env.update(extra or {})
result = subprocess.run([str(self.caller), str(self.image)], env=env, capture_output=True, text=True, check=True)
return result.stdout.split()
@@ -179,7 +193,30 @@ class ExecmodRebakeTest(unittest.TestCase):
def test_processes_share_one_copy_of_the_code(self):
self.run_with(self.lib, self.execmod)
cached = next(self.cache.iterdir())
before = cached.stat()
self.run_with(self.lib, self.execmod)
after = cached.stat()
self.assertEqual((before.st_ino, before.st_mtime_ns), (after.st_ino, after.st_mtime_ns))
self.assertEqual(len(os.listdir(self.cache)), 1)
def test_unavailable_cache_uses_executable_anonymous_memory(self):
self.cache = self.tmp / "missing-parent" / ".droiddeck-rx"
self.assertEqual(self.run_with(self.lib, self.execmod), ["protect", "0", "call", "42", "data", "7", "shared", "0"])
def test_failed_fallback_preserves_original_writable_mapping(self):
self.cache = self.tmp / "missing-parent" / ".droiddeck-rx"
self.assertEqual(self.run_with(self.lib, self.execmod, extra={
"DROIDDECK_TEST_FAIL_ANON": "1", "DROIDDECK_TEST_WRITE_AFTER_FAILURE": "1"
}), ["protect", "13", "data", "9", "shared", "0"])
def test_concurrent_processes_can_execute_the_same_relocated_code(self):
env = dict(os.environ, LD_PRELOAD=f"{self.lib}:{self.execmod}", DROIDDECK_NOEXEC_CACHE_DIR=str(self.cache))
children = [subprocess.Popen([str(self.caller), str(self.image)], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) for _ in range(8)]
for child in children:
output, error = child.communicate(timeout=10)
self.assertEqual(child.returncode, 0, error)
self.assertIn("call 42", output)
self.assertEqual(len(os.listdir(self.cache)), 1)
def test_without_the_preload_the_refusal_stands(self):
+580
View File
@@ -0,0 +1,580 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
NTDLL = r"""
#define _GNU_SOURCE
#include <signal.h>
#include <string.h>
int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen) {
struct sigaction sa, old;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = fn;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigemptyset(&sa.sa_mask);
if (sigaction(sig, &sa, NULL) != 0) return -1;
if (sigaction(sig, NULL, &old) != 0) return -1;
*seen = (void *)old.sa_sigaction;
return 0;
}
int install_oneshot(int sig, void (*fn)(int, siginfo_t *, void *)) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = fn;
sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_RESETHAND;
sigemptyset(&sa.sa_mask);
return sigaction(sig, &sa, NULL);
}
"""
PROGRAM = r"""
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
struct sem_args { uint32_t count, max; };
struct mutex_args { uint32_t owner, count; };
struct event_args { uint32_t manual, signaled; };
struct wait_args { uint64_t timeout, objs; uint32_t count, index, flags, owner, alert, pad; };
#define NT_CREATE_SEM _IOW('N', 0x80, struct sem_args)
#define NT_SEM_RELEASE _IOWR('N', 0x81, uint32_t)
#define NT_WAIT_ANY _IOWR('N', 0x82, struct wait_args)
#define NT_WAIT_ALL _IOWR('N', 0x83, struct wait_args)
#define NT_CREATE_MUTEX _IOW('N', 0x84, struct mutex_args)
#define NT_MUTEX_UNLOCK _IOWR('N', 0x85, struct mutex_args)
#define NT_MUTEX_KILL _IOW('N', 0x86, uint32_t)
#define NT_CREATE_EVENT _IOW('N', 0x87, struct event_args)
#define NT_EVENT_SET _IOR('N', 0x88, uint32_t)
#define NT_EVENT_RESET _IOR('N', 0x89, uint32_t)
#define NT_EVENT_PULSE _IOR('N', 0x8a, uint32_t)
#define NT_SEM_READ _IOR('N', 0x8b, struct sem_args)
#define NT_MUTEX_READ _IOR('N', 0x8c, struct mutex_args)
#define NT_EVENT_READ _IOR('N', 0x8d, struct event_args)
int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen);
int install_oneshot(int sig, void (*fn)(int, siginfo_t *, void *));
static int dev, fails;
#define CHECK(name, cond) do { if (!(cond)) { printf("FAIL %s line %d errno %d\n", name, __LINE__, errno); fails++; } } while (0)
static uint64_t mono_ns(int64_t delta_ms) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec + (uint64_t)(delta_ms * 1000000);
}
static int wait_on(unsigned long req, const int *objs, uint32_t count, uint32_t owner, int alert, uint64_t timeout, uint32_t *index) {
struct wait_args w = {timeout, (uintptr_t)objs, count, 0xdead, 0, owner, (uint32_t)alert, 0};
int r = ioctl(dev, req, &w);
if (index) *index = w.index;
return r < 0 ? errno : 0;
}
static int event(int manual, int signaled) {
struct event_args a = {(uint32_t)manual, (uint32_t)signaled};
return ioctl(dev, NT_CREATE_EVENT, &a);
}
static int sem(uint32_t count, uint32_t max) {
struct sem_args a = {count, max};
return ioctl(dev, NT_CREATE_SEM, &a);
}
static int mutex(uint32_t owner, uint32_t count) {
struct mutex_args a = {owner, count};
return ioctl(dev, NT_CREATE_MUTEX, &a);
}
static uint32_t prev_of(int fd, unsigned long req, uint32_t in) {
uint32_t v = in;
if (ioctl(fd, req, &v) < 0) return 0xffffffff;
return v;
}
static void semaphores(void) {
struct sem_args bad = {3, 2}, r;
CHECK("sem_create_invalid", ioctl(dev, NT_CREATE_SEM, &bad) < 0 && errno == EINVAL);
int s = sem(2, 2);
CHECK("sem_create", s >= 0);
uint32_t idx;
CHECK("sem_wait1", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0 && idx == 0);
CHECK("sem_wait2", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0);
CHECK("sem_empty", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, NULL) == ETIMEDOUT);
CHECK("sem_release_prev", prev_of(s, NT_SEM_RELEASE, 1) == 0);
CHECK("sem_overflow", prev_of(s, NT_SEM_RELEASE, 2) == 0xffffffff && errno == EOVERFLOW);
CHECK("sem_read", ioctl(s, NT_SEM_READ, &r) == 0 && r.count == 1 && r.max == 2);
close(s);
}
static void mutexes(void) {
struct mutex_args bad = {0, 1}, r, u;
CHECK("mutex_create_invalid", ioctl(dev, NT_CREATE_MUTEX, &bad) < 0 && errno == EINVAL);
int m = mutex(0, 0);
uint32_t idx;
CHECK("mutex_owner_zero", wait_on(NT_WAIT_ANY, &m, 1, 0, 0, 0, NULL) == EINVAL);
CHECK("mutex_take", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, &idx) == 0 && idx == 0);
CHECK("mutex_recurse", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, NULL) == 0);
CHECK("mutex_other", wait_on(NT_WAIT_ANY, &m, 1, 8, 0, 0, NULL) == ETIMEDOUT);
CHECK("mutex_read", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 7 && r.count == 2);
u = (struct mutex_args){8, 0};
CHECK("mutex_unlock_eperm", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EPERM);
u = (struct mutex_args){0, 0};
CHECK("mutex_unlock_einval", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EINVAL);
u = (struct mutex_args){7, 0};
CHECK("mutex_unlock_prev", ioctl(m, NT_MUTEX_UNLOCK, &u) == 0 && u.count == 2);
uint32_t who = 8;
CHECK("mutex_kill_eperm", ioctl(m, NT_MUTEX_KILL, &who) < 0 && errno == EPERM);
who = 7;
CHECK("mutex_kill", ioctl(m, NT_MUTEX_KILL, &who) == 0);
CHECK("mutex_read_dead", ioctl(m, NT_MUTEX_READ, &r) < 0 && errno == EOWNERDEAD && r.owner == 0 && r.count == 0);
CHECK("mutex_abandoned", wait_on(NT_WAIT_ANY, &m, 1, 9, 0, 0, &idx) == EOWNERDEAD && idx == 0);
CHECK("mutex_after_abandon", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 9 && r.count == 1);
close(m);
}
static void events(void) {
int a = event(0, 1), m = event(1, 0);
struct event_args r;
CHECK("auto_take", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == 0);
CHECK("auto_consumed", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == ETIMEDOUT);
CHECK("set_prev0", prev_of(m, NT_EVENT_SET, 0) == 0);
CHECK("set_prev1", prev_of(m, NT_EVENT_SET, 0) == 1);
CHECK("manual_kept", wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0 && wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0);
CHECK("reset_prev", prev_of(m, NT_EVENT_RESET, 0) == 1);
CHECK("pulse_prev", prev_of(m, NT_EVENT_PULSE, 0) == 0);
CHECK("pulse_leaves_unsignaled", ioctl(m, NT_EVENT_READ, &r) == 0 && r.manual == 1 && r.signaled == 0);
close(a);
close(m);
}
static void any_order(void) {
int o[4] = {event(1, 0), sem(1, 5), event(1, 1), sem(0, 5)};
int alert = event(0, 1);
uint32_t idx;
CHECK("any_lowest", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 1);
CHECK("any_next", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 2);
prev_of(o[2], NT_EVENT_RESET, 0);
CHECK("any_alert", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 4);
CHECK("any_alert_consumed", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, NULL) == ETIMEDOUT);
int dup[3] = {o[3], o[0], o[3]};
prev_of(o[3], NT_SEM_RELEASE, 1);
CHECK("any_duplicate_lowest", wait_on(NT_WAIT_ANY, dup, 3, 1, 0, 0, &idx) == 0 && idx == 0);
int many[65];
for (int i = 0; i < 65; i++) many[i] = o[0];
CHECK("any_too_many", wait_on(NT_WAIT_ANY, many, 65, 1, 0, 0, NULL) == EINVAL);
int badfd = 0;
CHECK("any_invalid_fd", wait_on(NT_WAIT_ANY, &badfd, 1, 1, 0, 0, NULL) == EINVAL);
CHECK("any_timeout", wait_on(NT_WAIT_ANY, o, 1, 1, 0, mono_ns(30), NULL) == ETIMEDOUT);
for (int i = 0; i < 4; i++) close(o[i]);
close(alert);
}
static void all_semantics(void) {
int s = sem(1, 5), e = event(0, 0), m = mutex(0, 0);
int o[3] = {s, e, m};
struct sem_args sr;
uint32_t idx;
CHECK("all_not_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, NULL) == ETIMEDOUT);
CHECK("all_untouched", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 1);
prev_of(e, NT_EVENT_SET, 0);
CHECK("all_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, &idx) == 0 && idx == 0);
CHECK("all_took_sem", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0);
int dup[2] = {s, s};
CHECK("all_duplicate", wait_on(NT_WAIT_ALL, dup, 2, 4, 0, 0, NULL) == EINVAL);
int alert = event(1, 1);
CHECK("all_alert", wait_on(NT_WAIT_ALL, o, 3, 4, alert, 0, &idx) == 0 && idx == 3);
close(s);
close(e);
close(m);
close(alert);
}
static int wake_obj;
static uint32_t wake_index;
static int wake_ret;
static void *waiter(void *p) {
int all = (int)(intptr_t)p;
int objs[2] = {wake_obj, wake_obj + 0};
wake_ret = wait_on(all ? NT_WAIT_ALL : NT_WAIT_ANY, objs, 1, 3, 0, UINT64_MAX, &wake_index);
return NULL;
}
static void cross_thread(void) {
wake_obj = event(0, 0);
pthread_t t;
pthread_create(&t, NULL, waiter, NULL);
usleep(30000);
prev_of(wake_obj, NT_EVENT_SET, 0);
pthread_join(t, NULL);
CHECK("thread_wake", wake_ret == 0 && wake_index == 0);
struct event_args r;
CHECK("thread_auto_reset", ioctl(wake_obj, NT_EVENT_READ, &r) == 0 && r.signaled == 0);
close(wake_obj);
int s = sem(0, 10);
wake_obj = s;
pthread_t ts[3];
for (int i = 0; i < 3; i++) pthread_create(&ts[i], NULL, waiter, NULL);
usleep(30000);
CHECK("sem_release_three", prev_of(s, NT_SEM_RELEASE, 3) == 0);
for (int i = 0; i < 3; i++) pthread_join(ts[i], NULL);
struct sem_args sr;
CHECK("sem_all_taken", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0);
close(s);
}
static void cross_process(void) {
int sv[2];
CHECK("socketpair", socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
int inherited = event(0, 0);
pid_t pid = fork();
if (pid == 0) {
uint32_t idx;
int r1 = wait_on(NT_WAIT_ANY, &inherited, 1, 5, 0, mono_ns(5000), &idx);
char c;
struct iovec io = {&c, 1};
char cbuf[CMSG_SPACE(sizeof(int))];
struct msghdr msg = {0};
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
int got = -1;
if (recvmsg(sv[1], &msg, 0) == 1) memcpy(&got, CMSG_DATA(CMSG_FIRSTHDR(&msg)), sizeof(int));
int r2 = wait_on(NT_WAIT_ANY, &got, 1, 5, 0, mono_ns(5000), NULL);
_exit(r1 == 0 && r2 == 0 ? 0 : 1);
}
usleep(30000);
prev_of(inherited, NT_EVENT_SET, 0);
int sent = sem(0, 1);
char c = 'x';
struct iovec io = {&c, 1};
char cbuf[CMSG_SPACE(sizeof(int))];
struct msghdr msg = {0};
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
struct cmsghdr *cm = CMSG_FIRSTHDR(&msg);
cm->cmsg_level = SOL_SOCKET;
cm->cmsg_type = SCM_RIGHTS;
cm->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(cm), &sent, sizeof(int));
sendmsg(sv[0], &msg, 0);
usleep(30000);
prev_of(sent, NT_SEM_RELEASE, 1);
int status = 0;
waitpid(pid, &status, 0);
CHECK("process_wake", WIFEXITED(status) && WEXITSTATUS(status) == 0);
close(inherited);
close(sent);
close(sv[0]);
close(sv[1]);
}
static int ping_obj, pong_obj, stress_sem;
static void *stress_ponger(void *p) {
long n = (long)p;
uint32_t v;
for (long i = 0; i < n; i++) {
if (wait_on(NT_WAIT_ANY, &ping_obj, 1, 21, 0, UINT64_MAX, NULL) != 0) return (void *)1;
ioctl(pong_obj, NT_EVENT_SET, &v);
}
return NULL;
}
static void *stress_hammer(void *p) {
long n = (long)p;
for (long i = 0; i < n; i++) {
if (wait_on(NT_WAIT_ANY, &stress_sem, 1, 22, 0, UINT64_MAX, NULL) != 0) return (void *)1;
uint32_t c = 1;
ioctl(stress_sem, NT_SEM_RELEASE, &c);
}
return NULL;
}
static void stress(void) {
long rounds = 20000;
ping_obj = event(0, 0);
pong_obj = event(0, 0);
pthread_t t;
void *bad = NULL;
pthread_create(&t, NULL, stress_ponger, (void *)rounds);
uint32_t v;
int ok = 1;
for (long i = 0; i < rounds && ok; i++) {
ioctl(ping_obj, NT_EVENT_SET, &v);
ok = wait_on(NT_WAIT_ANY, &pong_obj, 1, 20, 0, UINT64_MAX, NULL) == 0;
}
pthread_join(t, &bad);
CHECK("stress_pingpong", ok && bad == NULL);
stress_sem = sem(1, 1);
pthread_t th[4];
for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, stress_hammer, (void *)5000L);
int hammer_ok = 1;
for (int i = 0; i < 4; i++) {
pthread_join(th[i], &bad);
hammer_ok &= bad == NULL;
}
struct sem_args sr;
CHECK("stress_contended", hammer_ok && ioctl(stress_sem, NT_SEM_READ, &sr) == 0 && sr.count == 1);
close(ping_obj);
close(pong_obj);
close(stress_sem);
}
static int busy_obj;
static volatile int handled, stop_worker;
static sem_t parked, unpark;
static void reentrant(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
uint32_t v = 0;
ioctl(busy_obj, NT_EVENT_SET, &v);
handled++;
}
static void suspending(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
sem_post(&parked);
while (sem_wait(&unpark) != 0) {}
handled++;
}
static void *worker(void *p) {
(void)p;
uint32_t v;
while (!stop_worker) {
ioctl(busy_obj, NT_EVENT_SET, &v);
wait_on(NT_WAIT_ANY, &busy_obj, 1, 11, 0, 0, NULL);
}
return NULL;
}
static void signals(void) {
void *seen = NULL;
busy_obj = event(0, 0);
CHECK("install_reentrant", install_handler(SIGUSR1, reentrant, &seen) == 0);
CHECK("query_original", seen == (void *)reentrant);
pthread_t w;
stop_worker = 0;
handled = 0;
pthread_create(&w, NULL, worker, NULL);
for (int i = 0; i < 2000; i++) {
pthread_kill(w, SIGUSR1);
if (i % 64 == 0) usleep(100);
}
usleep(20000);
stop_worker = 1;
pthread_join(w, NULL);
CHECK("reentrant_ran", handled > 0);
sem_init(&parked, 0, 0);
sem_init(&unpark, 0, 0);
CHECK("install_suspending", install_handler(SIGUSR1, suspending, &seen) == 0);
stop_worker = 0;
handled = 0;
pthread_create(&w, NULL, worker, NULL);
uint32_t v;
for (int i = 0; i < 300; i++) {
pthread_kill(w, SIGUSR1);
while (sem_wait(&parked) != 0) {}
ioctl(busy_obj, NT_EVENT_SET, &v);
wait_on(NT_WAIT_ANY, &busy_obj, 1, 12, 0, 0, NULL);
sem_post(&unpark);
}
stop_worker = 1;
pthread_join(w, NULL);
CHECK("suspend_ran", handled == 300);
close(busy_obj);
}
static volatile int burst_count;
static void bursting(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
uint32_t v = 0;
ioctl(busy_obj, NT_EVENT_SET, &v);
__atomic_add_fetch(&burst_count, 1, __ATOMIC_RELAXED);
}
static void bursts(void) {
void *seen = NULL;
busy_obj = event(0, 0);
for (int s = 0; s < 8; s++) CHECK("install_burst", install_handler(SIGRTMIN + s, bursting, &seen) == 0);
pthread_t w;
stop_worker = 0;
burst_count = 0;
pthread_create(&w, NULL, worker, NULL);
union sigval val = {0};
int sent = 0;
for (int i = 0; i < 400; i++) {
for (int s = 0; s < 8; s++)
if (pthread_sigqueue(w, SIGRTMIN + s, val) == 0) sent++;
usleep(200);
}
for (int i = 0; i < 2000 && __atomic_load_n(&burst_count, __ATOMIC_RELAXED) < sent; i++) usleep(1000);
stop_worker = 1;
pthread_join(w, NULL);
CHECK("burst_all_handled", burst_count == sent && sent > 0);
close(busy_obj);
}
static volatile int oneshot_count;
static void oneshot(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
__atomic_add_fetch(&oneshot_count, 1, __ATOMIC_RELAXED);
}
static void oneshots(void) {
busy_obj = event(0, 0);
pthread_t w;
stop_worker = 0;
oneshot_count = 0;
pthread_create(&w, NULL, worker, NULL);
int fired = 0;
for (int i = 0; i < 300; i++) {
if (install_oneshot(SIGWINCH, oneshot) != 0) break;
int before = __atomic_load_n(&oneshot_count, __ATOMIC_RELAXED);
pthread_kill(w, SIGWINCH);
fired++;
for (int j = 0; j < 2000 && __atomic_load_n(&oneshot_count, __ATOMIC_RELAXED) == before; j++) usleep(50);
}
stop_worker = 1;
pthread_join(w, NULL);
CHECK("oneshot_handlers_run", oneshot_count == fired && fired == 300);
close(busy_obj);
}
static int outer_obj, inner_obj, other_dev, other_obj;
static volatile int nested_ok, nested_done, outer_done;
static void nesting(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
uint32_t idx = 99;
int ok = wait_on(NT_WAIT_ANY, &inner_obj, 1, 21, 0, mono_ns(50), &idx) == 0 && idx == 0;
struct wait_args w = {mono_ns(2), (uintptr_t)&other_obj, 1, 0xdead, 0, 22, 0, 0};
ok = ok && ioctl(other_dev, NT_WAIT_ANY, &w) < 0 && errno == ETIMEDOUT;
nested_ok += ok;
__atomic_store_n(&nested_done, 1, __ATOMIC_RELEASE);
}
static void *outer_waiter(void *p) {
struct wait_args w = {mono_ns(3000), (uintptr_t)&outer_obj, 1, 99, 0, 23, 0, 0};
int r;
do { r = ioctl(dev, NT_WAIT_ANY, &w); } while (r < 0 && errno == EINTR);
*(int *)p = r == 0 && w.index == 0;
__atomic_store_n(&outer_done, 1, __ATOMIC_RELEASE);
return NULL;
}
static void nested_waits(void) {
void *seen = NULL;
other_dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC);
CHECK("second_device", other_dev >= 0);
struct event_args ea = {0, 0};
other_obj = ioctl(other_dev, NT_CREATE_EVENT, &ea);
inner_obj = event(1, 1);
CHECK("install_nesting", install_handler(SIGUSR2, nesting, &seen) == 0);
int good = 0, early = 0;
nested_ok = 0;
for (int i = 0; i < 40; i++) {
outer_obj = event(0, 0);
int result = 0;
pthread_t t;
nested_done = outer_done = 0;
pthread_create(&t, NULL, outer_waiter, &result);
usleep(5000);
pthread_kill(t, SIGUSR2);
while (!__atomic_load_n(&nested_done, __ATOMIC_ACQUIRE)) usleep(200);
usleep(5000);
early += __atomic_load_n(&outer_done, __ATOMIC_ACQUIRE);
uint32_t v = 0;
ioctl(outer_obj, NT_EVENT_SET, &v);
pthread_join(t, NULL);
good += result;
close(outer_obj);
}
CHECK("outer_wait_not_woken_by_nested_wait", early == 0);
CHECK("outer_wait_survives_nested_waits", good == 40);
CHECK("nested_waits_complete", nested_ok == 40);
close(inner_obj);
close(other_obj);
close(other_dev);
}
int main(void) {
alarm(60);
dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC);
if (dev < 0) {
printf("no device %d\n", errno);
return 1;
}
semaphores();
mutexes();
events();
any_order();
all_semantics();
cross_thread();
cross_process();
stress();
signals();
bursts();
oneshots();
nested_waits();
printf("done fails=%d\n", fails);
return fails != 0;
}
"""
class NtsyncPreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
if not shutil.which("cc"):
raise unittest.SkipTest("no compiler")
cls.tmp = tempfile.TemporaryDirectory()
tmp = Path(cls.tmp.name)
(tmp / "ntdll.c").write_text(NTDLL)
(tmp / "prog.c").write_text(PROGRAM)
sources = sorted(str(p) for p in PRELOAD.glob("*.c"))
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(tmp / "libblsession.so"), *sources, "-ldl"],
check=True)
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(tmp / "ntdll.so"), str(tmp / "ntdll.c")], check=True)
subprocess.run(["cc", "-O1", "-pthread", "-o", str(tmp / "prog"), str(tmp / "prog.c"), str(tmp / "ntdll.so"),
"-Wl,-rpath," + str(tmp)], check=True)
cls.program = tmp / "prog"
cls.preload = tmp / "libblsession.so"
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def run_program(self):
env = dict(os.environ, LD_PRELOAD=str(self.preload), BL_SYNC="1")
return subprocess.run([str(self.program)], env=env, capture_output=True, text=True, timeout=90)
def test_emulation_matches_the_ntsync_driver(self):
out = self.run_program()
self.assertEqual(out.returncode, 0, out.stdout + out.stderr)
self.assertIn("done fails=0", out.stdout)
def test_repeated_runs_stay_consistent(self):
for _ in range(3):
out = self.run_program()
self.assertEqual(out.returncode, 0, out.stdout + out.stderr)
if __name__ == "__main__":
unittest.main()
+124 -9
View File
@@ -9,8 +9,6 @@ import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
FASTPATH = Path(__file__).resolve().parents[1] / "proot/fastpath/fastpath.c"
# Opens each "<dirfd>\t<flags>\t<path>" case with openat2(RESOLVE_IN_ROOT) as FEX does: first through the kernel's own
# openat2 on argv[1] when it has one, then, with openat2 failing ENOSYS as under proot, through the preload on argv[2].
PROGRAM = r"""
#define _GNU_SOURCE
#include <dlfcn.h>
@@ -94,7 +92,7 @@ COUNTER = r"""
#include <sys/stat.h>
#include <unistd.h>
static int opens, stats, links;
static int opens, stats, links, statxs;
int openat(int dirfd, const char *path, int flags, ...) {
va_list args;
@@ -120,8 +118,13 @@ ssize_t readlinkat(int dirfd, const char *path, char *buf, size_t size) {
return ((ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat"))(dirfd, path, buf, size);
}
int statx(int dirfd, const char *path, int flags, unsigned int mask, struct statx *out) {
statxs++;
return ((int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(RTLD_NEXT, "statx"))(dirfd, path, flags, mask, out);
}
__attribute__((destructor)) static void report(void) {
fprintf(stderr, "calls %d %d %d\n", opens, stats, links);
fprintf(stderr, "calls %d %d %d %d\n", opens, stats, links, statxs);
}
"""
@@ -141,9 +144,85 @@ int main(int argc, char **argv) {
}
"""
WRAPPER = r"""
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#ifndef SYS_openat2
#define SYS_openat2 437
#endif
struct how { uint64_t flags, mode, resolve; };
int main(int argc, char **argv) {
struct sock_filter filter[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_openat2, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | ENOSYS),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
};
struct sock_fprog prog = {sizeof(filter) / sizeof(filter[0]), filter};
if (argc < 3 || prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) return 3;
int (*call)(int, const char *, struct how *, size_t) = (int (*)(int, const char *, struct how *, size_t))dlsym(RTLD_DEFAULT, "openat2");
if (!call) return 4;
int dir = open(argv[1], O_PATH | O_DIRECTORY | O_CLOEXEC);
struct how h = {O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644, 0x08};
int fd = call(dir, argv[2], &h, sizeof(h));
if (fd < 0) {
printf("ERR %d\n", errno);
return 1;
}
printf("OK\n");
return close(fd);
}
"""
RAW_LOOKUP = r"""
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#ifdef SYS_newfstatat
#define RAW_FSTATAT SYS_newfstatat
#else
#define RAW_FSTATAT SYS_fstatat64
#endif
int main(int argc, char **argv) {
struct stat st;
struct statx sx;
char link[64] = "";
if (syscall(RAW_FSTATAT, AT_FDCWD, argv[1], &st, 0) != 0) return 1;
if (syscall(RAW_FSTATAT, AT_FDCWD, argv[2], &st, 0) != -1 || errno != ENOENT) return 2;
long n = syscall(SYS_readlinkat, AT_FDCWD, argv[3], link, sizeof(link) - 1);
if (n <= 0) return 3;
sx.stx_size = 0;
long found = syscall(SYS_statx, AT_FDCWD, argv[1], 0, STATX_SIZE, &sx);
int have_statx = !(found == -1 && errno == ENOSYS);
if (have_statx && found != 0) return 4;
long missing = syscall(SYS_statx, AT_FDCWD, argv[2], 0, STATX_SIZE, &sx);
if (have_statx && (missing != -1 || errno != ENOENT)) return 5;
printf("%lld %.*s %llu\n", (long long)st.st_size, (int)n, link, (unsigned long long)sx.stx_size);
return 0;
}
"""
O_PATH = getattr(os, "O_PATH", 0o10000000)
# (dirfd: r = the tree, o = "/"), flags, path, expected: a path in the tree, an errno name, or "proc" for a file outside it.
CASES = [
("r", os.O_RDONLY, "etc/alternatives/hn", "etc/hostname"),
("r", os.O_RDONLY, "/lib64/x86_64-linux-gnu/libfoo.so.1", "usr/lib/x86_64-linux-gnu/libfoo.so.1.2"),
@@ -174,7 +253,6 @@ CASES = [
("o", os.O_RDONLY, "proc/self/status", "proc"),
]
# Only the preload: an absolute link to a path missing in the tree but present outside it, as droiddeck-fex's rootfs has.
HOST_CASES = [
("r", os.O_RDONLY, "lib/x86_64-linux-gnu/libbar.so.2", "host:libbar.so.2"),
("r", os.O_RDONLY, "hostdir/sub/data", "host:sub/data"),
@@ -230,7 +308,7 @@ class Openat2PreloadTest(unittest.TestCase):
cls.tmp = Path(tempfile.mkdtemp())
cls.lib = cls.tmp / "libopenat2.so"
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib),
str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), str(PRELOAD / "clockpause.c"), "-ldl"], check=True)
source = cls.tmp / "openat2.c"
source.write_text(PROGRAM)
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-o", str(cls.tmp / "openat2"), str(source), "-ldl"], check=True)
@@ -268,6 +346,14 @@ class Openat2PreloadTest(unittest.TestCase):
cls.raw = subprocess.run([str(cls.tmp / "raw_open"), str(tree / "etc/hostname"), str(tree / "missing")],
capture_output=True, text=True, timeout=60,
env=dict(os.environ, LD_PRELOAD="%s:%s" % (cls.lib, cls.tmp / "fp/libblfastpath.so")))
lookup = cls.tmp / "raw_lookup.c"
lookup.write_text(RAW_LOOKUP)
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-o", str(cls.tmp / "raw_lookup"), str(lookup)], check=True)
cls.lookup_args = [str(tree / "etc/hostname"), str(tree / "missing"), str(tree / "etc/alternatives/hn")]
cls.lookup_plain = subprocess.run([str(cls.tmp / "raw_lookup"), *cls.lookup_args], capture_output=True, text=True,
timeout=60, env=dict(os.environ, LD_PRELOAD=str(cls.lib)))
cls.lookup = subprocess.run([str(cls.tmp / "raw_lookup"), *cls.lookup_args], capture_output=True, text=True, timeout=60,
env=dict(os.environ, LD_PRELOAD="%s:%s" % (cls.lib, cls.tmp / "fp/libblfastpath.so")))
(cls.tmp / "real").mkdir()
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-o",
str(cls.tmp / "real/libblfastpath.so"), str(FASTPATH), "-ldl"], check=True)
@@ -279,6 +365,20 @@ class Openat2PreloadTest(unittest.TestCase):
LD_PRELOAD="%s:%s" % (cls.lib, cls.tmp / "real/libblfastpath.so")))
except subprocess.TimeoutExpired:
cls.fex = None
wrapper = cls.tmp / "wrapper.c"
wrapper.write_text(WRAPPER)
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-o", str(cls.tmp / "wrapper"), str(wrapper), "-ldl"], check=True)
(cls.tmp / "extract").mkdir()
cls.wrapper = subprocess.run([str(cls.tmp / "wrapper"), str(cls.tmp / "extract"), "file"], capture_output=True,
text=True, timeout=20, env=dict(os.environ, LD_PRELOAD=str(cls.lib)))
(cls.tmp / "fex-lookup").mkdir()
shutil.copy(cls.tmp / "raw_lookup", cls.tmp / "fex-lookup/FEX")
try:
cls.fex_lookup = subprocess.run([str(cls.tmp / "fex-lookup/FEX"), *cls.lookup_args], capture_output=True, text=True,
timeout=20, env=dict(os.environ, PROOT_FP_ROOT="/", PROOT_FP_KEY="test",
LD_PRELOAD="%s:%s" % (cls.lib, cls.tmp / "real/libblfastpath.so")))
except subprocess.TimeoutExpired:
cls.fex_lookup = None
@classmethod
def tearDownClass(cls):
@@ -295,18 +395,33 @@ class Openat2PreloadTest(unittest.TestCase):
def test_the_walk_asks_the_fast_path_when_it_is_loaded(self):
self.assertEqual(self.counted, self.preload)
self.assertTrue(all(n > 0 for n in self.calls), self.calls)
self.assertTrue(all(n > 0 for n in self.calls[:3]), self.calls)
def test_a_raw_openat_syscall_asks_the_fast_path_too(self):
self.assertEqual(self.raw.returncode, 0, self.raw.stderr)
self.assertEqual(self.raw.stdout, "inside\n")
self.assertEqual(self.raw.stderr.split("calls ")[-1].split(), ["2", "0", "0"])
self.assertEqual(self.raw.stderr.split("calls ")[-1].split(), ["2", "0", "0", "0"])
def test_raw_stat_readlink_and_statx_syscalls_ask_the_fast_path(self):
self.assertEqual(self.lookup_plain.returncode, 0, self.lookup_plain.stderr)
self.assertEqual(self.lookup.returncode, 0, self.lookup.stderr)
self.assertEqual(self.lookup.stdout, self.lookup_plain.stdout)
self.assertEqual(self.lookup.stderr.split("calls ")[-1].split(), ["0", "2", "1", "2"])
def test_raw_lookups_in_fex_answer_through_the_real_fast_path(self):
self.assertIsNotNone(self.fex_lookup, "hung")
self.assertEqual(self.fex_lookup.returncode, 0, self.fex_lookup.stderr)
self.assertEqual(self.fex_lookup.stdout, self.lookup_plain.stdout)
def test_a_first_raw_openat_in_fex_does_not_wait_on_the_fast_path_starting(self):
self.assertIsNotNone(self.fex, "hung")
self.assertEqual(self.fex.returncode, 0, self.fex.stderr)
self.assertEqual(self.fex.stdout, "inside\n")
def test_the_libc_openat2_wrapper_creates_files_without_openat2(self):
self.assertEqual(self.wrapper.returncode, 0, self.wrapper.stdout + self.wrapper.stderr)
self.assertTrue((self.tmp / "extract/file").exists())
def test_the_kernel_agrees(self):
if self.kernel is None:
self.skipTest("this kernel or sandbox has no openat2")
+2
View File
@@ -66,6 +66,8 @@ static void send_toplevel_configure(struct surface *s) { (void)s; resizes++; }
#define pointer_button(...) ((void)0)
#define scroll_event(...) ((void)0)
#define axis_event(...) ((void)0)
#define stream_take(...) ((void)0)
#define stream_forget_all(...) ((void)0)
#define pointer_delta(...) ((void)0)
#define deliver_touch(m) ((void)0)
#define deliver_pointer(m) (pointers++)
+125
View File
@@ -0,0 +1,125 @@
import hashlib
import http.server
import json
import os
from pathlib import Path
import shutil
import socket
import subprocess
import tempfile
import threading
import unittest
ROOT = Path(__file__).resolve().parents[2]
FETCH = ROOT / 'tools/packs/fetch-verified.sh'
class PackDownloadsTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
self.cache = self.root / 'cache'
self.cache.mkdir()
self.destination = self.root / 'download'
self.remote = self.root / 'remote'
self.remote.write_bytes(b'verified input')
self.checksum = hashlib.sha256(self.remote.read_bytes()).hexdigest()
self.log = self.root / 'curl.log'
tool = self.root / 'bin/curl'
tool.parent.mkdir()
tool.write_text('#!/usr/bin/env python3\nimport json,os,shutil,sys\n'
'args=sys.argv[1:]\n'
'with open(os.environ["CURL_LOG"],"a") as f: f.write(json.dumps(args)+"\\n")\n'
'shutil.copyfile(os.environ["REMOTE"],args[args.index("-o")+1])\n')
tool.chmod(0o755)
self.env = dict(os.environ, DROIDDECK_PACK_CACHE=str(self.cache), CURL_LOG=str(self.log),
REMOTE=str(self.remote), PATH=str(tool.parent) + os.pathsep + os.environ['PATH'])
def fetch(self, checksum=None, url='https://example.test/pinned', env=None):
return subprocess.run(['bash', str(FETCH), url, checksum or self.checksum, str(self.destination)],
env=env or self.env, capture_output=True, text=True)
def test_cold_download_then_warm_reuse_and_corruption_repair(self):
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 1)
self.assertEqual(self.destination.read_bytes(), b'verified input')
(self.cache / self.checksum).write_bytes(b'corrupt')
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 2)
self.assertEqual((self.cache / self.checksum).read_bytes(), b'verified input')
def test_directory_cache_entry_is_replaced_only_after_verification(self):
entry = self.cache / self.checksum
entry.mkdir()
sentinel = entry / 'old-entry'
sentinel.write_bytes(b'preserve until verified')
self.remote.write_bytes(b'tampered download')
self.assertNotEqual(self.fetch().returncode, 0)
self.assertEqual(sentinel.read_bytes(), b'preserve until verified')
self.assertFalse(self.destination.exists())
self.remote.write_bytes(b'verified input')
result = self.fetch()
self.assertEqual(result.returncode, 0, result.stderr)
self.assertTrue(entry.is_file())
self.assertEqual(entry.read_bytes(), b'verified input')
self.assertEqual(self.destination.read_bytes(), b'verified input')
self.assertEqual(list(self.cache.iterdir()), [entry])
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 2)
def test_changed_pin_cannot_reuse_old_bytes(self):
self.assertEqual(self.fetch().returncode, 0)
self.remote.write_bytes(b'new approved input')
updated = hashlib.sha256(self.remote.read_bytes()).hexdigest()
self.assertEqual(self.fetch(updated).returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 2)
self.assertEqual(self.destination.read_bytes(), b'new approved input')
def test_bad_download_is_not_cached_or_written(self):
self.remote.write_bytes(b'tampered download')
self.assertNotEqual(self.fetch().returncode, 0)
self.assertFalse((self.cache / self.checksum).exists())
self.assertFalse(self.destination.exists())
self.assertEqual(list(self.cache.iterdir()), [])
arguments = json.loads(self.log.read_text())
self.assertIn('--retry-all-errors', arguments)
self.assertIn('--max-time', arguments)
self.assertIn('--retry-max-time', arguments)
def test_connection_reset_retries_then_verifies_real_download(self):
if not shutil.which('curl'):
self.skipTest('curl required for transport test')
content = self.remote.read_bytes()
attempts = []
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
attempts.append(True)
if len(attempts) == 1:
self.connection.shutdown(socket.SHUT_RDWR)
self.connection.close()
return
self.send_response(200)
self.send_header('Content-Length', str(len(content)))
self.end_headers()
self.wfile.write(content)
def log_message(self, *args):
pass
server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
env = dict(self.env, PATH=os.environ['PATH'])
result = self.fetch(url=f'http://127.0.0.1:{server.server_port}/input', env=env)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertGreaterEqual(len(attempts), 2)
self.assertEqual(self.destination.read_bytes(), content)
finally:
server.shutdown()
server.server_close()
thread.join()
+71
View File
@@ -0,0 +1,71 @@
import io
import os
import runpy
import subprocess
import tarfile
import tempfile
import unittest
from pathlib import Path
PACMAN = Path(__file__).resolve().parents[1] / "desktop-kde/overlay/usr/share/droiddeck/pacman"
class PacmanSharedFilesTest(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.top = Path(temp.name)
def keep(self, action):
lib = self.top / "lib"
lib.mkdir(exist_ok=True)
script = (PACMAN / "keep").read_text().replace("/var/lib/pacman", str(lib))
subprocess.run(["bash", "-c", script, "keep", action], check=True, timeout=30)
return lib
def test_an_upgrade_that_drops_a_shared_file_gets_it_back(self):
shared = self.top / "usr/lib/libshared.so"
shared.parent.mkdir(parents=True)
shared.write_text("shared")
lib = self.top / "lib"
lib.mkdir()
(lib / "droiddeck-shared-files").write_text("%s\n%s\n" % (shared, self.top / "usr/lib/never-there"))
self.keep("save")
shared.unlink()
self.keep("restore")
self.assertEqual("shared", shared.read_text())
self.assertFalse((self.top / "usr/lib/never-there").exists())
self.assertFalse((lib / "droiddeck-shared-keep").exists())
def test_a_backup_for_a_file_still_missing_survives_the_next_save(self):
shared = self.top / "usr/lib/libshared.so"
shared.parent.mkdir(parents=True)
shared.write_text("shared")
lib = self.top / "lib"
lib.mkdir()
(lib / "droiddeck-shared-files").write_text("%s\n" % shared)
self.keep("save")
shared.unlink()
self.keep("save")
self.keep("restore")
self.assertEqual("shared", shared.read_text())
def test_only_identical_files_are_taken_over(self):
adopt = runpy.run_path(str(PACMAN / "adopt"), run_name="adopt")
package = io.BytesIO()
with tarfile.open(fileobj=package, mode="w") as archive:
for name, data in (("same", b"one"), ("different", b"two")):
info = tarfile.TarInfo(name)
info.size = len(data)
archive.addfile(info, io.BytesIO(data))
(self.top / "same").write_bytes(b"one")
(self.top / "different").write_bytes(b"other")
package.seek(0)
with tarfile.open(fileobj=package) as archive:
members = {m.name: m for m in archive}
self.assertTrue(adopt["same"](archive, members["same"], str(self.top / "same")))
self.assertFalse(adopt["same"](archive, members["different"], str(self.top / "different")))
if __name__ == "__main__":
unittest.main()
@@ -1,4 +1,3 @@
"""Test the renderer's actual map and inverse map without a Vulkan device."""
import os
from pathlib import Path
import shutil
-1
View File
@@ -1,4 +1,3 @@
"""Exercise the compositor's real button dispatcher with a recording Wayland sink."""
import os
from pathlib import Path
import shutil
+186
View File
@@ -0,0 +1,186 @@
import copy
import importlib.util
import io
import json
import os
from pathlib import Path
import subprocess
import tarfile
import tempfile
import unittest
from unittest.mock import patch
SPEC = importlib.util.spec_from_file_location('preload_snapshots', Path(__file__).resolve().parents[1] / 'components/preloads.py')
snapshots = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(snapshots)
def archive(files):
stream = io.BytesIO()
with tarfile.open(fileobj=stream, mode='w:gz') as tar:
for name, content in files.items():
entry = tarfile.TarInfo(name)
entry.size = len(content)
tar.addfile(entry, io.BytesIO(content))
return stream.getvalue()
class PreloadSnapshotTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
for group in snapshots.INPUTS:
for name in snapshots.source_files(group):
if name.startswith('@'):
continue
target = self.root / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes((snapshots.ROOT / name).read_bytes())
recipe = 'tools/components/build-arm64-preloads.sh'
(self.root / recipe).parent.mkdir(parents=True, exist_ok=True)
(self.root / recipe).write_text((snapshots.ROOT / recipe).read_text())
x86 = 'tools/components/recipes/x86-preloads.sh'
(self.root / x86).parent.mkdir(parents=True, exist_ok=True)
(self.root / x86).write_text((snapshots.ROOT / x86).read_text())
(self.root / '.github/workflows').mkdir(parents=True)
(self.root / '.github/workflows/build.yml').write_text((snapshots.ROOT / '.github/workflows/build.yml').read_text())
self.git('init', '-q')
self.git('config', 'core.autocrlf', 'false')
self.git('config', 'user.name', 'Test Developer')
self.git('config', 'user.email', 'test@example.test')
self.git('add', '.')
self.binaries = {name: ('verified-' + name).encode() for name in snapshots.FILES}
self.content = archive(self.binaries)
self.data = dict(version=1, origin=dict(run=123, commit='abc', artifact=456, artifact_sha256='0' * 64),
groups={g: snapshots.source_files(g, self.root) for g in snapshots.INPUTS},
arm64_recipe=snapshots.recipe(self.root), x86_recipe=snapshots.x86_recipe(self.root),
archive_sha256=snapshots.sha(self.content),
outputs={name: snapshots.sha(value) for name, value in self.binaries.items()})
(self.root / snapshots.LOCK).write_text(json.dumps(self.data))
self.git('add', '.')
self.git('commit', '-qm', 'Approved baseline')
self.base = self.git('rev-parse', 'HEAD').strip()
def git(self, *args):
return subprocess.check_output(['git', *args], cwd=self.root, text=True)
def restore(self, base=None):
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=base or self.base, FORCE_PRELOAD_REBUILD='false'), \
patch.object(snapshots.urllib.request, 'urlopen', return_value=io.BytesIO(self.content)) as fetch, \
patch.object(snapshots, 'prove_origin') as prove:
result = snapshots.restore(self.root)
return result, fetch, prove
def test_reviewed_unchanged_binaries_reused_and_written(self):
result, fetch, prove = self.restore()
self.assertEqual(result, dict(arm64=True, x86=True))
prove.assert_not_called()
fetch.assert_called_once()
for name, value in self.binaries.items():
self.assertEqual((self.root / 'app/src/main/assets/linuxfs' / name).read_bytes(), value)
def test_changed_common_source_requires_both_rebuilds(self):
path = self.root / snapshots.COMMON[0]
path.write_text(path.read_text() + '\n// Changed source\n')
result, fetch, prove = self.restore()
self.assertEqual(result, dict(arm64=False, x86=False))
fetch.assert_not_called()
prove.assert_not_called()
def test_new_transitive_header_is_an_input(self):
source = self.root / snapshots.COMMON[0]
source.write_text(source.read_text() + '\n#include "new-header.h"\n')
header = source.with_name('new-header.h')
header.write_text('#define VALUE 1\n')
before = snapshots.source_files('arm64', self.root)
header.write_text('#define VALUE 2\n')
self.assertNotEqual(before, snapshots.source_files('arm64', self.root))
def test_changed_recipe_rebuilds_arm64_only(self):
script = self.root / 'tools/components/build-arm64-preloads.sh'
script.write_text(script.read_text().replace('-O2', '-O3'))
result, _, _ = self.restore()
self.assertEqual(result, dict(arm64=False, x86=True))
def test_shell_newline_changes_invalidate_the_recipe(self):
script = self.root / 'tools/components/build-arm64-preloads.sh'
original = snapshots.recipe(self.root)
text = script.read_text()
script.write_text(text.replace('-ldl\n', '-ldl ', 1))
self.assertNotEqual(original, snapshots.recipe(self.root))
def test_x86_container_recipe_changes_rebuild_x86_only(self):
script = self.root / 'tools/components/recipes/x86-preloads.sh'
script.write_text(script.read_text().replace('debian:bullseye', 'debian:bookworm'))
result, _, _ = self.restore()
self.assertEqual(result, dict(arm64=True, x86=False))
def test_new_manifest_requires_main_build_proof(self):
result, _, prove = self.restore('HEAD~1')
self.assertTrue(all(result.values()))
prove.assert_called_once()
def test_forged_binary_hash_cannot_prove_main_origin(self):
data = copy.deepcopy(self.data)
data['outputs']['libblsession.so'] = 'f' * 64
with patch.object(snapshots, 'artifact_bytes', return_value=self.binaries):
with self.assertRaisesRegex(ValueError, 'original main APK'):
snapshots.prove_origin(data, self.root / 'cache', self.root)
def test_modified_input_policy_cannot_omit_producer_dependencies(self):
data = copy.deepcopy(self.data)
data['origin']['commit'] = self.base
omitted = snapshots.COMMON[0]
del data['groups']['arm64'][omitted]
with patch.object(snapshots, 'artifact_bytes', return_value=self.binaries), \
patch.object(snapshots, 'run', return_value=b''), \
patch.object(snapshots, 'source_files', side_effect=lambda group, *args: data['groups'][group]), \
patch.object(snapshots, 'producer_sources', return_value={omitted: self.data['groups']['arm64'][omitted]}):
with self.assertRaisesRegex(ValueError, 'producing build dependency'):
snapshots.prove_origin(data, self.root / 'cache', self.root)
def test_non_main_or_unsuccessful_run_cannot_prove_origin(self):
build = dict(event='pull_request', head_branch='main', conclusion='success', head_sha='abc',
path='.github/workflows/build.yml', repository=dict(full_name=snapshots.REPO))
for field, value in [('event', 'pull_request'), ('head_branch', 'feature'), ('conclusion', 'failure')]:
candidate = dict(build, event='push')
candidate[field] = value
with self.subTest(field=field), patch.object(snapshots, 'api', return_value=candidate):
with self.assertRaisesRegex(ValueError, 'successful main'):
snapshots.artifact_bytes(self.data['origin'], self.root / 'cache')
def test_corrupt_archive_is_rejected(self):
with self.assertRaisesRegex(ValueError, 'checksum'):
snapshots.unpack(self.content + b'tampered', self.data)
def test_missing_duplicate_or_traversal_entries_are_rejected(self):
for files in ({}, {'../escaped': b'bad'}, {'libblsession.so': b'wrong'}):
content = archive(files)
data = dict(self.data, archive_sha256=snapshots.sha(content))
with self.subTest(names=list(files)), self.assertRaises(ValueError):
snapshots.unpack(content, data)
def test_missing_release_rebuilds_without_accepting_unverified_bytes(self):
error = snapshots.urllib.error.HTTPError('https://example.test', 404, 'missing', {}, None)
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
patch.object(snapshots.urllib.request, 'urlopen', side_effect=error):
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
def test_transient_download_failure_rebuilds_but_integrity_errors_fail(self):
errors = [TimeoutError('network'), snapshots.http.client.IncompleteRead(b'partial'),
snapshots.urllib.error.HTTPError('https://example.test', 503, 'unavailable', {}, None)]
for error in errors:
with self.subTest(error=type(error).__name__), patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
patch.object(snapshots.urllib.request, 'urlopen', side_effect=error):
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
patch.object(snapshots.urllib.request, 'urlopen', return_value=io.BytesIO(b'corrupt')):
with self.assertRaisesRegex(ValueError, 'checksum'):
snapshots.restore(self.root)
def test_manual_rebuild_does_not_download_snapshot(self):
with patch.dict(os.environ, FORCE_PRELOAD_REBUILD='true'), \
patch.object(snapshots.urllib.request, 'urlopen') as fetch:
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
fetch.assert_not_called()
+91
View File
@@ -0,0 +1,91 @@
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
TOOLS = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(TOOLS))
from proot_profile import report
PROGRAM = r'''
#define _POSIX_C_SOURCE 200809L
#define PR_NB_SYSNUM 2
#define PR_void 0
static const char *stringify_sysnum(unsigned nr) { return nr ? "openat" : "void"; }
#include "profile.h"
int main(void) {
errno = EBUSY;
if (!dd_profile_enabled()) return errno != EBUSY;
if (errno != EBUSY) return 1;
for (int i = 0; i < 40; i++) {
uint64_t wall = dd_profile_clock(CLOCK_MONOTONIC);
uint64_t cpu = dd_profile_clock(CLOCK_THREAD_CPUTIME_ID);
struct timespec wait = {0, 2000000};
nanosleep(&wait, 0);
errno = ENOENT;
dd_profile_record(1, 1, 123, wall, cpu);
if (errno != ENOENT) return 2;
if (i == 9) dd_profile_flush();
}
dd_profile_next = 0;
uint64_t wall = dd_profile_clock(CLOCK_MONOTONIC);
uint64_t cpu = dd_profile_clock(CLOCK_THREAD_CPUTIME_ID);
struct timespec wait = {0, 2000000};
nanosleep(&wait, 0);
dd_profile_record(1, 0, 123, wall, cpu);
return 0;
}
'''
class ProotProfileTest(unittest.TestCase):
def test_snapshots_replace_instead_of_adding_and_tracers_remain_separate(self):
with tempfile.TemporaryDirectory() as work:
path = Path(work) / 'session.log'
records = [dict(type='total', tracer=1, id=2, syscall='statx', stage='enter',
calls=n, wall_ns=n * 100, cpu_ns=n * 10, max_ns=100) for n in (3, 8)]
records.append(dict(records[-1], tracer=2, calls=2))
records += [dict(type='total', tracer=1, id=3, syscall='openat', stage='enter',
calls=1, wall_ns=1000, cpu_ns=1000, max_ns=1000)]
path.write_text('unrelated log\n' + ''.join('prefix proot-profile ' + json.dumps(r) + '\n' for r in records)
+ 'proot-profile {truncated\n' + 'proot-profile []\n')
result = report(path)
self.assertEqual(11, result['handlerCalls'])
self.assertEqual(3, result['tracers'])
self.assertEqual(2, result['malformedRecords'])
self.assertEqual('statx', result['totals'][0]['syscall'])
self.assertEqual('openat', report(path, 'cpu')['totals'][0]['syscall'])
def test_empty_logs_do_not_claim_profiling_ran(self):
with tempfile.TemporaryDirectory() as work:
path = Path(work) / 'session.log'
path.write_text('ordinary log\n')
self.assertFalse(report(path)['hasCounters'])
@unittest.skipUnless(shutil.which('cc'), 'C compiler required')
def test_real_clocks_bounded_samples_errno_and_periodic_counters(self):
with tempfile.TemporaryDirectory() as work:
source = Path(work) / 'profile.c'
binary = Path(work) / 'profile'
source.write_text(PROGRAM)
subprocess.run(['cc', '-Wall', '-Wextra', '-Werror', '-std=c99', '-I', str(TOOLS / 'proot'),
str(source), '-o', str(binary)], check=True)
for setting in ('0', ''):
result = subprocess.run([binary], env=dict(os.environ, PROOT_PROFILE=setting), capture_output=True, check=True)
self.assertEqual(b'', result.stderr)
result = subprocess.run([binary], env=dict(os.environ, PROOT_PROFILE='1', PROOT_PROFILE_SLOW_US='1'),
capture_output=True, check=True)
rows = [json.loads(line.split(b'proot-profile ', 1)[1]) for line in result.stderr.splitlines()]
self.assertEqual(33, sum(row['type'] == 'slow' for row in rows))
self.assertEqual(32, sum(row['type'] == 'slow' and row['stage'] == 'enter' for row in rows))
self.assertGreater(sum(row['type'] == 'total' for row in rows), 2)
log = Path(work) / 'session.log'
log.write_bytes(result.stderr)
counters = report(log)
self.assertEqual(41, counters['handlerCalls'])
self.assertGreater(counters['handlerWallNs'], counters['handlerCpuNs'])
self.assertEqual(40, counters['totals'][0]['calls'])
-2
View File
@@ -10,8 +10,6 @@ ROOT = Path(__file__).resolve().parents[2]
@unittest.skipUnless(shutil.which('cc'), 'needs a C compiler')
class QuestBackdropTest(unittest.TestCase):
def test_first_frame_requires_client_content(self):
# Compile the real entrypoint with controlled layer outcomes, including the
# successful no-op that previously dismissed the loading UI on a black frame.
source = (ROOT / 'app/src/main/cpp/waylandcomp/src/vk_present.c').read_text()
start = source.index('\nint vkp_render(')
end = source.index('\nint vkp_render_hdr(', start)
+225
View File
@@ -0,0 +1,225 @@
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
from test_output_refresh import SOURCE, extract_function
PREAMBLE = r'''
#include <assert.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
struct wl_event_source { int armed; };
static struct wl_event_source idle_source, timer_source;
static void (*idle_func)(void *);
static int idles_added, timers_armed, renders, window_requests, effects_changes, pose_changes;
static int64_t clock_ns;
static int g_display_storage;
static void *g_display = &g_display_storage;
static struct wl_event_source *g_render_idle, *g_fallback_timer;
static int g_fallback_armed, g_dirty, g_hud_fresh, framegen;
static void *g_hud_surface;
static int64_t g_dirty_since_ns, g_last_vsync_ns, g_tick_vsync_ns, g_hud_last_ns, g_rendered_tick_ns;
static int g_tick_held;
static int64_t g_hold_missed_ns;
#define HOLD_RETRY_NS 1000000000LL
static int64_t g_refresh_ns = 16666667;
volatile int g_render_on_commit = 1;
volatile int g_release_unpaced = 1;
volatile int g_fps_limit;
static int64_t g_scene_due_ns;
static atomic_int g_stream_on;
static struct { unsigned ticks, ticks_late_2ms, commit_renders; int64_t tick_late_ns, tick_late_max_ns; } g_pace;
static struct { unsigned ticks; } g_perf;
static int64_t now_ns(void) { return clock_ns; }
static void *wl_display_get_event_loop(void *display) { return display; }
static struct wl_event_source *wl_event_loop_add_idle(void *loop, void (*func)(void *), void *data) {
idles_added++; idle_func = func; return &idle_source;
}
static struct wl_event_source *wl_event_loop_add_timer(void *loop, int (*func)(void *), void *data) { return &timer_source; }
static int wl_event_source_timer_update(struct wl_event_source *source, int ms) { timers_armed++; return 0; }
static int vkp_framegen_active(void) { return framegen; }
static int vkp_apply_window_request(void) { return window_requests; }
static int vkp_effects_sync(void) { return effects_changes; }
static int vkp_cardboard_pose_changed(void) { return pose_changes; }
static void render_scene(void) { renders++; g_dirty = 0; g_dirty_since_ns = 0; g_rendered_tick_ns = g_tick_vsync_ns; g_tick_held = 0; }
static int on_fallback_timer(void *data) { return 0; }
static void on_commit_idle(void *data);
'''
CHECKS = r'''
static void run_idle(void) {
void (*func)(void *) = idle_func;
idle_func = NULL;
if (func) func(NULL);
}
static void tick(int64_t t) { clock_ns = t; on_vsync(t); }
static void arrive(int64_t after_ns) { clock_ns += after_ns; schedule_render(); }
int main(void) {
const int64_t refresh = 6944444, ms = 1000000;
int64_t t = 1000000000LL;
int r0;
unsigned c0;
g_refresh_ns = refresh;
tick(t);
assert(renders == 0);
/* a new frame is drawn when it arrives */
arrive(2 * ms);
assert(idles_added == 1 && renders == 0);
run_idle();
assert(renders == 1 && g_pace.commit_renders == 1 && g_render_idle == NULL);
/* a second frame in the same refresh is left for the next arrival, which is drawn when it arrives */
arrive(1 * ms);
assert(idles_added == 1);
tick(t += refresh);
assert(renders == 1 && g_tick_held == 1);
arrive(2 * ms);
assert(idles_added == 2);
run_idle();
assert(renders == 2 && g_pace.commit_renders == 2 && g_tick_held == 0);
/* a frame no other frame follows is drawn at the tick after next */
arrive(1 * ms);
tick(t += refresh);
assert(renders == 2);
tick(t += refresh);
assert(renders == 3 && g_pace.commit_renders == 2);
/* after that, the tick draws every frame for a second, then holds one again */
arrive(1 * ms);
tick(t += refresh);
assert(renders == 4 && g_tick_held == 0);
t += HOLD_RETRY_NS;
tick(t);
arrive(1 * ms);
run_idle();
arrive(1 * ms);
tick(t += refresh);
assert(renders == 5 && g_tick_held == 1);
arrive(2 * ms);
run_idle();
assert(renders == 6 && g_pace.commit_renders == 4);
/* one frame per refresh, starting right after a tick draw, settles on drawing every frame when it arrives */
window_requests = 1;
tick(t += refresh);
window_requests = 0;
arrive(2 * ms);
r0 = renders; c0 = g_pace.commit_renders;
for (int i = 0; i < 10; i++) {
tick(t += refresh);
arrive(2 * ms);
run_idle();
}
assert(renders - r0 == 10 && g_pace.commit_renders - c0 == 10);
/* a client that draws again only after its frame callback (sent when the scene is drawn) keeps one frame
per refresh: two held frames in 200 refreshes, about 1.4 s */
r0 = renders; c0 = g_pace.commit_renders;
{
int seen = renders - 1;
for (int i = 0; i < 200; i++) {
if (renders != seen) { seen = renders; arrive(1 * ms); run_idle(); }
tick(t += refresh);
}
}
assert(renders - r0 == 198 && g_pace.commit_renders == c0);
/* a window, effect or headset change at the tick draws the waiting frame now */
t += HOLD_RETRY_NS;
tick(t);
arrive(1 * ms);
run_idle();
r0 = renders;
arrive(1 * ms);
window_requests = 1;
tick(t += refresh);
window_requests = 0;
assert(renders - r0 == 1 && g_tick_held == 0);
/* a draw queued before the tick still draws the frame once */
tick(t += refresh);
r0 = renders; c0 = g_pace.commit_renders;
arrive(1 * ms);
tick(t += refresh);
assert(renders == r0);
run_idle();
assert(renders - r0 == 1 && g_pace.commit_renders - c0 == 1);
tick(t += refresh);
assert(renders - r0 == 1);
/* streaming switched on after the draw was queued leaves the frame for the tick */
tick(t += refresh);
r0 = renders; c0 = g_pace.commit_renders;
arrive(1 * ms);
atomic_store(&g_stream_on, 1);
run_idle();
assert(renders == r0);
tick(t += refresh);
atomic_store(&g_stream_on, 0);
assert(renders - r0 == 1 && g_pace.commit_renders == c0);
/* paced output, frame generation, the switch off and streaming keep drawing on the tick */
const int idles = idles_added;
r0 = renders;
for (int c = 0; c < 4; c++) {
tick(t += refresh);
if (c == 0) g_release_unpaced = 0;
if (c == 1) framegen = 1;
if (c == 2) g_render_on_commit = 0;
if (c == 3) atomic_store(&g_stream_on, 1);
arrive(1 * ms);
assert(idles_added == idles);
tick(t += refresh);
g_release_unpaced = 1; framegen = 0; g_render_on_commit = 1; atomic_store(&g_stream_on, 0);
assert(renders - r0 == c + 1);
}
/* without screen ticks the fallback timer still draws */
clock_ns += 200000000LL;
schedule_render();
assert(idles_added == idles && g_fallback_armed == 1 && timers_armed == 1);
puts("PASS");
return 0;
}
'''
@unittest.skipUnless(sys.platform.startswith('linux'), 'needs a Linux host compiler')
class RenderOnCommitTest(unittest.TestCase):
def test_a_new_frame_is_drawn_on_arrival_at_most_once_per_refresh(self):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
self.skipTest('A host C compiler is required')
source = SOURCE.read_text()
render = extract_function(source, 'render_scene')
self.assertIn('g_rendered_tick_ns = g_tick_vsync_ns;', render)
self.assertIn('g_tick_held = 0;', render)
functions = ('scene_held_for_limit', 'commit_renders_on', 'render_on_commit', 'schedule_render', 'scene_waits_for_game', 'on_commit_idle', 'on_vsync')
harness = PREAMBLE + '\n'.join(extract_function(source, name) for name in functions) + CHECKS
with tempfile.TemporaryDirectory(prefix='droiddeck-render-on-commit-') as folder:
path = Path(folder)
(path / 'commit.c').write_text(harness)
binary = path / 'commit-test'
build = subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror',
'-Wno-unused-parameter', '-Wno-unused-function', '-O2',
str(path / 'commit.c'), '-o', str(binary)], capture_output=True, text=True)
self.assertEqual(0, build.returncode, build.stderr)
result = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('PASS', result.stdout.strip())
if __name__ == '__main__':
unittest.main()
-1
View File
@@ -1,4 +1,3 @@
"""Exercise the Windows launcher without building an APK or contacting a phone."""
import os
from pathlib import Path
import shutil
+25 -5
View File
@@ -48,13 +48,14 @@ class RuntimeInputsTest(unittest.TestCase):
for name, (pack, rel) in runtime.PACKED_FILES.items():
proot[rel] = (self.root / runtime.outputs(self.root)[name]).read_bytes()
self.pack('proot', proot)
self.pack('butler', {'butler': b'butler', '7z.so': b'7z', 'libc7zip.so': b'c7zip', 'version': b'15.32.0'})
for prefix, name in runtime.PACKED.items():
base = self.root / 'app/src/main' / prefix
self.pack(name, {path.relative_to(base).as_posix(): path.read_bytes() for path in base.rglob('*') if path.is_file()})
@staticmethod
def in_apk(name):
return name not in runtime.PACKED_FILES and not any(name.startswith(prefix) for prefix in runtime.PACKED)
return not any(name.startswith(prefix) for prefix in runtime.PACKED)
def audio(self, names):
stream = io.BytesIO()
@@ -66,10 +67,16 @@ class RuntimeInputsTest(unittest.TestCase):
path = self.root / 'app/build/prepared-assets/pulseaudio.tzst'
path.write_bytes(subprocess.run(['zstd', '-q', '-c'], input=stream.getvalue(), capture_output=True, check=True).stdout)
def test_an_apk_still_carrying_proot_is_rejected(self):
apk = self.current_apk(self.root / 'bundled.apk', {'lib/arm64-v8a/libproot.so': b'prepared runtime'})
with self.assertRaisesRegex(ValueError, 'proot pack provides'):
runtime.check_apk(apk, self.root)
def test_an_apk_must_carry_the_matching_proot_loader_pair(self):
apk = self.current_apk(self.root / 'bundled.apk')
runtime.check_apk(apk, self.root)
missing = self.root / 'missing-loader.apk'
with ZipFile(apk) as source, ZipFile(missing, 'w') as destination:
for name in source.namelist():
if name != 'lib/arm64-v8a/libproot-loader.so':
destination.writestr(name, source.read(name))
with self.assertRaisesRegex(ValueError, 'kernel-executed runtime input'):
runtime.check_apk(missing, self.root)
def test_missing_proot_cannot_be_attested(self):
(self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').unlink()
@@ -87,6 +94,19 @@ class RuntimeInputsTest(unittest.TestCase):
with self.assertRaisesRegex(ValueError, 'sources changed'):
runtime.verify(self.root)
def test_dev_loop_can_reuse_assets_from_other_sources(self):
runtime.record(self.root)
self.source.write_text('new implementation')
with mock.patch('sys.stderr', new_callable=io.StringIO) as stderr:
runtime.verify(self.root, stale_sources_ok=True)
self.assertIn('tools/linuxfs/preload/test.c', stderr.getvalue())
def test_dev_loop_still_rejects_changed_outputs(self):
runtime.record(self.root)
(self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').write_bytes(b'old APK binary')
with self.assertRaisesRegex(ValueError, 'changed runtime input'):
runtime.verify(self.root, stale_sources_ok=True)
def test_changed_quest_source_rejects_old_outputs(self):
source = self.root / 'tools/quest/kgsl_compat.c'
source.parent.mkdir(parents=True)
+105
View File
@@ -0,0 +1,105 @@
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SOURCE = ROOT / 'app/src/main/cpp/waylandcomp/src/compositor.c'
def extract_function(source, name):
match = re.search(r'^static int(?:64_t)? ' + re.escape(name) + r'\([^;]*?\) \{', source, re.MULTILINE)
if match is None:
raise AssertionError(f'Missing compositor function: {name}')
cursor, depth = match.end(), 1
while depth:
depth += (source[cursor] == '{') - (source[cursor] == '}')
cursor += 1
return source[match.start():cursor]
HARNESS = r'''
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
@FUNCTIONS@
#define MS 1000000LL
static int run(int64_t refresh, int limit, int64_t jitter, int seconds) {
int renders = 0;
int64_t due = 0;
for (int64_t tick = 0; tick * refresh < seconds * 1000 * MS; tick++) {
int64_t now = 1000 * MS + tick * refresh + (tick % 2 ? jitter : -jitter);
if (scene_held_for_limit(now, due, limit, 0)) continue;
renders++;
due = scene_next_due(now, due, limit);
}
return renders;
}
int main(void) {
assert(!scene_held_for_limit(100 * MS, 0, 30, 0));
assert(!scene_held_for_limit(100 * MS, 130 * MS, 0, 0));
assert(!scene_held_for_limit(100 * MS, 130 * MS, 30, 1));
assert(scene_held_for_limit(100 * MS, 130 * MS, 30, 0));
assert(scene_held_for_limit(100 * MS, 102 * MS, 30, 0));
assert(!scene_held_for_limit(100 * MS, 101 * MS, 30, 0));
assert(!scene_held_for_limit(100 * MS, 90 * MS, 30, 0));
assert(scene_next_due(100 * MS, 0, 0) == 0);
assert(scene_next_due(100 * MS, 0, 25) == 140 * MS);
assert(scene_next_due(101 * MS, 100 * MS, 25) == 140 * MS);
assert(scene_next_due(200 * MS, 100 * MS, 25) == 240 * MS);
const struct { int64_t refresh; int limit; int expected; } cases[] = {
{8333333, 30, 300}, {8333333, 60, 600}, {16666667, 25, 250}, {16666667, 30, 300},
{10000000, 30, 300}, {11111111, 30, 300}, {6944444, 40, 400}, {16666667, 120, 600},
};
for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) {
int got = run(cases[i].refresh, cases[i].limit, 0, 10);
if (got > cases[i].expected + 1 || got < cases[i].expected - 2) {
fprintf(stderr, "refresh %lld limit %d: %d renders, expected %d\n",
(long long)cases[i].refresh, cases[i].limit, got, cases[i].expected);
return 1;
}
got = run(cases[i].refresh, cases[i].limit, 300000, 10);
if (got > cases[i].expected + 1 || got < cases[i].expected - 2) {
fprintf(stderr, "jittered refresh %lld limit %d: %d renders, expected %d\n",
(long long)cases[i].refresh, cases[i].limit, got, cases[i].expected);
return 1;
}
}
int fallback = run(8 * MS, 30, 0, 10);
assert(fallback <= 301 && fallback >= 290);
puts("PASS");
return 0;
}
'''
class SceneLimitHoldTest(unittest.TestCase):
def test_a_frame_limit_holds_scene_renders_to_its_rate_without_slowing_paced_frames(self):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
self.skipTest('A host C compiler is required')
source = SOURCE.read_text()
harness = HARNESS.replace('@FUNCTIONS@', '\n'.join(extract_function(source, name) for name in ('scene_held_for_limit', 'scene_next_due')))
with tempfile.TemporaryDirectory(prefix='droiddeck-scene-hold-') as folder:
path = Path(folder)
(path / 'hold.c').write_text(harness)
binary = path / 'hold-test'
build = subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror', '-O2',
str(path / 'hold.c'), '-o', str(binary)],
capture_output=True, text=True)
self.assertEqual(0, build.returncode, build.stderr)
result = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('PASS', result.stdout.strip())
if __name__ == '__main__':
unittest.main()
+48
View File
@@ -45,6 +45,26 @@ VULKAN_SCRIPT = r'''"InstallScript"
'''
UBISOFT_SCRIPT = r'''"installscript"
{
"Run Process"
{
"Ubisoft Connect PC Client"
{
"process 1" "%INSTALLDIR%\\UbisoftConnectInstaller.exe"
"command 1" "/S"
}
"easyanticheat"
{
"HasRunKey" "HKEY_CURRENT_USER\\SOFTWARE\\Ubisoft\\WatchDogs2EAC"
"process 1" "%INSTALLDIR%\\EasyAntiCheat\\InstallWD2EAC.cmd"
"command 1" ""
}
}
}
'''
class SeedRedistsTest(unittest.TestCase):
def setUp(self):
self.tmp = Path(tempfile.mkdtemp())
@@ -353,5 +373,33 @@ class SeedRedistsTest(unittest.TestCase):
self.assertEqual(self.reg.read_text().count('"Vulkan111080"'), 2)
def ubisoft(self):
script = self.tmp / "legacycompat/evaluatorscript_447040.vdf"
script.parent.mkdir(parents=True)
script.write_text('"evaluatorscript"\n{\n\t"appid"\t\t"447040"\n\t"1"\n'
+ "\n".join("\t" + line for line in UBISOFT_SCRIPT.splitlines()) + "\n}\n")
env = dict(os.environ, DROIDDECK_LIBRARIES="", DROIDDECK_SEED_SCRIPTS=str(script))
return subprocess.run(["bash", str(SCRIPT), str(self.steam)], env=env, capture_output=True, text=True, check=True)
def test_the_ubisoft_connect_installer_is_left_to_run(self):
self.ubisoft()
self.assertIn('"easyanticheat"=dword:00000001', self.user.read_text())
self.assertNotIn("Ubisoft Connect PC Client", self.reg.read_text())
self.assertNotIn("Ubisoft Connect PC Client", (self.reg.parent / ".droiddeck-redists").read_text())
def test_a_ubisoft_connect_marker_an_earlier_seeding_wrote_comes_out(self):
marker = "HKEY_LOCAL_MACHINE\\\\Software\\\\Valve\\\\Steam\\\\Apps\\\\447040\tUbisoft Connect PC Client"
self.reg.write_text(
"WINE REGISTRY Version 2\n"
"\n[Software\\\\Valve\\\\Steam\\\\Apps\\\\447040] 1791729070\n"
'"Ubisoft Connect PC Client"=dword:00000001\n'
"\n[Software\\\\Wow6432Node\\\\Valve\\\\Steam\\\\Apps\\\\447040] 1791729070\n"
'"Ubisoft Connect PC Client"=dword:00000001\n')
(self.reg.parent / ".droiddeck-redists").write_text(marker + "\n")
result = self.ubisoft()
self.assertNotIn("Ubisoft Connect PC Client", self.reg.read_text())
self.assertNotIn("Ubisoft Connect PC Client", (self.reg.parent / ".droiddeck-redists").read_text())
self.assertIn("Ubisoft Connect markers out", result.stdout)
if __name__ == "__main__":
unittest.main()
+7
View File
@@ -43,3 +43,10 @@ class SessionAssetsTest(unittest.TestCase):
assets.check_extraction(self.overlay, ''))
mapping = '"usr/local/bin/droiddeck-login1" to "usr/local/bin/droiddeck-login1"'
self.assertEqual([], assets.check_extraction(self.overlay, mapping))
def test_helper_extracted_under_another_name_counts(self):
mapping = '"usr/local/bin/droiddeck-login1" to "usr/local/bin/login1"'
self.assertEqual([], assets.check_extraction(self.overlay, mapping))
mapping = '"usr/local/bin/droiddeck-login1" to "usr/local/bin/sub/login1"'
self.assertEqual(['not extracted at session start: usr/local/bin/droiddeck-login1'],
assets.check_extraction(self.overlay, mapping))
+14 -7
View File
@@ -1,4 +1,3 @@
"""The XTest cursor path is enabled only for Steam sessions."""
import os
from pathlib import Path
import re
@@ -8,29 +7,37 @@ import unittest
SESSION = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session'
SOURCE = SESSION.read_text()
MATCH = re.search(
r'^ if \[ "\$mode" = steam \]; then\n.*?^ fi\n', SOURCE, re.M | re.S)
r'^ if \[ "\$mode" = steam \] && \[ "\$\{BL_CURSOR_COMPOSITE:-0\}" = 1 \]; then\n.*?^ fi\n',
SOURCE, re.M | re.S)
assert MATCH is not None
CURSOR_SETUP = MATCH.group(0)
class SessionCursorRoutingTest(unittest.TestCase):
def run_setup(self, mode, initial=None):
def run_setup(self, mode, initial=None, composite=None):
env = dict(os.environ, mode=mode)
if initial is not None:
env['gamescope_cursor_composite'] = initial
else:
env.pop('gamescope_cursor_composite', None)
if composite is not None:
env['BL_CURSOR_COMPOSITE'] = composite
else:
env.pop('BL_CURSOR_COMPOSITE', None)
result = subprocess.run(
['bash', '-c', CURSOR_SETUP + 'printf "%s\n" "${gamescope_cursor_composite-}"'],
env=env, text=True, capture_output=True, check=True)
return result.stdout.strip()
def test_steam_enables_manual_composition_for_the_nested_cursor(self):
self.assertEqual('2', self.run_setup('steam'))
def test_steam_leaves_the_cursor_with_the_host_unless_asked(self):
self.assertEqual('', self.run_setup('steam'))
def test_steam_composites_the_cursor_when_the_setting_is_on(self):
self.assertEqual('2', self.run_setup('steam', composite='1'))
def test_other_sessions_keep_their_existing_cursor_composition(self):
self.assertEqual('', self.run_setup('desktop'))
self.assertEqual('1', self.run_setup('run', '1'))
self.assertEqual('', self.run_setup('desktop', composite='1'))
self.assertEqual('1', self.run_setup('run', '1', composite='1'))
if __name__ == '__main__':
+1 -7
View File
@@ -1,9 +1,3 @@
"""Exercise the literal launch prefix from SessionService without an Android runtime.
This is a focused source/exec regression check, not a Kotlin or PRoot integration
test. PRoot executes the first guest argument directly; KEY=VALUE is meaningful
only after /usr/bin/env. Read the production prefix rather than duplicate it.
"""
import json
from pathlib import Path
import re
@@ -22,7 +16,7 @@ PACKAGES = [line.split()[1] for line in VARIANTS.read_text().splitlines()
def guest_prefix(package):
source = SERVICE.read_text()
before_environment = source.split("val guest = ArrayList<String>()", 1)[1].split(
"addClientEnvironment(guest, steamHere)", 1)[0]
"addClientEnvironment(guest,", 1)[0]
environment = source.split("private fun addClientEnvironment(", 1)[1].split(
'guest.add("HOME=/root")', 1)[0]
prefix = []
+2 -2
View File
@@ -9,8 +9,8 @@ SESSION = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/d
def log_redirect():
match = re.search(r"^\s*(exec > >\(.*\) 2>&1)$", SESSION.read_text(), re.M)
return match.group(1)
match = re.search(r"^\s*(dd=.*)\n\s*(exec > >\(.*\) 2>&1)$", SESSION.read_text(), re.M)
return "%s\n%s" % match.groups()
class SessionLogTest(unittest.TestCase):
+63
View File
@@ -0,0 +1,63 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SESSION = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session'
class SessionPacingTest(unittest.TestCase):
def launch(self, refresh, **overrides):
source = SESSION.read_text()
start = source.index(' width=${BL_WIDTH:-1280}')
end = source.index('\nfi\n\nbl_displays()', start)
block = source[start:end].replace('/usr/local/bin/droiddeck-pipewire', 'pipewire_stub')
with tempfile.TemporaryDirectory() as directory:
env = dict(os.environ, XDG_RUNTIME_DIR=directory, BL_REFRESH=str(refresh),
BL_MANGOAPP='0', BL_FPS='0', DROIDDECK_GAME_FRAME_LIMIT='0')
env.pop('GAMESCOPE_NESTED_REFRESH_FOLLOWS_HOST', None)
env.update(overrides)
script = '''
set -u
mode=steam
pipewire_stub() { return 1; }
run_gamescope() { printf 'ARG:%s\\n' "$@"; printf 'FOLLOW:%s\\n' "${GAMESCOPE_NESTED_REFRESH_FOLLOWS_HOST:-}"; }
''' + block
result = subprocess.run(['bash', '-c', script], env=env, capture_output=True,
text=True, timeout=5)
self.assertEqual(0, result.returncode, result.stderr)
self.follow = [line.removeprefix('FOLLOW:') for line in result.stdout.splitlines()
if line.startswith('FOLLOW:')]
return [line.removeprefix('ARG:') for line in result.stdout.splitlines()
if line.startswith('ARG:')]
def test_display_refresh_sets_the_nested_compositor_rate(self):
for refresh in (60, 90, 120):
with self.subTest(refresh=refresh):
args = self.launch(refresh)
self.assertEqual(str(refresh), args[args.index('-r') + 1])
self.assertIn('-e', args)
def test_gamescope_follows_the_panel_after_the_starting_rate(self):
self.launch(144)
self.assertEqual(['1'], self.follow)
self.launch(0)
self.assertEqual([''], self.follow)
def test_retired_cap_variables_do_not_override_the_display_rate(self):
for performance in ('0', '1'):
with self.subTest(performance=performance):
args = self.launch(120, BL_FPS='30', DROIDDECK_GAME_FRAME_LIMIT=performance)
self.assertEqual('120', args[args.index('-r') + 1])
self.assertNotIn('--framerate-limit', args)
def test_retired_cap_cannot_set_the_rate_when_the_display_rate_is_unknown(self):
args = self.launch(0, BL_FPS='30', DROIDDECK_GAME_FRAME_LIMIT='1')
self.assertNotIn('-r', args)
self.assertNotIn('--framerate-limit', args)
if __name__ == '__main__':
unittest.main()
+27
View File
@@ -0,0 +1,27 @@
import re
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
PACKAGE = 'com.droiddeck.launcher'
class SignApkRenamesTest(unittest.TestCase):
def test_variants_rename_every_authority_and_task_named_after_the_package(self):
manifest = (ROOT / 'app/src/main/AndroidManifest.xml').read_text()
names = set()
for attribute in ('authorities', 'taskAffinity'):
for value in re.findall(r'android:%s="([^"]*)"' % attribute, manifest):
self.assertNotIn(';', value, 'sign-apk.sh renames whole values, so give each authority its own provider')
name = value.strip().replace('${applicationId}', PACKAGE)
if name.startswith(PACKAGE + '.'):
names.add(name[len(PACKAGE):])
self.assertIn('.agent', names)
script = (ROOT / 'tools/release/sign-apk.sh').read_text()
renamed = set(re.findall(r'"\$P(\.[A-Za-z0-9_.-]+)=\$pkg\1"', script))
self.assertEqual(set(), names - renamed, 'add these to the axml_rename call in tools/release/sign-apk.sh')
if __name__ == '__main__':
unittest.main()
-2
View File
@@ -1,4 +1,3 @@
"""Retry a dropped shortcut without duplicating a launch that is still preparing its first window."""
import os
from pathlib import Path
import subprocess
@@ -29,7 +28,6 @@ class SteamColdLaunchTest(unittest.TestCase):
self.agent_count = self.root / "agent-count"
self.focus_count = self.root / "focus-count"
# FOCUS lists the focused app per xprop call; the last value repeats.
xprop = self.bin / "xprop"
xprop.write_text(
"#!/bin/sh\n"
@@ -45,6 +45,68 @@ class SteamCompatEnvironmentTest(unittest.TestCase):
client_path += ':' + inherited['LD_LIBRARY_PATH']
self.assertEqual(result.stdout.splitlines(), ['1', client_path])
def test_session_exit_stops_the_library_helper(self):
source = (BIN / 'droiddeck-session').read_text()
launch = next(line for line in source.splitlines()
if 'droiddeck-steam-compat "$steam_root"' in line)
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
pid = root / 'pid'
helper = root / 'droiddeck-steam-compat'
helper.write_text('#!/bin/sh\necho $$ > "$PID_FILE"\nexec sleep 30\n')
helper.chmod(0o755)
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
PID_FILE=str(pid), steam_root='/steam', steam_x64='0')
subprocess.run(['bash', '-c', '_bl_compat_env=(env)\n' + launch + '\n'
'until [ -s "$PID_FILE" ]; do sleep 0.05; done\n'
'kill $(jobs -p)\nwait'],
env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=10)
helper_pid = int(pid.read_text())
try:
os.kill(helper_pid, 0)
except ProcessLookupError:
return
os.kill(helper_pid, 9)
self.fail('droiddeck-steam-compat outlived the session')
def test_the_x86_64_client_gets_only_the_file_dialog_bridge(self):
source = (BIN / 'droiddeck-session').read_text()
start = source.index(' if [ "$steam_x64" = 1 ]; then\n { droiddeck-steam-compat --files-only')
block = source[start:source.index(' fi\n', start) + len(' fi\n')]
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
helper = root / 'droiddeck-steam-compat'
helper.write_text('#!/bin/sh\necho "$@" >> "$ARGS_FILE"\n')
helper.chmod(0o755)
for x64, expected in (('1', '--files-only /steam'), ('0', '/steam')):
with self.subTest(x64=x64):
args = root / ('args-' + x64)
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
ARGS_FILE=str(args), steam_root='/steam', steam_x64=x64)
subprocess.run(['bash', '-c', '_bl_compat_env=(env)\n' + block + 'wait\n'], env=env, timeout=10, check=True)
self.assertEqual(args.read_text().split('\n')[0], expected)
def test_files_only_mode_installs_only_the_file_dialog_hook(self):
import runpy
compat = runpy.run_path(str(BIN / 'droiddeck-steam-compat'))
calls = []
class Page:
def __init__(self, port, path):
self.ws = None
def evaluate(self, expression):
calls.append(('evaluate', expression[:40]))
return True
def call(self, method, params):
calls.append((method, params['name']))
helper = compat['Helper']('/steam', 1234, files_only=True)
helper.attach.__globals__.update(shared_context=lambda port: '/devtools/page/1', Page=Page)
self.assertTrue(helper.attach())
bindings = [name for method, name in calls if method == 'Runtime.addBinding']
self.assertEqual(bindings, [compat['FILE_BINDING']])
self.assertIn(('evaluate', compat['FILE_JS'][:40]), calls)
if __name__ == '__main__':
unittest.main()
+108
View File
@@ -641,6 +641,114 @@ class ReconcileTest(unittest.TestCase):
self.assertEqual(page.mapping["10"], "GE-Proton9-1")
self.assertEqual(state["auto"], {"11": TOOL})
def games(self):
return json.loads((self.state / "games.json").read_text())["games"]
def write_games(self, games):
(self.state / "games.json").write_text(json.dumps({"version": 1, "games": games}))
def test_a_steam_pick_after_the_app_choice_is_kept_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
state = self.reconcile(page)
self.assertEqual(page.sets, [("10", TOOL_11)])
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
page.sets.clear()
page.mapping["10"] = COMPAT["FEX_TOOL"]
state = self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
self.assertEqual(self.games(), {})
self.assertEqual(state["games_applied"], {})
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
self.assertEqual(page.sets, [("10", TOOL_11)])
def test_a_steam_pick_of_our_proton_becomes_the_app_choice_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
page.sets.clear()
page.mapping["10"] = TOOL
state = self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(self.games(), {"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
self.assertEqual(state["games_applied"], self.games())
def test_use_the_default_applies_once_then_steam_picks_stand_live(self):
page = FakePage({"0": TOOL, "10": TOOL_11, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": None})
self.reconcile(page)
self.assertEqual(page.sets, [("10", TOOL)])
page.sets.clear()
self.reconcile(page)
self.assertEqual(self.games(), {})
page.mapping["10"] = COMPAT["FEX_TOOL"]
self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
def test_choices_an_earlier_build_applied_give_way_to_steam_picks_live(self):
(self.state / "state.json").write_text(json.dumps({"default": TOOL, "auto": {"11": TOOL}, "tools": []}))
self.write_games({"10": None, "11": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
page = FakePage({"0": TOOL, "10": COMPAT["FEX_TOOL"], "11": "steamlinuxruntime_sniper"},
[TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(self.games(), {})
def test_an_app_choice_written_during_a_sync_is_not_overwritten(self):
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
requests, text = COMPAT["read_game_requests"]()
self.write_games({"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
self.assertFalse(COMPAT["write_game_requests"]({}, text))
self.assertEqual(self.games(), {"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
def test_an_app_the_client_briefly_refuses_keeps_its_applied_choice_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
page.refused = {"10"}
state = self.reconcile(page)
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
page.refused = set()
page.sets.clear()
page.mapping["10"] = COMPAT["FEX_TOOL"]
self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
def test_an_app_rewriting_its_choices_during_both_tries_applies_none_of_them_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
page.sets.clear()
page.mapping["10"] = COMPAT["FEX_TOOL"]
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}, "11": None})
with mock.patch.dict(COMPAT["settled_game_requests"].__globals__, write_game_requests=lambda *_: False):
state = self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
def test_a_steam_pick_is_kept_at_the_next_start(self):
config = self.steam / "config/config.vdf"
config.parent.mkdir(parents=True)
vdf = '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }'
entry = '"%s" { "name" "%s" "config" "" "priority" "250" }'
catalog = COMPAT["tool_catalog"](str(self.steam))
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
config.write_text(vdf % (entry % ("0", TOOL) + entry % ("10", TOOL)))
with mock.patch.dict(COMPAT["register_default"].__globals__, steam_running=lambda: False):
COMPAT["register_default"](str(config), ["10"], catalog=catalog, directory=str(self.state), settle=True)
self.assertIn(entry % ("10", TOOL_11), " ".join(config.read_text().split()))
config.write_text(vdf % (entry % ("0", TOOL) + entry % ("10", COMPAT["FEX_TOOL"])))
COMPAT["register_default"](str(config), ["10"], catalog=catalog, directory=str(self.state), settle=True)
self.assertIn(entry % ("10", COMPAT["FEX_TOOL"]), " ".join(config.read_text().split()))
self.assertEqual(self.games(), {})
def test_file_mode_defers_to_the_live_helper(self):
(self.state / "live.pid").write_text("%d\n" % os.getpid())
self.assertTrue(COMPAT["live_helper_running"]())
-4
View File
@@ -50,14 +50,12 @@ CONFIG = '''"InstallConfigStore"
class X64RepointTest(unittest.TestCase):
"""steam-compatibility --x64: the x86-64 client's Proton entries go to its arm64 wrapper."""
def setUp(self):
self.home = tempfile.TemporaryDirectory()
self.root = Path(self.home.name) / "Steam"
(self.root / "config").mkdir(parents=True)
(self.root / "config" / "config.vdf").write_text(CONFIG)
# A wrapper of its own whose name starts with "proton": kept, never repointed.
own = self.root / "compatibilitytools.d" / "proton-cachyos-a64"
own.mkdir(parents=True)
(own / "compatibilitytool.vdf").write_text('"compatibilitytools"\n{\n\t"compat_tools"\n\t{\n\t\t"proton-cachyos-a64"\n\t\t{\n\t\t}\n\t}\n}\n')
@@ -82,12 +80,10 @@ class X64RepointTest(unittest.TestCase):
self.assertEqual(m["550"], "GE-Proton11-7-a64")
self.assertEqual(m["620"], "proton-cachyos-a64")
self.assertEqual(m["730"], "steamlinuxruntime")
# The arm64 client's own Proton name, which this client ignores.
self.assertEqual(m["880"], "GE-Proton11-7-a64")
self.assertEqual(m["0"], "GE-Proton11-7-a64")
def test_repoints_arm64_name_even_with_its_stray_tool(self):
# An arm64 compat pass that ran against this client also left its tool folder here.
stray = self.root / "compatibilitytools.d" / "droiddeck-proton-arm64"
stray.mkdir(parents=True)
(stray / "compatibilitytool.vdf").write_text('"compatibilitytools"\n{\n\t"compat_tools"\n\t{\n\t\t"droiddeck-proton-arm64"\n\t\t{\n\t\t}\n\t}\n}\n')
+50 -1
View File
@@ -1,4 +1,3 @@
"""Desktop Steam stays on KDE; explicit Big Picture still switches sessions."""
import fcntl
import os
from pathlib import Path
@@ -42,12 +41,38 @@ class DesktopSteamLaunchTest(unittest.TestCase):
self.assertEqual(result.stdout.splitlines(), ['1', 'desktop', ':7', 'steam', 'steam://open/library'])
self.assertFalse((self.session / 'steam-launch').exists())
def test_desktop_games_present_without_dri3(self):
(self.root / 'supervisor').write_text('#!/bin/bash\nprintf "%s\\n" "$MESA_VK_WSI_DEBUG"\n')
self.env.pop('MESA_VK_WSI_DEBUG', None)
self.assertEqual(self.launch('desktop', '945360').stdout.splitlines(), ['sw'])
self.env['MESA_VK_WSI_DEBUG'] = 'noshm'
self.assertEqual(self.launch('desktop', '945360').stdout.splitlines(), ['noshm,sw'])
self.env['MESA_VK_WSI_DEBUG'] = 'sw,noshm'
self.assertEqual(self.launch('desktop', '945360').stdout.splitlines(), ['sw,noshm'])
def test_desktop_game_url_goes_to_client(self):
result = self.launch('desktop', '501590')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines()[-1], 'steam://rungameid/501590')
self.assertFalse((self.session / 'steam-launch').exists())
def test_exit_without_a_running_client_starts_nothing(self):
result = self.launch('desktop', 'steam://exit')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout, '')
self.assertFalse((self.session / 'steam-launch').exists())
def test_shim_turns_shutdown_into_exit_url(self):
launch = self.root / 'record-launch'
launch.write_text('#!/bin/bash\nprintf "%s\\n" "$@"\n')
launch.chmod(0o755)
shim = self.root / 'shim'
shim.write_text((BIN / 'droiddeck-steam-shim').read_text().replace(
'/usr/local/bin/droiddeck-steam-launch', str(launch)))
result = subprocess.run(['bash', str(shim), '-shutdown'], env=self.env, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines(), ['desktop', 'steam://exit'])
def test_second_click_forwards_without_another_supervisor(self):
lock = self.root / 'cache/droiddeck/steam-desktop.lock'
lock.parent.mkdir(parents=True)
@@ -66,6 +91,30 @@ class DesktopSteamLaunchTest(unittest.TestCase):
'-cef-disable-gpu', 'steam://open/settings'])
self.assertFalse((self.session / 'steam-launch').exists())
def _running_client(self):
steam = self.root / 'data/Steam/steamrtarm64/steam'
steam.parent.mkdir(parents=True)
steam.write_text('#!/bin/bash\nprintf "%s\\n" "$@"\n')
steam.chmod(0o755)
(self.root / '.steam').mkdir()
os.mkfifo(self.root / '.steam/steam.pipe')
(self.root / '.steam/steam.pid').write_text(str(os.getpid()))
def test_bottom_screen_desktop_hands_games_to_the_running_client(self):
self._running_client()
self.env['BL_BOTTOM_DESKTOP'] = '1'
result = self.launch('desktop', '501590')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines(), ['-nobigpicture', '-no-cef-sandbox', '-cef-ozone-platform=x11',
'-cef-disable-gpu', 'steam://rungameid/501590'])
self.assertFalse((self.root / 'cache/droiddeck/steam-desktop.lock').exists())
def test_bottom_screen_desktop_starts_its_own_client_when_none_runs(self):
self.env.update(BL_BOTTOM_DESKTOP='1', DISPLAY=':7')
result = self.launch('desktop')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.splitlines(), ['1', 'desktop', ':7', 'steam', 'steam://open/library'])
def test_second_click_forwards_to_the_x86_64_client_in_its_own_home(self):
lock = self.root / 'cache/droiddeck/steam-desktop.lock'
lock.parent.mkdir(parents=True)
+78
View File
@@ -0,0 +1,78 @@
import re
import shlex
import socket
import subprocess
import tempfile
import unittest
from pathlib import Path
SESSION = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-session"
def devtools_function():
match = re.search(r"^steam_devtools\(\) \{\n.*?^\}\n", SESSION.read_text(), re.M | re.S)
return match.group(0) if match else None
class SteamDevtoolsPortTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
def free_port(self):
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
return sock.getsockname()[1]
def choose(self, port):
function = devtools_function()
self.assertIsNotNone(function, "droiddeck-session has no steam_devtools()")
script = function + "steam_root=%s\nsteam_devtools %d\n" % (shlex.quote(str(self.root)), port)
script += 'echo "port=$BL_CDP_PORT"\necho "args=${_bl_cdp[*]}"\n'
out = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30, check=True).stdout
lines = out.splitlines()
port_line = next(l for l in lines if l.startswith("port="))
args_line = next(l for l in lines if l.startswith("args="))
return int(port_line[5:]), args_line[5:].split(), out
def test_without_remote_debugging_steam_gets_a_private_port(self):
port, args, _ = self.choose(self.free_port())
self.assertEqual(["-cef-enable-debugging", "-devtools-port", str(port)], args)
self.assertTrue(20000 <= port < 50000)
def test_remote_debugging_keeps_the_fixed_port_when_it_is_free(self):
(self.root / ".cef-enable-remote-debugging").touch()
fixed = self.free_port()
port, args, out = self.choose(fixed)
self.assertEqual(fixed, port)
self.assertEqual([], args)
self.assertNotIn("held by another app", out)
def test_a_fixed_port_another_app_holds_falls_back_to_a_private_one(self):
(self.root / ".cef-enable-remote-debugging").touch()
with socket.socket() as other:
other.bind(("127.0.0.1", 0))
other.listen(1)
taken = other.getsockname()[1]
port, args, out = self.choose(taken)
self.assertNotEqual(taken, port)
self.assertEqual(["-cef-enable-debugging", "-devtools-port", str(port)], args)
self.assertIn("held by another app", out)
def test_a_fixed_port_bound_without_listening_also_falls_back(self):
(self.root / ".cef-enable-remote-debugging").touch()
with socket.socket() as other:
other.bind(("127.0.0.1", 0))
taken = other.getsockname()[1]
port, args, out = self.choose(taken)
self.assertNotEqual(taken, port)
self.assertEqual(["-cef-enable-debugging", "-devtools-port", str(port)], args)
def test_the_x86_64_decky_exemption_only_covers_the_fixed_port(self):
text = SESSION.read_text()
self.assertIn('[ "${decky_arch:-}" = x86_64 ] && [ ${#_bl_cdp[@]} = 0 ]', text)
if __name__ == "__main__":
unittest.main()
+142
View File
@@ -0,0 +1,142 @@
import io
import json
import os
import runpy
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
LIVE = runpy.run_path(str(BIN / "droiddeck-steam-compat"))
HARNESS = r"""
const assert = require('assert');
globalThis.window = globalThis;
const native = () => Promise.reject({ result: 2, message: '' });
const popupCallbacks = [];
const popup = { window: { SteamClient: { System: { OpenFileDialog: native } } } };
const later = { window: { SteamClient: { System: { OpenFileDialog: native } } } };
globalThis.SteamClient = { System: { OpenFileDialog: native } };
window.g_PopupManager = { GetPopups: () => [popup], AddPopupCreatedCallback: cb => popupCallbacks.push(cb) };
const asked = [];
window.droiddeckOpenFileDialog = payload => asked.push(JSON.parse(payload));
const script = require('fs').readFileSync(process.argv[2], 'utf8');
(async () => {
assert.strictEqual(eval(script), 'installed');
const orphan = SteamClient.System.OpenFileDialog({ strTitle: 'Before reconnect' });
assert.strictEqual(eval(script), 'present');
await assert.rejects(orphan, e => e.result === 2);
assert.strictEqual(window.__droiddeckFiles.done(1, '/late'), false);
asked.length = 0;
assert.notStrictEqual(SteamClient.System.OpenFileDialog, native);
assert.strictEqual(popup.window.SteamClient.System.OpenFileDialog, SteamClient.System.OpenFileDialog);
popupCallbacks.forEach(cb => cb(later));
assert.strictEqual(later.window.SteamClient.System.OpenFileDialog, SteamClient.System.OpenFileDialog);
const options = { strTitle: 'Pick', bChooseDirectory: true };
const chosen = SteamClient.System.OpenFileDialog(options);
const cancelled = later.window.SteamClient.System.OpenFileDialog({ strTitle: 'Other' });
assert.deepStrictEqual(asked, [{ id: 2, options }, { id: 3, options: { strTitle: 'Other' } }]);
assert.strictEqual(window.__droiddeckFiles.done(2, '/root/Storage/Games'), true);
assert.strictEqual(window.__droiddeckFiles.done(2, '/again'), false);
assert.strictEqual(await chosen, '/root/Storage/Games');
window.__droiddeckFiles.done(3, null);
await assert.rejects(cancelled, e => e.result === 2);
delete window.droiddeckOpenFileDialog;
await assert.rejects(SteamClient.System.OpenFileDialog({}), e => e.result === 2);
})().catch(e => { console.error(e); process.exit(1); });
"""
def bound(payload):
return {"method": "Runtime.bindingCalled", "params": {"name": LIVE["FILE_BINDING"], "payload": payload}}
class FileDialogScriptTest(unittest.TestCase):
def test_every_window_asks_the_bridge_and_gets_the_path_or_a_cancel(self):
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "files.js"
script.write_text(LIVE["FILE_JS"])
harness = Path(tmp) / "harness.js"
harness.write_text(HARNESS)
result = subprocess.run(["node", str(harness), str(script)], capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
class FileDialogBridgeTest(unittest.TestCase):
def setUp(self):
self.globals = LIVE["Helper"].attach.__globals__
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
self.helper = LIVE["Helper"](str(self.root), 1)
self.helper.page = mock.Mock()
def answer(self):
return self.helper.page.evaluate.call_args.args[0]
def test_the_chooser_runs_under_a_steam_reaper_with_the_options_on_stdin(self):
reaper = self.root / "steamrtarm64/reaper"
reaper.parent.mkdir()
reaper.write_text("#!/bin/sh\n")
reaper.chmod(0o755)
proc = mock.Mock(stdin=io.BytesIO())
proc.stdin.close = lambda: None
with mock.patch.object(self.globals["subprocess"], "Popen", return_value=proc) as popen:
self.helper.page.drain.return_value = [bound(json.dumps({"id": 3, "options": {"strTitle": "Pick"}}))]
self.helper.drain()
command = popen.call_args.args[0]
self.assertEqual(command[:4], [str(reaper), "SteamLaunch", "AppId=769", "--"])
self.assertEqual(command[-1], "--choose-file")
self.assertEqual(json.loads(proc.stdin.getvalue()), {"strTitle": "Pick"})
self.assertEqual(self.helper.chooser, (3, proc))
def test_a_second_request_while_one_is_open_is_cancelled(self):
first = mock.Mock()
self.helper.chooser = (1, first)
self.helper.open_chooser(json.dumps({"id": 2, "options": {}}))
self.assertEqual(self.answer(), "window.__droiddeckFiles?.done(2, null)")
self.assertEqual(self.helper.chooser, (1, first))
def test_the_chosen_path_or_a_cancel_goes_back_to_the_page(self):
for code, out, expected in ((0, b'"/root/Storage/My Game/game.sh"\n', '"/root/Storage/My Game/game.sh"'),
(0, b'" spaced name "\n', '" spaced name "'),
(1, b"", "null"), (2, b"", "null"), (0, b"\n", "null"), (0, b"not json\n", "null")):
proc = mock.Mock(stdout=io.BytesIO(out), returncode=code)
self.helper.chooser = (7, proc)
self.helper.chooser_done()
self.assertEqual(self.answer(), "window.__droiddeckFiles?.done(7, %s)" % expected)
self.assertIsNone(self.helper.chooser)
def test_bad_requests_are_ignored(self):
with mock.patch.object(self.globals["subprocess"], "Popen") as popen:
for payload in ("not json", json.dumps({"options": {}}), json.dumps({"id": 1, "options": []})):
self.helper.open_chooser(payload)
popen.assert_not_called()
self.helper.page.evaluate.assert_not_called()
def test_choose_file_mode_prints_the_path_and_fails_on_cancel(self):
for chooser, code, printed in ((mock.Mock(return_value="/root/Storage/a.exe "), 0, '"/root/Storage/a.exe "\n'),
(mock.Mock(return_value=None), 1, ""),
(mock.Mock(side_effect=ValueError("Namespace Gtk not available for version 3.0")), 2, ""),
(mock.Mock(side_effect=ImportError("No module named 'gi'")), 2, "")):
with mock.patch.dict(LIVE["chooser_main"].__globals__, {"choose_file": chooser}), \
mock.patch.object(sys, "stdin", io.StringIO('{"strTitle": "Pick"}')), \
mock.patch.object(sys, "stdout", io.StringIO()) as out:
self.assertEqual(LIVE["chooser_main"](), code)
self.assertEqual(out.getvalue(), printed)
def test_attach_installs_the_file_dialog_bridge(self):
page = mock.Mock()
page.evaluate.return_value = True
with mock.patch.dict(self.globals, {"shared_context": lambda port: "/devtools/page/1",
"Page": mock.Mock(return_value=page)}):
self.helper.attach()
page.call.assert_any_call("Runtime.addBinding", {"name": LIVE["FILE_BINDING"]})
page.evaluate.assert_any_call(LIVE["FILE_JS"])
if __name__ == "__main__":
unittest.main()
+116 -1
View File
@@ -113,9 +113,11 @@ class ImportsTest(unittest.TestCase):
listing = self.root / 'games.json'
listing.write_text(json.dumps([self.game]))
from unittest.mock import Mock
idle = imports.downloads_idle
result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True})))
with patch.object(imports, 'identify', side_effect=[None, 42]) as identify, \
patch.object(imports, 'request_restart'), \
patch.object(imports, 'ask_restart', return_value=True), \
patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=[0, 301]),
sleep=Mock(side_effect=[None, RuntimeError('stop')]))), \
patch.object(imports.subprocess, 'run', return_value=result), \
@@ -126,7 +128,7 @@ class ImportsTest(unittest.TestCase):
snapshot = imports.read_json(self.acct / 'config' / imports.STATE, {})
self.assertTrue(snapshot['owned']['42'])
def run_watch(self, beats, routes=None, busy=False):
def run_watch(self, beats, routes=None, busy=False, answer=True, started=False):
listing = self.root / 'games.json'
listing.write_text(json.dumps([self.game]))
if routes is not None:
@@ -134,21 +136,64 @@ class ImportsTest(unittest.TestCase):
if busy:
(self.steam / 'steamapps/downloading/7').mkdir(parents=True)
from unittest.mock import Mock
idle = imports.downloads_idle
result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True})))
with patch.object(imports, 'identify', return_value=42), \
patch.object(imports, 'game_running', return_value=False), \
patch.object(imports, 'request_restart') as restart, \
patch.object(imports, 'ask_restart', return_value=answer) as asked, \
patch.object(imports, 'downloads_idle', side_effect=lambda root: idle(root) and not (started and asked.called)), \
patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=list(range(0, 30 * beats, 30))),
sleep=Mock(side_effect=[None] * (beats - 1) + [RuntimeError('stop')]))), \
patch.object(imports.subprocess, 'run', return_value=result), \
patch('builtins.print'):
with self.assertRaisesRegex(RuntimeError, 'stop'):
imports.watch(self.steam, listing)
self.asked = asked
return restart
def test_newly_owned_game_restarts_the_client_once(self):
self.assertEqual(1, self.run_watch(3).call_count)
def test_an_import_that_did_not_take_restarts_only_once_across_sessions(self):
self.assertEqual(1, self.run_watch(3).call_count)
self.assertEqual(0, self.run_watch(3).call_count)
self.assertEqual([42], imports.read_json(self.acct / 'config' / imports.TRIED, None))
def test_later_skips_the_restart_and_does_not_ask_again(self):
self.assertEqual(0, self.run_watch(3, answer=False).call_count)
self.assertEqual([['Example']], [c.args[0] for c in self.asked.call_args_list])
self.assertEqual(0, self.run_watch(3).call_count)
self.assertEqual(0, self.asked.call_count)
def test_a_download_started_while_asking_leaves_the_import_for_the_next_start(self):
self.assertEqual(0, self.run_watch(3, started=True).call_count)
self.assertEqual(1, self.asked.call_count)
def test_the_app_answers_the_restart_question(self):
home = self.root / 'home'
folder = home / '.cache/droiddeck'
replies = []
def sleep(_):
ask = json.loads((folder / 'steam-import.json').read_text())
replies.append(ask['games'])
(folder / 'steam-import.answer').write_text(json.dumps(dict(token=ask['token'], choice=choice)))
for choice, expected in (('now', True), ('later', False)):
with patch.dict(os.environ, HOME=str(home)):
self.assertEqual(expected, imports.ask_restart(['Example'], clock=iter(range(100)).__next__, sleep=sleep))
self.assertFalse((folder / 'steam-import.json').exists())
self.assertFalse((folder / 'steam-import.answer').exists())
self.assertEqual([['Example'], ['Example']], replies)
def test_no_answer_means_later(self):
with patch.dict(os.environ, HOME=str(self.root / 'home')):
self.assertFalse(imports.ask_restart(['Example'], timeout=5, clock=iter(range(100)).__next__, sleep=lambda _: None))
def test_a_tried_import_that_took_is_forgotten(self):
imports.save_json(self.acct / 'config' / imports.TRIED, [42])
self.assertEqual(0, self.run_watch(2, routes={'1': 42}).call_count)
self.assertEqual([], imports.read_json(self.acct / 'config' / imports.TRIED, None))
def test_imported_game_does_not_restart_the_client(self):
self.assertEqual(0, self.run_watch(2, routes={'1': 42}).call_count)
@@ -346,6 +391,61 @@ class ImportsTest(unittest.TestCase):
with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError):
self.assertEqual(42, imports.identify(self.game))
def test_spacewar_appid_names_no_game(self):
(self.folder / 'steam_appid.txt').write_text('480\n')
with patch.object(imports.urllib.request, 'urlopen', side_effect=OSError):
self.assertIsNone(imports.identify(self.game))
def import_as(self, appid):
self.assertTrue(imports.seed_install(self.steam, self.game, appid))
self.assertTrue((self.steam / f'steamapps/appmanifest_{appid}.acf').is_file())
def snapshot_names(self, appid, owned):
imports.save_json(self.acct / 'config' / imports.STATE,
dict(account='123', owned={str(appid): owned},
candidates={str(self.folder): appid},
sources={str(self.folder): imports.source_stamp(self.game)}))
def assert_not_imported_as(self, appid):
self.assertFalse((self.steam / f'steamapps/appmanifest_{appid}.acf').exists())
self.assertFalse(os.path.lexists(self.steam / f'steamapps/common/DroidDeck-{appid}'))
def test_spacewar_import_is_released_and_never_made_again(self):
self.import_as(480)
self.snapshot_names(480, owned=True)
with patch('builtins.print'):
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
self.assert_not_imported_as(480)
self.assertEqual(b'game', self.exe.read_bytes())
def test_import_follows_the_app_the_files_name_now(self):
self.snapshot()
imports.route(self.steam, self.acct, [self.game])
self.snapshot_names(43, owned=True)
with patch('builtins.print'):
self.assertEqual(([], {str(self.game['appid']): 43}), imports.route(self.steam, self.acct, [self.game]))
self.assert_not_imported_as(42)
self.assertEqual(self.folder, (self.steam / 'steamapps/common/DroidDeck-43').resolve())
def test_unowned_new_appid_leaves_a_shortcut(self):
self.snapshot()
imports.route(self.steam, self.acct, [self.game])
self.snapshot_names(43, owned=False)
with patch('builtins.print'):
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
self.assert_not_imported_as(42)
self.assert_not_imported_as(43)
def test_import_stays_while_the_new_identity_is_unknown(self):
self.snapshot()
imports.route(self.steam, self.acct, [self.game])
for candidates in ({}, {str(self.folder): 43}):
imports.save_json(self.acct / 'config' / imports.STATE,
dict(account='123', owned={'43': True}, candidates=candidates,
sources={str(self.folder): ['stale']}))
imports.route(self.steam, self.acct, [self.game])
self.assertTrue((self.steam / 'steamapps/appmanifest_42.acf').is_file())
def test_manifest_identity_uses_install_directory(self):
folder = self.root / 'second/steamapps/common/Custom Name'
folder.mkdir(parents=True)
@@ -354,6 +454,21 @@ class ImportsTest(unittest.TestCase):
with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError):
self.assertEqual(42, imports.identify(game))
def test_a_spacewar_manifest_names_no_game(self):
folder = self.root / 'second/steamapps/common/PEAK'
folder.mkdir(parents=True)
(folder.parent.parent / 'appmanifest_480.acf').write_text('"AppState" { "installdir" "PEAK" }')
(folder / 'steam_appid.txt').write_text('3527290\n')
game = dict(self.game, folder=str(folder), exe=str(folder / 'PEAK.exe'))
with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError):
self.assertEqual(3527290, imports.identify(game))
def test_a_store_search_answering_spacewar_names_no_game(self):
with patch.object(imports.urllib.request, 'urlopen') as request:
request.return_value.__enter__.return_value.read.return_value = json.dumps(
{'items': [{'id': 480, 'name': 'Spacewar'}]}).encode()
self.assertIsNone(imports.identify(dict(self.game, name='Spacewar')))
def test_store_requires_unique_exact_title(self):
cases = [([{'id': 42, 'name': 'Example Deluxe'}], None),
([{'id': 42, 'name': 'Example'}, {'id': 43, 'name': 'Example'}], None),
+260 -23
View File
@@ -26,25 +26,48 @@ class SteamInstallTest(unittest.TestCase):
self.bin = self.root / 'bin'
self.bin.mkdir()
curl = self.bin / 'curl'
curl.write_text('''#!/bin/bash
set -eu
url=""
out=""
while [ $# -gt 0 ]; do
case $1 in
-o) out=$2; shift 2 ;;
--connect-timeout|--max-time|--speed-limit|--speed-time|--retry|--retry-delay) shift 2 ;;
-*) shift ;;
*) url=$1; shift ;;
esac
done
printf '%s\\n' "$url" >> "$TEST_REQUESTS"
if [ -n "${TEST_FAIL_HOST:-}" ]; then
case $url in *"$TEST_FAIL_HOST"*) exit 22 ;; esac
fi
cp "$TEST_CDN/${url##*/}" "$out"
curl.write_text('''#!/usr/bin/env python3
import os, sys
args = sys.argv[1:]
url = next(a for a in args if a.startswith('https://'))
out = args[args.index('-o') + 1]
name = url.rsplit('/', 1)[1]
offset = os.path.getsize(out) if '-C' in args and os.path.exists(out) else 0
with open(os.environ['TEST_REQUESTS'], 'a') as log:
log.write(url + '\\n')
with open(os.environ['TEST_REQUESTS'] + '.offsets', 'a') as log:
log.write(f'{name} {offset}\\n')
def done(status, code='200'):
if '-w' in args:
sys.stdout.write(code)
sys.exit(status)
host_fail = os.environ.get('TEST_FAIL_HOST')
if host_fail and host_fail in url:
done(22, '000')
action = []
plan = os.environ.get('TEST_PLAN')
if plan and os.path.exists(plan):
lines = open(plan).read().splitlines()
for n, line in enumerate(lines):
if line.split()[0] == name:
action = line.split()[1:]
del lines[n]
open(plan, 'w').write(''.join(l + '\\n' for l in lines))
break
if action[:1] == ['fail']:
done(int(action[1]), '000')
if action[:1] == ['http']:
done(22, action[1])
if action[:1] == ['norange'] and offset:
done(33)
data = open(os.path.join(os.environ['TEST_CDN'], name), 'rb').read()
end = int(action[1]) if action[:1] == ['cut'] else len(data)
with open(out, 'ab' if offset else 'wb') as target:
target.write(data[offset:end])
done(28 if end < len(data) else 0)
''')
curl.chmod(0o755)
self.plan = self.root / 'plan'
if shutil.which('sha256sum') is None:
sha = self.bin / 'sha256sum'
sha.write_text('#!/bin/bash\nexec shasum -a 256 "$@"\n')
@@ -55,8 +78,9 @@ cp "$TEST_CDN/${url##*/}" "$out"
sha1.chmod(0o755)
self.env = dict(os.environ, HOME=str(self.home), XDG_DATA_HOME=str(self.data),
PATH=str(self.bin) + os.pathsep + os.environ['PATH'],
TEST_CDN=str(self.cdn), TEST_REQUESTS=str(self.requests))
for channel in ('steamdeck_publicbeta',):
TEST_CDN=str(self.cdn), TEST_REQUESTS=str(self.requests),
TEST_PLAN=str(self.plan), BL_STEAM_RETRY_DELAYS='0 0 0')
for channel in ('steamdeck_publicbeta', 'steamdeck_stable'):
archive = self.cdn / (channel + '.zip')
with zipfile.ZipFile(archive, 'w') as package:
package.writestr('steamrtarm64/steam', channel)
@@ -68,12 +92,17 @@ cp "$TEST_CDN/${url##*/}" "$out"
f'\t\t"file" "{archive.name}"\n\t\t"sha2" "{digest}"\n'
'\t}\n}\n')
def install(self, fail_host=None):
def install(self, fail_host=None, *args, **extra):
env = self.env if fail_host is None else dict(self.env, TEST_FAIL_HOST=fail_host)
return subprocess.run(['bash', str(SCRIPT)], env=env, text=True, capture_output=True)
return subprocess.run(['bash', str(SCRIPT), *args], env=dict(env, **extra), text=True, capture_output=True)
def assert_installed(self, channel='steamdeck_publicbeta', fail_host=None, expected=None):
result = self.install(fail_host)
def channel(self, **extra):
result = self.install(None, '--channel', **extra)
self.assertEqual(0, result.returncode, result.stderr)
return result.stdout.strip()
def assert_installed(self, channel='steamdeck_publicbeta', fail_host=None, expected=None, **extra):
result = self.install(fail_host, **extra)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(channel + '\n', (self.steam / 'package/beta').read_text())
self.assertEqual('123456\n', (self.steam / 'package/droiddeck-installed').read_text())
@@ -87,6 +116,7 @@ cp "$TEST_CDN/${url##*/}" "$out"
'https://client-update.steamstatic.com/' + channel + '.zip',
]
self.assertEqual(expected, self.requests.read_text().splitlines())
return result
def test_downloads_the_deck_beta_client(self):
self.assert_installed()
@@ -155,8 +185,80 @@ cp "$TEST_CDN/${url##*/}" "$out"
self.requests.unlink()
with (package / 'droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\nstart\n')
self.assertEqual(0, self.install(BL_STEAM_CHANNEL='beta').returncode)
self.assertFalse(self.requests.exists())
def test_the_default_channel_is_the_deck_beta(self):
self.assertEqual('steamdeck_publicbeta', self.channel())
def stable_requests(self, beta_manifest=True):
stable = [
'https://client-update.steamstatic.com/steam_client_steamdeck_stable_linuxarm64',
'https://client-update.steamstatic.com/steamdeck_stable.zip',
]
if beta_manifest:
stable.insert(0, 'https://client-update.steamstatic.com/steam_client_steamdeck_publicbeta_linuxarm64')
return stable
def test_a_repaired_beta_client_that_still_shows_nothing_falls_back_to_stable(self):
package = self.break_client(2)
self.assertEqual(0, self.install().returncode)
self.requests.unlink()
with (package / 'droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\n')
result = self.assert_installed('steamdeck_stable', expected=self.stable_requests(False))
self.assertIn("falling back to Valve's steamdeck_stable Steam client", result.stdout)
self.assertEqual('steamdeck_stable\n', (package / 'droiddeck-channel').read_text())
self.assertEqual('repaired\n', (package / 'droiddeck-unconfirmed').read_text())
self.assertEqual('steamdeck_stable', self.channel())
def test_the_stable_fallback_is_not_repeated(self):
self.test_a_repaired_beta_client_that_still_shows_nothing_falls_back_to_stable()
self.requests.unlink()
with (self.steam / 'package/droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\nstart\n')
self.assertEqual(0, self.install().returncode)
self.assertFalse(self.requests.exists())
self.assertEqual('steamdeck_stable\n', (self.steam / 'package/beta').read_text())
def test_a_pinned_beta_channel_never_falls_back(self):
package = self.break_client(2)
self.assertEqual(0, self.install().returncode)
with (package / 'droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\n')
self.assertEqual('steamdeck_publicbeta', self.channel(BL_STEAM_CHANNEL='beta'))
self.assertEqual(0, self.install(BL_STEAM_CHANNEL='beta').returncode)
self.assertFalse((package / 'droiddeck-channel').exists())
def test_the_channel_setting_selects_stable(self):
setting = self.home / '.config/droiddeck/steam-channel'
setting.parent.mkdir(parents=True)
setting.write_text('stable\n')
self.assertEqual('steamdeck_stable', self.channel())
self.assert_installed('steamdeck_stable', expected=self.stable_requests(False))
self.assertFalse((self.steam / 'package/droiddeck-channel').exists())
def empty_beta_manifest(self):
(self.cdn / 'steam_client_steamdeck_publicbeta_linuxarm64').write_text(
'"linuxarm64"\n{\n\t"version" "123457"\n'
'\t"client_steamrtarm64_linuxarm64"\n\t{\n'
'\t\t"file" "client.zip"\n\t\t"sha2" "00"\n'
'\t}\n}\n')
def test_a_beta_manifest_without_an_arm64_client_falls_back_to_stable(self):
self.empty_beta_manifest()
result = self.assert_installed('steamdeck_stable', expected=self.stable_requests())
self.assertIn('no client components in steam_client_steamdeck_publicbeta_linuxarm64', result.stderr)
self.assertIn('client_steamrtarm64_linuxarm64', result.stderr)
self.assertEqual('steamdeck_stable\n', (self.steam / 'package/droiddeck-channel').read_text())
self.assertEqual('steamdeck_stable', self.channel())
def test_a_pinned_beta_manifest_without_an_arm64_client_fails_and_names_its_blocks(self):
self.empty_beta_manifest()
result = self.install(BL_STEAM_CHANNEL='beta')
self.assertNotEqual(0, result.returncode)
self.assertIn('client_steamrtarm64_linuxarm64', result.stderr)
self.assertFalse((self.steam / 'package/droiddeck-installed').exists())
def test_requested_repair_runs_once_and_is_retried_until_it_completes(self):
package = self.break_client(0)
@@ -177,6 +279,141 @@ cp "$TEST_CDN/${url##*/}" "$out"
self.assertEqual(0, self.install().returncode)
self.assertFalse(self.requests.exists())
def publish(self, **parts):
files = {}
blocks = ''
for block, length in parts.items():
archive = self.cdn / f'{block}.zip.{hashlib.sha1(block.encode()).hexdigest()}'
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_STORED) as package:
package.writestr(f'parts/{block}', os.urandom(length))
if block.endswith('_linuxarm64_linuxarm64'):
package.writestr('steamrtarm64/steam', 'steamdeck_publicbeta')
data = archive.read_bytes()
files[block] = archive.name
blocks += (f'\t"{block}"\n\t{{\n\t\t"file"\t\t"{archive.name}"\n\t\t"size"\t\t"{len(data)}"\n'
f'\t\t"sha2"\t\t"{hashlib.sha256(data).hexdigest()}"\n\t}}\n')
(self.cdn / 'steam_client_steamdeck_publicbeta_linuxarm64').write_text(
'"linuxarm64"\n{\n\t"version"\t\t"123456"\n' + blocks + '}\n')
return files
def two_parts(self):
return self.publish(a_all=1_200_000, client_linuxarm64_linuxarm64=1_300_000)
def plan_for(self, name, *actions):
with self.plan.open('a') as plan:
plan.writelines(f'{name} {action}\n' for action in actions)
def offsets(self, name):
log = Path(str(self.requests) + '.offsets')
return [int(line.split()[1]) for line in log.read_text().splitlines() if line.split()[0] == name]
def requested(self):
return [line.rsplit('/', 1)[1] for line in self.requests.read_text().splitlines()]
@property
def download(self):
return self.steam / 'package/droiddeck-download'
def test_progress_is_counted_in_megabytes_across_parts(self):
self.two_parts()
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn('downloading Steam: a_all (1/2), 0 of 2 MB\n', result.stdout)
self.assertIn('downloading Steam: client_linuxarm64_linuxarm64 (2/2), 1 of 2 MB\n', result.stdout)
self.assertTrue((self.steam / 'parts/a_all').is_file())
self.assertFalse(self.download.exists())
def test_a_dropped_connection_resumes_from_what_arrived(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'cut 500000')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual([0, 500000], self.offsets(client))
self.assertIn('interrupted (curl exit 28), trying again in 0 s', result.stdout)
self.assertEqual('123456\n', (self.steam / 'package/droiddeck-installed').read_text())
def test_a_failed_session_keeps_what_it_finished_and_fetched(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'cut 400000', *['fail 6'] * 12)
result = self.install()
self.assertNotEqual(0, result.returncode)
self.assertIn('could not download client_linuxarm64_linuxarm64 (curl exit 6)', result.stderr)
self.assertFalse((self.steam / 'package/droiddeck-installed').exists())
self.assertTrue((self.download / (files['a_all'] + '.unpacked')).is_file())
self.assertFalse((self.download / files['a_all']).exists())
self.assertEqual(400000, (self.download / client).stat().st_size)
self.requests.unlink()
Path(str(self.requests) + '.offsets').unlink()
self.assertEqual(0, self.install().returncode)
self.assertEqual(['steam_client_steamdeck_publicbeta_linuxarm64', client], self.requested())
self.assertEqual([400000], self.offsets(client))
self.assertFalse(self.download.exists())
def test_a_server_that_will_not_resume_starts_the_part_over(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'cut 400000', 'norange')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual([0, 400000, 0], self.offsets(client))
def test_a_missing_part_hands_over_to_the_next_cdn_at_once(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'http 404')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['https://client-update.steamstatic.com/' + client,
'https://client-update.akamai.steamstatic.com/' + client],
[line for line in self.requests.read_text().splitlines() if line.endswith(client)])
self.assertIn('HTTP 404 from https://client-update.steamstatic.com', result.stderr)
self.assertNotIn('trying again', result.stdout)
def test_a_server_error_is_retried_on_the_same_cdn(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'http 503')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['https://client-update.steamstatic.com/' + client] * 2,
[line for line in self.requests.read_text().splitlines() if line.endswith(client)])
self.assertIn('interrupted (curl exit 22), trying again in 0 s', result.stdout)
def test_bytes_that_do_not_check_out_are_fetched_again(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.download.mkdir(parents=True)
(self.download / client).write_bytes(b'x' * (self.cdn / client).stat().st_size)
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual([0], self.offsets(client))
def test_parts_left_for_another_client_version_are_dropped(self):
files = self.two_parts()
self.download.mkdir(parents=True)
(self.download / 'a_all.zip.0123').write_bytes(b'older')
(self.download / 'b_all.zip.4567.unpacked').write_bytes(b'')
self.plan_for(files['client_linuxarm64_linuxarm64'], *['fail 7'] * 12)
self.assertNotEqual(0, self.install().returncode)
self.assertEqual([files['a_all'] + '.unpacked'], sorted(p.name for p in self.download.iterdir()))
def test_a_repair_unpacks_every_part_again_once(self):
files = self.two_parts()
self.assertEqual(0, self.install().returncode)
(self.home / '.bl-steam-repair').write_text('1\n')
self.requests.unlink()
self.plan_for(files['client_linuxarm64_linuxarm64'], *['fail 7'] * 12)
self.assertNotEqual(0, self.install().returncode)
self.assertIn(files['a_all'], self.requested())
self.requests.unlink()
self.assertEqual(0, self.install().returncode)
self.assertEqual(['steam_client_steamdeck_publicbeta_linuxarm64', files['client_linuxarm64_linuxarm64']],
self.requested())
self.assertFalse((self.home / '.bl-steam-repair').exists())
self.assertFalse(self.download.exists())
if __name__ == '__main__':
unittest.main()
+16
View File
@@ -90,6 +90,20 @@ class LibraryListTest(unittest.TestCase):
self.assertIn(self.pc, self.paths(self.run_main()))
self.assertIn(self.card, self.paths(self.vdf.read_text()))
def test_added_library_is_registered_while_listed_and_forgotten_once_removed(self):
added = self.root / "lib" / "1"
manifest(added / "steamapps", 20)
with patch.object(library, "ADDED_DIR", str(self.root / "lib") + "/"):
self.listing.write_text(json.dumps([{"path": str(added), "label": "Documents"}]))
text = self.run_main()
self.assertIn(added, self.paths(text))
self.assertIn('"20"\t\t"0"', text)
self.listing.write_text(json.dumps([]))
text = self.run_main()
self.assertNotIn(added, self.paths(text))
self.assertIn(self.user, self.paths(text))
self.assertTrue((added / "steamapps/appmanifest_20.acf").is_file())
def test_without_a_list_only_the_card_is_managed(self):
text = self.run_main(listing=False)
self.assertEqual([self.steam, self.old, self.user, self.card], self.paths(text))
@@ -153,6 +167,8 @@ class DesktopGamesTest(unittest.TestCase):
steam = root / "Steam"
manifest(steam / "steamapps", 40, name="Game")
manifest(steam / "steamapps", 1628350, name="Steam Linux Runtime 3.0 (sniper)", installdir="SteamLinuxRuntime_sniper")
manifest(steam / "steamapps", 1826330, name="Proton EasyAntiCheat Runtime", installdir="Proton EasyAntiCheat Runtime")
manifest(steam / "steamapps", 1161040, name="Proton BattlEye Runtime", installdir="Proton BattlEye Runtime")
manifest(steam / "steamapps", 5000, name="Some Proton", installdir="Some Proton")
(steam / "steamapps/common/Some Proton").mkdir(parents=True)
(steam / "steamapps/common/Some Proton/toolmanifest.vdf").write_text('"manifest"\n{\n}\n')
+26
View File
@@ -1,5 +1,7 @@
import re
import shlex
import subprocess
import tempfile
import unittest
from pathlib import Path
@@ -20,6 +22,30 @@ class SteamRestartCapTest(unittest.TestCase):
return subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30,
check=True).stdout.split()
def installs(self, *steps):
function = stuck_function()
self.assertIsNotNone(function, "droiddeck-session has no steam_update_stuck()")
with tempfile.TemporaryDirectory() as root:
script = function + "steam_root=%s\nmkdir -p \"$steam_root/package\"\n" % shlex.quote(root)
script += "installed=\"$steam_root/package/steam_client_linuxarm64.installed\"\n"
script += "echo 0 > \"$installed\"\nsteam_updates=0\nsteam_installs=0\n"
script += "steam_update_stamp=$(stat -c '%n %Y %s' \"$steam_root\"/package/*.installed)\n"
for n, installed in enumerate(steps, 1):
if installed:
script += "printf '%%0%dd' 0 > \"$installed\"\n" % (n + 1)
script += "if steam_update_stuck 10; then echo stop; else echo restart; fi\n"
return subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30,
check=True).stdout.split()
def test_restarts_that_each_install_an_update_go_on(self):
self.assertEqual(["restart"] * 4, self.installs(True, False, True, False))
def test_an_update_that_never_installs_is_stopped_on_the_third(self):
self.assertEqual(["restart", "restart", "stop"], self.installs(False, False, False))
def test_installs_that_never_settle_are_stopped(self):
self.assertIn("stop", self.installs(*[True] * 12))
def test_an_update_that_keeps_asking_at_once_is_stopped_on_the_third(self):
self.assertEqual(["restart", "restart", "stop"], self.restarts(11, 13, 9))
+88
View File
@@ -0,0 +1,88 @@
from pathlib import Path
import runpy
import tempfile
import unittest
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
MODULE = runpy.run_path(str(BIN / 'droiddeck-steam-shaders'))
apply = MODULE['apply']
set_text = MODULE['set_text']
parse = runpy.run_path(str(BIN / 'droiddeck-steam-ui-scale'))['parse']
THOR = '''"InstallConfigStore"
{
\t"Software"
\t{
\t\t"Valve"
\t\t{
\t\t\t"Steam"
\t\t\t{
\t\t\t\t"ShaderCacheManager"
\t\t\t\t{
\t\t\t\t\t"HasCurrentBucket"\t\t"1"
\t\t\t\t\t"ProcessingQueue"\t\t"207140;848350;"
\t\t\t\t\t"App"
\t\t\t\t\t{
\t\t\t\t\t}
\t\t\t\t}
\t\t\t}
\t\t}
\t}
}
'''
def disabled(text):
steam = parse(text).get('InstallConfigStore').get('Software').get('Valve').get('Steam')
return steam.get('ShaderCacheManager').get('DisableShaderCache')
class SteamShadersTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.config = self.root / 'config/config.vdf'
self.config.parent.mkdir()
def test_off_adds_the_key_the_client_writes(self):
text, changed = set_text(THOR, False)
self.assertTrue(changed)
self.assertEqual('1', disabled(text))
self.assertIn('\t\t\t\t\t"DisableShaderCache"\t\t"1"\n\t\t\t\t}', text)
self.assertIn('"ProcessingQueue"\t\t"207140;848350;"', text)
def test_existing_value_is_replaced_in_place(self):
off, _ = set_text(THOR, False)
on, changed = set_text(off, True)
self.assertTrue(changed)
self.assertEqual('0', disabled(on))
self.assertEqual(off.replace('"DisableShaderCache"\t\t"1"', '"DisableShaderCache"\t\t"0"'), on)
def test_matching_value_leaves_the_file_alone(self):
off, _ = set_text(THOR, False)
self.assertEqual((off, False), set_text(off, False))
def test_missing_sections_are_created(self):
text, changed = set_text('"InstallConfigStore"\n{\n}\n', False)
self.assertTrue(changed)
self.assertEqual('1', disabled(text))
def test_apply_writes_config_and_creates_a_fresh_one(self):
self.config.write_text(THOR)
self.assertTrue(apply(self.root, False))
self.assertEqual('1', disabled(self.config.read_text()))
self.assertFalse(apply(self.root, False))
fresh = Path(self.temp.name) / 'fresh'
fresh.mkdir()
self.assertTrue(apply(fresh, True))
self.assertEqual('0', disabled((fresh / 'config/config.vdf').read_text()))
def test_invalid_config_is_rejected(self):
with self.assertRaises(ValueError):
set_text('"InstallConfigStore" {', False)
if __name__ == '__main__':
unittest.main()
+167
View File
@@ -0,0 +1,167 @@
from pathlib import Path
import contextlib
import io
import json
import os
import runpy
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-steam-share'
SHARE = runpy.run_path(str(SCRIPT))
CONFIG = ('"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n'
'\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n%s\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n')
def mapping(**tools):
return ''.join('\t\t\t\t\t"%s"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name"\t\t"%s"\n\t\t\t\t\t\t"config"\t\t""\n'
'\t\t\t\t\t\t"priority"\t\t"250"\n\t\t\t\t\t}\n' % (app.lstrip('_'), name)
for app, name in tools.items())
def manifest(app, folder, updated=100, flags=4):
return ('"AppState"\n{\n\t"appid"\t\t"%s"\n\t"name"\t\t"%s"\n\t"StateFlags"\t\t"%d"\n'
'\t"installdir"\t\t"%s"\n\t"LastUpdated"\t\t"%d"\n}\n' % (app, folder, flags, folder, updated))
class SteamShareTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
home = Path(self.temp.name)
self.arm = home / 'arm/Steam'
self.x64 = home / 'x64/Steam'
self.card = home / 'card'
self.shared = home / 'x64/shared'
for root, tool in ((self.arm, 'droiddeck-proton-arm64'), (self.x64, 'droiddeck-proton-a64')):
(root / 'steamapps/common').mkdir(parents=True)
(root / 'config').mkdir()
(root / 'compatibilitytools.d/proton').mkdir(parents=True)
(root / 'compatibilitytools.d/proton/compatibilitytool.vdf').write_text(
'"compatibilitytools"\n{\n\t"compat_tools"\n\t{\n\t\t"%s"\n\t\t{\n\t\t}\n\t}\n}\n' % tool)
(self.arm / 'config/config.vdf').write_text(CONFIG % mapping(_0='droiddeck-proton-arm64', _413150='droiddeck-fex'))
(self.x64 / 'config/config.vdf').write_text(CONFIG % '')
self.install(self.arm, '413150', 'Stardew Valley')
self.install(self.arm, '13210', 'Unreal Tournament 3')
self.install(self.arm, '1493710', 'Proton - Experimental')
def install(self, root, app, folder, updated=100):
(root / 'steamapps/common' / folder).mkdir(parents=True, exist_ok=True)
(root / 'steamapps/common' / folder / 'game.bin').write_text('x')
(root / ('steamapps/appmanifest_%s.acf' % app)).write_text(manifest(app, folder, updated))
def run_share(self, here):
with contextlib.redirect_stdout(io.StringIO()):
return SHARE['main']([here, str(self.arm), str(self.x64), str(self.card), str(self.shared)])
def view(self, *parts):
return self.shared.joinpath('arm64', 'steamapps', *parts)
def test_x64_client_sees_arm64_games_in_place(self):
self.assertEqual(self.run_share('x86-64'), 0)
self.assertEqual(os.readlink(self.view('common/Stardew Valley')), str(self.arm / 'steamapps/common/Stardew Valley'))
self.assertTrue(self.view('appmanifest_413150.acf').is_file())
self.assertTrue(self.view('appmanifest_13210.acf').is_file())
self.assertFalse(self.view('appmanifest_1493710.acf').exists())
folders = (self.x64 / 'steamapps/libraryfolders.vdf').read_text()
self.assertIn(str(self.shared / 'arm64'), folders)
self.assertIn('ARM64 client games', folders)
def test_tools_follow_the_other_client(self):
self.run_share('x86-64')
tools = SHARE['entries']((self.x64 / 'config/config.vdf').read_text())
self.assertEqual(tools['413150'], 'droiddeck-direct')
self.assertEqual(tools['13210'], 'droiddeck-proton-a64')
def test_a_proton_pick_stays_when_the_other_client_uses_another_proton(self):
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_13210='droiddeck-proton-11-a64'))
self.run_share('x86-64')
self.assertEqual(SHARE['entries']((self.x64 / 'config/config.vdf').read_text())['13210'], 'droiddeck-proton-11-a64')
def test_an_automatic_proton_gives_way_to_the_installed_linux_build(self):
self.install(self.x64, '550', 'Left 4 Dead 2')
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_550='droiddeck-direct'))
(self.arm / 'config/config.vdf').write_text(CONFIG % mapping(_0='droiddeck-proton-arm64', _550='droiddeck-proton-arm64'))
self.run_share('arm64')
self.assertEqual(SHARE['entries']((self.arm / 'config/config.vdf').read_text())['550'], 'droiddeck-fex')
def test_an_x64_proton_pick_reaches_the_arm64_client(self):
self.install(self.x64, '550', 'Left 4 Dead 2')
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_550='droiddeck-proton-a64'))
self.run_share('arm64')
self.assertEqual(SHARE['entries']((self.arm / 'config/config.vdf').read_text())['550'], 'droiddeck-proton-arm64')
def test_a_tool_the_user_chose_is_kept(self):
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_13210='my-proton'))
self.run_share('x86-64')
self.assertEqual(SHARE['entries']((self.x64 / 'config/config.vdf').read_text())['13210'], 'my-proton')
def test_own_install_wins(self):
self.install(self.x64, '413150', 'Stardew Valley')
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_413150.acf').exists())
self.assertFalse(os.path.lexists(self.view('common/Stardew Valley')))
def test_newer_update_goes_back_to_the_source(self):
self.run_share('x86-64')
self.view('appmanifest_413150.acf').write_text(manifest('413150', 'Stardew Valley', 200))
self.run_share('x86-64')
self.assertIn('"LastUpdated"\t\t"200"', (self.arm / 'steamapps/appmanifest_413150.acf').read_text())
def test_uninstall_from_the_view_hides_until_reinstalled(self):
self.run_share('x86-64')
self.view('appmanifest_13210.acf').unlink()
self.run_share('x86-64')
self.assertFalse(os.path.lexists(self.view('common/Unreal Tournament 3')))
self.assertTrue((self.arm / 'steamapps/common/Unreal Tournament 3/game.bin').is_file())
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_13210.acf').exists())
self.install(self.arm, '13210', 'Unreal Tournament 3', 300)
self.run_share('x86-64')
self.assertTrue(self.view('appmanifest_13210.acf').is_file())
def test_files_deleted_through_the_view_drop_the_source_manifest(self):
self.run_share('x86-64')
(self.arm / 'steamapps/common/Unreal Tournament 3/game.bin').unlink()
self.view('appmanifest_13210.acf').unlink()
self.run_share('x86-64')
self.assertFalse((self.arm / 'steamapps/appmanifest_13210.acf').exists())
def test_removed_source_leaves_the_view(self):
self.run_share('x86-64')
(self.arm / 'steamapps/appmanifest_413150.acf').unlink()
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_413150.acf').exists())
self.assertFalse(os.path.lexists(self.view('common/Stardew Valley')))
state = json.loads((self.shared / 'arm64/.droiddeck-shared.json').read_text())
self.assertNotIn('413150', state['shared'])
def test_arm64_client_sees_x64_games(self):
self.install(self.x64, '550', 'Left 4 Dead 2')
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_550='droiddeck-direct'))
self.run_share('arm64')
self.assertTrue(self.shared.joinpath('x86-64/steamapps/appmanifest_550.acf').is_file())
self.assertEqual(SHARE['entries']((self.arm / 'config/config.vdf').read_text())['550'], 'droiddeck-fex')
self.assertIn('x86-64 client games', (self.arm / 'steamapps/libraryfolders.vdf').read_text())
def test_an_empty_view_is_not_offered(self):
self.run_share('x86-64')
for app in ('413150', '13210', '1493710'):
(self.arm / ('steamapps/appmanifest_%s.acf' % app)).unlink()
self.run_share('x86-64')
self.assertNotIn(str(self.shared / 'arm64'), (self.x64 / 'steamapps/libraryfolders.vdf').read_text())
def test_unfinished_installs_are_not_shared(self):
(self.arm / 'steamapps/appmanifest_413150.acf').write_text(manifest('413150', 'Stardew Valley', flags=1026))
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_413150.acf').exists())
def test_bad_arguments(self):
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(SHARE['main'](['mips']), 2)
if __name__ == '__main__':
unittest.main()
+24
View File
@@ -203,6 +203,30 @@ class ShortcutsTest(unittest.TestCase):
self.assertEqual('-vulkan', steam['LaunchOptions'])
self.assertEqual('Mine', steam['AppName'])
def users(self, *blocks):
body = ''.join('\t"%d"\n\t{\n%s\t}\n' % (76561197960265728 + acct, ''.join('\t\t"%s"\t\t"%s"\n' % kv for kv in keys.items()))
for acct, keys in blocks)
(self.steam / 'config').mkdir(exist_ok=True)
(self.steam / 'config/loginusers.vdf').write_text('"users"\n{\n%s}\n' % body)
(self.steam / 'userdata/456/config').mkdir(parents=True, exist_ok=True)
def test_the_account_marked_most_recent_is_used(self):
self.users((123, dict(MostRecent='1', AutoLogin='0', timestamp='100')), (456, dict(MostRecent='0', AutoLogin='1', timestamp='200')))
self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam)))
def test_without_most_recent_the_account_that_signs_in_is_used(self):
self.users((123, dict(AutoLogin='1', timestamp='100')), (456, dict(AutoLogin='0', timestamp='200')))
self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam)))
self.assertIn(APPID, self.run_writer(self.game()))
def test_without_either_mark_the_account_used_last_is_used(self):
self.users((456, dict(AutoLogin='0', timestamp='100')), (123, dict(AutoLogin='0', timestamp='200')))
self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam)))
def test_two_accounts_without_any_mark_write_nothing(self):
self.users((123, dict(AccountName='a')), (456, dict(AccountName='b')))
self.assertIsNone(shortcuts.account_dir(str(self.steam)))
def test_an_older_record_without_launch_options_still_takes_the_apps(self):
self.run_writer(self.game())
record = self.record()
+79
View File
@@ -0,0 +1,79 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-steam-x64'
class SteamX64CpuTest(unittest.TestCase):
def selection(self, features, *, forced=False, same_build=True, unreadable=False):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
arm = root / 'arm'
x64 = root / 'x64'
for base in (arm, x64):
(base / 'package').mkdir(parents=True)
(arm / 'steamrtarm64').mkdir()
(arm / 'steamrtarm64/steamwebhelper').touch(mode=0o700)
shim = root / 'arm-shim'
shim.touch(mode=0o700)
(x64 / 'package/beta').write_text('publicbeta\n')
for base, arch, version in ((x64, 'steamrt64', '123'), (arm, 'linuxarm64', '123' if same_build else '122')):
(base / f'package/steam_client_publicbeta_{arch}.manifest').write_text(f'"version" "{version}"\n')
cpuinfo = root / 'cpuinfo'
if not unreadable:
cpuinfo.write_text(features)
text = SCRIPT.read_text()
start = text.find('x64_arm_webhelper_cpu_supported() {')
helper = text[start:text.index('x64_exec() {')] if start >= 0 else ''
selection = text.split('x64_exec() {', 1)[1].split(' mkdir -p "$STEAM_X64/logs"', 1)[0]
shell = helper + '\nx64_env() { :; }\nx64_webhelper_gpu() { return 1; }\nselect_helper() {' + selection + '\nprintf "%s|%s\\n" "${BL_X64_ARM_WEBHELPER:-x86}" "${BL_WEBHELPER_ARGS:-}";\n}\nselect_helper\n'
shell = shell.replace('/proc/cpuinfo', str(cpuinfo)).replace('/usr/local/bin/droiddeck-x64-arm-webhelper', str(shim))
env = dict(os.environ, STEAM_X64=str(x64), ARM_STEAM=str(arm), X64_CHANNEL='publicbeta', BL_X64_X86_WEBHELPER='1' if forced else '0')
env.pop('BL_X64_ARM_WEBHELPER', None)
env.pop('BL_WEBHELPER_ARGS', None)
result = subprocess.run(['bash', '-eu', '-c', shell], env=env, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
return result.stdout.strip(), str(shim)
def test_missing_atomics_keeps_x86_helper(self):
result, _ = self.selection('Features : fp asimd aes crc32\n')
self.assertEqual(result, 'x86|stock')
def test_atomics_keeps_same_build_arm_helper(self):
result, arm = self.selection('Features : fp asimd atomics aes\n')
self.assertEqual(result, arm + '|')
def test_exact_token_required(self):
result, _ = self.selection('Features : fp noatomics atomics_extra\n')
self.assertEqual(result, 'x86|stock')
def test_any_explicit_core_without_atomics_keeps_x86_helper(self):
result, _ = self.selection('processor : 0\nFeatures : fp atomics\nprocessor : 1\nFeatures : fp aes\n')
self.assertEqual(result, 'x86|stock')
def test_empty_features_is_explicitly_unsupported(self):
result, _ = self.selection('Features : \n')
self.assertEqual(result, 'x86|stock')
def test_absent_features_preserves_existing_behavior(self):
result, arm = self.selection('processor : 0\n')
self.assertEqual(result, arm + '|')
def test_unreadable_cpuinfo_preserves_existing_behavior(self):
result, arm = self.selection('', unreadable=True)
self.assertEqual(result, arm + '|')
def test_forced_x86_still_wins(self):
result, _ = self.selection('Features : atomics\n', forced=True)
self.assertEqual(result, 'x86|stock')
def test_supported_cpu_still_requires_same_build(self):
result, _ = self.selection('Features : atomics\n', same_build=False)
self.assertEqual(result, 'x86|stock')
if __name__ == '__main__':
unittest.main()
+5 -1
View File
@@ -39,12 +39,16 @@ class X64SignInTest(unittest.TestCase):
self.write(self.x64 / 'config/loginusers.vdf', '"users"\n{\n}\n')
self.write(self.x64 / 'config/config.vdf', CONFIG % '')
self.write(self.home / '.droiddeck-x64/.steam/registry.vdf', REGISTRY % '')
self.bin = self.home / 'bin'
self.bin.mkdir()
(self.bin / 'pgrep').write_text('#!/bin/sh\nexit 1\n')
(self.bin / 'pgrep').chmod(0o755)
def write(self, path, text):
path.write_text(text)
def sync(self):
env = dict(os.environ, HOME=str(self.home))
env = dict(os.environ, HOME=str(self.home), PATH='%s:%s' % (self.bin, os.environ['PATH']))
env.pop('XDG_DATA_HOME', None)
env.pop('BL_X64_HOME', None)
return subprocess.run(['bash', str(SCRIPT), '--sync-login'], env=env, capture_output=True, text=True, check=True)
+123
View File
@@ -0,0 +1,123 @@
import os
from pathlib import Path
import re
import subprocess
import tempfile
import unittest
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
SHIM = BIN / 'droiddeck-steamrt-shim'
SESSION = (BIN / 'droiddeck-session').read_text()
X64 = (BIN / 'droiddeck-steam-x64').read_text()
class SteamrtShimTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.client = self.root / 'steamrtarm64'
self.client.mkdir()
self.shim = self.root / 'shim'
def runtime(self, path):
runtime = self.client / path
(runtime / 'bin').mkdir(parents=True)
(runtime / 'bin/steam-runtime-check-requirements').write_text('#!/bin/sh\nexit 1\n')
(runtime / 'bin/steam-runtime-system-info').write_text('#!/bin/sh\necho info\n')
(runtime / 'files').mkdir()
(runtime / 'files/marker').write_text('runtime files')
return runtime
def build(self):
return subprocess.run(['bash', str(SHIM), str(self.client), str(self.shim)],
capture_output=True, text=True, timeout=30)
def sh(self, *args):
return subprocess.run(['sh', *map(str, args)], capture_output=True, text=True, timeout=30)
def assert_shim(self):
result = self.build()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(0, self.sh(self.shim / 'bin/steam-runtime-check-requirements').returncode)
self.assertEqual('info\n', self.sh(self.shim / 'bin/steam-runtime-system-info').stdout)
self.assertEqual('runtime files', (self.shim / 'files/marker').read_text())
launched = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--env=BL_SHIM_TEST=yes',
f'--directory={self.root}', '--', 'sh', '-c', 'echo "$BL_SHIM_TEST $(basename "$PWD")"')
self.assertEqual(f'yes {self.root.name}\n', launched.stdout)
launched = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--env', 'BL_SHIM_TEST=split',
'--directory', self.root, '--', 'sh', '-c', 'echo "$BL_SHIM_TEST $(basename "$PWD")"')
self.assertEqual(f'split {self.root.name}\n', launched.stdout)
missing = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--directory', self.root / 'missing',
'--', 'true')
self.assertEqual(127, missing.returncode)
def test_a_client_without_a_container_gets_no_shim(self):
self.shim.mkdir()
self.assertEqual(1, self.build().returncode)
self.assertFalse(self.shim.exists())
def test_the_pressure_vessel_runtime_is_shimmed(self):
self.runtime('pv-runtime/steam-runtime-steamrt')
self.assert_shim()
def test_a_requirements_probe_elsewhere_is_shimmed(self):
self.runtime('steam-runtime')
self.assert_shim()
def test_the_arm64_session_starts_a_containerized_client_through_the_shim(self):
block = re.search(r'\n( +steam_client=\("\$steam_root/steamrtarm64/steam"\)\n.*?\n fi\n)', SESSION, re.S).group(1)
stub = self.root / 'droiddeck-steamrt-shim'
for status, shimmed in ((1, False), (0, True)):
stub.write_text(f'#!/bin/bash\nexit {status}\n')
stub.chmod(0o755)
script = 'if :; then\n' + block + 'printf "%s\\n" "${steam_client[@]}"\n'
env = dict(os.environ, HOME=str(self.root), steam_root='/steam',
PATH=str(self.root) + os.pathsep + os.environ['PATH'])
result = subprocess.run(['bash', '-c', script], env=env, capture_output=True, text=True, timeout=30)
self.assertEqual(0, result.returncode, result.stderr)
lines = result.stdout.splitlines()
self.assertEqual('/steam/steamrtarm64/steam', lines[-1])
self.assertEqual(shimmed, 'Valve containerized the arm64 Steam client' in result.stdout)
self.assertEqual(shimmed, any(line.startswith('STEAM_RUNTIME_STEAMRT=') and
line.endswith('/.droiddeck-steamrt-shim') for line in lines))
def test_both_clients_are_told_they_already_run_inside_the_container(self):
guard = SESSION.index('export STEAM_STEAMRT_RECURSION_GUARD=1')
self.assertLess(guard, SESSION.index('"${steam_client[@]}" "${_bl_ui[@]}"'))
self.assertIn('-u STEAM_STEAMRT_RECURSION_GUARD', SESSION[:guard])
self.assertIn('export STEAM_STEAMRT_RECURSION_GUARD=1', X64)
self.assertIn('droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"', X64)
class SessionChannelTest(unittest.TestCase):
BLOCK = re.search(r'\n( +steam_channel=steamdeck_publicbeta\n.*?\n +fi\n)', SESSION, re.S).group(1)
def channel(self, x64, installer):
with tempfile.TemporaryDirectory() as temp:
stub = Path(temp) / 'droiddeck-steam-install'
stub.write_text('#!/bin/bash\n' + installer + '\n')
stub.chmod(0o755)
env = dict(os.environ, steam_x64=x64, PATH=temp + os.pathsep + os.environ['PATH'])
result = subprocess.run(['bash', '-c', self.BLOCK + 'echo "$steam_channel"\n'], env=env,
capture_output=True, text=True, timeout=30)
self.assertEqual(0, result.returncode, result.stderr)
return result.stdout.strip()
def test_the_arm64_client_follows_the_installed_channel(self):
self.assertEqual('steamdeck_stable', self.channel('0', '[ "$1" = --channel ] && echo steamdeck_stable'))
def test_an_installer_without_an_answer_keeps_the_deck_beta(self):
self.assertEqual('steamdeck_publicbeta', self.channel('0', 'exit 1'))
def test_the_x86_client_keeps_its_own_channel(self):
self.assertEqual('steamdeck_publicbeta', self.channel('1', 'echo steamdeck_stable'))
def test_the_client_branch_on_disk_and_clientbeta_use_the_same_channel(self):
self.assertIn('-clientbeta "$steam_channel"', SESSION)
self.assertIn('echo "$steam_channel" > "$steam_beta"', SESSION)
if __name__ == '__main__':
unittest.main()
+105
View File
@@ -0,0 +1,105 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
FAKE = r"""
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/statfs.h>
#include <sys/syscall.h>
#include <unistd.h>
uid_t getuid(void) {
return (uid_t)syscall(SYS_getuid) + 1;
}
int fstatfs(int fd, struct statfs *out) {
char link[32], path[4096];
snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
long n = syscall(SYS_readlinkat, AT_FDCWD, link, path, sizeof(path) - 1);
path[n > 0 ? n : 0] = '\0';
int result = (int)syscall(SYS_fstatfs, fd, out);
if (result == 0 && strstr(path, "/fuse/")) out->f_type = 0x65735546;
if (result == 0 && strstr(path, "/sdcardfs/")) out->f_type = 0x5dca2df5;
if (result == 0 && strstr(path, "/ext4/")) out->f_type = 0xef53;
return result;
}
"""
PROGRAM = r"""
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
int main(int argc, char **argv) {
uid_t real = (uid_t)syscall(SYS_getuid);
for (int i = 1; i < argc; i++) {
int fd = open(argv[i], O_RDONLY);
struct stat st;
errno = 1234;
int result = fstat(fd, &st);
printf("%s %d\n", st.st_uid == real ? "real" : st.st_uid == real + 1 ? "user" : "other", result == 0 && errno == 1234);
close(fd);
}
return 0;
}
"""
@unittest.skipUnless(shutil.which("cc"), "needs a C compiler")
class StorageOwnerPreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory()
cls.dir = Path(cls.temp.name)
build = cls.dir / "build"
build.mkdir()
(build / "fake.c").write_text(FAKE)
(build / "program.c").write_text(PROGRAM)
cls.preload = build / "libblsession.so"
cls.fake = build / "fake.so"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.preload),
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.fake), str(build / "fake.c")], check=True)
cls.wineserver = build / "wineserver"
subprocess.run(["cc", "-O2", "-o", str(cls.wineserver), str(build / "program.c")], check=True)
cls.other = build / "wine64-preloader"
shutil.copy(cls.wineserver, cls.other)
for kind in ("fuse", "sdcardfs", "ext4"):
(cls.dir / kind).mkdir()
(cls.dir / kind / "file").write_text("x")
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
def owners(self, program, *kinds):
env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.fake, self.preload))
env.pop("BL_STORAGE_DIAGNOSTICS", None)
paths = [str(self.dir / kind / "file") for kind in kinds]
result = subprocess.run([str(program)] + paths, env=env, capture_output=True, text=True, check=True)
return result.stdout.split()
def test_wineserver_owns_what_is_on_android_storage(self):
self.assertEqual(["user", "1", "user", "1"], self.owners(self.wineserver, "fuse", "sdcardfs"))
def test_other_filesystems_keep_their_owner(self):
self.assertEqual(["real", "1"], self.owners(self.wineserver, "ext4"))
def test_other_programs_see_the_real_owner(self):
self.assertEqual(["real", "1", "real", "1"], self.owners(self.other, "fuse", "sdcardfs"))
if __name__ == "__main__":
unittest.main()
+1 -1
View File
@@ -81,7 +81,7 @@ class StoragePreloadTest(unittest.TestCase):
(build / "program.c").write_text(PROGRAM)
cls.storage = build / "storage.so"
cls.fake = build / "fake.so"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.storage), str(PRELOAD / "storage.c"), "-ldl"], check=True)
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.storage), str(PRELOAD / "storage.c"), str(PRELOAD / "owner.c"), "-ldl"], check=True)
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.fake), str(build / "fake.c")], check=True)
cls.steam = cls.dir / "steamrtarm64/steam"
cls.steam.parent.mkdir()
+54 -4
View File
@@ -1,6 +1,3 @@
"""droiddeck-store-launch: a launch from anywhere - the Steam client's own Play button included -
asks the app for an Epic game's sign-in code before Proton starts. These pin how it recognises a
store game, how it asks, and that both Proton launchers call it."""
import importlib.machinery
import importlib.util
import json
@@ -277,11 +274,40 @@ class CloudHooks(unittest.TestCase):
for sh in (compat.LAUNCHER_SH, compat.EXTRA_WRAPPER_SH):
self.assertIn('bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', sh)
self.assertNotIn('exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', sh)
self.assertIn('[ $? -eq 10 ] && BL_STORE_EXIT=1', compat.BL_STORE_SETUP)
self.assertIn('[ $rc -eq 10 ] && BL_STORE_EXIT=1', compat.BL_STORE_SETUP)
self.assertIn('droiddeck-store-launch --exited --code "$rc" "${BL_STORE_ARGS[@]}"', compat.BL_STORE_SETUP)
self.assertIn('exec "$@"', compat.BL_STORE_SETUP)
BNET_EXE = '/root/.local/share/Steam/steamapps/compatdata/2596113170/pfx/drive_c/Program Files (x86)/Battle.net/Battle.net.exe'
class BattleNet(unittest.TestCase):
def test_a_games_shortcut_is_handed_to_droiddeck_battlenet(self):
self.assertEqual([store.BATTLENET, 'launch', 'Fen'], store.battlenet(['waitforexitandrun', BNET_EXE, '--exec=launch Fen'], env={}))
self.assertEqual([store.BATTLENET, 'launch', 'OSI'], store.battlenet(['"%s"' % BNET_EXE, '--exec="launch OSI"'], env={}))
self.assertEqual([store.BATTLENET, 'open'], store.battlenet([BNET_EXE], env={}))
def test_anything_else_and_its_own_starts_are_left_alone(self):
other = BNET_EXE.replace('2596113170', '123')
self.assertIsNone(store.battlenet([other, '--exec=launch Fen'], env={}))
self.assertIsNone(store.battlenet(['/x/Diablo IV/Diablo IV.exe'], env={}))
self.assertIsNone(store.battlenet([BNET_EXE, '--exec=launch Fen'], env={'BL_BATTLENET': '1'}))
self.assertEqual([store.BATTLENET, 'open'], store.battlenet([BNET_EXE, '--exec=launch Fen;rm'], env={}))
def test_the_launch_runs_there_and_the_wrapper_ends_with_it(self):
with patch.dict(os.environ, {}, clear=False), patch.object(store.subprocess, 'call', return_value=0) as call:
os.environ.pop('BL_BATTLENET', None)
self.assertEqual(store.RAN, store.main([BNET_EXE, '--exec=launch Pro']))
call.assert_called_once_with([store.BATTLENET, 'launch', 'Pro'])
with patch.dict(os.environ, {}, clear=False), patch.object(store.subprocess, 'call', return_value=1):
os.environ.pop('BL_BATTLENET', None)
self.assertEqual(store.RAN_FAILED, store.main([BNET_EXE, '--exec=launch Pro']))
self.assertIn('[ $rc -eq 11 ] && exit 0', compat.BL_STORE_SETUP)
self.assertIn('[ $rc -eq 12 ] && exit 1', compat.BL_STORE_SETUP)
self.assertLess(compat.BL_STORE_SETUP.index('droiddeck-store-launch "$@"'), compat.BL_STORE_SETUP.index('[ $rc -eq 11 ] && exit 0'))
class Launchers(unittest.TestCase):
def test_both_proton_launchers_ask_before_proton_starts(self):
sh = compat.LAUNCHER_SH
@@ -362,6 +388,30 @@ def set_registry(**arguments):
class GogRegistry(unittest.TestCase):
def test_patch_registration_uses_authentic_build_and_the_prefix_registry_executable(self):
with tempfile.TemporaryDirectory() as root:
folder = game(root, store_id='gog', ident='7')
(folder / 'goggame-7.id').write_text(json.dumps({'buildId': 'opaque-0001'}))
proton = Path(root) / 'proton'; proton.write_text('selected Proton')
data = Path(root) / 'compat'
executable = data / 'pfx/drive_c/windows/system32/reg.exe'
executable.parent.mkdir(parents=True); executable.write_bytes(b'MZreg')
(data / 'pfx/system.reg').write_text('registry')
imported = []
def run(argv, **kwargs):
self.assertEqual([str(proton), 'run', str(executable), 'import'], argv[:4])
text = Path(argv[4][2:].replace('\\', '/')).read_bytes()[2:].decode('utf-16-le')
imported.append(text)
return __import__('subprocess').CompletedProcess(argv, 0)
record = {'id': '7', 'build': 'opaque-0001', 'version': 'RC-final'}
with patch.object(store.subprocess, 'run', side_effect=run):
self.assertTrue(store.apply_gog_registry(folder, str(data), str(proton), installation=record).startswith('written='))
self.assertIn('[HKEY_LOCAL_MACHINE\\Software\\GOG.com\\Games\\7]', imported[0])
self.assertIn('"BUILDID"="opaque-0001"', imported[0])
self.assertIn('"VER"="RC-final"', imported[0])
with self.assertRaises(ValueError):
store.apply_gog_registry(folder, str(data), str(proton), installation=dict(record, build='invented'))
def test_the_scripts_registry_values_are_read_with_the_games_paths(self):
with tempfile.TemporaryDirectory() as root:
folder = game(root, store_id='gog', ident='1454587428')
-5
View File
@@ -1,6 +1,3 @@
"""The shortcuts writer and a store install: the app registers a game GOG, Epic or Amazon
installed through the same listing as an added folder, pointing the shortcut at the game's
launcher .bat when it has one. These pin the parts of the writer that contract relies on."""
import importlib.machinery
import importlib.util
import json
@@ -28,8 +25,6 @@ LAUNCHER = FOLDER + '/.droiddeck-launch.bat'
def app_id(exe, name):
"""The appid the app computes for a shortcut (AddedGames.scanGame): CRC32 of the quoted exe
and the name, high bit set - what Steam itself derives for a shortcut it is given."""
return (zlib.crc32(('"%s"' % exe + name).encode()) | 0x80000000) & 0xFFFFFFFF
+53
View File
@@ -0,0 +1,53 @@
import getpass
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
SU = Path(__file__).resolve().parents[1] / "ubuntu/overlay-runtime/usr/local/bin/su"
class SuWrapperTest(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.dir = Path(temp.name)
sudo = self.dir / "sudo"
sudo.write_text("#!/bin/bash\nprintf 'sudo'; printf ' <%s>' \"$@\"; echo\n")
sudo.chmod(0o755)
self.su = self.dir / "su"
self.su.write_text(SU.read_text().replace("/usr/local/bin/sudo", str(sudo)))
self.su.chmod(0o755)
def run_su(self, *args):
env = dict(os.environ, SHELL="/bin/bash")
return subprocess.run([str(self.su), *args], capture_output=True, text=True, timeout=30, env=env)
def test_a_command_after_the_user_reaches_the_root_shell(self):
out = self.run_su("root", "-c", "echo hi")
self.assertEqual("sudo <-s> <echo hi>\n", out.stdout)
def test_the_current_user_is_not_elevated(self):
out = self.run_su(getpass.getuser(), "-c", "echo as-me")
if getpass.getuser() == "root":
self.assertEqual("sudo <-s> <echo as-me>\n", out.stdout)
else:
self.assertEqual("as-me\n", out.stdout)
def test_arguments_after_the_user_go_to_the_shell(self):
out = self.run_su("-", "root", "-c", "echo $0 $1", "zero", "one")
self.assertEqual("sudo <-i> </bin/bash> <-c> <echo $0 $1> <zero> <one>\n", out.stdout)
def test_other_users_are_refused(self):
out = self.run_su("droiddeck-nobody-here")
self.assertEqual(1, out.returncode)
self.assertEqual("", out.stdout)
def test_plain_su_opens_a_root_shell(self):
self.assertEqual("sudo <-s>\n", self.run_su().stdout)
self.assertEqual("sudo <-i>\n", self.run_su("-").stdout)
if __name__ == "__main__":
unittest.main()
-2
View File
@@ -12,7 +12,6 @@ TERMUX_DIR = TESTS.parents[1] / "app/src/main/cpp/termux"
def jni_include_dir():
"""A directory holding jni.h: JNI_INCLUDE, then the JDK, then the Android NDK."""
candidates = [os.environ.get("JNI_INCLUDE")]
java_home = os.environ.get("JAVA_HOME")
if java_home:
@@ -29,7 +28,6 @@ def jni_include_dir():
def include_flags():
"""-I flags for jni.h, plus the JDK's per-platform directory that holds jni_md.h."""
base = jni_include_dir()
flags = ["-I", base]
for sub in glob.glob(os.path.join(base, "*")):
+17 -1
View File
@@ -28,7 +28,8 @@ class ValidateApkTest(unittest.TestCase):
def git(self, *args):
return subprocess.check_output(["git", "-C", self.root, *args], text=True).strip()
def validate(self, message="Source change", body="", result="success", stale=False, completed=False, filename="source.kt"):
def validate(self, message="Source change", body="", result="success", stale=False, completed=False, filename="source.kt", permission="write", role_name="write"):
(self.root / filename).parent.mkdir(parents=True, exist_ok=True)
(self.root / filename).write_text(message)
self.git("add", filename)
self.git("commit", "--allow-empty", "-qm", message)
@@ -42,6 +43,8 @@ class ValidateApkTest(unittest.TestCase):
path.write_text(json.dumps(event))
def api(path, *args):
if path.endswith("/collaborators/human/permission"):
return {"permission": permission, "role_name": role_name}
if "/statuses/" in path:
self.statuses.append(dict(value.split("=", 1) for value in args if "=" in value))
return {}
@@ -96,6 +99,19 @@ class ValidateApkTest(unittest.TestCase):
self.validate(filename="README.md", result="pending")
self.assertEqual(self.statuses[-1]["state"], "success")
def test_trusted_list_requires_maintainer_pr_opener(self):
with self.assertRaisesRegex(RuntimeError, "Only a maintainer"):
self.validate(filename=".github/trusted-test-builds.txt")
self.assertEqual(self.statuses[-1]["state"], "failure")
def test_maintainer_can_change_trusted_list(self):
self.validate(filename=".github/trusted-test-builds.txt", permission="write", role_name="maintain")
self.assertEqual(self.statuses[-1]["state"], "success")
def test_admin_can_change_trusted_list(self):
self.validate(filename=".github/trusted-test-builds.txt", permission="admin", role_name="admin")
self.assertEqual(self.statuses[-1]["state"], "success")
def test_renaming_code_to_documentation_still_requires_compilation(self):
(self.root / "source.kt").write_text("source")
self.git("add", "source.kt")
+282
View File
@@ -0,0 +1,282 @@
import array
import mmap
import os
import socket
import struct
import subprocess
import tempfile
import threading
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
VDEC = ROOT / 'tools/linuxfs/preload/vdec.c'
VENC = ROOT / 'tools/linuxfs/preload/venc.c'
DRIVER = r'''
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <poll.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
int bl_vdec_open(const char *path, int flags);
int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc);
void bl_vdec_closed(int fd);
void *SDL_LoadObject(const char *name);
void *SDL_LoadFunction(void *handle, const char *name);
struct heap_alloc { uint64_t len; uint32_t fd, fd_flags; uint64_t heap_flags; };
struct create_dumb { uint32_t height, width, bpp, flags, handle, pitch; uint64_t size; };
struct prime_handle { uint32_t handle, flags; int32_t fd; };
struct destroy_dumb { uint32_t handle; };
struct drm_device { char **nodes; int available_nodes; };
static int heap = -1;
int open(const char *path, int flags, ...) {
static int (*real)(const char *, int, ...);
va_list ap;
va_start(ap, flags);
int mode = va_arg(ap, int);
va_end(ap);
if (!real) real = dlsym(RTLD_NEXT, "open");
if (strcmp(path, "/dev/dma_heap/system") == 0) return heap = memfd_create("heap", 0);
return real(path, flags, mode);
}
int ioctl(int fd, unsigned long req, ...) {
static int (*real)(int, unsigned long, ...);
va_list ap;
va_start(ap, req);
void *arg = va_arg(ap, void *);
va_end(ap);
if (!real) real = dlsym(RTLD_NEXT, "ioctl");
if (fd == heap && req == _IOWR('H', 0, struct heap_alloc)) {
struct heap_alloc *a = arg;
int buf = memfd_create("dmabuf", 0);
if (ftruncate(buf, (off_t)a->len) != 0) return -1;
a->fd = (uint32_t)buf;
return 0;
}
if (req == _IOW('b', 0, uint64_t)) return 0;
return real(fd, req, arg);
}
static int vfd;
static int call(unsigned long req, void *arg) {
int rc;
if (!bl_vdec_ioctl(vfd, req, arg, &rc)) return -100;
return rc < 0 ? -errno : 0;
}
int main(void) {
if (bl_vdec_open("/dev/video0", O_RDWR) != -2) return 2;
void *drm = SDL_LoadObject("libdrm.so.2");
if (!drm) return 3;
int (*devices)(uint32_t, struct drm_device **, int) = SDL_LoadFunction(drm, "drmGetDevices2");
int (*drm_ioctl)(int, unsigned long, void *) = SDL_LoadFunction(drm, "drmIoctl");
if (!devices || !drm_ioctl || !SDL_LoadFunction(drm, "drmModeAtomicCommit")) return 4;
struct drm_device *dev[1];
if (devices(0, NULL, 0) != 1 || devices(0, dev, 1) != 1 || !(dev[0]->available_nodes & 1)) return 5;
int card = open(dev[0]->nodes[0], O_RDWR | O_CLOEXEC);
if (card < 0) return 6;
vfd = bl_vdec_open("/dev/video-dec0", O_RDWR);
if (vfd < 0) return 7;
struct v4l2_capability cap;
if (call(VIDIOC_QUERYCAP, &cap) || (cap.capabilities & 0x04004000) != 0x04004000) return 8;
struct v4l2_fmtdesc d = { .index = 1, .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE };
if (call(VIDIOC_ENUM_FMT, &d) || d.pixelformat != V4L2_PIX_FMT_HEVC) return 9;
struct v4l2_format f = { .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE };
f.fmt.pix_mp.width = 1280; f.fmt.pix_mp.height = 720; f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_HEVC;
if (call(VIDIOC_S_FMT, &f)) return 10;
struct v4l2_requestbuffers r = { .count = 4, .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, .memory = V4L2_MEMORY_MMAP };
if (call(VIDIOC_REQBUFS, &r) || r.count != 4) return 11;
unsigned char *in[4];
for (int i = 0; i < 4; i++) {
struct v4l2_plane p = { 0 };
struct v4l2_buffer b = { .index = i, .type = r.type, .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &p };
if (call(VIDIOC_QUERYBUF, &b)) return 12;
in[i] = mmap(NULL, p.length, PROT_READ | PROT_WRITE, MAP_SHARED, vfd, p.m.mem_offset);
if (in[i] == MAP_FAILED) return 13;
}
f = (struct v4l2_format){ .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE };
f.fmt.pix_mp.width = 1280; f.fmt.pix_mp.height = 736; f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12;
if (call(VIDIOC_S_FMT, &f) || f.fmt.pix_mp.num_planes != 1) return 14;
uint32_t bpl = f.fmt.pix_mp.plane_fmt[0].bytesperline, size = f.fmt.pix_mp.plane_fmt[0].sizeimage;
if (bpl != 1280 || size != 1280 * 736 * 3 / 2) return 15;
r = (struct v4l2_requestbuffers){ .count = 3, .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, .memory = V4L2_MEMORY_DMABUF };
if (call(VIDIOC_REQBUFS, &r) || r.count != 3) return 16;
int out[3];
for (int i = 0; i < 3; i++) {
struct create_dumb c = { .height = 736, .width = 1280, .bpp = 16 };
if (drm_ioctl(card, 0xc02064b2, &c) || !c.handle || c.size < size) return 17;
struct prime_handle h = { .handle = c.handle, .flags = O_CLOEXEC };
if (drm_ioctl(card, 0xc00c642d, &h)) return 18;
struct destroy_dumb x = { .handle = c.handle };
if (drm_ioctl(card, 0xc00464b4, &x)) return 19;
out[i] = h.fd;
}
unsigned types[2] = { V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE };
if (call(VIDIOC_STREAMON, &types[0]) || call(VIDIOC_STREAMON, &types[1])) return 20;
for (int i = 0; i < 3; i++) {
struct v4l2_plane p = { .m.fd = out[i] };
struct v4l2_buffer b = { .index = i, .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &p };
if (call(VIDIOC_QBUF, &b)) return 21;
}
struct pollfd pf = { .fd = vfd, .events = 0xc9 };
if (poll(&pf, 1, 0) != 0) return 22;
for (int frame = 1; frame <= 5; frame++) {
int slot = frame % 4;
memset(in[slot], 'a' + frame, 100 + frame);
struct v4l2_plane p = { .bytesused = 100 + frame };
struct v4l2_buffer b = { .index = slot, .type = types[0], .memory = V4L2_MEMORY_MMAP, .flags = 0x4008, .length = 1, .m.planes = &p };
b.timestamp.tv_usec = frame;
if (call(VIDIOC_QBUF, &b)) return 23;
pf = (struct pollfd){ .fd = vfd, .events = 0xc9 };
if (poll(&pf, 1, 30) != 1 || !(pf.revents & POLLIN)) return 24;
struct v4l2_plane cp = { 0 };
struct v4l2_buffer cb = { .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &cp };
if (call(VIDIOC_DQBUF, &cb)) return 25;
unsigned char *m = mmap(NULL, size, PROT_READ, MAP_SHARED, out[cb.index], 0);
printf("%ld %u %c%c %u\n", (long)cb.timestamp.tv_usec, cb.index, m[0], m[(size_t)bpl * 736], cp.bytesused == size);
munmap(m, size);
pf = (struct pollfd){ .fd = vfd, .events = 0x306 };
if (poll(&pf, 1, 0) != 1) return 26;
struct v4l2_plane op = { 0 };
struct v4l2_buffer ob = { .type = types[0], .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &op };
if (call(VIDIOC_DQBUF, &ob) || ob.index != (unsigned)slot) return 27;
struct v4l2_plane rp = { .m.fd = out[cb.index] };
struct v4l2_buffer rb = { .index = cb.index, .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &rp };
if (call(VIDIOC_QBUF, &rb)) return 28;
}
if (call(VIDIOC_STREAMOFF, &types[1])) return 29;
f = (struct v4l2_format){ .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE };
f.fmt.pix_mp.width = 1920; f.fmt.pix_mp.height = 1088; f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12;
if (call(VIDIOC_S_FMT, &f)) return 30;
size = f.fmt.pix_mp.plane_fmt[0].sizeimage;
r = (struct v4l2_requestbuffers){ .count = 1, .type = types[1], .memory = V4L2_MEMORY_DMABUF };
if (call(VIDIOC_REQBUFS, &r)) return 31;
struct create_dumb big = { .height = 1088, .width = 1920, .bpp = 16 };
if (drm_ioctl(card, 0xc02064b2, &big)) return 32;
struct prime_handle bh = { .handle = big.handle, .flags = O_CLOEXEC };
if (drm_ioctl(card, 0xc00c642d, &bh)) return 33;
if (call(VIDIOC_STREAMON, &types[1])) return 34;
struct v4l2_plane bp = { .m.fd = bh.fd };
struct v4l2_buffer bb = { .index = 0, .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &bp };
if (call(VIDIOC_QBUF, &bb)) return 35;
memset(in[2], 'z', 50);
struct v4l2_plane zp = { .bytesused = 50 };
struct v4l2_buffer zb = { .index = 2, .type = types[0], .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &zp };
zb.timestamp.tv_usec = 6;
if (call(VIDIOC_QBUF, &zb)) return 36;
struct v4l2_plane dp = { 0 };
struct v4l2_buffer db = { .type = types[1], .memory = V4L2_MEMORY_DMABUF, .length = 1, .m.planes = &dp };
if (call(VIDIOC_DQBUF, &db)) return 37;
unsigned char *m = mmap(NULL, size, PROT_READ, MAP_SHARED, bh.fd, 0);
printf("%ld %u %c%c\n", (long)db.timestamp.tv_usec, db.index, m[0], m[(size_t)1920 * 1088]);
bl_vdec_closed(vfd);
return 0;
}
'''
CONFIG = struct.Struct('<IIIIIIII')
CAPTURE = struct.Struct('<III')
PACKET = struct.Struct('<IIII q')
RESULT = struct.Struct('<IiI')
class FakeDecoder(threading.Thread):
def __init__(self, path):
super().__init__(daemon=True)
self.listener = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
self.listener.bind(path)
self.listener.listen(1)
self.configs, self.packets = [], []
def run(self):
while True:
try:
conn, _ = self.listener.accept()
except OSError:
return
threading.Thread(target=self.serve, args=(conn,), daemon=True).start()
def serve(self, conn):
bitstream, captures, config = None, {}, None
while True:
fds = array.array('i')
msg, ancdata, _, _ = conn.recvmsg(256, socket.CMSG_LEN(4 * fds.itemsize))
if not msg:
return
for level, kind, data in ancdata:
if level == socket.SOL_SOCKET and kind == socket.SCM_RIGHTS:
fds.frombytes(data[:len(data) - len(data) % fds.itemsize])
kind = struct.unpack_from('<I', msg)[0]
if kind == 6:
config = CONFIG.unpack(msg)
self.configs.append(config)
bitstream = mmap.mmap(fds[0], config[6] * config[7], prot=mmap.PROT_READ)
os.close(fds[0])
conn.send(RESULT.pack(9, 0, 0))
elif kind == 7:
_, index, size = CAPTURE.unpack(msg)
captures[index] = mmap.mmap(fds[0], size)
os.close(fds[0])
elif kind == 8:
_, source, nbytes, target, pts = PACKET.unpack(msg)
self.packets.append((source, nbytes, target, pts))
first = bitstream[source * config[7]]
frame = captures[target]
frame[0] = first
frame[config[4] * config[5]] = ord('u')
conn.send(RESULT.pack(9, 0, 1))
class VdecEmulatorTest(unittest.TestCase):
def test_remote_play_client_decodes_through_the_app_socket(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
driver = tmp / 'driver.c'
driver.write_text(DRIVER)
binary = tmp / 'streaming_client'
subprocess.run(['cc', '-O1', '-pthread', '-DBL_VDEC_ANY_ARCH', '-o', str(binary), str(driver), str(VDEC),
str(VENC), '-ldl'], check=True)
sock = tmp / 'venc.sock'
server = FakeDecoder(str(sock))
self.addCleanup(server.listener.close)
server.start()
done = subprocess.run([str(binary)], env=dict(os.environ, BL_VENC_SOCKET=str(sock)),
capture_output=True, text=True, timeout=20)
self.assertEqual(done.returncode, 0, done.stdout + done.stderr)
self.assertEqual(done.stdout.split('\n')[:6],
['1 0 bu 1', '2 1 cu 1', '3 2 du 1', '4 0 eu 1', '5 1 fu 1', '6 0 zu'])
self.assertEqual([c[1:6] for c in server.configs],
[(1, 1280, 720, 1280, 736), (1, 1280, 720, 1920, 1088)])
self.assertEqual([p[:3] for p in server.packets],
[(1, 101, 0), (2, 102, 1), (3, 103, 2), (0, 104, 0), (1, 105, 1), (2, 50, 0)])
def test_other_programs_keep_the_real_libdrm(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
(tmp / 'main.c').write_text(
'void *SDL_LoadObject(const char *);\n'
'int main(void) { return SDL_LoadObject("libdrm.so.2") != 0; }\n')
binary = tmp / 'steam'
subprocess.run(['cc', '-O1', '-pthread', '-DBL_VDEC_ANY_ARCH', '-o', str(binary), str(tmp / 'main.c'),
str(VDEC), str(VENC), '-ldl'], check=True)
done = subprocess.run([str(binary)], capture_output=True, text=True, timeout=20)
self.assertEqual(done.returncode, 0, done.stderr)
if __name__ == '__main__':
unittest.main()
+30
View File
@@ -223,5 +223,35 @@ class VencEmulatorTest(unittest.TestCase):
self.assertEqual(server.touches, [(0, 65535, 32767), (1, 32767, 0), (2, 32767, 0)])
def test_xtest_calls_from_a_locally_loaded_library_still_reach_libxtst(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
(tmp / 'xtst.c').write_text(
'int XTestFakeButtonEvent(void *d, unsigned b, int p, unsigned long t) { return 7; }\n'
'int XTestFakeMotionEvent(void *d, int s, int x, int y, unsigned long t) { return 7; }\n')
(tmp / 'ui.c').write_text(
'int XTestFakeButtonEvent(void *, unsigned, int, unsigned long);\n'
'int XTestFakeMotionEvent(void *, int, int, int, unsigned long);\n'
'int ui_click(void) { return XTestFakeButtonEvent(0, 1, 1, 0) + XTestFakeButtonEvent(0, 3, 0, 0); }\n'
'int ui_move(void) { return XTestFakeMotionEvent(0, 0, 10, 20, 0); }\n')
(tmp / 'main.c').write_text(
'#include <dlfcn.h>\n#include <stdio.h>\n'
'int main(int argc, char **argv) {\n'
' void *ui = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);\n'
' if (!ui) return 2;\n'
' int (*click)(void) = dlsym(ui, "ui_click"), (*move)(void) = dlsym(ui, "ui_move");\n'
' printf("%d %d\\n", click(), move());\n'
' return click() == 14 && move() == 7 ? 0 : 1;\n'
'}\n')
run = lambda *a: subprocess.run(['cc', *a], check=True, cwd=tmp)
run('-shared', '-fPIC', '-Wl,-soname,libXtst.so.6', '-o', 'libXtst.so.6', 'xtst.c')
run('-shared', '-fPIC', '-o', 'libui.so', 'ui.c', '-L.', '-l:libXtst.so.6', "-Wl,-rpath,$ORIGIN")
run('-shared', '-fPIC', '-pthread', '-o', 'libhook.so', str(VENC), str(TOUCH), '-ldl')
run('-o', 'main', 'main.c', '-ldl')
done = subprocess.run([str(tmp / 'main'), str(tmp / 'libui.so')],
env=dict(os.environ, LD_PRELOAD=str(tmp / 'libhook.so')),
capture_output=True, text=True, timeout=20)
self.assertEqual(done.returncode, 0, done.stdout + done.stderr)
if __name__ == '__main__':
unittest.main()
Loaded 100 of 1553 files, more files were not shown because too many files have changed in this diff. Show more