Skip to content

Commit

Permalink
Refactoring and added root tests to check TUN device functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb James DeLisle committed Dec 8, 2012
1 parent 53df63c commit be0561f
Show file tree
Hide file tree
Showing 52 changed files with 1,477 additions and 577 deletions.
2 changes: 1 addition & 1 deletion admin/angel/Core.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void Core_initTunnel(String* desiredDeviceName,
//Ducttape_setUserInterface(dt, &icmp->internal);
Ducttape_setUserInterface(dt, &tun->iface);

TUNConfigurator_setIpAddress(assignedTunName, ipAddr, addressPrefix, logger, eh);
TUNConfigurator_addIp6Address(assignedTunName, ipAddr, addressPrefix, logger, eh);
TUNConfigurator_setMTU(assignedTunName, DEFAULT_MTU, logger, eh);
}

Expand Down
56 changes: 56 additions & 0 deletions cmake/modules/HasRoot.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# You may redistribute this program and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if (NOT HasRoot_CMAKE)

set(content "
#ifdef WIN32
#include <windows.h>
#include <string.h>
int main()
{
char buff[32] = {0};
DWORD length = 32;
GetUserNameA(buff, &length);
printf(\"%s\\n\", buff);
return strcmp(\"Administrator\", buff);
}
#else
#include <unistd.h>
int main()
{
return getuid();
}
#endif
")

file(WRITE ${CMAKE_BINARY_DIR}/HasRoot.c "${content}")
RemoteTest_tryRun(runRes compRes ${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}/HasRoot.c
COMPILE_OUTPUT_VARIABLE compileOut
RUN_OUTPUT_VARIABLE output
)
if (NOT compRes)
message("${compileOut}")
message(FATAL_ERROR "Compilation failed")
endif()
if ("${runRes}" STREQUAL "PLEASE_FILL_OUT-FAILED_TO_RUN")
message(FATAL_ERROR "RunFailed")
endif()
if (NOT runRes)
set(HasRoot TRUE CACHE INTERNAL "")
message("Testing with root access, root tests enabled")
else()
set(HasRoot FALSE CACHE INTERNAL "")
message("Testing without root access, root tests disabled")
endif()

set(HasRoot_CMAKE TRUE CACHE INTERNAL "")
endif()
57 changes: 48 additions & 9 deletions cmake/modules/RemoteTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,40 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if (NOT REMOTE_TEST)
set(REMOTE_TEST TRUE)

file(READ ${CMAKE_SOURCE_DIR}/cmake/modules/RemoteTestTemplate.bash template)
string(REPLACE "__REMOTE_TEST_IP_PORT__" "${REMOTE_TEST_IP_PORT}" FILE_CONTENT "${template}")
set(template FALSE)
if (CMAKE_CROSSCOMPILING AND REMOTE_TEST_IP_PORT)
file(READ ${CMAKE_SOURCE_DIR}/cmake/modules/RemoteTestTemplate.bash template)
string(REPLACE "__REMOTE_TEST_IP_PORT__" "${REMOTE_TEST_IP_PORT}" FILE_CONTENT "${template}")

# try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
# bindir srcfile [CMAKE_FLAGS <Flags>]
# [COMPILE_DEFINITIONS <flags>]
# [COMPILE_OUTPUT_VARIABLE comp]
# [RUN_OUTPUT_VARIABLE run]
# [OUTPUT_VARIABLE var]
# [ARGS <arg1> <arg2>...])
function(RemoteTest_tryRun RUN_RESULT COMPILE_RESULT bindir srcfile)
set(params "")
foreach(i RANGE ${ARGC})
list(APPEND params ${ARGV${i}})
endforeach()
string(RANDOM rand)
set(execFile ${CMAKE_CURRENT_BINARY_DIR}/${rand}_remote_executable)
try_compile(${COMPILE_RESULT} ${bindir} ${srcfile} COPY_FILE ${execFile})

message("Running remote test: ${execFile}_remote.sh")
string(REPLACE "__TEST_FILE__" "${execFile}" fileContent "${FILE_CONTENT}")
file(WRITE "${execFile}_remote.sh" "${fileContent}")
execute_process(COMMAND chmod "a+x" ${execFile}_remote.sh)
execute_process(COMMAND ${execFile}_remote.sh
RESULT_VARIABLE runResult
OUTPUT_VARIABLE output
)
set(${RUN_RESULT} ${runResult} PARENT_SCOPE)
set(${RUN_OUTPUT_VARIABLE} ${output} PARENT_SCOPE)

endfunction()

if (REMOTE_TEST_IP_PORT)
function(remoteTest executable)
string(REPLACE "__TEST_FILE__" "${CMAKE_CURRENT_BINARY_DIR}/${executable}${CMAKE_EXECUTABLE_SUFFIX}" fileContent "${FILE_CONTENT}")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${executable}_remote.sh "${fileContent}")
Expand All @@ -27,10 +52,24 @@ if (REMOTE_TEST_IP_PORT)
add_test(${executable} ${CMAKE_CURRENT_BINARY_DIR}/${executable}_remote.sh)
message(" Running test remotely")
endFunction()
else()
function(remoteTest executable)
elseif(CMAKE_CROSSCOMPILING)
function(RemoteTest_addTest executable)
message(" REMOTE_TEST_IP_PORT unspecified, skipping test.")
endfunction()
endif()

function(RemoteTest_tryRun)
message(FATAL_ERROR "REMOTE_TEST_IP_PORT unspecified.")
endfunction()
else()
function(RemoteTest_addTest executable)
add_test(${executable} ${executable})
endfunction()

function(RemoteTest_tryRun)
set(params "")
foreach(i RANGE ${ARGC})
list(APPEND params ${ARGV${i}})
endforeach()
try_run(${params})
endfunction()
endif()
17 changes: 12 additions & 5 deletions cmake/modules/Test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
include(${CMAKE_SOURCE_DIR}/cmake/modules/RemoteTest.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/modules/HasRoot.cmake)

string(REGEX REPLACE "^.*/" "" main_dir_name ${CMAKE_SOURCE_DIR})
string(REPLACE ${CMAKE_SOURCE_DIR} ${main_dir_name} this_dir ${CMAKE_CURRENT_SOURCE_DIR})
message("-- Tests to run for " ${this_dir})
add_definitions(-g -DTEST)

foreach(test_source ${Test_FILES})
function(Test_process test_source isRootTest)
string(REPLACE "test.c" "test" test_bin ${test_source})
message(" " ${test_source})
add_executable(${test_bin} ${test_source})
target_link_libraries(${test_bin} ${Test_LIBRARIES})
if (CMAKE_CROSSCOMPILING)
remoteTest(${test_bin})
else()
add_test(${test_bin} ${test_bin})
if (NOT isRootTest OR HasRoot)
RemoteTest_addTest(${test_bin})
endif()
endfunction()

foreach(test_source ${Test_FILES})
Test_process(${test_source} FALSE)
endforeach()
foreach(test_source ${Test_ROOT_FILES})
Test_process(${test_source} TRUE)
endforeach()

message("")
4 changes: 3 additions & 1 deletion interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ add_library(interface
InterfaceConnector.c
SessionManager.c
ICMP6Generator.c
MultiInterface.c
)

target_link_libraries(interface
util
cjdns-crypto-key
${LIBEVENT2_LIBRARIES}
cjdns-util-events-libevent
cjdns-util-platform-socket
)

add_library(cjdns-interface-pipeinterface
Expand Down
2 changes: 1 addition & 1 deletion interface/ETHInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "util/events/EventBase.h"
#include "interface/Interface.h"
#include "net/InterfaceController.h"
#include "interface/InterfaceController.h"
#include "util/log/Log.h"
#include "memory/Allocator.h"

Expand Down
Loading

0 comments on commit be0561f

Please sign in to comment.