./test/test-cm.c
Patch: Prevent frox hanging on a race condition in SSL code
+- /* Tests of the cache manager. Build is horrible - compile frox first, and
+- * then run make-tests. The empty function defs below are so we can link
+- * with misc.o, but nothing else. */
+-
+- #define TEST_SIZE 1000
+-
+- int config_loport(socketuse use) { return 0; };
+- int config_hiport(socketuse use) {return 0;};
+- int il_free(void) {return 0;};
+- void send_cmessage(int i, const char* s){};
+- struct options config;
+-
+- void check_integrity(void)
+- {
+- struct cache_entry *p;
+- time_t sla;
+- time(&sla);
+-
+- for(p=head; p; p=p->next) {
+- if(! p->prev && p!=head) {
+- printf("no p->prev in middle of list\n");
+- exit(-1);
+- }
+- if(!p->next && p!=tail) {
+- printf("no p->next in middle of list\n");
+- exit(-1);
+- }
+- if(p->prev && p->prev->next!=p) {
+- printf("ppn!=p\n");
+- exit(-1);
+- }
+- if(p->next && p->next->prev!=p) {
+- printf("pnp!=p\n");
+- exit(-1);
+- }
+- if(*hash_loc(p->uri)!=p) {
+- printf("p doesn't appear in the hash table\n");
+- exit(-1);
+- }
+- if(p->last_access > sla) {
+- printf("ll not in lru order\n");
+- exit(-1);
+- }
+- sla=p->last_access;
+- }
+- write(1, ".", 1);
+- }
+-
+- void docache(int i)
+- {
+- int fd;
+- char buf[256];
+- static sstr *s=NULL;
+- if(!s) s=sstr_init(0);
+-
+- sstr_empty(s);
+- sstr_apprintf(s, "G ftp://frox/frox.%d 20020220 7 0 0\n", i);
+- switch(uri_request(s, &fd)){
+- case CACHE_MISS:
+- sprintf(buf, "%.6d\n", i);
+- write(fd, buf, 7);
+- close(fd);
+- break;
+- case CACHE_HIT:
+- read(fd, buf, 7);
+- write(1, buf, 7);
+- close(fd);
+- break;
+- default:
+- printf("Cache returned oddly\n");
+- }
+- check_integrity();
+- }
+-
+- void check_hashdist(void)
+- {
+- int i, j;
+- struct cache_entry *p;
+-
+- for(i=0;i<table_size;i++){
+- for(p=hash_table[i],j=0;p;p=p->collision) j++;
+- printf("%d, ", j);
+- }
+- printf("\n");
+- }
+-
+- int main()
+- {
+- int i, j;
+-
+- init_dir("/tmp");
+- cachedir=sstr_dup2("/tmp/cache/");
+- cachesize = TEST_SIZE * 5; /*Just too small to hold everything*/
+-
+- docache(TEST_SIZE + 1);
+- docache(TEST_SIZE + 2);
+- docache(TEST_SIZE + 3);
+- docache(TEST_SIZE + 4);
+- docache(TEST_SIZE + 5);
+- check_integrity();
+-
+- /*Remove a middle item*/
+- purge_entry(head->next->next); check_integrity();
+-
+- /*A nearly middle one*/
+- purge_entry(head->next); check_integrity();
+-
+- /*A head*/
+- purge_entry(head); check_integrity();
+-
+- /*A tail*/
+- purge_entry(tail); check_integrity();
+-
+- /*The last entry*/
+- purge_entry(head); check_integrity();
+-
+- /*Make a big list*/
+- for(j=0; j<TEST_SIZE; j++){
+- docache(j);
+- printf("%d/%d\n", total_size, cachesize);
+- }
+- /*Retrieve files at random. This also tests LRUing*/
+- for(j=0; j<1000; j++){
+- i = rand() % TEST_SIZE;
+- docache(i);
+- }
+- check_hashdist();
+- return 0;
+- }
+-