static void test_attrib_rwstring(CuTest *tc) { gamedata data; storage store; attrib a = { 0 }; test_setup(); a.data.v = _strdup("Hello World"); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writestring(&a, NULL, &store); a_finalizestring(&a); data.strm.api->rewind(data.strm.handle); a_readstring(&a, NULL, &data); CuAssertStrEquals(tc, "Hello World", (const char *)a.data.v); mstream_done(&data.strm); gamedata_done(&data); test_cleanup(); }
static void test_attrib_rwshorts(CuTest *tc) { gamedata data; storage store; attrib a = { 0 }; a.data.sa[0] = -4; a.data.sa[1] = 42; test_setup(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writeint(&a, NULL, &store); memset(a.data.ca, 0, 4); data.strm.api->rewind(data.strm.handle); a_readint(&a, NULL, &data); CuAssertIntEquals(tc, -4, a.data.sa[0]); CuAssertIntEquals(tc, 42, a.data.sa[1]); mstream_done(&data.strm); gamedata_done(&data); test_cleanup(); }
static void test_cr_unit(CuTest *tc) { stream strm; char line[1024]; faction *f; region *r; unit *u; test_setup(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); u = test_create_unit(f, r); renumber_unit(u, 1234); mstream_init(&strm); cr_output_unit(&strm, f, u, seen_unit); strm.api->rewind(strm.handle); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, line, "EINHEIT 1234"); mstream_done(&strm); test_teardown(); }
static void test_memstream(CuTest *tc) { storage store; stream out = { 0 }; char buf[1024]; int val=0; mstream_init(&out); binstore_init(&store, &out); store.handle.data = &out; WRITE_INT(&store, 999999); WRITE_TOK(&store, "fortytwo"); WRITE_INT(&store, 42); out.api->rewind(out.handle); READ_INT(&store, &val); READ_TOK(&store, buf, 1024); CuAssertIntEquals(tc, 999999, val); CuAssertStrEquals(tc, "fortytwo", buf); READ_INT(&store, &val); CuAssertIntEquals(tc, 42, val); mstream_done(&out); }
static void test_cr_resources(CuTest *tc) { stream strm; char line[1024]; faction *f; region *r; unit *u; setup_resources(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); u = test_create_unit(f, r); set_level(u, SK_QUARRYING, 1); r->land->horses = 1; r->land->peasants = 200; r->land->money = 300; rsettrees(r, 0, 1); rsettrees(r, 1, 2); rsettrees(r, 2, 3); region_setresource(r, get_resourcetype(R_STONE), 1); mstream_init(&strm); cr_output_resources(&strm, f, r, true); strm.api->rewind(strm.handle); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "3;Baeume", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "2;Schoesslinge", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;Steine", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Schoesslinge\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "2;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Blumen\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "3;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Silber\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "300;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Bauern\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "200;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Pferde\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Steine\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;skill", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;number", line); mstream_done(&strm); test_teardown(); }
static void test_cr_factionstealth(CuTest *tc) { stream strm; faction *f1, *f2; region *r; unit *u; test_setup(); f1 = test_create_faction(NULL); f2 = test_create_faction(NULL); r = test_create_region(0, 0, NULL); u = test_create_unit(f1, r); /* report to ourselves */ mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... also when we are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); /* we see that our unit is cloaked */ set_factionstealth(u, f2); CuAssertPtrNotNull(tc, u->attribs); mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... also when we are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); /* we can tell that someone is presenting as us */ mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... but not if they are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); /* we see the same thing as them when we are an ally */ ally_set(&f1->allies, f2, HELP_FSTEALTH); mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... also when they are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); test_teardown(); }
BGPDUMP_ENTRY* bgpdump_read_next(BGPDUMP *dump) { BGPDUMP_ENTRY *this_entry=NULL; struct mstream s; u_char *buffer; int ok=0; u_int32_t bytes_read; this_entry = malloc(sizeof(BGPDUMP_ENTRY)); bytes_read = cfr_read_n(dump->f, &(this_entry->time), 4); bytes_read += cfr_read_n(dump->f, &(this_entry->type), 2); bytes_read += cfr_read_n(dump->f, &(this_entry->subtype), 2); bytes_read += cfr_read_n(dump->f, &(this_entry->length), 4); if(bytes_read != 12) { if(bytes_read > 0) { /* Malformed record */ dump->parsed++; syslog(LOG_ERR, "bgpdump_read_next: incomplete MRT header (%d bytes read, expecting 12)", bytes_read); } /* Nothing more to read, quit */ free(this_entry); dump->eof=1; return(NULL); } dump->parsed++; /* Intel byte ordering stuff ... */ this_entry->type=ntohs(this_entry->type); this_entry->subtype=ntohs(this_entry->subtype); this_entry->time=ntohl(this_entry->time); this_entry->length=ntohl(this_entry->length); this_entry->attr=NULL; buffer = malloc(this_entry->length); bytes_read = cfr_read_n(dump->f, buffer, this_entry->length); if(bytes_read != this_entry->length) { syslog(LOG_ERR, "bgpdump_read_next: incomplete dump record (%d bytes read, expecting %d)", bytes_read, this_entry->length); free(this_entry); free(buffer); dump->eof=1; return(NULL); } ok=0; mstream_init(&s,buffer,this_entry->length); switch(this_entry->type) { case BGPDUMP_TYPE_MRTD_BGP: break; case BGPDUMP_TYPE_MRTD_TABLE_DUMP: ok = process_mrtd_table_dump(&s,this_entry); break; case BGPDUMP_TYPE_ZEBRA_BGP: ok = process_zebra_bgp(&s,this_entry); break; case BGPDUMP_TYPE_TABLE_DUMP_V2: ok = process_mrtd_table_dump_v2(&s,this_entry); break; } free(buffer); if(ok) { dump->parsed_ok++; } else { bgpdump_free_mem(this_entry); return NULL; } return this_entry; }