Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Mozgunova committed Dec 17, 2021
1 parent 07dca82 commit b448ca5
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 140 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/run_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Testing

on:
push:

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}

steps:
- uses: actions/checkout@v2

- name: Clone googletest repo
run: |
git clone https://github.com/google/googletest.git -b release-1.11.0
- name: Configure
run: |
cmake -S. -Bbuild
- name: Build
run: |
cmake --build build
- name: Test
run: |
./build/tests_hw3
- name: Make .exe file
run: |
gcc parse_server_logs.c -lm -o parse_server_logs.exe
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.12) # version can be different

project(parse_server_logs) #name of your project

add_subdirectory(googletest) # add googletest subdirectory

include_directories(googletest/include) # this is so we can #include <gtest/gtest.h>

add_executable(tests_hw3 tests.cpp) # add this executable

target_link_libraries(tests_hw3 PRIVATE gtest) # link google test to this executable
146 changes: 6 additions & 140 deletions process_logs.c
Original file line number Diff line number Diff line change
@@ -1,145 +1,11 @@
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

const int DATE_LENGTH = 21;
const int LOG_LENGTH = 300;
uint64_t count_5xx = 0;

time_t time_since_the_epoch(char str_time[]) {
int year, month, day, hour, min, sec;
day = strtol(str_time, 0, 10);

char smonth[4];
strncpy(smonth, str_time + 3, 3);

if (!strcmp(smonth, "Jan")) {month = 0;}
if (!strcmp(smonth, "Feb")) {month = 1;}
if (!strcmp(smonth, "Mar")) {month = 2;}
if (!strcmp(smonth, "Apr")) {month = 3;}
if (!strcmp(smonth, "May")) {month = 4;}
if (!strcmp(smonth, "Jun")) {month = 5;}
if (!strcmp(smonth, "Jul")) {month = 6;}
if (!strcmp(smonth, "Aug")) {month = 7;}
if (!strcmp(smonth, "Sep")) {month = 8;}
if (!strcmp(smonth, "Oct")) {month = 9;}
if (!strcmp(smonth, "Nov")) {month = 10;}
if (!strcmp(smonth, "Dec")) {month = 11;}

year = strtol(str_time + 7, 0, 10);
hour = strtol(str_time + 12, 0, 10);
min = strtol(str_time + 15, 0, 10);
sec = strtol(str_time + 18, 0, 10);

struct tm breakdown;
breakdown.tm_year = year - 1900;
breakdown.tm_mon = month;
breakdown.tm_mday = day;
breakdown.tm_hour = hour;
breakdown.tm_min = min;
breakdown.tm_sec = sec;

time_t res = mktime(&breakdown);

return res;
}

void getline(FILE * file, char line[], char date_str[], uint8_t is_end_file) {
char c = '\0';

if ((c = fgetc(file)) == EOF) {
line[0] = '\0';
return;
}

line[0] = c;
int i = 1;
int j = 0;
uint8_t is_date = 0;
uint8_t is_5xx = 0;

while ((c = fgetc(file)) != '\n' && c != EOF) {
line[i] = c;
i++;

if (i > 2 && c == '5' && line[i - 2] == ' ' && line[i - 3] == '"') {is_5xx = 1;}

if (is_date && c == ' ') {is_date = 0;}

if (is_date) {date_str[j] = c; j++;}

if (c == '[') {is_date = 1;}

}

line[i] = '\0';
date_str[j] = '\0';

if (is_end_file && is_5xx) {
printf("%s\n", line);
count_5xx++;
}
}
#define __STDC_FORMAT_MACROS 1
#include "utils.c"

