| 1 |
#!/usr/bin/python3 |
| 2 |
# GDBus++ - glib2 GDBus C++ wrapper |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: AGPL-3.0-only |
| 5 |
# |
| 6 |
# Copyright (C) OpenVPN Inc <[email protected]> |
| 7 |
# Copyright (C) David Sommerseth <[email protected]> |
| 8 |
# |
| 9 |
|
| 10 |
## |
| 11 |
# @file tests/scripts/test-signals |
| 12 |
# |
| 13 |
# @brief Runs two programs, one subscribing to specific signals and the |
| 14 |
# other one emiting signals. Checking that the content and right |
| 15 |
# amount of messages are received correctly |
| 16 |
# |
| 17 |
|
| 18 |
import os |
| 19 |
import subprocess |
| 20 |
import sys |
| 21 |
from time import sleep |
| 22 |
|
| 23 |
|
| 24 |
def prog(bin_name): |
| 25 |
build_dir = os.getenv('BUILD_DIR') |
| 26 |
if build_dir is None: |
| 27 |
build_dir='.' |
| 28 |
return build_dir + '/' + bin_name |
| 29 |
|
| 30 |
|
| 31 |
def parse_result(res, expect_fail, timeout=2): |
| 32 |
out = '' |
| 33 |
err = '' |
| 34 |
try: |
| 35 |
res.wait(timeout=timeout) |
| 36 |
except subprocess.TimeoutExpired: |
| 37 |
pass |
| 38 |
|
| 39 |
try: |
| 40 |
o, e = res.communicate(timeout=timeout*2) |
| 41 |
if o: |
| 42 |
out = out + o.decode('utf-8') |
| 43 |
if e: |
| 44 |
err = err + e.decode('utf-8') |
| 45 |
except subprocess.TimeoutExpired: |
| 46 |
pass |
| 47 |
|
| 48 |
ec = res.returncode |
| 49 |
if (expect_fail == False and ec > 0) \ |
| 50 |
or (expect_fail == True and ec == 0): |
| 51 |
print('~'*40) |
| 52 |
print('FAILED: {}'.format(" ".join(res.args))) |
| 53 |
print(' >> Expected {}, exit code: {}'.format(expect_fail and "failure" or "success", ec)) |
| 54 |
print(out) |
| 55 |
print('-'*20) |
| 56 |
print(err) |
| 57 |
print('~'*40) |
| 58 |
return False |
| 59 |
return ec == 0 |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def run_signal_subscribe(args): |
| 64 |
all_args = [prog('test_signal-subscribe'), '-p', '/gdbuspp/tests/signals', '-i', 'gdbuspp.test.signals'] + args |
| 65 |
print('signal subscribe: {}'.format(' '.join(all_args))) |
| 66 |
ret = subprocess.Popen(all_args, bufsize=0, stdout=subprocess.PIPE) |
| 67 |
sleep(1) |
| 68 |
return ret |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def run_signal_emit(args, expect_fail, timeout=None): |
| 73 |
build_dir = os.getenv('BUILD_DIR') |
| 74 |
if build_dir is None: |
| 75 |
build_dir='.' |
| 76 |
|
| 77 |
all_args = [prog('test_signal-emit')] + args |
| 78 |
print('signal emit: {}'.format(' '.join(all_args))) |
| 79 |
p = subprocess.run(all_args + ['-q'], stdout=subprocess.PIPE, timeout=timeout) |
| 80 |
if (expect_fail == False and p.returncode > 0) \ |
| 81 |
or (expect_fail == True and p.returncode == 0): |
| 82 |
print('~'*40) |
| 83 |
print('FAILED: {}'.format(" ".join(all_args))) |
| 84 |
print(' >> Expected {}, exit code: {}'.format(expect_fail and "failure" or "success", p.returncode)) |
| 85 |
print(p.stdout.decode('utf-8')) |
| 86 |
print('~'*40) |
| 87 |
return False |
| 88 |
return True |
| 89 |
|
| 90 |
|
| 91 |
def run_signal_signal(args, expect_fail, timeout=None): |
| 92 |
build_dir = os.getenv('BUILD_DIR') |
| 93 |
if build_dir is None: |
| 94 |
build_dir='.' |
| 95 |
|
| 96 |
all_args = [prog('test_signal-signal')] + args |
| 97 |
print('signal emit: {}'.format(' '.join(all_args))) |
| 98 |
p = subprocess.run(all_args + ['-q'], stdout=subprocess.PIPE, timeout=timeout) |
| 99 |
if (expect_fail == False and p.returncode > 0) \ |
| 100 |
or (expect_fail == True and p.returncode == 0): |
| 101 |
print('~'*40) |
| 102 |
print('FAILED: {}'.format(" ".join(all_args))) |
| 103 |
print(' >> Expected {}, exit code: {}'.format(expect_fail and "failure" or "success", p.returncode)) |
| 104 |
print(p.stdout.decode('utf-8')) |
| 105 |
print('~'*40) |
| 106 |
return False |
| 107 |
return True |
| 108 |
|
| 109 |
|
| 110 |
def run_signal_group(args, expect_fail, timeout=None): |
| 111 |
build_dir = os.getenv('BUILD_DIR') |
| 112 |
if build_dir is None: |
| 113 |
build_dir='.' |
| 114 |
|
| 115 |
all_args = [prog('test_signal-group')] + args |
| 116 |
print('signal emit: {}'.format(' '.join(all_args))) |
| 117 |
p = subprocess.run(all_args + ['-q'], stdout=subprocess.PIPE, timeout=timeout) |
| 118 |
if (expect_fail == False and p.returncode > 0) \ |
| 119 |
or (expect_fail == True and p.returncode == 0): |
| 120 |
print('~'*40) |
| 121 |
print('FAILED: {}'.format(" ".join(all_args))) |
| 122 |
print(' >> Expected {}, exit code: {}'.format(expect_fail and "failure" or "success", p.returncode)) |
| 123 |
print(p.stdout.decode('utf-8')) |
| 124 |
print('~'*40) |
| 125 |
return False |
| 126 |
return True |
| 127 |
|
| 128 |
|
| 129 |
errors = 0 |
| 130 |
|
| 131 |
|
| 132 |
# Single basic string |
| 133 |
s = run_signal_subscribe(['-X','s','-x','Basic test, simple string','-s','BasicTest','-C','1']) |
| 134 |
err = run_signal_emit(['-t','s','-v','Basic test, simple string','-s','BasicTest'], False) |
| 135 |
if not (parse_result(s, False) or err): |
| 136 |
errors = errors + 1 |
| 137 |
|
| 138 |
# Receive unexpected data type in signal |
| 139 |
s = run_signal_subscribe(['-X','s','-x','Basic test, simple string','-s','WrongType','-C','1']) |
| 140 |
err = run_signal_emit(['-t','b','-v','1','-s','WrongType'], False) |
| 141 |
if not (parse_result(s, True) or err): |
| 142 |
errors = errors + 1 |
| 143 |
|
| 144 |
# Send 10.000 signals with structured data |
| 145 |
s = run_signal_subscribe(['-X','sub','-x','Structured data test','-x','3141592653','-x','true','-s','StructSignal', '-C', '10000']) |
| 146 |
err = run_signal_emit(['-t','sub','-v','Structured data test','-v','3141592653','-v','1','-s','StructSignal','-r','7000'], False) |
| 147 |
err2 = run_signal_emit(['-t','sub','-v','Structured data test','-v','3141592653','-v','1','-s','StructSignal','-r','3000'], False) |
| 148 |
if not (parse_result(s, False) or err or err2): |
| 149 |
errors = errors + 1 |
| 150 |
|
| 151 |
|
| 152 |
# Test the Signals::Signal API |
| 153 |
s = run_signal_subscribe(['-X', 'sbu', '-x' , 'Test Signal 1', '-x', 'false', '-x', '101', '-C', '1']) |
| 154 |
err = run_signal_signal([], False) |
| 155 |
if (not (parse_result(s, True) or err)): |
| 156 |
errors = errors + 1 |
| 157 |
|
| 158 |
# |
| 159 |
# Tests the DBus::Signal::Group API |
| 160 |
# |
| 161 |
|
| 162 |
# Test case 1 - Info signal |
| 163 |
s = run_signal_subscribe(['-s','Info','-X','is','-x','1','-x', 'Testing Info signal','-C','1']) |
| 164 |
err = run_signal_group(['-l','info'], False) |
| 165 |
if not (parse_result(s, False) or err): |
| 166 |
errors = errors + 1 |
| 167 |
|
| 168 |
# Test case 2 - Error signal, with more details |
| 169 |
s = run_signal_subscribe(['-s','Error','-X','uss','-x','2','-x', 'Error signal test', '-x', 'signal-group','-C','1']) |
| 170 |
err = run_signal_group(['-l','error'], False) |
| 171 |
if not (parse_result(s, False) or err): |
| 172 |
errors = errors + 1 |
| 173 |
|
| 174 |
# Test case 3 - Debug signal, with even more details |
| 175 |
s = run_signal_subscribe(['-s','Debug','-X','tsss','-x','3','-x', 'A debug message', '-x', 'With details here', '-x', 'signal-group', '-C','1']) |
| 176 |
err = run_signal_group(['-l','debug'], False) |
| 177 |
if not (parse_result(s, False) or err): |
| 178 |
errors = errors + 1 |
| 179 |
|
| 180 |
# Test case 4 - Try sending a signal with the wrong data type - stopped before being sent |
| 181 |
if not run_signal_group(['-l','invalid'], False): |
| 182 |
errors = errors + 1 |
| 183 |
|
| 184 |
|
| 185 |
# |
| 186 |
# Wrap up the test results |
| 187 |
# |
| 188 |
print('-'*40) |
| 189 |
if errors > 0: |
| 190 |
print('Overall test result: FAIL ... {} errors'.format(errors)) |
| 191 |
sys.exit(2) |
| 192 |
else: |
| 193 |
print('Overall test result: Pass') |
| 194 |
sys.exit(0) |