Skip to content

Commit f0a5a73

Browse files
authored
Merge pull request #1221 from ingve/unittests
Unittests additions
2 parents 3ccfd26 + f3cd7f9 commit f0a5a73

10 files changed

Lines changed: 173 additions & 50 deletions

File tree

etc/service.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ target_link_libraries(service
293293
cxxabi
294294
libos
295295
libc
296+
libos
296297
libm
297298
libg
298299
libgcc

src/kernel/syscalls.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <info>
3333
#include <smp>
3434

35-
3635
#if defined (UNITTESTS) && !defined(__MACH__)
3736
#define THROW throw()
3837
#else
@@ -56,40 +55,6 @@ void _exit(int status) {
5655
default_exit();
5756
}
5857

59-
int execve(const char*,
60-
char* const*,
61-
char* const*)
62-
{
63-
panic("SYSCALL EXECVE NOT SUPPORTED");
64-
return -1;
65-
}
66-
67-
int fork() {
68-
panic("SYSCALL FORK NOT SUPPORTED");
69-
return -1;
70-
}
71-
72-
int fstat(int, struct stat* st) {
73-
debug("SYSCALL FSTAT Dummy, returning OK 0");
74-
st->st_mode = S_IFCHR;
75-
return 0;
76-
}
77-
78-
int getpid() {
79-
debug("SYSCALL GETPID Dummy, returning 1");
80-
return 1;
81-
}
82-
83-
int link(const char*, const char*) {
84-
panic("SYSCALL LINK unsupported");
85-
return -1;
86-
}
87-
88-
int unlink(const char*) {
89-
panic("SYSCALL UNLINK unsupported");
90-
return -1;
91-
}
92-
9358
void* sbrk(ptrdiff_t incr) {
9459
/// NOTE:
9560
/// sbrk gets called really early on, before everything else
@@ -102,14 +67,6 @@ void* sbrk(ptrdiff_t incr) {
10267
return (void*) prev_heap_end;
10368
}
10469

105-
/*
106-
int stat(const char*, struct stat *st) {
107-
debug("SYSCALL STAT Dummy");
108-
st->st_mode = S_IFCHR;
109-
return 0;
110-
}
111-
*/
112-
11370
clock_t times(struct tms*) {
11471
panic("SYSCALL TIMES Dummy, returning -1");
11572
return -1;
@@ -252,4 +209,3 @@ void _init_syscalls()
252209
// make sure that the buffers length is zero so it won't always show up in crashes
253210
_crash_context_buffer[0] = 0;
254211
}
255-

src/posix/sys/stat.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,9 @@ mode_t umask(mode_t cmask)
219219
(void) cmask;
220220
return DEFAULT_UMASK;
221221
}
222+
223+
int fstat(int, struct stat* st) {
224+
debug("SYSCALL FSTAT Dummy, returning OK 0");
225+
st->st_mode = S_IFCHR;
226+
return 0;
227+
}

src/posix/unistd.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,31 @@ long pathconf(const char *path, int name) {
416416
close(fd);
417417
return res;
418418
}
419+
420+
int execve(const char*,
421+
char* const*,
422+
char* const*)
423+
{
424+
panic("SYSCALL EXECVE NOT SUPPORTED");
425+
return -1;
426+
}
427+
428+
int fork() {
429+
panic("SYSCALL FORK NOT SUPPORTED");
430+
return -1;
431+
}
432+
433+
int getpid() {
434+
debug("SYSCALL GETPID Dummy, returning 1");
435+
return 1;
436+
}
437+
438+
int link(const char*, const char*) {
439+
panic("SYSCALL LINK unsupported");
440+
return -1;
441+
}
442+
443+
int unlink(const char*) {
444+
panic("SYSCALL UNLINK unsupported");
445+
return -1;
446+
}

src/util/statman.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ Stat& Statman::create(const Stat::Stat_type type, const std::string& name) {
103103
if(idx >= stats_.size()) {
104104
throw Stats_out_of_memory{};
105105
}
106-
107-
return (*new (&stats_[next_available_++]) Stat{type, idx, name});
106+
auto& stat = *new (&stats_[next_available_]) Stat(type, idx, name);
107+
next_available_++;
108+
return stat;
108109
}
109110

110111
///////////////////////////////////////////////////////////////////////////////

test/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.8.9)
22
project(unittests C CXX)
33