int main(int argc, char* argv[]) {
time_t time_interval = atoi(argv[1]);

const char* file_name = argv[2];
FILE * start_date_file = fopen(file_name, "r");
FILE * end_date_file = fopen(file_name, "r");

uint64_t cur_logs = 0;
uint64_t max_logs = 0;

char end_date_line[LOG_LENGTH];
char start_date_line[LOG_LENGTH];

char end_date_str[DATE_LENGTH];
char start_date_str[DATE_LENGTH];
char buffer_date_str[DATE_LENGTH];

char max_end_date_str[DATE_LENGTH];
char max_start_date_str[DATE_LENGTH];

getline(end_date_file, end_date_line, end_date_str, 1);
getline(start_date_file, start_date_line, start_date_str, 0);

while (end_date_line[0] != '\0') {
time_t start_date = time_since_the_epoch(start_date_str);
time_t end_date = time_since_the_epoch(end_date_str);
time_t future_limit = start_date + time_interval;

while (end_date_line[0] && end_date < future_limit) {
strcpy(buffer_date_str, end_date_str);
getline(end_date_file, end_date_line, end_date_str, 1);
end_date = time_since_the_epoch(end_date_str);
cur_logs++;
}

if (cur_logs > max_logs) {
max_logs = cur_logs;
strcpy(max_start_date_str, start_date_str);
strcpy(max_end_date_str, buffer_date_str);
}

strcpy(buffer_date_str, start_date_str);

while (end_date_line[0] && !strcmp(buffer_date_str, start_date_str)) {
getline(start_date_file, start_date_line, start_date_str, 0);
cur_logs--;
}
}
uint64_t count_5xx;
uint64_t max_logs;

printf("# of logs with 5xx status: %" PRIu64 "\n", count_5xx);
printf(
"The maximum number of request (%" PRIu64 ") was between %s and %s\n",
max_logs,
max_start_date_str,
max_end_date_str
);
solve(argc, argv, &count_5xx, &max_logs);

return 0;
}
}
Binary file removed process_logs.exe
Binary file not shown.
52 changes: 52 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "utils.c"

#include <gtest/gtest.h>

TEST(ParseLogs, No5xxError) {
FILE* in = fopen("some_name.txt", "w+");
char contents[] =
"burger.letters.com - - [01/Jul/1995:00:00:12 -0400] \"GET /images/NASA-logosmall.gif HTTP/1.0\" 304 0\n"
"burger.letters.com - - [01/Jul/1995:00:00:12 -0400] \"GET /shuttle/countdown/video/livevideo.gif HTTP/1.0\" 200 0\n"
"205.212.115.106 - - [01/Jul/1995:00:00:12 -0400] \"GET /shuttle/countdown/countdown.html HTTP/1.0\" 200 3985\n"
"d104.aa.net - - [01/Jul/1995:00:00:13 -0400] \"GET /shuttle/countdown/ HTTP/1.0\" 200 3985\n"
"129.94.144.152 - - [01/Jul/1995:00:00:13 -0400] \"GET / HTTP/1.0\" 200 7074\n";
fprintf(in, contents);
rewind(in);
uint64_t count_5xx;
uint64_t max_logs;
char* argv[] = {"", "2", "some_name.txt"};

solve(3, argv, &count_5xx, &max_logs);
ASSERT_EQ(count_5xx, 0) << "Wrong number of count_5xx No5xxError";
ASSERT_EQ(max_logs, 5) << "Wrong number of max_logs No5xxError";

fclose(in);
remove("some_name.txt");
}

TEST(ParseLogs, Include5xxError) {
FILE* in = fopen("some_name.txt", "w+");
char contents[] =
"tallyho.facs.bellcore.com - - [03/Jul/1995:09:07:05 -0400] \"GET /shuttle/missions/sts-71/images/KSC-95EC-0917.jpg HTTP/1.0\" 200 52491\n"
"134.83.184.18 - - [03/Jul/1995:09:07:06 -0400] \"GET /shuttle/countdown/video/livevideo.gif HTTP/1.0\" 200 50073\n"
"amherst-ts-12.nstn.ca - - [03/Jul/1995:09:07:06 -0400] \"GET /icons/blank.xbm HTTP/1.0\" 500 509\n"
"ereapp.erenj.com - - [03/Jul/1995:09:07:08 -0400] \"GET /history/mercury/ma-9/ma-9-patch-small.gif HTTP/1.0\" 500 21214\n"
"ereapp.erenj.com - - [03/Jul/1995:09:07:09 -0400] \"GET /history/apollo/images/APOLLO-logosmall.gif HTTP/1.0\" 200 1173\n";
fprintf(in, contents);
rewind(in);
uint64_t count_5xx;
uint64_t max_logs;
char* argv[] = {"", "2", "some_name.txt"};

solve(3, argv, &count_5xx, &max_logs);
ASSERT_EQ(count_5xx, 2) << "Wrong number of count_5xx Include5xxError";
ASSERT_EQ(max_logs, 3) << "Wrong number of max_logs Include5xxError";

fclose(in);
remove("some_name.txt");
}

int main(int argc, char **argv) {
testing::InitGoogleTest();
return RUN_ALL_TESTS();
}
Loading

0 comments on commit b448ca5

Please sign in to comment.