| @@ -0,0 +1,196 @@ |
| 1 | +//! `deleteCalendarEvent` on Android — the first action here that answers |
| 2 | +//! through the reply channel rather than by returning. |
| 3 | +//! |
| 4 | +//! ## What the shim does, and the one thing this does not reproduce |
| 5 | +//! |
| 6 | +//! The Kotlin builds a content URI from the event id, deletes through the |
| 7 | +//! `ContentResolver`, and resolves `true`; on any exception it rejects with |
| 8 | +//! the message. That is all ported. |
| 9 | +//! |
| 10 | +//! What is not ported is how it *sends* the rejection: |
| 11 | +//! |
| 12 | +//! "window._craftDeleteEventReject && window._craftDeleteEventReject('${e.message}')" |
| 13 | +//! |
| 14 | +//! The message goes into a single-quoted JavaScript string with no escaping, |
| 15 | +//! and `eventId.toLong()` puts the caller's own text in that message — |
| 16 | +//! `NumberFormatException: For input string: "1'x"`. A `'` in the id therefore |
| 17 | +//! produces a script that does not parse, `evaluateJavascript` runs nothing, |
| 18 | +//! and **neither the resolve nor the reject fires**. The promise never |
| 19 | +//! settles, and the injected JS builds it by hand with no timeout, so the page |
| 20 | +//! waits forever with nothing in the console. |
| 21 | +//! |
| 22 | +//! Here the payload is built as JSON through `bridge_error.appendJsonEscaped`, |
| 23 | +//! so the same id rejects normally. That is a divergence in the page's favour |
| 24 | +//! and it is deliberate; the shim keeps the bug on the twenty other paths |
| 25 | +//! until #154 is fixed. |
| 26 | +//! |
| 27 | +//! ## `toLong()` is reproduced exactly, including what it refuses |
| 28 | +//! |
| 29 | +//! `Long.parseLong` accepts an optional sign and decimal digits, and nothing |
| 30 | +//! else — no whitespace, no underscores, no hex, no `+1_000`. Zig's |
| 31 | +//! `parseInt(i64, s, 10)` is more permissive about underscores, so the digits |
| 32 | +//! are checked before parsing. An id the shim would reject must reject here, |
| 33 | +//! or Zig would delete an event the shim would have refused to look up. |
| 34 | + |
| 35 | +const std = ; |
| 36 | +const jni = ; |
| 37 | +const events = ; |
| 38 | +const bridge_error = ; |
| 39 | + |
| 40 | +const Jni = jni.Jni; |
| 41 | +const jobject = jni.jobject; |
| 42 | + |
| 43 | +pub const A = struct { |
| 44 | + pub const delete_calendar_event = "deleteCalendarEvent"; |
| 45 | +}; |
| 46 | + |
| 47 | +/// The globals the injected JS assigns for this action. |
| 48 | +pub const resolve_global = "_craftDeleteEventResolve"; |
| 49 | +pub const reject_global = "_craftDeleteEventReject"; |
| 50 | + |
| 51 | +/// `Long.parseLong(text)`, or null where Java would throw. |
| 52 | +/// |
| 53 | +/// Deliberately stricter than `std.fmt.parseInt`, which accepts `1_000`. |
| 54 | +/// Java does not, and an id this accepted but the shim would not is an id Zig |
| 55 | +/// would act on and the shim would refuse. |
| 56 | +pub fn parseEventId(text: []const u8) ?i64 { |
| 57 | + if (text.len == 0) return null; |
| 58 | + |
| 59 | + var digits = text; |
| 60 | + if (digits[0] == '+' or digits[0] == '-') digits = digits[1..]; |
| 61 | + if (digits.len == 0) return null; |
| 62 | + |
| 63 | + for (digits) |c| { |
| 64 | + if (!std.ascii.isDigit(c)) return null; |
| 65 | + } |
| 66 | + return std.fmt.parseInt(i64, text, 10) catch null; |
| 67 | +} |
| 68 | + |
| 69 | +/// `contentResolver.delete(ContentUris.withAppendedId(Events.CONTENT_URI, id), null, null)`. |
| 70 | +pub fn deleteEvent(j: Jni, activity: jobject, id: i64) !void { |
| 71 | + try j.pushLocalFrame(16); |
| 72 | + defer _ = j.popLocalFrame(null); |
| 73 | + |
| 74 | + // `CalendarContract$Events` — a nested class, so the binary name takes the |
| 75 | + // dollar. `android/provider/CalendarContract/Events` finds nothing. |
| 76 | + const events_cls = try j.findClass("android/provider/CalendarContract$Events"); |
| 77 | + const base_uri = try j.staticObjectField( |
| 78 | + events_cls, |
| 79 | + try j.staticFieldId(events_cls, "CONTENT_URI", "Landroid/net/Uri;"), |
| 80 | + ); |
| 81 | + |
| 82 | + const content_uris_cls = try j.findClass("android/content/ContentUris"); |
| 83 | + const uri = try j.callStaticObjectMethodA( |
| 84 | + content_uris_cls, |
| 85 | + try j.staticMethodId( |
| 86 | + content_uris_cls, |
| 87 | + "withAppendedId", |
| 88 | + "(Landroid/net/Uri;J)Landroid/net/Uri;", |
| 89 | + ), |
| 90 | + &.{ .{ .l = base_uri }, .{ .j = id } }, |
| 91 | + ); |
| 92 | + |
| 93 | + const activity_cls = try j.objectClass(activity); |
| 94 | + const resolver = try j.callObjectMethod( |
| 95 | + activity, |
| 96 | + try j.methodId(activity_cls, "getContentResolver", "()Landroid/content/ContentResolver;"), |
| 97 | + ); |
| 98 | + |
| 99 | + // The row count is discarded, as the Kotlin discards it. Deleting an event |
| 100 | + // that is not there returns 0 and is not an error on either side — a page |
| 101 | + // removing something already gone has done nothing wrong. |
| 102 | + const resolver_cls = try j.objectClass(resolver); |
| 103 | + _ = try j.callIntMethodA( |
| 104 | + resolver, |
| 105 | + try j.methodId( |
| 106 | + resolver_cls, |
| 107 | + "delete", |
| 108 | + "(Landroid/net/Uri;Ljava/lang/String;[Ljava/lang/String;)I", |
| 109 | + ), |
| 110 | + &.{ .{ .l = uri }, .{ .l = null }, .{ .l = null } }, |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +/// Reject with `message`, escaped. |
| 115 | +/// |
| 116 | +/// The escaping is the whole point of this function existing rather than the |
| 117 | +/// call being inline — see the module comment and #154. |
| 118 | +pub fn rejectWith(allocator: std.mem.Allocator, message: []const u8) !void { |
| 119 | + const payload = try rejectPayload(allocator, message); |
| 120 | + defer allocator.free(payload); |
| 121 | + try events.settle(allocator, reject_global, payload); |
| 122 | +} |
| 123 | + |
| 124 | +/// `message` as a JSON string, quotes included. |
| 125 | +/// |
| 126 | +/// Split out so the test exercises this rather than a copy of it. An earlier |
| 127 | +/// version inlined the escaping here and asserted on the same three lines |
| 128 | +/// rewritten in the test — which passed happily when the escaping was removed |
| 129 | +/// from the code, because the test was not calling the code. |
| 130 | +pub fn rejectPayload(allocator: std.mem.Allocator, message: []const u8) ![]u8 { |
| 131 | + var payload: std.ArrayListUnmanaged(u8) = .empty; |
| 132 | + errdefer payload.deinit(allocator); |
| 133 | + |
| 134 | + // The quotes are this caller's job: `appendJsonEscaped` escapes the |
| 135 | + // contents and writes no delimiters. |
| 136 | + try payload.append(allocator, '"'); |
| 137 | + try bridge_error.appendJsonEscaped(allocator, &payload, message); |
| 138 | + try payload.append(allocator, '"'); |
| 139 | + |
| 140 | + return payload.toOwnedSlice(allocator); |
| 141 | +} |
| 142 | + |
| 143 | +// ============================================================================= |
| 144 | +// Tests |
| 145 | +// ============================================================================= |
| 146 | + |
| 147 | +const testing = std.testing; |
| 148 | + |
| 149 | +test "an id parses exactly where Long.parseLong would" { |
| 150 | + try testing.expectEqual(@as(i64, 42), parseEventId("42").?); |
| 151 | + try testing.expectEqual(@as(i64, -42), parseEventId("-42").?); |
| 152 | + try testing.expectEqual(@as(i64, 42), parseEventId("+42").?); |
| 153 | + try testing.expectEqual(@as(i64, 0), parseEventId("0").?); |
| 154 | + try testing.expectEqual(@as(i64, 9_223_372_036_854_775_807), parseEventId("9223372036854775807").?); |
| 155 | +} |
| 156 | + |
| 157 | +test "an id Java would refuse is refused here too" { |
| 158 | + // Each of these throws NumberFormatException on the shim, so each must |
| 159 | + // reject here. An id Zig accepted and the shim did not is an id Zig would |
| 160 | + // delete an event for that the shim would never have looked up. |
| 161 | + for ([_][]const u8{ |
| 162 | + "", "abc", "1'x", " 42", "42 ", "4 2", "0x2a", "1e3", "42L", "+", "-", |
| 163 | + "٤٢", |
| 164 | + // Zig's parseInt accepts this and Java does not. |
| 165 | + "1_000", |
| 166 | + // Past Long.MAX_VALUE. |
| 167 | + "9223372036854775808", |
| 168 | + }) |bad| { |
| 169 | + try testing.expect(parseEventId(bad) == null); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +test "a rejection message with a quote is escaped rather than emitted raw" { |
| 174 | + // The bug this action does not reproduce. On the shim, an id of `1'x` |
| 175 | + // produces `…Reject('For input string: "1'x"')`, which does not parse — so |
| 176 | + // evaluateJavascript runs nothing and the promise never settles. |
| 177 | + const payload = try rejectPayload(testing.allocator, "For input string: \"1'x\""); |
| 178 | + defer testing.allocator.free(payload); |
| 179 | + |
| 180 | + // Valid JSON, and it round-trips to exactly the original message. |
| 181 | + var parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, payload, .{}); |
| 182 | + defer parsed.deinit(); |
| 183 | + try testing.expectEqualStrings("For input string: \"1'x\"", parsed.value.string); |
| 184 | + |
| 185 | + // And the apostrophe survives as itself rather than as an escape, which is |
| 186 | + // what makes the emitted script parse where the shim's does not. |
| 187 | + try testing.expect(std.mem.indexOf(u8, payload, "'") != null); |
| 188 | +} |
| 189 | + |
| 190 | +test "the action name and its globals match the shim exactly" { |
| 191 | + // The globals are assigned by the injected JS, and a rename on either side |
| 192 | + // is a promise that never settles. |
| 193 | + try testing.expectEqualStrings("deleteCalendarEvent", A.delete_calendar_event); |
| 194 | + try testing.expectEqualStrings("_craftDeleteEventResolve", resolve_global); |
| 195 | + try testing.expectEqualStrings("_craftDeleteEventReject", reject_global); |
| 196 | +} |