44
option(COVERAGE "Build with coverage generation" OFF)
5+
option(SILENT_BUILD "Build with some warnings turned off" ON)
56

67
if ("${ARCH}" STREQUAL "")
78
set (ARCH "ARCH_X86")
@@ -204,7 +205,12 @@ if ("${ARCH}" STREQUAL "ARCH_ARMv7")
204205
set_property(SOURCE ${SOURCES} PROPERTY COMPILE_FLAGS -mfpu=vfpv3-d16)
205206
endif("${ARCH}" STREQUAL "ARCH_ARMv7")
206207

208+
if(SILENT_BUILD)
209+
message(STATUS "NOTE: Building with some warnings turned off")
210+
set_property(SOURCE ${SOURCES} APPEND_STRING PROPERTY COMPILE_FLAGS
211+
"-Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -Wno-format")
212+
endif()
213+
207214
add_executable(unittests ${SOURCES})
208215
install(TARGETS unittests DESTINATION ${TEST})
209216
install(DIRECTORY lest/include/lest DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
210-

test/net/unit/http_request_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,13 @@ CASE("make_request makes requests")
124124
auto req_ptr = http::make_request("GET / HTTP/1.1");
125125
EXPECT(req_ptr->version().to_string() == "HTTP/1.1");
126126
}
127+
128+
CASE("operator std::string converts request to string")
129+
{
130+
http::Request r("GET /");
131+
r.set_method(http::HEAD);
132+
r.set_uri(uri::URI("/data/json/includeos_stats"));
133+
r.set_version(http::Version(1, 1));
134+
std::string s = r;
135+
EXPECT(s.size() > 30);
136+
}

test/net/unit/http_time_test.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,26 @@ CASE("from_time_t() returns time as string")
3030

3131
CASE("to_time_t() returns time_t from string")
3232
{
33-
auto str = "Tue, 01 Jan 1980 00:00:00 GMT"s;
34-
time_t time {http::time::to_time_t(str)};
35-
EXPECT(time != std::time_t{});
33+
auto str1 = "Tue, 01 Jan 1980 00:00:00 GMT"s;
34+
time_t time1 {http::time::to_time_t(str1)};
35+
EXPECT(time1 != std::time_t{});
36+
37+
auto str2 = "Tuesday, 01-Jan-80 00:00:00 GMT"s;
38+
time_t time2 {http::time::to_time_t(str2)};
39+
EXPECT(time2 != std::time_t{});
40+
41+
auto str3 = "Tue Jan 1 00:00:00 1980"s;
42+
time_t time3 {http::time::to_time_t(str3)};
43+
EXPECT(time3 != std::time_t{});
44+
45+
// invalid
46+
auto str4 = "Zxe Jyn 32 23:59:99 1930"s;
47+
time_t time4 {http::time::to_time_t(str4)};
48+
EXPECT(time4 == std::time_t{});
49+
}
50+
51+
CASE("now() returns current time")
52+
{
53+
auto str = http::time::now();
54+
EXPECT(str.size() > 0ul);
3655
}

