File: grammar-tester.cpp

package info (click to toggle)
belr 5.3.105-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: cpp: 3,172; makefile: 14
file content (130 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download
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
/*
 * Copyright (c) 2016-2019 Belledonne Communications SARL.
 *
 * This file is part of belr - a language recognition library for ABNF-defined grammars.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "bctoolbox/logging.h"
#include "belr-tester.h"
#include <cstdio>

using namespace ::std;
using namespace ::belr;

static bool parseMessage(shared_ptr<Grammar> grammar, const string &message) {
	shared_ptr<DebugParser> parser = make_shared<DebugParser>(grammar);
	parser->setObservedRules({"sip-message"});
	size_t pos = 0;
	shared_ptr<DebugElement> elem = parser->parseInput("sip-message", message, &pos);
	BC_ASSERT_TRUE(elem != nullptr);
	if (!elem) return FALSE;
	BC_ASSERT_EQUAL((int)pos, (int)message.size(), int, "%i");
	BC_ASSERT_TRUE(message == elem->getValue());

	return message.size() == pos && message == elem->getValue();
}

static void sipgrammar_save_and_load(void) {

	string grammarToParse = bcTesterRes("sipgrammar.txt");
	string grammarDump = bcTesterFile("grammarDump.bin");
	string sipmessage = openFile(bcTesterRes("register.txt"));

	remove(grammarDump.c_str());

	BC_ASSERT_TRUE(sipmessage.size() > 0);

	ABNFGrammarBuilder builder;

	// Read grammar put it in object grammar
	auto start = std::chrono::high_resolution_clock::now();
	shared_ptr<Grammar> grammar = builder.createFromAbnfFile(grammarToParse, make_shared<CoreRules>());
	auto finish = std::chrono::high_resolution_clock::now();
	std::chrono::duration<double> elapsedFirst = finish - start;

	BC_ASSERT_FALSE(!grammar);

	if (!grammar) return;

	// make sure we're able to parse a SIP message with the created grammar
	BC_ASSERT_TRUE(parseMessage(grammar, sipmessage));

	// Save grammar

	grammar->save(grammarDump);

	// Load grammar
	start = std::chrono::high_resolution_clock::now();
	shared_ptr<Grammar> loadedGram = make_shared<Grammar>("loaded");
	BC_ASSERT_TRUE(loadedGram->load(grammarDump) == 0);
	finish = std::chrono::high_resolution_clock::now();
	std::chrono::duration<double> elapsedSecond = finish - start;

	// Make sure the loaded from binary file grammar is still able to parse the sip message:
	BC_ASSERT_TRUE(parseMessage(loadedGram, sipmessage));

	BC_ASSERT_TRUE(elapsedFirst.count() > 2 * elapsedSecond.count());
}

/**
 * In SIP grammar, we have:
 * header-name = token
 * This kind of rules were causing issues in previous version of belr because "header-name" rule could not be attached
 * with a parser handler, as the recognizer behind it was named "token".
 */
static void aliases_rules(void) {
	string grammarToParse = bcTesterRes("sipgrammar.txt");
	string sipmessage = openFile(bcTesterRes("response.txt"));

	BC_ASSERT_TRUE(sipmessage.size() > 0);

	ABNFGrammarBuilder builder;

	// Read grammar put it in object grammar
	shared_ptr<Grammar> grammar = builder.createFromAbnfFile(grammarToParse, make_shared<CoreRules>());

	BC_ASSERT_FALSE(!grammar);

	if (!grammar) return;

	shared_ptr<DebugParser> parser = make_shared<DebugParser>(grammar);
	parser->setObservedRules({"sip-message", "header-name"});
	size_t pos = 0;
	shared_ptr<DebugElement> elem = parser->parseInput("sip-message", sipmessage, &pos);
	BC_ASSERT_TRUE(elem != nullptr);
	if (!elem) return;
	BC_ASSERT_EQUAL((int)pos, (int)sipmessage.size(), int, "%i");
	BC_ASSERT_TRUE(sipmessage == elem->getValue());
	list<shared_ptr<DebugElement>> headerNames;
	elem->findChildren("header-name", headerNames);
	BC_ASSERT_TRUE(headerNames.size() == 1);
	if (!headerNames.empty()) {
		BC_ASSERT_TRUE(headerNames.front()->getValue() == "Custom-header");
	}
}

static void test_grammar_loader(void) {
	GrammarLoader &loader = GrammarLoader::get();
	loader.addPath(std::string(bc_tester_get_resource_dir_prefix()));
	shared_ptr<Grammar> grammar = loader.load("belr-grammar-example.blr");
	BC_ASSERT_PTR_NOT_NULL(grammar);
}

static test_t tests[] = {TEST_NO_TAG("SIP grammar save and load", sipgrammar_save_and_load),
                         TEST_NO_TAG("SIP grammar with aliases rules", aliases_rules),
                         TEST_NO_TAG("Grammar loader", test_grammar_loader)};

test_suite_t grammar_suite = {"Grammar", NULL, NULL, NULL, NULL, sizeof(tests) / sizeof(tests[0]), tests, 0, 0};