Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / cunit / test_pcap.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * pcap File Format Unit Tests
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/freerdp.h>
21 #include <freerdp/utils/hexdump.h>
22 #include <freerdp/utils/pcap.h>
23
24 #include "test_pcap.h"
25
26 int init_pcap_suite(void)
27 {
28         return 0;
29 }
30
31 int clean_pcap_suite(void)
32 {
33         return 0;
34 }
35
36 int add_pcap_suite(void)
37 {
38         add_test_suite(pcap);
39
40         add_test_function(pcap);
41
42         return 0;
43 }
44
45 uint8 test_packet_1[16] =
46         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
47
48 uint8 test_packet_2[32] =
49         "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
50         "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB";
51
52 uint8 test_packet_3[64] =
53         "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
54         "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
55         "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
56         "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC";
57
58 typedef struct
59 {
60         void* data;
61         uint32 length;
62 } test_packet;
63
64 void test_pcap(void)
65 {
66         rdpPcap* pcap;
67         pcap_record record;
68         test_packet packets[3];
69
70         packets[0].data = test_packet_1;
71         packets[0].length = sizeof(test_packet_1);
72         packets[1].data = test_packet_2;
73         packets[1].length = sizeof(test_packet_2);
74         packets[2].data = test_packet_3;
75         packets[2].length = sizeof(test_packet_3);
76
77         pcap = pcap_open("/tmp/test.pcap", true);
78         pcap_add_record(pcap, test_packet_1, sizeof(test_packet_1));
79         pcap_flush(pcap);
80         pcap_add_record(pcap, test_packet_2, sizeof(test_packet_2));
81         pcap_flush(pcap);
82         pcap_add_record(pcap, test_packet_3, sizeof(test_packet_3));
83         pcap_close(pcap);
84
85         pcap = pcap_open("/tmp/test.pcap", false);
86
87         int i = 0;
88         while (pcap_has_next_record(pcap))
89         {
90                 pcap_get_next_record(pcap, &record);
91                 CU_ASSERT(record.length == packets[i].length)
92                 i++;
93         }
94
95         CU_ASSERT(i == 3);
96
97         pcap_close(pcap);
98 }
99