Skip to content

Commit bf016d0

Browse files
authored
Merge pull request #4 from gistrec/tests
Move tests to master branch
2 parents 405b74a + 7ce4b6d commit bf016d0

File tree

11 files changed

+289
-43
lines changed

11 files changed

+289
-43
lines changed

.circleci/config.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: 2
2+
jobs:
3+
gtest:
4+
working_directory: ~/root
5+
docker:
6+
- image: gcc:latest
7+
steps:
8+
- run:
9+
name: Downloading dependencies
10+
command: |
11+
apt-get update
12+
apt-get install -y cmake
13+
apt-get install -y libgtest-dev
14+
- run:
15+
name: Installing dependencies
16+
working_directory: /usr/src/gtest/
17+
command: |
18+
cmake CMakeLists.txt
19+
make
20+
cp /usr/src/gtest/*.a /usr/lib
21+
- checkout
22+
- run:
23+
name: "Pull Submodules"
24+
command: |
25+
git submodule init
26+
git submodule update --recursive --remote
27+
- run:
28+
name: Building tests
29+
command: make gtests
30+
- run:
31+
name: Running tests
32+
command: ./GTests --gtest_filter=*
33+
workflows:
34+
version: 2
35+
build_and_test:
36+
jobs:
37+
- gtest:
38+
filters:
39+
branches:
40+
only:
41+
- master
42+
- develop
43+
- tests

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ x64/
3737
x86/
3838
Debug/
3939
Release/
40-
FileBroadcaster
40+
packages/

FileBroadcaster.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2047
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FileBroadcaster", "src\FileBroadcaster.vcxproj", "{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}"
77
EndProject
8+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests\tests.vcxproj", "{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|x64 = Debug|x64
@@ -21,6 +23,14 @@ Global
2123
{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x64.Build.0 = Release|x64
2224
{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x86.ActiveCfg = Release|Win32
2325
{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x86.Build.0 = Release|Win32
26+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x64.ActiveCfg = Debug|x64
27+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x64.Build.0 = Debug|x64
28+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x86.ActiveCfg = Debug|Win32
29+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x86.Build.0 = Debug|Win32
30+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x64.ActiveCfg = Release|x64
31+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x64.Build.0 = Release|x64
32+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x86.ActiveCfg = Release|Win32
33+
{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x86.Build.0 = Release|Win32
2434
EndGlobalSection
2535
GlobalSection(SolutionProperties) = preSolution
2636
HideSolutionNode = FALSE

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
all:
1+
program:
22
g++ src/Main.cpp \
33
-std=c++14 -pthread \
44
-Ilib/cxxopts/include \
55
-o FileBroadcaster
6+
7+
gtests:
8+
g++ tests/Tests.cpp \
9+
-std=c++14 -pthread \
10+
-Ilib/cxxopts/include \
11+
-lgtest \
12+
-o GTests

README.md

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
11
# File-Broadcaster
22

3+
<p align="left">
4+
<a href="https://circleci.com/gh/gistrec/File-Broadcaster/tree/master">
5+
<img src="https://img.shields.io/circleci/build/github/gistrec/File-Broadcaster/master" alt="Build status"></a>
6+
<a>
7+
<img src="https://img.shields.io/codacy/grade/4c8169bcab3a4df18baad4e5658ec8ce" alt="Code quality"></a>
8+
<a href="https://github.com/gistrec/File-Broadcaster/releases">
9+
<img src="https://img.shields.io/github/v/release/gistrec/File-Broadcaster" alt="Release"></a>
10+
<a>
11+
<img src="https://img.shields.io/badge/platform-windows%20%7C%20linux-brightgreen" alt="Code quality"></a>
12+
<a href="https://github.com/gistrec/File-Broadcaster/blob/master/LICENSE">
13+
<img src="https://img.shields.io/github/license/gistrec/File-Broadcaster?color=brightgreen" alt="License"></a>
14+
</p>
15+
316
UDP File sender and receiver
417
Can use broadcast address to send file on all computers in LAN
518

6-
719
## Features
820

9-
- Send file to one or all computers in LAN
10-
- Reliability of data transmission
11-
- Server timeout detection
12-
- Change MTU
13-
21+
- Send file to one or all computers in LAN
22+
- Reliability of data transmission
23+
- Server timeout detection
24+
- Change MTU
1425

1526
## Overview
1627

17-
- [Requirements](#requirements)
18-
- [Download](#download)
19-
- [Installation](#installation)
20-
- [Script Parameters](#script-parameters)
21-
- [Packets Specification](#packets-specification)
22-
- [Script Specification](#script-specification)
28+
- [Requirements](#requirements)
29+
- [Download](#download)
30+
- [Installation](#installation)
31+
- [Script Parameters](#script-parameters)
32+
- [Packets Specification](#packets-specification)
33+
- [Script Specification](#script-specification)
2334

24-
# Download
35+
## Download
2536
Clone the [source repository](http://github.com/gistrec/File-Broadcaster) from Github.
26-
* On the command line, enter:
37+
* On the command line, enter:
2738
````
2839
git clone https://github.com/gistrec/File-Broadcaster.git
2940
git submodule init
3041
git submodule update --recursive --remote
3142
````
32-
* 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.
43+
44+
* 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.
3345
3446
3547
## Requirements
36-
* Windows:
37-
* Visual Studio 2015 or 2017
38-
* Linux:
39-
* g++
40-
* pthread
41-
* arpa
48+
* Windows:
49+
* Visual Studio 2015 or 2017
50+
* Linux:
51+
* g++
52+
* pthread
53+
* arpa
4254
4355
4456
4557
## Installation
46-
* Windows
47-
* Open FileBroadcaster.sln via Visual Studio
48-
* Build project
49-
* Linux
50-
* Open a terminal/console/command prompt, change to the directory where you cloned project, and type:
58+
* Windows
59+
* Open FileBroadcaster.sln via Visual Studio
60+
* Build project
61+
* Linux
62+
* Open a terminal/console/command prompt, change to the directory where you cloned project, and type:
5163
````
5264
make all
5365
````

src/Receiver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void checkParts() {
3333
while (ttl && emptyParts.size() > 0) {
3434
for (auto index : emptyParts) {
3535
snprintf(buffer, 7, "RESEND"); //
36-
Utils::writeBytesFromInt(buffer + 6, index, 4); // Create request packet
36+
Utils::writeBytesFromNumber(buffer + 6, index, 4); // Create request packet
3737
sendto(_socket, buffer, 10, 0, (sockaddr*) &broadcast_address,//
3838
sizeof(broadcast_address)); //
3939

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

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

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

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

src/Sender.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ void sendPart(int part_index) {
1616
if (packet_length > mtu) packet_length = mtu; //
1717

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

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

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

@@ -85,7 +85,7 @@ void run(cxxopts::ParseResult &options) {
8585
}
8686

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

9090
auto now = std::chrono::system_clock::now();
9191
auto now_ms = std::chrono::time_point_cast<std::chrono::seconds>(now);

src/Utils.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ namespace Utils {
3737
* @param buffer - bytes array
3838
* @param count - bytes count
3939
*/
40-
int getIntFromBytes(char* buffer, int count) {
41-
int value = 0;
40+
size_t getNumberFromBytes(char* buffer, int count) {
41+
size_t number = 0;
4242
for (int i = 0; i < count; i++) {
43-
value = value << 8;
44-
value = value | (buffer[i] & 0xFF);
43+
number = number << 8;
44+
number = number | (buffer[i] & 0xFF);
4545
}
46-
return value;
46+
return number;
4747
}
4848

4949
/**
@@ -52,9 +52,9 @@ namespace Utils {
5252
* @param value - number
5353
* @param count - count bytes
5454
*/
55-
void writeBytesFromInt(char* buffer, size_t value, int count) {
55+
void writeBytesFromNumber(char* buffer, size_t number, int count) {
5656
for (int i = 0; i < count; i++) {
57-
buffer[count - i - 1] = (char) (value >> (i * 8));
57+
buffer[count - i - 1] = (char) (number >> (i * 8));
5858
}
5959
}
6060
}

tests/Tests.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "../src/Utils.hpp"
4+
5+
class BytesConverter : public ::testing::TestWithParam<std::tuple<size_t, int>> {
6+
public:
7+
size_t value;
8+
int count;
9+
10+
protected:
11+
void SetUp() override {
12+
std::tie(value, count) = GetParam();
13+
}
14+
};
15+
16+
INSTANTIATE_TEST_CASE_P(
17+
CombinationsTest, BytesConverter,
18+
::testing::Combine(
19+
::testing::Values(0U, 1U, 2U, 1000U, 65535U, 2147483647U),
20+
::testing::Values(4, 8, 16)));
21+
22+
TEST_P(BytesConverter, getIntFromBytes) {
23+
char buffer[16] = { 0 };
24+
25+
Utils::writeBytesFromNumber(buffer, value, count);
26+
27+
size_t result = Utils::getNumberFromBytes(buffer, count);
28+
29+
EXPECT_EQ(result, value);
30+
}
31+
32+
int main(int argc, char** argv) {
33+
testing::InitGoogleTest(&argc, argv);
34+
return RUN_ALL_TESTS();
35+
}

tests/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.0" targetFramework="native" />
4+
</packages>

0 commit comments

Comments
 (0)