-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestCommsClient.c
More file actions
102 lines (81 loc) · 4.21 KB
/
testCommsClient.c
File metadata and controls
102 lines (81 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright (C) 2020 Embed Creativity LLC
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include "comms.h"
#define BUF_LEN 1500
#define PLAINTEXT_CLIENT_TO_SERVER_LEN 15 // + NULL terminator
const char szPlaintextClientToServer[] = "Hello, World!\n";
#define PLAINTEXT_SERVER_TO_CLIENT_LEN 13 // + NULL terminator
const char szPlaintextServerToClient[] = "And to you.\n";
#define CIPHERTEXT_CLIENT_TO_SERVER_LEN 44 // + NULL terminator
const char szCiphertextClientToServer[] = "Super Secret Message from Client to Server\n";
#define CIPHERTEXT_SERVER_TO_CLIENT_LEN 41 // + NULL terminator
const char szCiphertextServerToClient[] = "I'll never tell - don't you worry, son.\n";
const char SERVER_ADDRESS[] = "127.0.0.1";
#define SERVER_PORT 52000
extern CommsInterface_t commsIntf;
const uint8_t cryptoKey[] = {
0x86, 0x09, 0x1F, 0x8D, 0x16, 0x11, 0x9F, 0x9A,
0xC9, 0x1A, 0xC2, 0xED, 0x00, 0x8D, 0x08, 0x16
};
static uint8_t buffer[BUF_LEN];
static int32_t bytesReceived = 0;
int main ( void )
{
int hSocketIntf;
printf("INFO: Client Attempting to connect...\n");
hSocketIntf = commsIntf.ConnectToServer(SERVER_ADDRESS, SERVER_PORT);
if ( -1 == hSocketIntf ) {
printf("ERROR: Connection Failed. Exiting Program.\n");
return -1;
}
printf("INFO: Connected!\n");
// Client initiates with a plaintext message
strncpy((char*)buffer, szPlaintextClientToServer, BUF_LEN);
commsIntf.Write(hSocketIntf, buffer, PLAINTEXT_CLIENT_TO_SERVER_LEN); // Send string plus NULL terminator
printf("INFO: Client initiating context by sending Plaintext Test to server - PASSED\n");
// Server responds with another message
bytesReceived = commsIntf.Read(hSocketIntf, buffer, BUF_LEN);
if ( bytesReceived != PLAINTEXT_SERVER_TO_CLIENT_LEN) {
printf("ERROR: Test 1 - Plaintext Server to client failed. %d bytes received, expecting %d.\n", bytesReceived,
PLAINTEXT_SERVER_TO_CLIENT_LEN);
commsIntf.Close(hSocketIntf);
return -1;
} else if ( 0 != (strncmp((char*)buffer, szPlaintextServerToClient, BUF_LEN)) ) {
printf("ERROR: Test 1 - Plaintext server to client failed. Incorrect data received:");
commsIntf.Close(hSocketIntf); // close connection to server
return -1;
}
printf("INFO: Client received Plaintext response from Server - PASSED\n");
strncpy((char*)buffer, szCiphertextClientToServer, BUF_LEN);
commsIntf.SecureWrite(hSocketIntf, buffer, CIPHERTEXT_CLIENT_TO_SERVER_LEN); // Send string plus NULL terminator
printf("INFO: Client sending Ciphertext to server - PASSED\n");
// Server now sends us an encrypted string
bytesReceived = commsIntf.SecureRead(hSocketIntf, buffer, BUF_LEN);
if ( bytesReceived != CIPHERTEXT_SERVER_TO_CLIENT_LEN) {
printf("ERROR: Test 2 - Ciphertext server to client failed. %d bytes received, expecting %d.\n", bytesReceived,
CIPHERTEXT_SERVER_TO_CLIENT_LEN);
commsIntf.Close(hSocketIntf);
return -1;
} else if ( 0 != (strncmp((char*)buffer, szCiphertextServerToClient, BUF_LEN)) ) {
printf("ERROR: Test 2 - Ciphertext server to client failed. Incorrect data received:");
commsIntf.Close(hSocketIntf); // close connection to server
return -1;
}
printf("INFO: Client received Ciphertext response from Server - PASSED\n");
commsIntf.Close(hSocketIntf); // close connection to server
printf("INFO: Client disconnected and shutting down - Test Complete!\n");
return 0;
}