fix(plugins): carry plugin file handle ids as uuid strings
This commit is contained in:
1 parent
9bd9b93267
commit
4f0f69131b
5 files changed
+104
-66
No files matched your search
@@ -148,7 +148,8 @@ describe("PluginWorkbenchHost initialization", () => {
|
|||||||
it("claims OS drops over its iframe and forwards opened handles to the plugin", async () => {
|
it("claims OS drops over its iframe and forwards opened handles to the plugin", async () => {
|
||||||
const { frame, postMessage } = await mountHost();
|
const { frame, postMessage } = await mountHost();
|
||||||
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
|
const elementFromPoint = vi.spyOn(document, "elementFromPoint").mockReturnValue(frame);
|
||||||
mocks.openPluginLocalFile.mockResolvedValue({ handleId: 7, name: "a.txt", size: 3, contentType: "text/plain", write: false });
|
// The Rust registry hands out uuid strings; the `t` prefix stays opaque.
|
||||||
|
mocks.openPluginLocalFile.mockResolvedValue({ handleId: "0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81", name: "a.txt", size: 3, contentType: "text/plain", write: false });
|
||||||
|
|
||||||
const claimed = !document.dispatchEvent(
|
const claimed = !document.dispatchEvent(
|
||||||
new CustomEvent("dbx:tauri-file-drop", {
|
new CustomEvent("dbx:tauri-file-drop", {
|
||||||
@@ -160,7 +161,7 @@ describe("PluginWorkbenchHost initialization", () => {
|
|||||||
expect(claimed).toBe(true);
|
expect(claimed).toBe(true);
|
||||||
await vi.waitFor(() => {
|
await vi.waitFor(() => {
|
||||||
const posted = postMessage.mock.calls.map(([message]) => message as Record<string, unknown>);
|
const posted = postMessage.mock.calls.map(([message]) => message as Record<string, unknown>);
|
||||||
expect(posted.some((message) => message.type === "filedrop" && (message.files as Array<Record<string, unknown>>)?.some((file) => file.handleId === "t7" && file.name === "a.txt"))).toBe(true);
|
expect(posted.some((message) => message.type === "filedrop" && (message.files as Array<Record<string, unknown>>)?.some((file) => file.handleId === "t0d9f6d26-9e0e-4b1f-8f9a-2b6d3c5a7e81" && file.name === "a.txt"))).toBe(true);
|
||||||
});
|
});
|
||||||
expect(mocks.openPluginLocalFile).toHaveBeenCalledWith("sample", "/tmp/a.txt", false);
|
expect(mocks.openPluginLocalFile).toHaveBeenCalledWith("sample", "/tmp/a.txt", false);
|
||||||
elementFromPoint.mockRestore();
|
elementFromPoint.mockRestore();
|
||||||
|
|||||||
@@ -60,15 +60,17 @@ let loadGeneration = 0;
|
|||||||
|
|
||||||
// --- Plugin file-transfer bridge (native dialogs + OS file drops) ---------
|
// --- Plugin file-transfer bridge (native dialogs + OS file drops) ---------
|
||||||
// The sandboxed iframe cannot reach local files, so handles live here: Tauri
|
// The sandboxed iframe cannot reach local files, so handles live here: Tauri
|
||||||
// handles wrap the plugin_file registry in Rust (`t<n>` ids); the web host
|
// handles wrap the plugin_file registry in Rust (`t<uuid>` ids); the web host
|
||||||
// keeps File objects and in-memory save buffers (`w<n>` ids). Only paths that
|
// keeps File objects and in-memory save buffers (`w<n>` ids). Handle ids are
|
||||||
// came from a native dialog or an OS drop reach plugin_file_open — never a
|
// opaque strings end to end — never run them through Number(): ids above
|
||||||
// plugin-supplied string.
|
// Number.MAX_SAFE_INTEGER silently round, and the registry then rejects every
|
||||||
|
// read with "unknown plugin file handle". Only paths that came from a native
|
||||||
|
// dialog or an OS drop reach plugin_file_open — never a plugin-supplied string.
|
||||||
|
|
||||||
let webFileSequence = 0;
|
let webFileSequence = 0;
|
||||||
const webPickedFiles = new Map<string, File>();
|
const webPickedFiles = new Map<string, File>();
|
||||||
const webSaveBuffers = new Map<string, { name: string; contentType: string; chunks: Map<number, Uint8Array> }>();
|
const webSaveBuffers = new Map<string, { name: string; contentType: string; chunks: Map<number, Uint8Array> }>();
|
||||||
const openTauriHandles = new Set<number>();
|
const openTauriHandles = new Set<string>();
|
||||||
const tauriHandlePrefix = "t";
|
const tauriHandlePrefix = "t";
|
||||||
const webHandlePrefix = "w";
|
const webHandlePrefix = "w";
|
||||||
|
|
||||||
@@ -81,9 +83,9 @@ function encodeBytesBase64(bytes: Uint8Array): string {
|
|||||||
return btoa(binary);
|
return btoa(binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseHandleId(handleId: string): { source: "tauri" | "web"; numericId: number } {
|
function parseHandleId(handleId: string): { source: "tauri" | "web"; rawId: string } {
|
||||||
if (handleId.startsWith(tauriHandlePrefix)) return { source: "tauri", numericId: Number(handleId.slice(tauriHandlePrefix.length)) };
|
if (handleId.startsWith(tauriHandlePrefix)) return { source: "tauri", rawId: handleId.slice(tauriHandlePrefix.length) };
|
||||||
if (handleId.startsWith(webHandlePrefix)) return { source: "web", numericId: Number(handleId.slice(webHandlePrefix.length)) };
|
if (handleId.startsWith(webHandlePrefix)) return { source: "web", rawId: handleId.slice(webHandlePrefix.length) };
|
||||||
throw new Error("Unknown file handle");
|
throw new Error("Unknown file handle");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +151,7 @@ async function readPluginFileChunkById(pluginId: string, handleId: string, offse
|
|||||||
const parsed = parseHandleId(handleId);
|
const parsed = parseHandleId(handleId);
|
||||||
if (parsed.source === "tauri") {
|
if (parsed.source === "tauri") {
|
||||||
const { readPluginLocalFileChunk } = await tauriFileApi();
|
const { readPluginLocalFileChunk } = await tauriFileApi();
|
||||||
return readPluginLocalFileChunk(pluginId, parsed.numericId, offset, length);
|
return readPluginLocalFileChunk(pluginId, parsed.rawId, offset, length);
|
||||||
}
|
}
|
||||||
const file = webPickedFiles.get(handleId);
|
const file = webPickedFiles.get(handleId);
|
||||||
if (!file) throw new Error("Unknown file handle");
|
if (!file) throw new Error("Unknown file handle");
|
||||||
@@ -217,7 +219,7 @@ async function writePluginFileChunkById(pluginId: string, handleId: string, offs
|
|||||||
const parsed = parseHandleId(handleId);
|
const parsed = parseHandleId(handleId);
|
||||||
if (parsed.source === "tauri") {
|
if (parsed.source === "tauri") {
|
||||||
const { writePluginLocalFileChunk } = await tauriFileApi();
|
const { writePluginLocalFileChunk } = await tauriFileApi();
|
||||||
return writePluginLocalFileChunk(pluginId, parsed.numericId, offset, encodeBytesBase64(bytes));
|
return writePluginLocalFileChunk(pluginId, parsed.rawId, offset, encodeBytesBase64(bytes));
|
||||||
}
|
}
|
||||||
const buffer = webSaveBuffers.get(handleId);
|
const buffer = webSaveBuffers.get(handleId);
|
||||||
if (!buffer) throw new Error("Unknown file handle");
|
if (!buffer) throw new Error("Unknown file handle");
|
||||||
@@ -229,8 +231,8 @@ async function finishPluginFileSave(pluginId: string, handleId: string): Promise
|
|||||||
const parsed = parseHandleId(handleId);
|
const parsed = parseHandleId(handleId);
|
||||||
if (parsed.source === "tauri") {
|
if (parsed.source === "tauri") {
|
||||||
const { closePluginLocalFile } = await tauriFileApi();
|
const { closePluginLocalFile } = await tauriFileApi();
|
||||||
openTauriHandles.delete(parsed.numericId);
|
openTauriHandles.delete(parsed.rawId);
|
||||||
await closePluginLocalFile(pluginId, parsed.numericId);
|
await closePluginLocalFile(pluginId, parsed.rawId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const buffer = webSaveBuffers.get(handleId);
|
const buffer = webSaveBuffers.get(handleId);
|
||||||
@@ -256,8 +258,8 @@ async function closePluginFileHandleById(pluginId: string, handleId: string): Pr
|
|||||||
const parsed = parseHandleId(handleId);
|
const parsed = parseHandleId(handleId);
|
||||||
if (parsed.source === "tauri") {
|
if (parsed.source === "tauri") {
|
||||||
const { closePluginLocalFile } = await tauriFileApi();
|
const { closePluginLocalFile } = await tauriFileApi();
|
||||||
openTauriHandles.delete(parsed.numericId);
|
openTauriHandles.delete(parsed.rawId);
|
||||||
await closePluginLocalFile(pluginId, parsed.numericId);
|
await closePluginLocalFile(pluginId, parsed.rawId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
webPickedFiles.delete(handleId);
|
webPickedFiles.delete(handleId);
|
||||||
|
|||||||
@@ -5181,15 +5181,15 @@ export async function openPluginLocalFile(_pluginId: string, _path: string, _wri
|
|||||||
throw new Error("Plugin local file access is not available in the web backend");
|
throw new Error("Plugin local file access is not available in the web backend");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readPluginLocalFileChunk(_pluginId: string, _handleId: number, _offset: number, _length?: number): Promise<PluginLocalFileChunk> {
|
export async function readPluginLocalFileChunk(_pluginId: string, _handleId: string, _offset: number, _length?: number): Promise<PluginLocalFileChunk> {
|
||||||
throw new Error("Plugin local file access is not available in the web backend");
|
throw new Error("Plugin local file access is not available in the web backend");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writePluginLocalFileChunk(_pluginId: string, _handleId: number, _offset: number, _dataBase64: string): Promise<PluginLocalFileWriteResult> {
|
export async function writePluginLocalFileChunk(_pluginId: string, _handleId: string, _offset: number, _dataBase64: string): Promise<PluginLocalFileWriteResult> {
|
||||||
throw new Error("Plugin local file access is not available in the web backend");
|
throw new Error("Plugin local file access is not available in the web backend");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function closePluginLocalFile(_pluginId: string, _handleId: number): Promise<void> {
|
export async function closePluginLocalFile(_pluginId: string, _handleId: string): Promise<void> {
|
||||||
throw new Error("Plugin local file access is not available in the web backend");
|
throw new Error("Plugin local file access is not available in the web backend");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2462,7 +2462,8 @@ export async function sendPluginBinary(pluginId: string, channel: string, dataBa
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface PluginLocalFileHandle {
|
export interface PluginLocalFileHandle {
|
||||||
handleId: number;
|
/** Opaque uuid string from the Rust registry — never a number (JS doubles lose precision above 2^53). */
|
||||||
|
handleId: string;
|
||||||
name: string;
|
name: string;
|
||||||
size: number;
|
size: number;
|
||||||
contentType: string;
|
contentType: string;
|
||||||
@@ -2484,15 +2485,15 @@ export async function openPluginLocalFile(pluginId: string, path: string, write:
|
|||||||
return invoke("plugin_file_open", { pluginId, path, write });
|
return invoke("plugin_file_open", { pluginId, path, write });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readPluginLocalFileChunk(pluginId: string, handleId: number, offset: number, length?: number): Promise<PluginLocalFileChunk> {
|
export async function readPluginLocalFileChunk(pluginId: string, handleId: string, offset: number, length?: number): Promise<PluginLocalFileChunk> {
|
||||||
return invoke("plugin_file_read", { pluginId, handleId, offset, length });
|
return invoke("plugin_file_read", { pluginId, handleId, offset, length });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writePluginLocalFileChunk(pluginId: string, handleId: number, offset: number, dataBase64: string): Promise<PluginLocalFileWriteResult> {
|
export async function writePluginLocalFileChunk(pluginId: string, handleId: string, offset: number, dataBase64: string): Promise<PluginLocalFileWriteResult> {
|
||||||
return invoke("plugin_file_write", { pluginId, handleId, offset, dataBase64 });
|
return invoke("plugin_file_write", { pluginId, handleId, offset, dataBase64 });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function closePluginLocalFile(pluginId: string, handleId: number): Promise<void> {
|
export async function closePluginLocalFile(pluginId: string, handleId: string): Promise<void> {
|
||||||
return invoke("plugin_file_close", { pluginId, handleId });
|
return invoke("plugin_file_close", { pluginId, handleId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ struct OpenFile {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PluginFileState {
|
pub struct PluginFileState {
|
||||||
handles: Mutex<HashMap<u64, OpenFile>>,
|
handles: Mutex<HashMap<String, OpenFile>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PluginFileState {
|
impl PluginFileState {
|
||||||
@@ -52,7 +52,12 @@ impl PluginFileState {
|
|||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PluginFileHandle {
|
pub struct PluginFileHandle {
|
||||||
handle_id: u64,
|
/// Opaque handle id, carried as a UUID string on the wire — the same
|
||||||
|
/// convention as `downloadId`. It must never be a JSON number: ids are
|
||||||
|
/// parsed as doubles in every JS layer, and a u64 loses precision above
|
||||||
|
/// `Number.MAX_SAFE_INTEGER`, which silently corrupted ids and failed
|
||||||
|
/// every bridge read/write with "unknown plugin file handle".
|
||||||
|
handle_id: String,
|
||||||
name: String,
|
name: String,
|
||||||
size: u64,
|
size: u64,
|
||||||
content_type: String,
|
content_type: String,
|
||||||
@@ -102,21 +107,23 @@ fn guess_content_type(path: &Path) -> String {
|
|||||||
|
|
||||||
fn with_handles<T>(
|
fn with_handles<T>(
|
||||||
state: &PluginFileState,
|
state: &PluginFileState,
|
||||||
body: impl FnOnce(&mut HashMap<u64, OpenFile>) -> Result<T, String>,
|
body: impl FnOnce(&mut HashMap<String, OpenFile>) -> Result<T, String>,
|
||||||
) -> Result<T, String> {
|
) -> Result<T, String> {
|
||||||
let mut handles = state.handles.lock().map_err(|_| "plugin file registry poisoned".to_string())?;
|
let mut handles = state.handles.lock().map_err(|_| "plugin file registry poisoned".to_string())?;
|
||||||
body(&mut handles)
|
body(&mut handles)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn owned_handle<'a>(
|
fn owned_handle<'a>(
|
||||||
handles: &'a mut HashMap<u64, OpenFile>,
|
handles: &'a mut HashMap<String, OpenFile>,
|
||||||
plugin_id: &str,
|
plugin_id: &str,
|
||||||
handle_id: u64,
|
handle_id: &str,
|
||||||
) -> Result<&'a mut OpenFile, String> {
|
) -> Result<&'a mut OpenFile, String> {
|
||||||
let entry = handles.get_mut(&handle_id).ok_or_else(|| "unknown plugin file handle".to_string())?;
|
let entry = handles.get_mut(handle_id).ok_or_else(|| "unknown plugin file handle".to_string())?;
|
||||||
if entry.owner != plugin_id {
|
if entry.owner != plugin_id {
|
||||||
// Deliberately indistinguishable from a missing handle: a plugin must
|
// Response stays indistinguishable from a missing handle: a plugin must
|
||||||
// not learn that another plugin's handle ids exist.
|
// not learn that another plugin's handle ids exist. The host log is the
|
||||||
|
// one place the two cases can be told apart when debugging.
|
||||||
|
log::warn!("plugin file handle belongs to another plugin (caller: {plugin_id})");
|
||||||
return Err("unknown plugin file handle".to_string());
|
return Err("unknown plugin file handle".to_string());
|
||||||
}
|
}
|
||||||
Ok(entry)
|
Ok(entry)
|
||||||
@@ -162,10 +169,11 @@ pub fn open_plugin_file(
|
|||||||
return Err(format!("too many open plugin file handles (max {MAX_OPEN_HANDLES})"));
|
return Err(format!("too many open plugin file handles (max {MAX_OPEN_HANDLES})"));
|
||||||
}
|
}
|
||||||
// Unguessable id: a plugin that can only reach its own bridge must not
|
// Unguessable id: a plugin that can only reach its own bridge must not
|
||||||
// be able to walk the registry by trying sequential ids.
|
// be able to walk the registry by trying sequential ids. Same wire
|
||||||
let bytes = Uuid::new_v4().into_bytes();
|
// convention as downloadId: uuid v4, carried as a string so no JS
|
||||||
let handle_id = u64::from_be_bytes(bytes[0..8].try_into().expect("uuid prefix"));
|
// layer can lose precision on it.
|
||||||
handles.insert(handle_id, OpenFile { file, write, size, owner: plugin_id.to_string() });
|
let handle_id = Uuid::new_v4().to_string();
|
||||||
|
handles.insert(handle_id.clone(), OpenFile { file, write, size, owner: plugin_id.to_string() });
|
||||||
Ok(PluginFileHandle { handle_id, name, size, content_type: guess_content_type(path), write })
|
Ok(PluginFileHandle { handle_id, name, size, content_type: guess_content_type(path), write })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -173,7 +181,7 @@ pub fn open_plugin_file(
|
|||||||
pub fn read_plugin_file_chunk(
|
pub fn read_plugin_file_chunk(
|
||||||
state: &PluginFileState,
|
state: &PluginFileState,
|
||||||
plugin_id: &str,
|
plugin_id: &str,
|
||||||
handle_id: u64,
|
handle_id: &str,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
length: Option<u32>,
|
length: Option<u32>,
|
||||||
) -> Result<PluginFileReadChunk, String> {
|
) -> Result<PluginFileReadChunk, String> {
|
||||||
@@ -202,7 +210,7 @@ pub fn read_plugin_file_chunk(
|
|||||||
pub fn write_plugin_file_chunk(
|
pub fn write_plugin_file_chunk(
|
||||||
state: &PluginFileState,
|
state: &PluginFileState,
|
||||||
plugin_id: &str,
|
plugin_id: &str,
|
||||||
handle_id: u64,
|
handle_id: &str,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
data_base64: &str,
|
data_base64: &str,
|
||||||
) -> Result<PluginFileWriteResult, String> {
|
) -> Result<PluginFileWriteResult, String> {
|
||||||
@@ -224,7 +232,7 @@ pub fn write_plugin_file_chunk(
|
|||||||
/// Close a handle. Write handles are flushed to disk before dropping; the
|
/// Close a handle. Write handles are flushed to disk before dropping; the
|
||||||
/// bytes were already handed to the OS through `write_all`, so failure here
|
/// bytes were already handed to the OS through `write_all`, so failure here
|
||||||
/// means the OS could not flush — surfaced to the plugin as an error.
|
/// means the OS could not flush — surfaced to the plugin as an error.
|
||||||
pub fn close_plugin_file(state: &PluginFileState, plugin_id: &str, handle_id: u64) -> Result<(), String> {
|
pub fn close_plugin_file(state: &PluginFileState, plugin_id: &str, handle_id: &str) -> Result<(), String> {
|
||||||
with_handles(state, |handles| {
|
with_handles(state, |handles| {
|
||||||
{
|
{
|
||||||
let entry = owned_handle(handles, plugin_id, handle_id)?;
|
let entry = owned_handle(handles, plugin_id, handle_id)?;
|
||||||
@@ -232,7 +240,7 @@ pub fn close_plugin_file(state: &PluginFileState, plugin_id: &str, handle_id: u6
|
|||||||
entry.file.sync_all().map_err(|error| format!("flush failed: {error}"))?;
|
entry.file.sync_all().map_err(|error| format!("flush failed: {error}"))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
handles.remove(&handle_id).map(|_| ()).ok_or_else(|| "unknown plugin file handle".to_string())
|
handles.remove(handle_id).map(|_| ()).ok_or_else(|| "unknown plugin file handle".to_string())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,27 +258,31 @@ pub fn plugin_file_open(
|
|||||||
pub fn plugin_file_read(
|
pub fn plugin_file_read(
|
||||||
state: State<'_, PluginFileState>,
|
state: State<'_, PluginFileState>,
|
||||||
plugin_id: String,
|
plugin_id: String,
|
||||||
handle_id: u64,
|
handle_id: String,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
length: Option<u32>,
|
length: Option<u32>,
|
||||||
) -> Result<PluginFileReadChunk, String> {
|
) -> Result<PluginFileReadChunk, String> {
|
||||||
read_plugin_file_chunk(&state, &plugin_id, handle_id, offset, length)
|
read_plugin_file_chunk(&state, &plugin_id, &handle_id, offset, length)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn plugin_file_write(
|
pub fn plugin_file_write(
|
||||||
state: State<'_, PluginFileState>,
|
state: State<'_, PluginFileState>,
|
||||||
plugin_id: String,
|
plugin_id: String,
|
||||||
handle_id: u64,
|
handle_id: String,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
data_base64: String,
|
data_base64: String,
|
||||||
) -> Result<PluginFileWriteResult, String> {
|
) -> Result<PluginFileWriteResult, String> {
|
||||||
write_plugin_file_chunk(&state, &plugin_id, handle_id, offset, &data_base64)
|
write_plugin_file_chunk(&state, &plugin_id, &handle_id, offset, &data_base64)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn plugin_file_close(state: State<'_, PluginFileState>, plugin_id: String, handle_id: u64) -> Result<(), String> {
|
pub fn plugin_file_close(
|
||||||
close_plugin_file(&state, &plugin_id, handle_id)
|
state: State<'_, PluginFileState>,
|
||||||
|
plugin_id: String,
|
||||||
|
handle_id: String,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
close_plugin_file(&state, &plugin_id, &handle_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -296,15 +308,15 @@ mod tests {
|
|||||||
assert!(!handle.write);
|
assert!(!handle.write);
|
||||||
assert_eq!(handle.content_type, "application/octet-stream");
|
assert_eq!(handle.content_type, "application/octet-stream");
|
||||||
|
|
||||||
let chunk = read_plugin_file_chunk(&state, OWNER, handle.handle_id, 6, Some(6)).expect("read");
|
let chunk = read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 6, Some(6)).expect("read");
|
||||||
assert_eq!(BASE64.decode(chunk.data_base64).unwrap(), b"plugin");
|
assert_eq!(BASE64.decode(chunk.data_base64).unwrap(), b"plugin");
|
||||||
assert!(!chunk.eof);
|
assert!(!chunk.eof);
|
||||||
let tail = read_plugin_file_chunk(&state, OWNER, handle.handle_id, 13, Some(64)).expect("read tail");
|
let tail = read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 13, Some(64)).expect("read tail");
|
||||||
assert_eq!(BASE64.decode(tail.data_base64).unwrap(), b"bridge");
|
assert_eq!(BASE64.decode(tail.data_base64).unwrap(), b"bridge");
|
||||||
assert!(tail.eof);
|
assert!(tail.eof);
|
||||||
|
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
assert!(read_plugin_file_chunk(&state, OWNER, handle.handle_id, 0, None).is_err());
|
assert!(read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 0, None).is_err());
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,16 +326,16 @@ mod tests {
|
|||||||
let state = PluginFileState::new();
|
let state = PluginFileState::new();
|
||||||
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
||||||
|
|
||||||
let foreign_read = read_plugin_file_chunk(&state, OTHER, handle.handle_id, 0, None).unwrap_err();
|
let foreign_read = read_plugin_file_chunk(&state, OTHER, &handle.handle_id, 0, None).unwrap_err();
|
||||||
assert_eq!(foreign_read, "unknown plugin file handle");
|
assert_eq!(foreign_read, "unknown plugin file handle");
|
||||||
let foreign_write =
|
let foreign_write =
|
||||||
write_plugin_file_chunk(&state, OTHER, handle.handle_id, 0, &BASE64.encode(b"x")).unwrap_err();
|
write_plugin_file_chunk(&state, OTHER, &handle.handle_id, 0, &BASE64.encode(b"x")).unwrap_err();
|
||||||
assert_eq!(foreign_write, "unknown plugin file handle");
|
assert_eq!(foreign_write, "unknown plugin file handle");
|
||||||
let foreign_close = close_plugin_file(&state, OTHER, handle.handle_id).unwrap_err();
|
let foreign_close = close_plugin_file(&state, OTHER, &handle.handle_id).unwrap_err();
|
||||||
assert_eq!(foreign_close, "unknown plugin file handle");
|
assert_eq!(foreign_close, "unknown plugin file handle");
|
||||||
assert!(read_plugin_file_chunk(&state, OWNER, handle.handle_id, 0, None).is_ok());
|
assert!(read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 0, None).is_ok());
|
||||||
|
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,15 +347,15 @@ mod tests {
|
|||||||
assert!(handle.write);
|
assert!(handle.write);
|
||||||
assert_eq!(handle.size, 0);
|
assert_eq!(handle.size, 0);
|
||||||
assert!(
|
assert!(
|
||||||
read_plugin_file_chunk(&state, OWNER, handle.handle_id, 0, None).is_err(),
|
read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 0, None).is_err(),
|
||||||
"write handles reject reads"
|
"write handles reject reads"
|
||||||
);
|
);
|
||||||
|
|
||||||
let written =
|
let written =
|
||||||
write_plugin_file_chunk(&state, OWNER, handle.handle_id, 0, &BASE64.encode(b"fresh")).expect("write");
|
write_plugin_file_chunk(&state, OWNER, &handle.handle_id, 0, &BASE64.encode(b"fresh")).expect("write");
|
||||||
assert_eq!(written.written, 5);
|
assert_eq!(written.written, 5);
|
||||||
assert_eq!(written.next_offset, 5);
|
assert_eq!(written.next_offset, 5);
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
|
|
||||||
assert_eq!(std::fs::read(&path).unwrap(), b"fresh");
|
assert_eq!(std::fs::read(&path).unwrap(), b"fresh");
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
@@ -355,8 +367,8 @@ mod tests {
|
|||||||
let _ = std::fs::remove_file(&path);
|
let _ = std::fs::remove_file(&path);
|
||||||
let state = PluginFileState::new();
|
let state = PluginFileState::new();
|
||||||
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), true).expect("open write");
|
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), true).expect("open write");
|
||||||
write_plugin_file_chunk(&state, OWNER, handle.handle_id, 0, &BASE64.encode(b"data")).expect("write");
|
write_plugin_file_chunk(&state, OWNER, &handle.handle_id, 0, &BASE64.encode(b"data")).expect("write");
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
assert_eq!(std::fs::read(&path).unwrap(), b"data");
|
assert_eq!(std::fs::read(&path).unwrap(), b"data");
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
@@ -368,8 +380,8 @@ mod tests {
|
|||||||
open_plugin_file(&state, OWNER, std::env::temp_dir().to_string_lossy().as_ref(), false).unwrap_err();
|
open_plugin_file(&state, OWNER, std::env::temp_dir().to_string_lossy().as_ref(), false).unwrap_err();
|
||||||
assert!(error.contains("directory"));
|
assert!(error.contains("directory"));
|
||||||
assert!(open_plugin_file(&state, " ", std::env::temp_dir().to_string_lossy().as_ref(), false).is_err());
|
assert!(open_plugin_file(&state, " ", std::env::temp_dir().to_string_lossy().as_ref(), false).is_err());
|
||||||
assert!(read_plugin_file_chunk(&state, OWNER, 999_999, 0, None).is_err());
|
assert!(read_plugin_file_chunk(&state, OWNER, "00000000-0000-4000-8000-000000000099", 0, None).is_err());
|
||||||
assert!(close_plugin_file(&state, OWNER, 999_999).is_err());
|
assert!(close_plugin_file(&state, OWNER, "00000000-0000-4000-8000-000000000099").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -377,11 +389,11 @@ mod tests {
|
|||||||
let path = temp_file("bounds.bin", b"abc");
|
let path = temp_file("bounds.bin", b"abc");
|
||||||
let state = PluginFileState::new();
|
let state = PluginFileState::new();
|
||||||
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
||||||
assert!(read_plugin_file_chunk(&state, OWNER, handle.handle_id, 4, None).is_err());
|
assert!(read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 4, None).is_err());
|
||||||
let at_end = read_plugin_file_chunk(&state, OWNER, handle.handle_id, 3, None).expect("read at eof");
|
let at_end = read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 3, None).expect("read at eof");
|
||||||
assert!(at_end.eof);
|
assert!(at_end.eof);
|
||||||
assert_eq!(at_end.length, 0);
|
assert_eq!(at_end.length, 0);
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,14 +403,36 @@ mod tests {
|
|||||||
let state = PluginFileState::new();
|
let state = PluginFileState::new();
|
||||||
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
||||||
assert_eq!(handle.content_type, "image/svg+xml");
|
assert_eq!(handle.content_type, "image/svg+xml");
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
let _ = std::fs::remove_file(&path);
|
let _ = std::fs::remove_file(&path);
|
||||||
|
|
||||||
path.set_extension("weird");
|
path.set_extension("weird");
|
||||||
std::fs::write(&path, b"x").unwrap();
|
std::fs::write(&path, b"x").unwrap();
|
||||||
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
||||||
assert_eq!(handle.content_type, "application/octet-stream");
|
assert_eq!(handle.content_type, "application/octet-stream");
|
||||||
close_plugin_file(&state, OWNER, handle.handle_id).expect("close");
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handle_id_follows_the_uuid_string_wire_convention() {
|
||||||
|
// Handle ids ride the bridge as opaque strings, same as downloadId.
|
||||||
|
// The old wire format serialized a u64 JSON number: ids above
|
||||||
|
// Number.MAX_SAFE_INTEGER were rounded by every JS layer, so the host
|
||||||
|
// looked a fresh handle up under a different key and failed every
|
||||||
|
// read/write with "unknown plugin file handle".
|
||||||
|
let path = temp_file("wire-uuid.bin", b"payload");
|
||||||
|
let state = PluginFileState::new();
|
||||||
|
let handle = open_plugin_file(&state, OWNER, path.to_string_lossy().as_ref(), false).expect("open");
|
||||||
|
let wire_json = serde_json::to_string(&handle.handle_id).expect("serialize id");
|
||||||
|
assert_eq!(wire_json, format!("\"{}\"", handle.handle_id), "the wire id is a JSON string, never a number");
|
||||||
|
let parsed = uuid::Uuid::parse_str(&handle.handle_id).expect("id parses as uuid, like downloadId");
|
||||||
|
assert_eq!(parsed.to_string(), handle.handle_id);
|
||||||
|
|
||||||
|
// The exact string the host sent back must hit the same registry key.
|
||||||
|
let chunk = read_plugin_file_chunk(&state, OWNER, &handle.handle_id, 0, None).expect("read via wire id");
|
||||||
|
assert_eq!(BASE64.decode(chunk.data_base64).unwrap(), b"payload");
|
||||||
|
close_plugin_file(&state, OWNER, &handle.handle_id).expect("close");
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user