tests: Verify build fails with mismatched byte order
authorErica Bugden <ebugden@efficios.com>
Thu, 25 May 2023 20:16:44 +0000 (16:16 -0400)
committerErica Bugden <ebugden@efficios.com>
Tue, 20 Jun 2023 20:14:45 +0000 (16:14 -0400)
This tests the byte order check added in the previous commit `Check that
native byte orders match at compilation`.

Change `byte-order-yes.yaml`:

This test configuration now defines the tracer byte order with
`native-byte-order: big-endian` (instead of
`trace-byte-order: big-endian`) so that this configuration can be used
both for the configuration tests and for the new byte order check test.
The `native-byte-order` property is used so that the C preprocessor
check will be present in the generated C code. The byte order is set to
`big-endian` so that building the generated C will fail on a
little-endian system.

This change does not reduce the test coverage because:
    a. `byte-order-yes.yaml` still tests that using one of the two byte
       order properties is a valid configuration, and
    b. The `trace-byte-order` property is tested via
       `trace-byte-order-big-endian.yaml` in the tracing tests.

Change-Id: Ic2aa7bdc7e56881233efe2e25153dc4e65ba309a
Signed-off-by: Erica Bugden <ebugden@efficios.com>
tests/config/yaml/3/configs/pass/type/byte-order-yes.yaml
tests/config/yaml/3/test_fail_byte_order_check.py [new file with mode: 0644]

index e327c839393452e3840388a4a121f6c84749ccc4..903ca2df9cb4f86809322fb64e02ff11945652d3 100644 (file)
 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 # Tests that configuration parsing succeeds if only one trace type byte
-# order property exists (`trace-byte-order` in this case).
+# order property exists (`native-byte-order` in this case).
 %YAML 1.2
 --- !<tag:barectf.org,2020/3/config>
 trace:
   type:
-    trace-byte-order: big-endian
+    native-byte-order: big-endian
     data-stream-types:
       my_stream:
         $is-default: true
diff --git a/tests/config/yaml/3/test_fail_byte_order_check.py b/tests/config/yaml/3/test_fail_byte_order_check.py
new file mode 100644 (file)
index 0000000..8b8f55d
--- /dev/null
@@ -0,0 +1,68 @@
+# The MIT License (MIT)
+#
+# Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+# Copyright (c) 2023 Erica Bugden <ebugden@efficios.com>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+import pytest
+import os.path
+import barectf
+import subprocess
+
+
+# Tests that the tracer generated by barectf will not build when the
+# configured native byte order (big endian in this case) does not match
+# the system's native byte order (little endian in this case).
+#
+# NOTE: This test does not validate that it runs on a little-endian
+# system, but we can assume that that is the case since it is what the
+# barectf tests currently require.
+def test_byte_order_check(request, tmpdir):
+    yaml_path = os.path.join(os.path.dirname(request.fspath), 'configs',
+                             'pass', 'type', 'byte-order-yes.yaml')
+
+    # Create barectf configuration
+    with open(yaml_path) as f:
+        cfg = barectf.configuration_from_file(f)
+
+    # Generate and write C code files
+    cg = barectf.CodeGenerator(cfg)
+
+    for file in cg.generate_c_headers() + cg.generate_c_sources():
+        with open(os.path.join(tmpdir, file.name), 'w') as f:
+            f.write(file.contents)
+
+    # Verify that building the C code fails because of the byte order
+    # check
+    cc = os.environ.get('CC', 'cc')
+    o_file = 'obj.o'
+
+    expected_output = ("barectf: The native byte order of the target architecture (little "
+                       "endian) doesn't match the configured native byte order (big "
+                       "endian): the generated tracer could produce invalid or corrupted "
+                       "CTF packets. Please make sure that the native byte order in the "
+                       "barectf configuration's trace type object is little endian.")
+
+    with pytest.raises(subprocess.CalledProcessError) as excinfo:
+        subprocess.check_output([cc, '-c', '-o', o_file, 'barectf.c'], cwd=tmpdir,
+                                text=True, stderr=subprocess.STDOUT)
+
+    assert(expected_output in excinfo.value.output)
This page took 0.025854 seconds and 4 git commands to generate.