secd-154-engine-backoff.c [plain text]
#include <utilities/SecCFWrappers.h>
#include "secd_regressions.h"
#include "SOSTestDataSource.h"
#include "SOSRegressionUtilities.h"
static int kTestTestCount = 10;
static int MAX_PENALTY_TIME = 32;
struct monitor_traffic {
CFStringRef key;
int penalty_box;
int total_consecutive_attempts;
int last_write_timestamp;
};
static void clear_penalty(struct monitor_traffic *monitor){
monitor->penalty_box = 0;
monitor->total_consecutive_attempts = 0;
monitor->last_write_timestamp = 0;
}
static int increase_penalty(int time){
if(time == 32)
return time;
else if(time == 0)
return 1;
else
return (time * 2);
}
static int decrease_penalty(int time){
if(time == 0)
return time;
else if(time == 1)
return 0;
else
return (time/2);
}
static void calculate_new_penalty(struct monitor_traffic *monitor, int current_time){
int difference = current_time - monitor->last_write_timestamp;
while(difference){
if(difference >= monitor->penalty_box * 2){
monitor->penalty_box = decrease_penalty(monitor->penalty_box);
difference =- monitor->penalty_box *2;
}
else
break;
}
}
static void keychain_changed_notification(struct monitor_traffic *monitor){
clear_penalty(monitor);
}
static void initialize_monitor(struct monitor_traffic *monitor){
monitor->key = CFSTR("ak|alskdfj:a;lskdjf");
monitor->penalty_box = 0;
monitor->total_consecutive_attempts = 0;
monitor->last_write_timestamp = 0;
}
static int backoff_algorithm(CFArrayRef timestamps, struct monitor_traffic *monitor)
{
__block int successful_writes = 0;
CFNumberRef timestamp;
CFArrayForEachC(timestamps, timestamp) {
int current_time;
if(!CFNumberGetValue(timestamp, kCFNumberSInt32Type, ¤t_time))
return successful_writes;
if(monitor->last_write_timestamp == 0){ successful_writes++;
}
else if(current_time == 0){ keychain_changed_notification(monitor);
}
else{
if(monitor->last_write_timestamp == (current_time -1) && monitor->total_consecutive_attempts >= 4){
monitor->penalty_box= increase_penalty(monitor->penalty_box);
monitor->total_consecutive_attempts++;
}
else if(monitor->last_write_timestamp == (current_time -1) && monitor->total_consecutive_attempts < 4 ){
monitor->total_consecutive_attempts++;
if(monitor->penalty_box == 0)
successful_writes++;
}
else if((current_time - monitor->last_write_timestamp) >= (2*monitor->penalty_box)){ monitor->total_consecutive_attempts = 0;
calculate_new_penalty(monitor, current_time);
successful_writes++;
}
else if((current_time - monitor->last_write_timestamp) <= (2*monitor->penalty_box)){ monitor->penalty_box= increase_penalty(monitor->penalty_box);
if(monitor->last_write_timestamp != (current_time-1))
monitor->total_consecutive_attempts = 0;
else
monitor->total_consecutive_attempts++;
}
}
if(current_time != 0)
monitor->last_write_timestamp = current_time;
}
return successful_writes;
}
static void tests(void)
{
struct monitor_traffic *monitor = (struct monitor_traffic*)malloc(sizeof(struct monitor_traffic));
initialize_monitor(monitor);
CFMutableArrayRef write_attempts = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
CFNumberRef timestamp = NULL;
int time;
for(int i = 1; i< 13; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
int successful_writes = backoff_algorithm(write_attempts, monitor);
ok(successful_writes == 5, "successfull writes should have only reached 5 minutes");
ok(monitor->penalty_box == MAX_PENALTY_TIME, "penalty box should have maxed out to 32 minutes");
initialize_monitor(monitor);
CFArrayRemoveAllValues(write_attempts);
for(int i = 1; i< 13; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
time = 77;
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
time = 109;
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
successful_writes = backoff_algorithm(write_attempts, monitor);
ok(successful_writes == 7, "successfull writes should have only reached 6"); ok(monitor->penalty_box == (MAX_PENALTY_TIME/4), "penalty box should have maxed out to 16 minutes");
initialize_monitor(monitor);
CFArrayRemoveAllValues(write_attempts);
for(int i = 1; i< 13; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
time = 76; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
time = 108; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
time = 124; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
time = 132; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
time = 136; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
time = 138; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
successful_writes = backoff_algorithm(write_attempts, monitor);
ok(successful_writes == 11, "successfull writes should have only reached 11");
ok(monitor->penalty_box == 0, "penalty box should reset back to 0");
initialize_monitor(monitor);
CFArrayRemoveAllValues(write_attempts);
for(int i = 1; i< 13; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
time = 0; timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
for(int i = 1; i< 13; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
successful_writes = backoff_algorithm(write_attempts, monitor);
ok(successful_writes == 10, "successfull writes should have only reached 10");
ok(monitor->penalty_box == MAX_PENALTY_TIME, "penalty box should reset back to 0");
initialize_monitor(monitor);
CFArrayRemoveAllValues(write_attempts);
for(int i = 1; i< 6; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
time = 40;
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
for(int i = 100; i< 106; i++){
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i);
CFArrayAppendValue(write_attempts, timestamp);
CFReleaseNull(timestamp);
}
time = 250;
timestamp = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &time);
CFArrayAppendValue(write_attempts, timestamp);
successful_writes = backoff_algorithm(write_attempts, monitor);
ok(successful_writes == 12, "successfull writes should have only reached 10");
ok(monitor->penalty_box == 0, "penalty box should reset back to 0");
}
int secd_154_engine_backoff(int argc, char *const *argv)
{
plan_tests(kTestTestCount);
tests();
return 0;
}