Skip to content

Commit

Permalink
Merge pull request #4 from gistrec/tests
Browse files Browse the repository at this point in the history
Move tests to master branch
  • Loading branch information
gistrec authored Feb 7, 2020
2 parents 405b74a + 7ce4b6d commit bf016d0
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 43 deletions.
43 changes: 43 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: 2
jobs:
gtest:
working_directory: ~/root
docker:
- image: gcc:latest
steps:
- run:
name: Downloading dependencies
command: |
apt-get update
apt-get install -y cmake
apt-get install -y libgtest-dev
- run:
name: Installing dependencies
working_directory: /usr/src/gtest/
command: |
cmake CMakeLists.txt
make
cp /usr/src/gtest/*.a /usr/lib
- checkout
- run:
name: "Pull Submodules"
command: |
git submodule init
git submodule update --recursive --remote
- run:
name: Building tests
command: make gtests
- run:
name: Running tests
command: ./GTests --gtest_filter=*
workflows:
version: 2
build_and_test:
jobs:
- gtest:
filters:
branches:
only:
- master
- develop
- tests
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ x64/
x86/
Debug/
Release/
FileBroadcaster
packages/
10 changes: 10 additions & 0 deletions FileBroadcaster.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2047
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FileBroadcaster", "src\FileBroadcaster.vcxproj", "{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests\tests.vcxproj", "{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand All @@ -21,6 +23,14 @@ Global
{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x64.Build.0 = Release|x64
{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x86.ActiveCfg = Release|Win32
{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x86.Build.0 = Release|Win32
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x64.ActiveCfg = Debug|x64
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x64.Build.0 = Debug|x64
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x86.ActiveCfg = Debug|Win32
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x86.Build.0 = Debug|Win32
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x64.ActiveCfg = Release|x64
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x64.Build.0 = Release|x64
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x86.ActiveCfg = Release|Win32
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
all:
program:
g++ src/Main.cpp \
-std=c++14 -pthread \
-Ilib/cxxopts/include \
-o FileBroadcaster

gtests:
g++ tests/Tests.cpp \
-std=c++14 -pthread \
-Ilib/cxxopts/include \
-lgtest \
-o GTests
64 changes: 38 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
# File-Broadcaster

<p align="left">
<a href="https://circleci.com/gh/gistrec/File-Broadcaster/tree/master">
<img src="https://img.shields.io/circleci/build/github/gistrec/File-Broadcaster/master" alt="Build status"></a>
<a>
<img src="https://img.shields.io/codacy/grade/4c8169bcab3a4df18baad4e5658ec8ce" alt="Code quality"></a>
<a href="https://github.com/gistrec/File-Broadcaster/releases">
<img src="https://img.shields.io/github/v/release/gistrec/File-Broadcaster" alt="Release"></a>
<a>
<img src="https://img.shields.io/badge/platform-windows%20%7C%20linux-brightgreen" alt="Code quality"></a>
<a href="https://github.com/gistrec/File-Broadcaster/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/gistrec/File-Broadcaster?color=brightgreen" alt="License"></a>
</p>

UDP File sender and receiver
Can use broadcast address to send file on all computers in LAN


## Features

- Send file to one or all computers in LAN
- Reliability of data transmission
- Server timeout detection
- Change MTU

- Send file to one or all computers in LAN
- Reliability of data transmission
- Server timeout detection
- Change MTU

## Overview

- [Requirements](#requirements)
- [Download](#download)
- [Installation](#installation)
- [Script Parameters](#script-parameters)
- [Packets Specification](#packets-specification)
- [Script Specification](#script-specification)
- [Requirements](#requirements)
- [Download](#download)
- [Installation](#installation)
- [Script Parameters](#script-parameters)
- [Packets Specification](#packets-specification)
- [Script Specification](#script-specification)

# Download
## Download
Clone the [source repository](http://github.com/gistrec/File-Broadcaster) from Github.
* On the command line, enter:
* On the command line, enter:
````
git clone https://github.com/gistrec/File-Broadcaster.git
git submodule init
git submodule update --recursive --remote
````
* You can probably use [Github for Windows](http://windows.github.com/) or [Github for Mac](http://mac.github.com/) instead of the command line, however these aren't tested/supported and we only use the command line for development. Use [this link](https://git-scm.com/downloads) to download the command line version.
* You can probably use [Github for Windows](http://windows.github.com/) or [Github for Mac](http://mac.github.com/) instead of the command line, however these aren't tested/supported and we only use the command line for development. Use [this link](https://git-scm.com/downloads) to download the command line version.
## Requirements
* Windows:
* Visual Studio 2015 or 2017
* Linux:
* g++
* pthread
* arpa
* Windows:
* Visual Studio 2015 or 2017
* Linux:
* g++
* pthread
* arpa
## Installation
* Windows
* Open FileBroadcaster.sln via Visual Studio
* Build project
* Linux
* Open a terminal/console/command prompt, change to the directory where you cloned project, and type:
* Windows
* Open FileBroadcaster.sln via Visual Studio
* Build project
* Linux
* Open a terminal/console/command prompt, change to the directory where you cloned project, and type:
````
make all
````
Expand Down
8 changes: 4 additions & 4 deletions src/Receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void checkParts() {
while (ttl && emptyParts.size() > 0) {
for (auto index : emptyParts) {
snprintf(buffer, 7, "RESEND"); //
Utils::writeBytesFromInt(buffer + 6, index, 4); // Create request packet
Utils::writeBytesFromNumber(buffer + 6, index, 4); // Create request packet
sendto(_socket, buffer, 10, 0, (sockaddr*) &broadcast_address,//
sizeof(broadcast_address)); //

Expand Down Expand Up @@ -69,16 +69,16 @@ void run(cxxopts::ParseResult &options) {
ttl = ttl_max; // Update ttl

if (strncmp(buffer, "NEW_PACKET", 10) == 0) {
file_length = Utils::getIntFromBytes(buffer + 10, 4); // Read section "file length"
file_length = Utils::getNumberFromBytes(buffer + 10, 4); // Read section "file length"

file = new char[file_length];
memset(file, 0, file_length);

std::cout << "Receive information about new file size: " << file_length << std::endl;
std::cout << "Number of parts: " << int((float)file_length / (float)mtu + 0.5) << std::endl;
} else if (strncmp(buffer, "TRANSFER", 8) == 0) {
int part = Utils::getIntFromBytes(buffer + 8, 4); // Read section "index"
int size = Utils::getIntFromBytes(buffer + 12, 4); // Read section "size"
int part = Utils::getNumberFromBytes(buffer + 8, 4); // Read section "index"
int size = Utils::getNumberFromBytes(buffer + 12, 4); // Read section "size"
parts.insert(part);
std::cout << "Receive " << part << " part with size " << size << std::endl;

Expand Down
8 changes: 4 additions & 4 deletions src/Sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void sendPart(int part_index) {
if (packet_length > mtu) packet_length = mtu; //

snprintf(buffer, 9, "TRANSFER");
Utils::writeBytesFromInt(buffer + 8, (size_t)part_index, 4); // Write section "number"
Utils::writeBytesFromInt(buffer + 12, (size_t)packet_length, 4); // Write section "length"
Utils::writeBytesFromNumber(buffer + 8, (size_t)part_index, 4); // Write section "number"
Utils::writeBytesFromNumber(buffer + 12, (size_t)packet_length, 4); // Write section "length"
memcpy(buffer + 16, (void *)(intptr_t)(file + part_index * mtu), packet_length); // Write section "data"

// Sending part to the broadcast address
Expand Down Expand Up @@ -49,7 +49,7 @@ void run(cxxopts::ParseResult &options) {
std::cout << "Ok: File successfully copied to RAM" << std::endl;

snprintf(buffer, 11, "NEW_PACKET"); //
Utils::writeBytesFromInt(buffer + 10, file_length, 4); // Sending information
Utils::writeBytesFromNumber(buffer + 10, file_length, 4); // Sending information
sendto(_socket, buffer, 14, 0, (sockaddr*) &broadcast_address, // about size of new file
sizeof(broadcast_address)); //

Expand Down Expand Up @@ -85,7 +85,7 @@ void run(cxxopts::ParseResult &options) {
}

if (strncmp(buffer, "RESEND", 6) == 0) {
int part = Utils::getIntFromBytes(buffer + 6, 4);
int part = Utils::getNumberFromBytes(buffer + 6, 4);

auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::seconds>(now);
Expand Down
14 changes: 7 additions & 7 deletions src/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ namespace Utils {
* @param buffer - bytes array
* @param count - bytes count
*/
int getIntFromBytes(char* buffer, int count) {
int value = 0;
size_t getNumberFromBytes(char* buffer, int count) {
size_t number = 0;
for (int i = 0; i < count; i++) {
value = value << 8;
value = value | (buffer[i] & 0xFF);
number = number << 8;
number = number | (buffer[i] & 0xFF);
}
return value;
return number;
}

/**
Expand All @@ -52,9 +52,9 @@ namespace Utils {
* @param value - number
* @param count - count bytes
*/
void writeBytesFromInt(char* buffer, size_t value, int count) {
void writeBytesFromNumber(char* buffer, size_t number, int count) {
for (int i = 0; i < count; i++) {
buffer[count - i - 1] = (char) (value >> (i * 8));
buffer[count - i - 1] = (char) (number >> (i * 8));
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <gtest/gtest.h>

#include "../src/Utils.hpp"

class BytesConverter : public ::testing::TestWithParam<std::tuple<size_t, int>> {
public:
size_t value;
int count;

protected:
void SetUp() override {
std::tie(value, count) = GetParam();
}
};

INSTANTIATE_TEST_CASE_P(
CombinationsTest, BytesConverter,
::testing::Combine(
::testing::Values(0U, 1U, 2U, 1000U, 65535U, 2147483647U),
::testing::Values(4, 8, 16)));

TEST_P(BytesConverter, getIntFromBytes) {
char buffer[16] = { 0 };

Utils::writeBytesFromNumber(buffer, value, count);

size_t result = Utils::getNumberFromBytes(buffer, count);

EXPECT_EQ(result, value);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
4 changes: 4 additions & 0 deletions tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.0" targetFramework="native" />
</packages>
Loading

0 comments on commit bf016d0

Please sign in to comment.