test/util/unit/statman.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,53 @@ CASE("A Stat is accessible through index operator")
285285
free(buffer);
286286
}
287287
}
288+
289+
CASE("stats names can only be MAX_NAME_LEN characters long")
290+
{
291+
uintptr_t buffer = (uintptr_t)malloc(8192);
292+
Statman statman_{buffer, 8192};
293+
// ok
294+
std::string statname1 {"a.stat"};
295+
Stat& stat1 = statman_.create(Stat::UINT32, statname1);
296+
// also ok
297+
const size_t MAX_NAME_LEN {48};
298+
std::string statname2(MAX_NAME_LEN, 'x');
299+
Stat& stat2 = statman_.create(Stat::UINT32, statname2);
300+
int num_stats_before = statman_.num_stats();
301+
// not ok
302+
std::string statname3(MAX_NAME_LEN + 1, 'y');
303+
EXPECT_THROWS(Stat& stat3 = statman_.create(Stat::FLOAT, statname3));
304+
int num_stats_after = statman_.num_stats();
305+
EXPECT(num_stats_before == num_stats_after);
306+
free((void*)buffer);
307+
}
308+
309+
CASE("get(\"name\") returns reference to stat with name, throws if not present")
310+
{
311+
uintptr_t buffer = (uintptr_t)malloc(8192);
312+
Statman statman_ {buffer, 8192};
313+
EXPECT_THROWS(Stat& res1 = statman_.get("some.important.stat"));
314+
EXPECT_THROWS(Stat& res2 = statman_.get("other.important.stat"));
315+
EXPECT_THROWS(Stat& res3 = statman_.get("very.important.stat"));
316+
Stat& stat1 = statman_.create(Stat::UINT32, "some.important.stat");
317+
Stat& stat2 = statman_.create(Stat::UINT64, "other.important.stat");
318+
Stat& stat3 = statman_.create(Stat::FLOAT, "very.important.stat");
319+
++stat1;
320+
++stat2;
321+
++stat3;
322+
EXPECT_NO_THROW(Stat& res1 = statman_.get("some.important.stat"));
323+
EXPECT_NO_THROW(Stat& res2 = statman_.get("other.important.stat"));
324+
EXPECT_NO_THROW(Stat& res3 = statman_.get("very.important.stat"));
325+
EXPECT_THROWS_AS(Stat& res5 = statman_.get("some.missing.stat"), Stats_exception);
326+
327+
free((void*)buffer);
328+
}
329+
330+
CASE("total_num_bytes() returns total number of bytes the Statman object takes up")
331+
{
332+
uintptr_t buffer = (uintptr_t)malloc(8192);
333+
Statman statman_ {buffer, 8192};
334+
Stat& stat1 = statman_.create(Stat::UINT32, "some.important.stat");
335+
EXPECT(statman_.total_num_bytes() > 8192);
336+
free((void*)buffer);
337+
}

test/util/unit/uri_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,50 @@ CASE("URI construction, assignment")
131131
EXPECT_NOT(uri == uri1);
132132
EXPECT_NOT(uri == uri2);
133133
EXPECT_NOT(uri == uri3);
134+
// test uri with query parameters
135+
const uri::URI uri4 {"http://example.com/test/?param1=test&param2=foo&param3=bar"};
136+
uri::URI uri5 {uri4};
137+
EXPECT(uri4 == uri5);
138+
EXPECT_NOT(uri == uri5);
139+
uri::URI uri6 = uri5;
140+
EXPECT(uri5 == uri6);
141+
}
142+
143+
CASE("host_and_port() returns the URI's host and port")
144+
{
145+
const uri::URI uri {"http://example.com:8080/?type=float"};
146+
EXPECT(uri.host_and_port() == "example.com:8080");
147+
}
148+
149+
CASE("is_valid returns whether the URI is \"valid\" (has host or path)")
150+
{
151+
const uri::URI uri {"http://example.com"};
152+
EXPECT(uri.is_valid());
153+
const uri::URI uri2 {"/users/42#email"};
154+
EXPECT(uri2.is_valid());
155+
}
156+
157+
CASE("operator std::string() converts URI to string")
158+
{
159+
const uri::URI uri {"http://example.com/test#p42"};
160+
std::string s = uri;
161+
EXPECT(s == "http://example.com/test#p42");
162+
}
163+
164+
CASE("operator bool checks whether uri is valid")
165+
{
166+
const uri::URI uri {"http://example.com/"};
167+
bool valid = uri;
168+
EXPECT(valid == true);
169+
}
170+
171+
CASE("operator <")
172+
{
173+
const uri::URI uri1 {"http://example.com/"};
174+
const uri::URI uri2 {"http://www.google.com/?hl=en"};
175+
bool res = (uri1 < uri2);
176+
EXPECT(res == true);
177+
const uri::URI uri3 = uri2;
178+
res = (uri2 < uri3);
179+
EXPECT(res == false);
134180
}

0 commit comments

Comments
 (0)