Skip to content

Commit f2c3714

Browse files
committed
test: widen payload buffer regression coverage
1 parent 65a504a commit f2c3714

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

test/unit.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5910,6 +5910,112 @@ bool do_test_alter(int nclients, int alter_version, bool print_result, bool clea
59105910

59115911
// MARK: -
59125912

5913+
bool do_test_payload_buffer (size_t blob_size) {
5914+
const char *table_name = "payload_buffer_test";
5915+
sqlite3 *db = NULL;
5916+
sqlite3_stmt *stmt = NULL;
5917+
unsigned char *blob = NULL;
5918+
char *errmsg = NULL;
5919+
bool success = false;
5920+
int rc = sqlite3_open(":memory:", &db);
5921+
if (rc != SQLITE_OK) goto cleanup;
5922+
5923+
rc = sqlite3_cloudsync_init(db, NULL, NULL);
5924+
if (rc != SQLITE_OK) goto cleanup;
5925+
5926+
rc = sqlite3_exec(db, "SELECT cloudsync_version();", NULL, NULL, &errmsg);
5927+
if (rc != SQLITE_OK) goto cleanup;
5928+
if (errmsg) { sqlite3_free(errmsg); errmsg = NULL; }
5929+
5930+
char *sql = sqlite3_mprintf("CREATE TABLE IF NOT EXISTS \"%w\" ("
5931+
"id TEXT PRIMARY KEY NOT NULL, "
5932+
"value BLOB, "
5933+
"created_at TEXT DEFAULT CURRENT_TIMESTAMP"
5934+
");", table_name);
5935+
if (!sql) {
5936+
rc = SQLITE_NOMEM;
5937+
goto cleanup;
5938+
}
5939+
rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
5940+
sqlite3_free(sql);
5941+
if (rc != SQLITE_OK) goto cleanup;
5942+
if (errmsg) { sqlite3_free(errmsg); errmsg = NULL; }
5943+
5944+
sql = sqlite3_mprintf("SELECT cloudsync_init('%q');", table_name);
5945+
if (!sql) {
5946+
rc = SQLITE_NOMEM;
5947+
goto cleanup;
5948+
}
5949+
rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
5950+
sqlite3_free(sql);
5951+
if (rc != SQLITE_OK) goto cleanup;
5952+
if (errmsg) { sqlite3_free(errmsg); errmsg = NULL; }
5953+
5954+
sql = sqlite3_mprintf("INSERT INTO \"%w\" (id, value) VALUES (?, ?);", table_name);
5955+
if (!sql) {
5956+
rc = SQLITE_NOMEM;
5957+
goto cleanup;
5958+
}
5959+
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
5960+
sqlite3_free(sql);
5961+
if (rc != SQLITE_OK) goto cleanup;
5962+
5963+
char dummy_id[UUID_STR_MAXLEN];
5964+
cloudsync_uuid_v7_string(dummy_id, true);
5965+
5966+
blob = sqlite3_malloc64(blob_size);
5967+
if (!blob) {
5968+
rc = SQLITE_NOMEM;
5969+
goto cleanup;
5970+
}
5971+
for (size_t i = 0; i < blob_size; ++i) {
5972+
blob[i] = (unsigned char)(i % 256);
5973+
}
5974+
5975+
rc = sqlite3_bind_text(stmt, 1, dummy_id, -1, SQLITE_TRANSIENT);
5976+
if (rc != SQLITE_OK) goto cleanup;
5977+
rc = sqlite3_bind_blob(stmt, 2, blob, (int)blob_size, SQLITE_TRANSIENT);
5978+
if (rc != SQLITE_OK) goto cleanup;
5979+
5980+
rc = sqlite3_step(stmt);
5981+
if (rc != SQLITE_DONE) goto cleanup;
5982+
rc = sqlite3_finalize(stmt);
5983+
stmt = NULL;
5984+
if (rc != SQLITE_OK) goto cleanup;
5985+
5986+
sqlite3_free(blob);
5987+
blob = NULL;
5988+
5989+
const char *payload_sql = "SELECT length(cloudsync_payload_encode(tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)) "
5990+
"FROM cloudsync_changes;";
5991+
rc = sqlite3_prepare_v2(db, payload_sql, -1, &stmt, NULL);
5992+
if (rc != SQLITE_OK) goto cleanup;
5993+
5994+
int row_count = 0;
5995+
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
5996+
(void)sqlite3_column_int64(stmt, 0);
5997+
row_count++;
5998+
}
5999+
if (rc != SQLITE_DONE || row_count == 0) goto cleanup;
6000+
6001+
success = true;
6002+
6003+
cleanup:
6004+
if (stmt) {
6005+
sqlite3_finalize(stmt);
6006+
}
6007+
if (blob) {
6008+
sqlite3_free(blob);
6009+
}
6010+
if (errmsg) {
6011+
fprintf(stderr, "do_test_android_initial_payload error: %s\n", errmsg);
6012+
sqlite3_free(errmsg);
6013+
}
6014+
if (db) db = close_db(db);
6015+
6016+
return success;
6017+
}
6018+
59136019
int test_report(const char *description, bool result){
59146020
printf("%-30s %s\n", description, (result) ? "OK" : "FAILED");
59156021
return result ? 0 : 1;
@@ -5954,6 +6060,10 @@ int main(int argc, const char * argv[]) {
59546060
result += test_report("Functions Test (Int):", do_test_internal_functions());
59556061
result += test_report("String Func Test:", do_test_string_replace_prefix());
59566062
result += test_report("Test Many Columns:", do_test_many_columns(600, db));
6063+
result += test_report("Payload Buffer Test (500KB):", do_test_payload_buffer(500 * 1024));
6064+
result += test_report("Payload Buffer Test (600KB):", do_test_payload_buffer(600 * 1024));
6065+
result += test_report("Payload Buffer Test (1MB):", do_test_payload_buffer(1024 * 1024));
6066+
result += test_report("Payload Buffer Test (10MB):", do_test_payload_buffer(10 * 1024 * 1024));
59576067

59586068
// close local database
59596069
db = close_db(db);

0 commit comments

Comments
 (0)