summaryrefslogtreecommitdiffstats
path: root/tinyMSRP/test/test/test_parser.h
blob: 6a46e497d21412e0d0bd1035bce1a3990c0af63d (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)yahoo.fr>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 DOUBANGO.
*
*/
#ifndef _TEST_MSRPPARSER_H
#define _TEST_MSRPPARSER_H

#include "tinymsrp/headers/tmsrp_header_Dummy.h"

#define MSRP_MSG_REQUEST \
	"MSRP a786hjs2 SEND\r\n" \
	"To-Path: msrp://biloxi.example.com:12763/kjhd37s2s20w2a;tcp\r\n" \
	"From-Path: msrp://atlanta.example.com:7654/jshA7weztas;tcp\r\n" \
	"Message-ID: 87652491\r\n" \
	"Byte-Dummy: 1-25/25\r\n" \
	"Byte-Range: 1-5/3\r\n" \
	"Byte-Range: 1-25/*\r\n" \
	"Byte-Range: 1-*/*\r\n" \
	"Failure-Report: partial\r\n" \
    "Success-Report: yes\r\n" \
	"Content-Type: text/plain;charset=utf8\r\n" \
	"\r\n" \
	"ab\r\nc\r\n" \
	"-------a786hjs2$\r\nMSRP"

#define MSRP_MSG_RESPONSE \
	"MSRP a786hjs2 200 OK\r\n" \
	"To-Path: msrp://atlanta.example.com:7654/jshA7weztas;tcp\r\n" \
	"From-Path: msrp://biloxi.example.com:12763/kjhd37s2s20w2a;tcp\r\n" \
	"-------a786hjs2$\r\nMSRP 000"

#define MSRP_MSG_REPORT \
	"MSRP dkei38sd REPORT\r\n" \
	"To-Path: msrp://alicepc.example.com:7777/iau39soe2843z;tcp\r\n" \
	"From-Path: msrp://bob.example.com:8888/9di4eae923wzd;tcp\r\n" \
	"Message-ID: 12339sdqwer\r\n" \
	"Byte-Range: 1-106/106\r\n" \
	"Status: 000 200 OK\r\n" \
	"-------dkei38sd$\r\nMSRP 000"

#define MSRP_MSG_TO_TEST MSRP_MSG_REQUEST

void test_parser()
{
    tmsrp_message_t *message;
    const tmsrp_header_Dummy_t* header;
    size_t msg_size;
    char* str;

    //
    //	Serialization / Deserialization
    //
    /* deserialize the message */
    if((message = tmsrp_message_parse_2(MSRP_MSG_TO_TEST, strlen(MSRP_MSG_TO_TEST), &msg_size))) {

        tmsrp_message_add_headers(message,
                                  TMSRP_HEADER_DUMMY_VA_ARGS("NS", "imdn <urn:ietf:params:imdn>"),
                                  TMSRP_HEADER_DUMMY_VA_ARGS("imdn.Message-ID", "MsgiQqFZqTYAA"),
                                  TMSRP_HEADER_DUMMY_VA_ARGS("DateTime", "2012-10-25T18:02:08.000Z"),
                                  TMSRP_HEADER_DUMMY_VA_ARGS("imdn.Disposition-Notification", "positive-delivery, display"),

                                  tsk_null);

        if((header = (const tmsrp_header_Dummy_t*)tmsrp_message_get_headerByName(message, "NS"))) {
            TSK_DEBUG_INFO("NS=%s\n", header->value);
        }
        if((header = (const tmsrp_header_Dummy_t*)tmsrp_message_get_headerByName(message, "imdn.Message-ID"))) {
            TSK_DEBUG_INFO("imdn.Message-ID=%s\n", header->value);
        }

        /* serialize the message */
        if((str = tmsrp_message_tostring(message))) {
            TSK_DEBUG_INFO("\nMSRP Message=\n%s\n\n", str);
            TSK_FREE(str);
        }
    }
    else {
        TSK_DEBUG_ERROR("Failed to parse MSRP message(1).");
    }
    TSK_OBJECT_SAFE_FREE(message);

    //
    //	Create Response from Request
    //
    if((message = tmsrp_message_parse(MSRP_MSG_REQUEST, strlen(MSRP_MSG_REQUEST)))) {
        tmsrp_response_t* response = tmsrp_create_response(message, 202, "Accepted");

        if((str = tmsrp_message_tostring(response))) {
            TSK_DEBUG_INFO("\nMSRP Response=\n%s\n\n", str);
            TSK_FREE(str);
        }

        TSK_OBJECT_SAFE_FREE(response);
    }
    else {
        TSK_DEBUG_ERROR("Failed to parse MSRP message(2).");
    }
    TSK_OBJECT_SAFE_FREE(message);

    //
    //	Create Report from Request
    //
    if((message = tmsrp_message_parse(MSRP_MSG_REQUEST, strlen(MSRP_MSG_REQUEST)))) {
        tmsrp_request_t* report = tmsrp_create_report(message, 403, "Stop-sending-message");

        if((str = tmsrp_message_tostring(report))) {
            TSK_DEBUG_INFO("\nMSRP Response=\n%s\n\n", str);
            TSK_FREE(str);
        }

        TSK_OBJECT_SAFE_FREE(report);
    }
    else {
        TSK_DEBUG_ERROR("Failed to parse MSRP message(2).");
    }
    TSK_OBJECT_SAFE_FREE(message);

    //
    // Create bodiless Request
    //
    {
        tmsrp_request_t* bodiless = tmsrp_create_bodiless(tsk_null, tsk_null);

        if((str = tmsrp_message_tostring(bodiless))) {
            TSK_DEBUG_INFO("\nMSRP Bodiless=\n%s\n\n", str);
            TSK_FREE(str);
        }

        TSK_OBJECT_SAFE_FREE(bodiless);
    }
}

#endif /* _TEST_MSRPPARSER_H */
OpenPOWER on IntegriCloud