Testing, split general logic into library, SFTP stuff

This commit is contained in:
Tim Warren 2015-04-07 17:08:28 -04:00
parent 3083b4e213
commit 1fb4c44cec
14 changed files with 9638 additions and 50 deletions

View File

@ -1,14 +1,22 @@
CXX = $(shell wx-config --cxx) -Wno-c++11-compat-deprecated-writable-strings CXX = $(shell wx-config --cxx)
TARGET = build/Tyro
LDLIBS = $(shell wx-config --libs all) -lssh2
WX_CXXFLAGS = -I./src -I./include -static $(shell wx-config --cxxflags)
DEV_CXXFLAGS = -g -Wall -Wextra $(WX_CXXFLAGS)
CXXFLAGS = -Os -s $(WX_CXXFLAGS)
SOURCES = $(wildcard src/**/*.cpp src/*.cpp include/**/*.cpp include/*.cpp) SOURCES = $(wildcard src/network/*.cpp include/**/*.cpp include/*.cpp)
OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES)) OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES))
TARGET = build/Tyro.a
all: build $(SOURCES) $(TARGET) PROGRAM_SRC = $(wildcard src/*.cpp src/widgets/*.cpp)
PROGRAM = build/Tyro
PROGRAM_OBJECTS = $(patsubst %.cpp,%.o, $(PROGRAM_SRC))
LDLIBS = -ldl $(TARGET) $(shell wx-config --libs all) -lssh2
WX_CXXFLAGS = $(shell wx-config --cxxflags)
DEV_CXXFLAGS = -g -Wall -Wextra
CXXFLAGS = -Os -I./src -I./include
TEST_SRC= $(wildcard tests/*.cpp)
TESTS = $(patsubst %.cpp,%,$(TEST_SRC))
all: build $(TARGET) $(PROGRAM)
dev: CXXFLAGS= $(DEV_CXXFLAGS) dev: CXXFLAGS= $(DEV_CXXFLAGS)
dev: all dev: all
@ -16,8 +24,18 @@ dev: all
build: build:
@mkdir -p build @mkdir -p build
$(TARGET): CXXFLAGS += -fPIC
$(TARGET): $(OBJECTS) $(TARGET): $(OBJECTS)
$(CXX) $(LDLIBS) $(OBJECTS) -o $@ ar rcs $@ $(OBJECTS)
ranlib $@
$(PROGRAM): CXXFLAGS += $(WX_CXXFLAGS) $(TARGET)
$(PROGRAM): $(PROGRAM_OBJECTS)
$(CXX) $(LDLIBS) $(PROGRAM_OBJECTS) -o $(PROGRAM)
$(PROGRAM_OBJECTS):
$(CXX) $(WX_CXXFLAGS) $(PROGRAM_SRC)
run: run:
./build/Tyro ./build/Tyro
@ -34,9 +52,14 @@ Tyro.app: all resources/platform/osx/Info.plist
cp build/Tyro Tyro.app/Contents/MacOS/Tyro cp build/Tyro Tyro.app/Contents/MacOS/Tyro
cp resources/platform/osx/tyro.icns Tyro.app/Contents/Resources/ cp resources/platform/osx/tyro.icns Tyro.app/Contents/Resources/
.PHONY: tests
tests: LDLIBS = $(TARGET) -lssh2
tests: $(TESTS)
sh ./tests/runtests.sh
clean: clean:
rm -f *.o rm -f *.o
rm -rf Tyro.app rm -rf Tyro.app
rm -rf build $(OBJECTS) $(PROGRAM) rm -rf build $(OBJECTS) $(PROGRAM) $(TARGET) $(TESTS)
find . -name "*.gc*" -exec rm {} \; find . -name "*.gc*" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print` rm -rf `find . -name "*.dSYM" -print`

View File

@ -9,7 +9,7 @@
#include "common.h" #include "common.h"
#include "TyroApp.h" #include "TyroApp.h"
#include "Main.h" #include "widgets/MainFrame.h"
IMPLEMENT_APP(TyroApp); IMPLEMENT_APP(TyroApp);

View File

@ -10,7 +10,7 @@
#ifndef TYROAPP_H #ifndef TYROAPP_H
#define TYROAPP_H #define TYROAPP_H
#include "common.h" #include "wx_common.h"
#include <wx/app.h> #include <wx/app.h>
#include <wx/debug.h> #include <wx/debug.h>

View File

@ -4,17 +4,10 @@
#ifndef TYRO_COMMON_H #ifndef TYRO_COMMON_H
#define TYRO_COMMON_H #define TYRO_COMMON_H
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
using namespace std;
#include <iostream> #include <iostream>
#include <string> #include <string>
using namespace std;
#endif // TYRO_COMMON_H #endif // TYRO_COMMON_H

View File

@ -1,19 +1,65 @@
#include "SFTP.h" #include "SFTP.h"
SFTP::SFTP(const char *host, string user, string pass) { SFTP::SFTP(const char *host, const char *user, const char *pass, const char *port)
struct addrinfo host_info; {
struct addrinfo *host_info_list; this->ssh_connect(host, user, pass, port);
int sock; this->sftp_connect();
int status; };
int rc;
SFTP::~SFTP() {
libssh2_sftp_shutdown(sftp_session);
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
close(sock);
libssh2_exit();
freeaddrinfo(host_info_list);
freeaddrinfo(&host_info);
};
string SFTP::getFile(const char *path)
{
string output = "";
fprintf(stderr, "libssh2_sftp_open()!\n");
LIBSSH2_SFTP_HANDLE *sftp_handle;
sftp_handle = libssh2_sftp_open(sftp_session, path, LIBSSH2_FXF_READ, 0);
if ( ! sftp_handle)
{
fprintf(stderr, "Unable to open file with SFTP: %ld\n", libssh2_sftp_last_error(sftp_session));
return output;
}
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do {
char mem[1024];
// loop until fail
rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
if (rc > 0)
{
output += (string)mem;
}
else break;
} while (1);
libssh2_sftp_close(sftp_handle);
return output;
}
void SFTP::ssh_connect(const char *host, const char *user, const char *pass, const char *port)
{
// Clear out memory in addr structure // Clear out memory in addr structure
memset(&host_info, 0, sizeof host_info); memset(&host_info, 0, sizeof host_info);
host_info.ai_family = AF_UNSPEC; host_info.ai_family = AF_UNSPEC;
host_info.ai_socktype = SOCK_STREAM; host_info.ai_socktype = SOCK_STREAM;
status = getaddrinfo(host, "22", &host_info, &host_info_list); status = getaddrinfo(host, port, &host_info, &host_info_list);
if (status != 0) if (status != 0)
{ {
@ -31,15 +77,54 @@ SFTP::SFTP(const char *host, string user, string pass) {
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (connect(sock, host_info_list->ai_addr, host_info_list->ai_addrlen) != 0)
{
cout << "Failed to connect to socket." << endl;
return;
}
session = libssh2_session_init(); session = libssh2_session_init();
/* Since we have set non-blocking, tell libssh2 we are blocking */ /* Since we have set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1); libssh2_session_set_blocking(session, 1);
// Actually do the ssh handshake // Actually do the ssh handshake
rc = libssh2_session_handshake(session, sock); rc = libssh2_session_handshake(session, sock);
};
SFTP::~SFTP() { if (rc)
{
cout << "Failure establishing SSH session:" << rc << endl;
return;
}
}; // Get the host fingerprint to check against known hosts
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
fprintf(stderr, "Fingerprint: ");
for (int i = 0; i < 20; i++)
{
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
}
fprintf(stderr, "\n");
//@TODO do something with the handling of known hosts
// Authenticate (by password)
if (libssh2_userauth_password(session, user, pass))
{
fprintf(stderr, "Authentication by password failed.\n");
return;
}
}
void SFTP::sftp_connect()
{
fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session);
if ( ! sftp_session)
{
fprintf(stderr, "Unable to start SFTP session \n");
return;
}
}

View File

@ -8,9 +8,10 @@
#ifndef SFTP_H #ifndef SFTP_H
#define SFTP_H #define SFTP_H
#define LIBSSH_STATIC 1
#include "../common.h" #include "../common.h"
#define LIBSSH_STATIC 1
// libssh2 includes // libssh2 includes
#include <libssh2.h> #include <libssh2.h>
#include <libssh2_sftp.h> #include <libssh2_sftp.h>
@ -22,12 +23,20 @@
class SFTP { class SFTP {
public: public:
SFTP(const char *host, string user, string pass); SFTP(const char *host, const char *user, const char *pass, const char *port="22");
~SFTP(); ~SFTP();
string getFile(const char *path);
private: private:
struct addrinfo host_info;
struct addrinfo *host_info_list;
const char *fingerprint;
int sock;
int status;
int rc;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
unsigned long hostaddr; void ssh_connect(const char *host, const char *user, const char *pass, const char *port);
void sftp_connect();
}; };
#endif /* SFTP_H */ #endif /* SFTP_H */

View File

@ -1,7 +1,7 @@
#ifndef TYROEDIT_PANE_H #ifndef TYROEDIT_PANE_H
#define TYROEDIT_PANE_H #define TYROEDIT_PANE_H
#include "../common.h" #include "../wx_common.h"
#include <wx/stc/stc.h> #include <wx/stc/stc.h>
class EditPane: public wxStyledTextCtrl class EditPane: public wxStyledTextCtrl

View File

@ -6,7 +6,7 @@
#include "wx_pch.h" #include "wx_pch.h"
#endif #endif
#include "Main.h" #include "MainFrame.h"
BEGIN_EVENT_TABLE(MainFrame, wxFrame) BEGIN_EVENT_TABLE(MainFrame, wxFrame)
@ -56,16 +56,16 @@ void MainFrame::SetupToolbar()
{ {
// Icon files // Icon files
#ifndef __WXGTK__ #ifndef __WXGTK__
#include "../resources/xpm/48/file-empty.xpm" #include "../../resources/xpm/48/file-empty.xpm"
#include "../resources/xpm/48/folder.xpm" #include "../../resources/xpm/48/folder.xpm"
#include "../resources/xpm/48/floppy.xpm" #include "../../resources/xpm/48/floppy.xpm"
#include "../resources/xpm/48/wrench-screwdriver.xpm" #include "../../resources/xpm/48/wrench-screwdriver.xpm"
#endif // __WXGTK__ #endif // __WXGTK__
#ifdef __WXGTK__ #ifdef __WXGTK__
#include "../resources/xpm/24/file-empty.xpm" #include "../../resources/xpm/24/file-empty.xpm"
#include "../resources/xpm/24/folder.xpm" #include "../../resources/xpm/24/folder.xpm"
#include "../resources/xpm/24/floppy.xpm" #include "../../resources/xpm/24/floppy.xpm"
#include "../resources/xpm/24/wrench-screwdriver.xpm" #include "../../"
#endif #endif
CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL); CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);

View File

@ -5,9 +5,9 @@
#ifndef TYROMAIN_H #ifndef TYROMAIN_H
#define TYROMAIN_H #define TYROMAIN_H
#include "common.h" #include "../wx_common.h"
#include "TyroApp.h" #include "../TyroApp.h"
#include "widgets/TabContainer.h" #include "TabContainer.h"
class MainFrame: public wxFrame class MainFrame: public wxFrame
{ {

View File

@ -5,7 +5,7 @@
#ifndef TABCONTAINER_H #ifndef TABCONTAINER_H
#define TABCONTAINER_H #define TABCONTAINER_H
#include "../common.h" #include "../wx_common.h"
#include <wx/aui/aui.h> #include <wx/aui/aui.h>
#include "EditPane.h" #include "EditPane.h"

21
src/wx_common.h Normal file
View File

@ -0,0 +1,21 @@
/*
* File: wx_common.h
* Author: twarren
*
* Created on April 7, 2015, 2:29 PM
*/
#ifndef WX_COMMON_H
#define WX_COMMON_H
#include "common.h"
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#endif /* WX_COMMON_H */

21
tests/SFTPTest.cpp Normal file
View File

@ -0,0 +1,21 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "../src/network/SFTP.h"
TEST_CASE("ssh connections work", "[SFTP]") {
SFTP *sftp = new SFTP("timshomepage.net", "test", "testpassword");
SECTION("sftp object is not null") {
REQUIRE(sftp != NULL);
}
/*SECTION("can retreive a file") {
string file;
file = sftp->getFile("~/test.txt");
REQUIRE(file != "");
REQUIRE(file == "SFTP works!");
}*/
}

9427
tests/catch.hpp Normal file

File diff suppressed because it is too large Load Diff

9
tests/runtests.sh Executable file
View File

@ -0,0 +1,9 @@
for i in tests/*Test
do
if test -f $i
then
./$i
fi
done
echo ""