1 /*
2  * Copyright (c) 2013 Balabit
3  * Copyright (c) 2013 Viktor Tusa <tusa@balabit.hu>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * As an additional exemption you are allowed to compile & link against the
19  * OpenSSL libraries as published by the OpenSSL project. See the file
20  * COPYING for details.
21  *
22  */
23 
24 #include "stomp.h"
25 #include <criterion/criterion.h>
26 
27 static void
assert_stomp_header(stomp_frame * frame,char * key,char * value)28 assert_stomp_header(stomp_frame *frame, char *key, char *value)
29 {
30   char *myvalue = g_hash_table_lookup(frame->headers, key);
31 
32   cr_assert_str_eq(myvalue, value, "Stomp header assertion failed!");
33 }
34 
35 static void
assert_stomp_command(stomp_frame * frame,char * command)36 assert_stomp_command(stomp_frame *frame, char *command)
37 {
38   cr_assert_str_eq(frame->command, command, "Stomp command assertion failed");
39 }
40 
41 static void
assert_stomp_body(stomp_frame * frame,char * body)42 assert_stomp_body(stomp_frame *frame, char *body)
43 {
44   cr_assert_str_eq(frame->body, body, "Stomp body assertion failed");
45 }
46 
Test(stomp_proto,test_only_command)47 Test(stomp_proto, test_only_command)
48 {
49   stomp_frame frame;
50 
51   stomp_parse_frame(g_string_new("CONNECTED\n\n"), &frame);
52   assert_stomp_command(&frame, "CONNECTED");
53   stomp_frame_deinit(&frame);
54 }
55 
Test(stomp_proto,test_command_and_data)56 Test(stomp_proto, test_command_and_data)
57 {
58   stomp_frame frame;
59 
60   stomp_parse_frame(g_string_new("CONNECTED\n\nalmafa"), &frame);
61   assert_stomp_command(&frame, "CONNECTED");
62   assert_stomp_body(&frame, "almafa");
63   stomp_frame_deinit(&frame);
64 };
65 
Test(stomp_proto,test_command_and_header_and_data)66 Test(stomp_proto, test_command_and_header_and_data)
67 {
68   stomp_frame frame;
69 
70   stomp_parse_frame(g_string_new("CONNECTED\nheader_name:header_value\n\nbelafa"), &frame);
71   assert_stomp_command(&frame, "CONNECTED");
72   assert_stomp_header(&frame, "header_name", "header_value");
73   assert_stomp_body(&frame, "belafa");
74   stomp_frame_deinit(&frame);
75 };
76 
Test(stomp_proto,test_command_and_header)77 Test(stomp_proto, test_command_and_header)
78 {
79   stomp_frame frame;
80 
81   stomp_parse_frame(g_string_new("CONNECTED\nsession:ID:tusa-38077-1378214843533-2:1\n"), &frame);
82   assert_stomp_command(&frame, "CONNECTED");
83   assert_stomp_header(&frame, "session", "ID:tusa-38077-1378214843533-2:1");
84   stomp_frame_deinit(&frame);
85 };
86 
Test(stomp_proto,test_generate_gstring_from_frame)87 Test(stomp_proto, test_generate_gstring_from_frame)
88 {
89   stomp_frame frame;
90   GString *actual;
91 
92   stomp_frame_init(&frame, "SEND", sizeof("SEND"));
93   stomp_frame_add_header(&frame, "header_name", "header_value");
94   stomp_frame_set_body(&frame, "body", sizeof("body"));
95   actual = create_gstring_from_frame(&frame);
96   cr_assert_str_eq(actual->str, "SEND\nheader_name:header_value\n\nbody", "Generated stomp frame does not match");
97   stomp_frame_deinit(&frame);
98 };
99 
Test(stomp_proto,test_invalid_command)100 Test(stomp_proto, test_invalid_command)
101 {
102   stomp_frame frame;
103 
104   cr_assert_not(stomp_parse_frame(g_string_new("CONNECTED\n no-colon\n"), &frame));
105   stomp_frame_deinit(&frame);
106 };
107