void dbrew_config_reset(Rewriter* r) { if (r->cc) cc_free(r->cc); r->cc = cc_new(); }
static CC *ed25519FromJSON(const cJSON *params, char *err) { size_t binsz; cJSON *pk_item = cJSON_GetObjectItem(params, "publicKey"); if (!cJSON_IsString(pk_item)) { strcpy(err, "publicKey must be a string"); return NULL; } unsigned char *pk = base64_decode(pk_item->valuestring, &binsz); if (32 != binsz) { strcpy(err, "publicKey has incorrect length"); free(pk); return NULL; } cJSON *signature_item = cJSON_GetObjectItem(params, "signature"); unsigned char *sig = NULL; if (signature_item && !cJSON_IsNull(signature_item)) { if (!cJSON_IsString(signature_item)) { strcpy(err, "signature must be null or a string"); return NULL; } sig = base64_decode(signature_item->valuestring, &binsz); if (64 != binsz) { strcpy(err, "signature has incorrect length"); free(sig); return NULL; } } CC *cond = cc_new(CC_Ed25519); cond->publicKey = pk; cond->signature = sig; return cond; }
cc_hook_t *cc_new_hook() { cc_hook_t *hook = cc_new(hook); hook->func = NULL; hook->next = NULL; return hook; }
static CC *ed25519FromFulfillment(const Fulfillment_t *ffill) { CC *cond = cc_new(CC_Ed25519); cond->publicKey = calloc(1,32); memcpy(cond->publicKey, ffill->choice.ed25519Sha256.publicKey.buf, 32); cond->signature = calloc(1,64); memcpy(cond->signature, ffill->choice.ed25519Sha256.signature.buf, 64); return cond; }
// allocate a configuration for the rewriter if not already existing static CaptureConfig* cc_get(Rewriter* r) { if (r->cc == 0) r->cc = cc_new(); return r->cc; }
cc_block_t *cc_new_block() { cc_block_t *block = cc_new(block); memset(block, 0, sizeof(cc_block_t)); for (int i = 0;i < 6;i++) { block->color[i][0] = 1.0; block->color[i][1] = 1.0; block->color[i][2] = 1.0; } return block; }
static CC *prefixFromFulfillment(const Fulfillment_t *ffill) { PrefixFulfillment_t *p = ffill->choice.prefixSha256; CC *sub = fulfillmentToCC(p->subfulfillment); if (!sub) return 0; CC *cond = cc_new(CC_Prefix); cond->maxMessageLength = p->maxMessageLength; cond->prefix = calloc(1, p->prefix.size); memcpy(cond->prefix, p->prefix.buf, p->prefix.size); cond->prefixLength = p->prefix.size; cond->subcondition = sub; return cond; }
static CC *prefixFromJSON(const cJSON *params, char *err) { cJSON *mml_item = cJSON_GetObjectItem(params, "maxMessageLength"); if (!cJSON_IsNumber(mml_item)) { strcpy(err, "maxMessageLength must be a number"); return NULL; } cJSON *subcond_item = cJSON_GetObjectItem(params, "subfulfillment"); CC *sub = cc_conditionFromJSON(subcond_item, err); if (!sub) { return NULL; } CC *cond = cc_new(CC_Prefix); cond->maxMessageLength = (unsigned long) mml_item->valuedouble; cond->subcondition = sub; if (!jsonGetBase64(params, "prefix", err, &cond->prefix, &cond->prefixLength)) { cc_free(cond); return NULL; } return cond; }
int main(void) { /* initialize a memory-only database */ fail_unless(db_init(NULL, NULL) == 0); /* initialize a share */ global_share = share_new("/tmp"); fail_unless(global_share); char *cwd = g_get_current_dir(); fail_unless(share_add(global_share, cwd, TRUE) == 0); free(cwd); hub_t *hub = hub_new(); fail_unless(hub); hub->me = user_new("nick", NULL, NULL, NULL, NULL, 0ULL, hub); fail_unless(hub->me); cc_t *cc = cc_new(-1, hub); fail_unless(cc); fail_unless(cc->state == CC_STATE_MYNICK); /* login a test user on the hub */ user_t *test_user = user_new("foo", NULL, NULL, NULL, NULL, 0ULL, hub); g_hash_table_insert(hub->users, strdup(test_user->nick), test_user); /* fake remote nick of the client connection */ cc->nick = strdup("foo"); /* requesting a non-existent file should fail */ int rc = cc_upload_prepare(cc, "non-existent-file", 0, 0); fail_unless(rc == -1); /* a user with same nick as me requesting a file should fail */ free(cc->nick); cc->nick = strdup("nick"); rc = cc_upload_prepare(cc, "sphubd\\sphubd.c", 0, 0); fail_unless(rc == -1); cc->nick = strdup("foo"); /* cc_upload_prepare should set cc->offset correctly after called */ cc->offset = 4711ULL; rc = cc_upload_prepare(cc, "sphubd\\sphubd.c", 0, 0); fail_unless(rc == 0); fail_unless(cc->offset == 0ULL); fail_unless(cc->bytes_to_transfer == cc->filesize); guint64 ofs = 17; rc = cc_upload_prepare(cc, "sphubd\\sphubd.c", ofs, 0); fail_unless(rc == 0); fail_unless(cc->offset == ofs); fail_unless(cc->bytes_to_transfer == cc->filesize - ofs); /* send commands to a file instead of to a hub */ cc->fd = open("/tmp/client_test.log", O_RDWR|O_CREAT); fail_unless(cc->fd != -1); /* nothing in the download queue yet */ fail_unless(cc_request_download(cc) == -1); /* ok, so add a file to the download queue */ rc = queue_add("bar", "share\\bar-file.zip", 4711ULL, "/tmp/client_test_file.zip", "ABCDEFGHIJKLMNOPQRSTUVWXYZ012"); fail_unless(rc == 0); /* wrong nick in the download queue */ fail_unless(cc_request_download(cc) == -1); /* ok, so add two files to the download queue with correct nick */ rc = queue_add("foo", "share\\foo-file2.zip", 17ULL, "/tmp/client_test_file2.zip", "ABCDEFGHIJKLMNOPQRSTUVWXYZ234"); fail_unless(rc == 0); rc = queue_add("foo", "share\\foo-file.zip", 4711ULL, "/tmp/client_test_file.zip", "ABCDEFGHIJKLMNOPQRSTUVWXYZ012"); fail_unless(rc == 0); fail_unless(cc_request_download(cc) == 0); fail_unless(cc->state == CC_STATE_REQUEST); /* should download foo-file.zip first, because they're sorted by filename */ fail_unless(strcmp(cc->current_queue->target_filename, "/tmp/client_test_file.zip") == 0); cc->has_adcget = TRUE; cc->has_tthf = TRUE; cc_start_download(cc); fail_unless(cc->state == CC_STATE_BUSY); cc_finish_download(cc); /* cc_finish_download should request another file directly if there is one * in the download queue */ fail_unless(cc->state == CC_STATE_REQUEST); fail_unless(strcmp(cc->current_queue->target_filename, "/tmp/client_test_file2.zip") == 0); cc_start_download(cc); fail_unless(cc->state == CC_STATE_BUSY); cc_finish_download(cc); fail_unless(cc->state == CC_STATE_READY); /* nothing more in the download queue */ fail_unless(cc_request_download(cc) == -1); /* zero-sized files are not downloaded (except filelists) */ rc = queue_add("foo", "share\\file3.zip", 0ULL, "/tmp/client_test_file3.zip", "ABCDEFGHIJKLMNOPQRSTUVWXYZ345"); fail_unless(rc == 0); fail_unless(cc_request_download(cc) == -1); /* download a filelist */ cc->has_xmlbzlist = true; rc = queue_add_filelist("foo", false); fail_unless(rc == 0); fail_unless(cc_request_download(cc) == 0); fail_unless(strcmp(cc->current_queue->target_filename, "/tmp/files.xml.foo.bz2") == 0); cc_start_download(cc); fail_unless(cc->state == CC_STATE_BUSY); cc_finish_download(cc); fail_unless(cc->state == CC_STATE_READY); return 0; }