test_policy_cache.c revision 2e840960b0fa8189b1271ae984b33c031ae69530
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * The contents of this file are subject to the terms of the Common Development and
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * Distribution License (the License). You may not use this file except in compliance with the
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * specific language governing permission and limitations under the License.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * When distributing Covered Software, include this CDDL Header Notice in each file and include
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * Header, with the fields enclosed by brackets [] replaced by your own identifying
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * information: "Portions copyright [year] [name of copyright owner]".
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * Copyright 2014 - 2015 ForgeRock AS.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordvoid* am_parse_policy_xml(unsigned long instance_id, const char* xml, size_t xml_sz, int scope);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordchar* policy_xml = "<PolicyService version='1.0' revisionNumber='60'>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "<PolicyResponse requestId='4' issueInstant='1424783306343' >"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <ResourceResult name='http://vb2.local.com:80/testwebsite'>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <PolicyDecision>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <ResponseAttributes> <!-- these can have multiple (0..n) value elements -->"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Attributes,key:0,0'/> <Value>Attributes,value:0,0,0</Value> <Value>Attributes,value:0,0,1</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Attributes,key:0,1'/> <Value>Attributes,value:0,1,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </ResponseAttributes>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <ActionDecision timeToLive='1234'> <!-- these can have no value elements, which defaults to ? -->"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='PUT'/> <Value>deny</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Advices>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,0'/> <Value>Advices,value:0,0,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,1'/> <Value>Advices,value:0,1,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </Advices>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </ActionDecision>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <ActionDecision timeToLive='5678'>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='GET'/> <Value>allow</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Advices>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,0'/> <Value>Advices,value:0,0,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,1'/> <Value>Advices,value:0,1,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </Advices>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </ActionDecision>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <ActionDecision timeToLive='9012'>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='POST'/> <Value>allow</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Advices>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,0'/> <Value>Advices,value:0,0,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,1'/> <Value>Advices,value:0,1,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Advices,key:0,2'/> <Value>Advices,value:0,2,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </Advices>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </ActionDecision>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <ResponseDecisions>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Decision,key:0,0'/> <Value>Decision,value:0,0,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Decision,key:0,1'/> <Value>Decision,value:0,1,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Attribute name='Decision,key:0,2'/> <Value>Decision,value:0,2,0</Value>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </AttributeValuePair>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </ResponseDecisions>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </PolicyDecision>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " </ResourceResult>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "</PolicyResponse>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "</PolicyService>";
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford "<PolicyService version=\"1.0\" revisionNumber=\"60\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <PolicyResponse requestId=\"4\" issueInstant=\"9999999999999\" >"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ResourceResult name=\"%s\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <PolicyDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ResponseAttributes>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ResponseAttributes>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"POST\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"PATCH\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"GET\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"DELETE\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"OPTIONS\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"HEAD\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <ActionDecision timeToLive=\"9999999999999999999\">"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Attribute name=\"PUT\"/>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Value>allow</Value>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </AttributeValuePair>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " <Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </Advices>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ActionDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </PolicyDecision>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </ResourceResult>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford " </PolicyResponse>"
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford "</PolicyService>";
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordchar* pll = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "<ResponseSet vers='1.0' svcid='poicy' reqid='48'>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford " <Response><![CDATA[%s]]></Response>"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "</ResponseSet>";
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford * Substitute our chosen URL into the "policy for url" string above. That way we can pretend
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford * we're getting different responses for different URLs. All the values are "allow" anyway.
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamfordstatic char* get_policy_for_url(const char* url) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordstatic void test_namevalue_pair(const char* prefix, struct am_namevalue* nvp)
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford int value_policy_seq, value_attr_seq, value_value_seq;
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs am_asprintf(&key_format, "%s%s", prefix, ",key:%d,%d");
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs am_asprintf(&value_format, "%s%s", prefix, ",value:%d,%d,%d");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(sscanf(nvp->n, key_format, &key_policy_seq, &key_attr_seq), 2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(sscanf(nvp->v, value_format, &value_policy_seq, &value_attr_seq, &value_value_seq), 3);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(key_policy_seq, value_policy_seq);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(key_attr_seq, value_attr_seq);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordstatic int test_attributes(const char * prefix, struct am_namevalue * head)
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordstatic void test_policy_structure(struct am_policy_result * result)
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs struct am_action_decision* ad = r != NULL ? r->action_decisions : NULL;
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(r->resource, "http://vb2.local.com:80/testwebsite");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(test_attributes("Attributes", r->response_attributes), 3);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(test_attributes("Decision", r->response_decisions), 3);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(am_method_num_to_str(ad->method), "PUT");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(ad->action ? "allow" : "deny", "deny");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(test_attributes("Advices", ad->advices), 2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(am_method_num_to_str(ad->method), "GET");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(ad->action ? "allow" : "deny", "allow");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(test_attributes("Advices", ad->advices), 2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(am_method_num_to_str(ad->method), "POST");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(ad->action ? "allow" : "deny", "allow");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(test_attributes("Advices", ad->advices), 3);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford/***************************************************************************************************/
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford result = am_parse_policy_xml(0l, buffer, size, 0);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs result = am_parse_policy_xml(0l, buffer, strlen(buffer), 0);
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford // destroy the cache, if it exists
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs am_add_session_policy_cache_entry(&request, "Policy-key", result, NULL);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford am_get_session_policy_cache_entry(&request, "Policy-key", &r, &session, &ets);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordconst char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*";
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordvoid create_random_cache_key(char * buffer, size_t size)
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for (i = 0; i < count; i++) {
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas Jamesstatic void test_cache_with_seed(int seed, int test_size, am_request_t * request, struct am_policy_result * result)
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* create initial entries */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for(i = 0; i < test_size; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs assert_int_equal(am_add_session_policy_cache_entry(request, key, result, NULL), AM_SUCCESS);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* should refresh the whole lot */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for(i = 0; i < test_size; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs assert_int_equal(am_add_session_policy_cache_entry(request, key, result, NULL), AM_SUCCESS);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* read them all back */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for(i = 0; i < test_size; i++) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(am_get_session_policy_cache_entry(request, key, &r, &session, &ets), AM_SUCCESS);
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas Jamesstatic void test_cache(int test_size, am_request_t * request, struct am_policy_result * result) {
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James test_cache_with_seed(543542, test_size, request, result);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordstatic void test_cache_keys(int test_size, char** keys, am_request_t* request, struct am_policy_result* result)
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* create initial entries */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for(i = 0; i < test_size; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs assert_int_equal(am_add_session_policy_cache_entry(request, keys [i], result, NULL), AM_SUCCESS);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* should refresh the whole lot */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for(i = 0; i < test_size; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs assert_int_equal(am_add_session_policy_cache_entry(request, keys [i], result, NULL), AM_SUCCESS);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* read them all back */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for(i = 0; i < test_size; i++) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(am_get_session_policy_cache_entry(request, keys [i], &r, &session, &ets), AM_SUCCESS);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordvoid test_policy_cache_many_entries(void **state) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs result = am_parse_policy_xml(0l, buffer, strlen(buffer), 0);
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford // destroy the cache, if it exists
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas Jamesvoid test_policy_cache_purge_many_entries(void **state) {
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James result = am_parse_policy_xml(0l, buffer, strlen(buffer), 0);
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James // destroy the cache, if it exists
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James assert_int_equal(am_purge_caches(time(NULL) + 10), test_size);
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas Jamesvoid test_policy_cache_purge_during_insert(void **state) {
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James result = am_parse_policy_xml(0l, buffer, strlen(buffer), 0);
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James // destroy the cache, if it exists
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James // wait 2 seconds for the TTL to expire
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James // another 10 is too many without the purge functionality
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James test_cache_with_seed(321213, 10, &request, result);
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James // check that there are only these 10 left
2e840960b0fa8189b1271ae984b33c031ae69530Nicholas James assert_int_equal(am_purge_caches(time(NULL) + 10), 10);
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford * Now vary the incoming URL a bit and check we can get the same values out.
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamfordvoid test_policy_cache_with_many_different_entries_single_session(void **state) {
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford "http://agent.a-example.com:8080/allowed.html",
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford "http://agent.b-example.com:8080/allowed.html?attr1=value1",
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford "http://agent.c-example.com:8080/also-allowed.html",
acd3d5e2fd1764e71799b27badbd349550ca9400Mareks Malnacs "http://agent.c-example.com:8080/also-allowed.html",
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford "http://agent.e-example.com:8080/allowed/index.html",
acd3d5e2fd1764e71799b27badbd349550ca9400Mareks Malnacs "http://agent.a-example.com:8080/allowed.html",
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford // destroy the cache, if it exists
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford create_random_cache_key(fake_session, sizeof(fake_session));
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford * Add the URLS above into the cache via the same session id
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford for (i = 0; i < sizeof(urls)/sizeof(urls[0]); i++) {
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford policy_result = am_parse_policy_xml(0l, buffer, strlen(buffer), 0);
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford assert_int_equal(am_add_session_policy_cache_entry(&request, fake_session, policy_result, NULL), AM_SUCCESS);
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford * Check we can retrieve the URLs above from the cache via the same session id
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford for (i = 0; i < sizeof(urls)/sizeof(urls[0]); i++) {
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford if (am_get_session_policy_cache_entry(&request, fake_session, &r, &session, &ets) == AM_SUCCESS) {
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford for (result = r; result != NULL; result = result->next) {
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford AM_LOG_ERROR(0, "Failed to match policy for URL %s, although results retrieved", urls[i]);
ef2c6005cfeddae287cd168584dd348ba8eba6adTony Bamford AM_LOG_ERROR(0, "Failed to retrieve policy for URL %s", urls[i]);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordstatic void* test_cache_procedure(void * params)
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford for (i = 0; i < p->iterations; i++) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford test_cache_keys(p->test_size, p->keys, p->request, p->result);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs /* this must be slightly less than the maximum because re-use of shm chunks might use more space */
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs result = am_parse_policy_xml(0l, buffer, strlen(buffer), 0);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs for (i = 0; i < TEST_SIZE; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs create_random_cache_key(key_buffer, sizeof (key_buffer));
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs assert_int_equal(am_cache_init(), AM_SUCCESS);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs fprintf(stdout, "info: started multithreaded cache tests.. ");
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs for (i = 0; i < NTHREADS; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs AM_THREAD_CREATE(threads[i], test_cache_procedure, ¶ms);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs for (i = 0; i < NTHREADS; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs dt = ((double) (clock() - t0)) / CLOCKS_PER_SEC;
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs fprintf(stdout, "finished after %lf secs\n", dt);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs assert_int_equal(am_cache_shutdown(), AM_SUCCESS);
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs for (i = 0; i < TEST_SIZE; i++) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * This is an internal test of the mechanism for replaying a given number of randomly generated strings
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs for(i = 0; i < TEST_SIZE_1; i++) {
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs for(i = 0; i < TEST_SIZE_1; i++) {