Roman Khassraf | fa92b61 | 2015-07-29 11:34:31 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 4 | # @author (C) 2015 by Roman Khassraf <rkhassraf@gmail.com> |
Roman Khassraf | fa92b61 | 2015-07-29 11:34:31 +0200 | [diff] [blame] | 5 | # @section LICENSE |
| 6 | # |
| 7 | # Gr-gsm is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 3, or (at your option) |
| 10 | # any later version. |
| 11 | # |
| 12 | # Gr-gsm is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with gr-gsm; see the file COPYING. If not, write to |
| 19 | # the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | # Boston, MA 02110-1301, USA. |
| 21 | # |
| 22 | # |
| 23 | |
| 24 | from gnuradio import gr, gr_unittest, blocks |
Piotr Krysik | 5c7b374 | 2016-06-21 07:12:32 +0200 | [diff] [blame] | 25 | import grgsm_swig as grgsm |
Roman Khassraf | fa92b61 | 2015-07-29 11:34:31 +0200 | [diff] [blame] | 26 | import os |
| 27 | import pmt |
| 28 | import sys |
| 29 | import tempfile |
| 30 | |
| 31 | class qa_message_printer (gr_unittest.TestCase): |
| 32 | |
| 33 | def setUp (self): |
| 34 | self.tb = gr.top_block() |
| 35 | self.tmpfile = tempfile.NamedTemporaryFile() |
| 36 | self.prevfd = os.dup(sys.stdout.fileno()) |
| 37 | os.dup2(self.tmpfile.fileno(), sys.stdout.fileno()) |
| 38 | self.prev = sys.stdout |
| 39 | sys.stdout = os.fdopen(self.prevfd, "w") |
| 40 | |
| 41 | def tearDown (self): |
| 42 | self.tb = None |
| 43 | os.dup2(self.prevfd, self.prev.fileno()) |
| 44 | sys.stdout = self.prev |
| 45 | self.tmpfile.close() |
| 46 | |
| 47 | def getOutput(self): |
| 48 | self.tmpfile.seek(0) |
| 49 | return self.tmpfile.read() |
| 50 | |
| 51 | def getOutputExpected(self, expected_lines): |
| 52 | out = "" |
| 53 | for l in expected_lines: |
| 54 | out = out + l + "\n" |
| 55 | return out |
| 56 | |
| 57 | def test_001_no_prefix_no_header (self): |
| 58 | """ |
| 59 | Four messages, without any prefix, no gsmtap header |
| 60 | """ |
| 61 | msgs_input = [ |
| 62 | "02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 63 | "02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 64 | "02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 65 | "02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 66 | ] |
| 67 | |
| 68 | # there is a whitespace at the beginning of message_printer output |
| 69 | msgs_expected = [ |
| 70 | " 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 71 | " 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 72 | " 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 73 | " 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 74 | ] |
| 75 | |
| 76 | src = grgsm.message_source(msgs_input) |
| 77 | printer = grgsm.message_printer(pmt.intern(""), False) |
| 78 | self.tb.msg_connect(src, "msgs", printer, "msgs") |
| 79 | self.tb.run() |
| 80 | |
| 81 | self.assertEqual(self.getOutput(), self.getOutputExpected(msgs_expected)) |
| 82 | |
| 83 | |
| 84 | def test_002_prefix_no_header (self): |
| 85 | """ |
| 86 | Four messages, with prefix "test_002:", no gsmtap header |
| 87 | """ |
| 88 | msgs_input = [ |
| 89 | "02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 90 | "02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 91 | "02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 92 | "02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 93 | ] |
| 94 | |
| 95 | msgs_expected = [ |
| 96 | "test_002: 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 97 | "test_002: 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 98 | "test_002: 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 99 | "test_002: 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 100 | ] |
| 101 | |
| 102 | src = grgsm.message_source(msgs_input) |
| 103 | printer = grgsm.message_printer(pmt.intern("test_002:"), False) |
| 104 | self.tb.msg_connect(src, "msgs", printer, "msgs") |
| 105 | self.tb.run() |
| 106 | |
| 107 | self.assertEqual(self.getOutput(), self.getOutputExpected(msgs_expected)) |
| 108 | |
| 109 | |
| 110 | def test_003_no_prefix_header (self): |
| 111 | """ |
| 112 | Four messages, without any prefix, with gsmtap header |
| 113 | """ |
| 114 | msgs_input = [ |
| 115 | "02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 116 | "02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 117 | "02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 118 | "02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 119 | ] |
| 120 | |
| 121 | # there is a whitespace at the beginning of message_printer output |
| 122 | msgs_expected = [ |
| 123 | " 02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 124 | " 02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 125 | " 02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 126 | " 02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 127 | ] |
| 128 | |
| 129 | src = grgsm.message_source(msgs_input) |
Piotr Krysik | dd66b3b | 2016-02-21 10:26:44 +0100 | [diff] [blame] | 130 | printer = grgsm.message_printer(pmt.intern(""), False, False, True) |
Roman Khassraf | fa92b61 | 2015-07-29 11:34:31 +0200 | [diff] [blame] | 131 | self.tb.msg_connect(src, "msgs", printer, "msgs") |
| 132 | self.tb.run() |
| 133 | |
| 134 | self.assertEqual(self.getOutput(), self.getOutputExpected(msgs_expected)) |
| 135 | |
| 136 | |
| 137 | def test_004_prefix_header (self): |
| 138 | """ |
| 139 | Four messages, with prefix "test_004:", with gsmtap header |
| 140 | """ |
| 141 | msgs_input = [ |
| 142 | "02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 143 | "02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 144 | "02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 145 | "02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 146 | ] |
| 147 | |
| 148 | # there is a whitespace at the beginning of message_printer output |
| 149 | msgs_expected = [ |
| 150 | "test_004: 02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 151 | "test_004: 02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", |
| 152 | "test_004: 02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", |
| 153 | "test_004: 02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" |
| 154 | ] |
| 155 | |
| 156 | src = grgsm.message_source(msgs_input) |
Piotr Krysik | dd66b3b | 2016-02-21 10:26:44 +0100 | [diff] [blame] | 157 | printer = grgsm.message_printer(pmt.intern("test_004:"), False, False, True) |
Roman Khassraf | fa92b61 | 2015-07-29 11:34:31 +0200 | [diff] [blame] | 158 | self.tb.msg_connect(src, "msgs", printer, "msgs") |
| 159 | self.tb.run() |
Piotr Krysik | dd66b3b | 2016-02-21 10:26:44 +0100 | [diff] [blame] | 160 | |
Roman Khassraf | fa92b61 | 2015-07-29 11:34:31 +0200 | [diff] [blame] | 161 | self.assertEqual(self.getOutput(), self.getOutputExpected(msgs_expected)) |
| 162 | |
| 163 | |
| 164 | if __name__ == '__main__': |
| 165 | gr_unittest.run(qa_message_printer, "qa_message_printer.xml") |
| 166 | |