mirror of
				https://github.com/falk-werner/webfuse
				synced 2025-06-13 12:54:15 +00:00 
			
		
		
		
	added test utilities
This commit is contained in:
		
							parent
							
								
									60594067b1
								
							
						
					
					
						commit
						9036aba41b
					
				
							
								
								
									
										23
									
								
								test-src/webfuse/test/tempdir.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								test-src/webfuse/test/tempdir.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | ||||
| #include "webfuse/test/tempdir.hpp" | ||||
| #include <unistd.h> | ||||
| 
 | ||||
| namespace webfuse | ||||
| { | ||||
| 
 | ||||
| tempdir::tempdir() | ||||
| { | ||||
|     char path_template[] = "/tmp/webfuse_test_XXXXXX"; | ||||
|     path = mkdtemp(path_template); | ||||
| } | ||||
| 
 | ||||
| tempdir::~tempdir() | ||||
| { | ||||
|     unlink(path.c_str()); | ||||
| } | ||||
| 
 | ||||
| std::string const tempdir::name() const | ||||
| { | ||||
|     return path; | ||||
| } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										22
									
								
								test-src/webfuse/test/tempdir.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								test-src/webfuse/test/tempdir.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| #ifndef WEBFUSE_TEMPDIR_HPP | ||||
| #define WEBFUSE_TEMPDIR_HPP | ||||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| namespace webfuse | ||||
| { | ||||
| 
 | ||||
| class tempdir | ||||
| { | ||||
| public: | ||||
|     tempdir(); | ||||
|     ~tempdir(); | ||||
|     std::string const name() const; | ||||
| private: | ||||
|     std::string path; | ||||
| 
 | ||||
| }; | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										39
									
								
								test-src/webfuse/test/thread.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								test-src/webfuse/test/thread.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | ||||
| #include "webfuse/test/thread.hpp" | ||||
| #include <csignal> | ||||
| 
 | ||||
| namespace | ||||
| { | ||||
| 
 | ||||
| extern "C" void * webfuse_thread_main(void * args) | ||||
| { | ||||
|     auto * run = reinterpret_cast<std::function<void(void)> *>(args); | ||||
|     (*run)(); | ||||
|     return nullptr; | ||||
| } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| namespace webfuse | ||||
| { | ||||
| 
 | ||||
| thread::thread(std::function<void(void)> run) | ||||
| { | ||||
|     pthread_create(&real_thread, nullptr,  | ||||
|         &webfuse_thread_main, | ||||
|         reinterpret_cast<void*>(&run)); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| thread::~thread() | ||||
| { | ||||
|     pthread_join(real_thread, nullptr); | ||||
| } | ||||
| 
 | ||||
| void thread::kill(int signal_id) | ||||
| { | ||||
|     pthread_kill(real_thread, signal_id); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										22
									
								
								test-src/webfuse/test/thread.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								test-src/webfuse/test/thread.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| #ifndef WEBFUSE_THREAD_HPP | ||||
| #define WEBFUSE_THREAD_HPP | ||||
| 
 | ||||
| #include <pthread.h> | ||||
| #include <functional> | ||||
| 
 | ||||
| namespace webfuse | ||||
| { | ||||
| 
 | ||||
| class thread | ||||
| { | ||||
| public: | ||||
|     explicit thread(std::function<void(void)> run); | ||||
|     ~thread(); | ||||
|     void kill(int signal_id); | ||||
| private: | ||||
|     pthread_t real_thread; | ||||
| }; | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
| @ -1,7 +1,38 @@ | ||||
| #include "webfuse/webfuse.hpp" | ||||
| #include "webfuse/test/thread.hpp" | ||||
| #include "webfuse/test/tempdir.hpp" | ||||
| #include <gtest/gtest.h> | ||||
| #include <csignal> | ||||
| #include <cstring> | ||||
| #include <cstdlib> | ||||
| #include <chrono> | ||||
| #include <thread> | ||||
| 
 | ||||
| extern "C" void * run(void * args) | ||||
| { | ||||
|     webfuse::app * app = reinterpret_cast<webfuse::app*>(args); | ||||
| 
 | ||||
|     return nullptr; | ||||
| } | ||||
| 
 | ||||
| TEST(app, init) | ||||
| { | ||||
|     webfuse::app app; | ||||
| } | ||||
| 
 | ||||
| TEST(app, run) | ||||
| {    | ||||
|     webfuse::tempdir dir; | ||||
|     webfuse::thread thread([&dir](){ | ||||
|         webfuse::app app; | ||||
|         char arg0[] = "webfuse"; | ||||
|         char arg1[] = "-f"; | ||||
|         char* arg2 = strdup(dir.name().c_str()); | ||||
|         char* argv[] = { arg0, arg1, arg2, nullptr}; | ||||
|         int rc = app.run(3, argv); | ||||
|         free(arg2); | ||||
|     }); | ||||
| 
 | ||||
|     std::this_thread::sleep_for(std::chrono::seconds(1)); | ||||
|     thread.kill(SIGINT); | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user