|
| 1 | +import { beforeEach, describe, expect, test } from 'react-native-harness'; |
| 2 | +import { NativeCookie, NativeCookieTestHelpers } from 'mendix-native'; |
| 3 | + |
| 4 | +// Cookie sizing constants. |
| 5 | +// 10 cookies × 7 000-char value → serialised blob ≈ 70–80 KB, above the 64 KB chunk threshold. |
| 6 | +const LARGE_COUNT = 10; |
| 7 | +const LARGE_VALUE_SIZE = 7_000; |
| 8 | +// 3 cookies with tiny values → blob well under 64 KB (single-item path). |
| 9 | +const SMALL_COUNT = 3; |
| 10 | +const SMALL_VALUE_SIZE = 10; |
| 11 | + |
| 12 | +describe('SessionCookieStore', () => { |
| 13 | + beforeEach(async () => { |
| 14 | + await NativeCookie.clearAll(); |
| 15 | + }); |
| 16 | + |
| 17 | + // --------------------------------------------------------------------------- |
| 18 | + // Small-blob (single-item keychain format, ≤ 64 KB) |
| 19 | + // --------------------------------------------------------------------------- |
| 20 | + |
| 21 | + describe('small-blob round-trip (single-item format)', () => { |
| 22 | + test('persists and restores small cookies', async () => { |
| 23 | + await NativeCookieTestHelpers.seedTestCookies( |
| 24 | + SMALL_COUNT, |
| 25 | + SMALL_VALUE_SIZE |
| 26 | + ); |
| 27 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 28 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 29 | + |
| 30 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 31 | + |
| 32 | + expect(names.length).toBe(SMALL_COUNT); |
| 33 | + for (let i = 0; i < SMALL_COUNT; i++) { |
| 34 | + expect(names).toContain(`testCookie${i}`); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + test('single-item write does not create a chunk commit-marker', async () => { |
| 39 | + await NativeCookieTestHelpers.seedTestCookies( |
| 40 | + SMALL_COUNT, |
| 41 | + SMALL_VALUE_SIZE |
| 42 | + ); |
| 43 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 44 | + |
| 45 | + const chunkCount = await NativeCookieTestHelpers.getKeychainChunkCount(); |
| 46 | + |
| 47 | + expect(chunkCount).toBe(0); |
| 48 | + }); |
| 49 | + |
| 50 | + test('keychain is empty after restore (cleared on read)', async () => { |
| 51 | + await NativeCookieTestHelpers.seedTestCookies( |
| 52 | + SMALL_COUNT, |
| 53 | + SMALL_VALUE_SIZE |
| 54 | + ); |
| 55 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 56 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 57 | + await NativeCookieTestHelpers.restoreSessionCookies(); |
| 58 | + |
| 59 | + // A second restore should find nothing. |
| 60 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 61 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 62 | + |
| 63 | + expect(names.length).toBe(0); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + // --------------------------------------------------------------------------- |
| 68 | + // Large-blob (chunked keychain format, > 64 KB) |
| 69 | + // --------------------------------------------------------------------------- |
| 70 | + |
| 71 | + describe('large-blob round-trip (chunked format)', () => { |
| 72 | + test('persists and restores large cookies', async () => { |
| 73 | + await NativeCookieTestHelpers.seedTestCookies( |
| 74 | + LARGE_COUNT, |
| 75 | + LARGE_VALUE_SIZE |
| 76 | + ); |
| 77 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 78 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 79 | + |
| 80 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 81 | + |
| 82 | + expect(names.length).toBe(LARGE_COUNT); |
| 83 | + for (let i = 0; i < LARGE_COUNT; i++) { |
| 84 | + expect(names).toContain(`testCookie${i}`); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + test('chunked write creates a commit-marker with count > 1', async () => { |
| 89 | + await NativeCookieTestHelpers.seedTestCookies( |
| 90 | + LARGE_COUNT, |
| 91 | + LARGE_VALUE_SIZE |
| 92 | + ); |
| 93 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 94 | + |
| 95 | + const chunkCount = await NativeCookieTestHelpers.getKeychainChunkCount(); |
| 96 | + |
| 97 | + expect(chunkCount).toBeGreaterThan(1); |
| 98 | + }); |
| 99 | + |
| 100 | + test('commit-marker is removed after restore (chunked keys cleared on read)', async () => { |
| 101 | + await NativeCookieTestHelpers.seedTestCookies( |
| 102 | + LARGE_COUNT, |
| 103 | + LARGE_VALUE_SIZE |
| 104 | + ); |
| 105 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 106 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 107 | + await NativeCookieTestHelpers.restoreSessionCookies(); |
| 108 | + |
| 109 | + const chunkCount = await NativeCookieTestHelpers.getKeychainChunkCount(); |
| 110 | + |
| 111 | + expect(chunkCount).toBe(0); |
| 112 | + }); |
| 113 | + |
| 114 | + test('keychain is empty after restore (no second restore possible)', async () => { |
| 115 | + await NativeCookieTestHelpers.seedTestCookies( |
| 116 | + LARGE_COUNT, |
| 117 | + LARGE_VALUE_SIZE |
| 118 | + ); |
| 119 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 120 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 121 | + await NativeCookieTestHelpers.restoreSessionCookies(); |
| 122 | + |
| 123 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 124 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 125 | + |
| 126 | + expect(names.length).toBe(0); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + // --------------------------------------------------------------------------- |
| 131 | + // Format transitions |
| 132 | + // --------------------------------------------------------------------------- |
| 133 | + |
| 134 | + describe('format transitions', () => { |
| 135 | + test('overwriting large (chunked) with small (single-item) leaves no chunk marker', async () => { |
| 136 | + await NativeCookieTestHelpers.seedTestCookies( |
| 137 | + LARGE_COUNT, |
| 138 | + LARGE_VALUE_SIZE |
| 139 | + ); |
| 140 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 141 | + |
| 142 | + // Replace with a small set. |
| 143 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 144 | + await NativeCookieTestHelpers.seedTestCookies( |
| 145 | + SMALL_COUNT, |
| 146 | + SMALL_VALUE_SIZE |
| 147 | + ); |
| 148 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 149 | + |
| 150 | + const chunkCount = await NativeCookieTestHelpers.getKeychainChunkCount(); |
| 151 | + expect(chunkCount).toBe(0); |
| 152 | + |
| 153 | + // Data is still correct. |
| 154 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 155 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 156 | + expect(names.length).toBe(SMALL_COUNT); |
| 157 | + }); |
| 158 | + |
| 159 | + test('overwriting small (single-item) with large (chunked) round-trips correctly', async () => { |
| 160 | + await NativeCookieTestHelpers.seedTestCookies( |
| 161 | + SMALL_COUNT, |
| 162 | + SMALL_VALUE_SIZE |
| 163 | + ); |
| 164 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 165 | + |
| 166 | + // Replace with a large set. |
| 167 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 168 | + await NativeCookieTestHelpers.seedTestCookies( |
| 169 | + LARGE_COUNT, |
| 170 | + LARGE_VALUE_SIZE |
| 171 | + ); |
| 172 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 173 | + |
| 174 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 175 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 176 | + expect(names.length).toBe(LARGE_COUNT); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + // --------------------------------------------------------------------------- |
| 181 | + // clearAll |
| 182 | + // --------------------------------------------------------------------------- |
| 183 | + |
| 184 | + describe('clearAll', () => { |
| 185 | + test('removes cookies after a small-blob persist', async () => { |
| 186 | + await NativeCookieTestHelpers.seedTestCookies( |
| 187 | + SMALL_COUNT, |
| 188 | + SMALL_VALUE_SIZE |
| 189 | + ); |
| 190 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 191 | + await NativeCookie.clearAll(); |
| 192 | + |
| 193 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 194 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 195 | + expect(names.length).toBe(0); |
| 196 | + }); |
| 197 | + |
| 198 | + test('removes cookies and chunk marker after a large-blob persist', async () => { |
| 199 | + await NativeCookieTestHelpers.seedTestCookies( |
| 200 | + LARGE_COUNT, |
| 201 | + LARGE_VALUE_SIZE |
| 202 | + ); |
| 203 | + await NativeCookieTestHelpers.persistSessionCookies(); |
| 204 | + await NativeCookie.clearAll(); |
| 205 | + |
| 206 | + const chunkCount = await NativeCookieTestHelpers.getKeychainChunkCount(); |
| 207 | + expect(chunkCount).toBe(0); |
| 208 | + |
| 209 | + await NativeCookieTestHelpers.clearHTTPCookies(); |
| 210 | + const names = await NativeCookieTestHelpers.restoreSessionCookies(); |
| 211 | + expect(names.length).toBe(0); |
| 212 | + }); |
| 213 | + |
| 214 | + test('does not throw when called on an already-empty store', async () => { |
| 215 | + await expect(NativeCookie.clearAll()).resolves.not.toThrow(); |
| 216 | + await expect(NativeCookie.clearAll()).resolves.not.toThrow(); |
| 217 | + }); |
| 218 | + }); |
| 219 | +}); |
0 commit comments