Move tests to relevant subprojects (#615)
- Moved test projects to the parent project they belong to - Improved test stability/corectness - Improved recursive directory deletion by safely ignoring current- and parent-directory entries. - Update docs
This commit is contained in:
committed by
GitHub
parent
d6b1d15e56
commit
f943c4dd69
@@ -3,12 +3,12 @@ project(tests)
|
||||
set(DOCTESTINC ${PROJECT_SOURCE_DIR}/Doctest/Include)
|
||||
|
||||
enable_testing()
|
||||
add_subdirectory(service-module)
|
||||
add_subdirectory(TactilityFreeRtos)
|
||||
add_subdirectory(TactilityKernel)
|
||||
add_subdirectory(Tactility)
|
||||
add_subdirectory(crypt-module)
|
||||
add_subdirectory(app-module)
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/Modules/service-module/tests ${CMAKE_CURRENT_BINARY_DIR}/service-module)
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/TactilityFreeRtos/Tests ${CMAKE_CURRENT_BINARY_DIR}/TactilityFreeRtos)
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/TactilityKernel/tests ${CMAKE_CURRENT_BINARY_DIR}/TactilityKernel)
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/Tactility/Tests ${CMAKE_CURRENT_BINARY_DIR}/Tactility)
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/Modules/crypt-module/tests ${CMAKE_CURRENT_BINARY_DIR}/crypt-module)
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/Modules/app-module/tests ${CMAKE_CURRENT_BINARY_DIR}/app-module)
|
||||
|
||||
add_custom_target(build-tests)
|
||||
add_dependencies(build-tests ServiceModuleTests)
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
project(TactilityTests)
|
||||
|
||||
enable_language(C CXX ASM)
|
||||
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
|
||||
add_executable(TactilityTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(TactilityTests PRIVATE ${DOCTESTINC})
|
||||
|
||||
add_test(NAME TactilityTests COMMAND TactilityTests)
|
||||
|
||||
target_link_libraries(TactilityTests PRIVATE
|
||||
Tactility
|
||||
TactilityKernel
|
||||
platform-posix
|
||||
lvgl-module
|
||||
lvgl-window-manager-module
|
||||
app-module
|
||||
crypt-module
|
||||
gps-module
|
||||
gps-generic-module
|
||||
gps-meshtastic-module
|
||||
service-module
|
||||
lvgl
|
||||
SDL2::SDL2-static SDL2-static
|
||||
)
|
||||
@@ -1,675 +0,0 @@
|
||||
# GNU GENERAL PUBLIC LICENSE
|
||||
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
<https://fsf.org/>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this
|
||||
license document, but changing it is not allowed.
|
||||
|
||||
## Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom
|
||||
to share and change all versions of a program--to make sure it remains
|
||||
free software for all its users. We, the Free Software Foundation, use
|
||||
the GNU General Public License for most of our software; it applies
|
||||
also to any other work released this way by its authors. You can apply
|
||||
it to your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you
|
||||
have certain responsibilities if you distribute copies of the
|
||||
software, or if you modify it: responsibilities to respect the freedom
|
||||
of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the
|
||||
manufacturer can do so. This is fundamentally incompatible with the
|
||||
aim of protecting users' freedom to change the software. The
|
||||
systematic pattern of such abuse occurs in the area of products for
|
||||
individuals to use, which is precisely where it is most unacceptable.
|
||||
Therefore, we have designed this version of the GPL to prohibit the
|
||||
practice for those products. If such problems arise substantially in
|
||||
other domains, we stand ready to extend this provision to those
|
||||
domains in future versions of the GPL, as needed to protect the
|
||||
freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish
|
||||
to avoid the special danger that patents applied to a free program
|
||||
could make it effectively proprietary. To prevent this, the GPL
|
||||
assures that patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
## TERMS AND CONDITIONS
|
||||
|
||||
### 0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds
|
||||
of works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of
|
||||
an exact copy. The resulting work is called a "modified version" of
|
||||
the earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user
|
||||
through a computer network, with no transfer of a copy, is not
|
||||
conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices" to
|
||||
the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
### 1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work for
|
||||
making modifications to it. "Object code" means any non-source form of
|
||||
a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users can
|
||||
regenerate automatically from other parts of the Corresponding Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that same
|
||||
work.
|
||||
|
||||
### 2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not convey,
|
||||
without conditions so long as your license otherwise remains in force.
|
||||
You may convey covered works to others for the sole purpose of having
|
||||
them make modifications exclusively for you, or provide you with
|
||||
facilities for running those works, provided that you comply with the
|
||||
terms of this License in conveying all material for which you do not
|
||||
control copyright. Those thus making or running the covered works for
|
||||
you must do so exclusively on your behalf, under your direction and
|
||||
control, on terms that prohibit them from making any copies of your
|
||||
copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under the
|
||||
conditions stated below. Sublicensing is not allowed; section 10 makes
|
||||
it unnecessary.
|
||||
|
||||
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such
|
||||
circumvention is effected by exercising rights under this License with
|
||||
respect to the covered work, and you disclaim any intention to limit
|
||||
operation or modification of the work as a means of enforcing, against
|
||||
the work's users, your or third parties' legal rights to forbid
|
||||
circumvention of technological measures.
|
||||
|
||||
### 4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
### 5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these
|
||||
conditions:
|
||||
|
||||
- a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
- b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under
|
||||
section 7. This requirement modifies the requirement in section 4
|
||||
to "keep intact all notices".
|
||||
- c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
- d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
### 6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms of
|
||||
sections 4 and 5, provided that you also convey the machine-readable
|
||||
Corresponding Source under the terms of this License, in one of these
|
||||
ways:
|
||||
|
||||
- a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
- b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the Corresponding
|
||||
Source from a network server at no charge.
|
||||
- c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
- d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
- e) Convey the object code using peer-to-peer transmission,
|
||||
provided you inform other peers where the object code and
|
||||
Corresponding Source of the work are being offered to the general
|
||||
public at no charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal,
|
||||
family, or household purposes, or (2) anything designed or sold for
|
||||
incorporation into a dwelling. In determining whether a product is a
|
||||
consumer product, doubtful cases shall be resolved in favor of
|
||||
coverage. For a particular product received by a particular user,
|
||||
"normally used" refers to a typical or common use of that class of
|
||||
product, regardless of the status of the particular user or of the way
|
||||
in which the particular user actually uses, or expects or is expected
|
||||
to use, the product. A product is a consumer product regardless of
|
||||
whether the product has substantial commercial, industrial or
|
||||
non-consumer uses, unless such uses represent the only significant
|
||||
mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to
|
||||
install and execute modified versions of a covered work in that User
|
||||
Product from a modified version of its Corresponding Source. The
|
||||
information must suffice to ensure that the continued functioning of
|
||||
the modified object code is in no case prevented or interfered with
|
||||
solely because modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or
|
||||
updates for a work that has been modified or installed by the
|
||||
recipient, or for the User Product in which it has been modified or
|
||||
installed. Access to a network may be denied when the modification
|
||||
itself materially and adversely affects the operation of the network
|
||||
or violates the rules and protocols for communication across the
|
||||
network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
### 7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders
|
||||
of that material) supplement the terms of this License with terms:
|
||||
|
||||
- a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
- b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
- c) Prohibiting misrepresentation of the origin of that material,
|
||||
or requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
- d) Limiting the use for publicity purposes of names of licensors
|
||||
or authors of the material; or
|
||||
- e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
- f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions
|
||||
of it) with contractual assumptions of liability to the recipient,
|
||||
for any liability that these contractual assumptions directly
|
||||
impose on those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions; the
|
||||
above requirements apply either way.
|
||||
|
||||
### 8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your license
|
||||
from a particular copyright holder is reinstated (a) provisionally,
|
||||
unless and until the copyright holder explicitly and finally
|
||||
terminates your license, and (b) permanently, if the copyright holder
|
||||
fails to notify you of the violation by some reasonable means prior to
|
||||
60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
### 9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or run
|
||||
a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
### 10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
### 11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims owned
|
||||
or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within the
|
||||
scope of its coverage, prohibits the exercise of, or is conditioned on
|
||||
the non-exercise of one or more of the rights that are specifically
|
||||
granted under this License. You may not convey a covered work if you
|
||||
are a party to an arrangement with a third party that is in the
|
||||
business of distributing software, under which you make payment to the
|
||||
third party based on the extent of your activity of conveying the
|
||||
work, and under which the third party grants, to any of the parties
|
||||
who would receive the covered work from you, a discriminatory patent
|
||||
license (a) in connection with copies of the covered work conveyed by
|
||||
you (or copies made from those copies), or (b) primarily for and in
|
||||
connection with specific products or compilations that contain the
|
||||
covered work, unless you entered into that arrangement, or that patent
|
||||
license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
### 12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under
|
||||
this License and any other pertinent obligations, then as a
|
||||
consequence you may not convey it at all. For example, if you agree to
|
||||
terms that obligate you to collect a royalty for further conveying
|
||||
from those to whom you convey the Program, the only way you could
|
||||
satisfy both those terms and this License would be to refrain entirely
|
||||
from conveying the Program.
|
||||
|
||||
### 13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
### 14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in
|
||||
detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies that a certain numbered version of the GNU General Public
|
||||
License "or any later version" applies to it, you have the option of
|
||||
following the terms and conditions either of that numbered version or
|
||||
of any later version published by the Free Software Foundation. If the
|
||||
Program does not specify a version number of the GNU General Public
|
||||
License, you may choose any version ever published by the Free
|
||||
Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future versions
|
||||
of the GNU General Public License can be used, that proxy's public
|
||||
statement of acceptance of a version permanently authorizes you to
|
||||
choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
### 15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
|
||||
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
|
||||
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
|
||||
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
|
||||
CORRECTION.
|
||||
|
||||
### 16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
|
||||
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
||||
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
|
||||
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
|
||||
LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
|
||||
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
|
||||
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
### 17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
## How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these
|
||||
terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest to
|
||||
attach them to the start of each source file to most effectively state
|
||||
the exclusion of warranty; and each file should have at least the
|
||||
"copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper
|
||||
mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands \`show w' and \`show c' should show the
|
||||
appropriate parts of the General Public License. Of course, your
|
||||
program's commands might be different; for a GUI interface, you would
|
||||
use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. For more information on this, and how to apply and follow
|
||||
the GNU GPL, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your
|
||||
program into proprietary programs. If your program is a subroutine
|
||||
library, you may consider it more useful to permit linking proprietary
|
||||
applications with the library. If this is what you want to do, use the
|
||||
GNU Lesser General Public License instead of this License. But first,
|
||||
please read <https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -1,14 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("findOrCreateDirectory can create a directory tree without prefix") {
|
||||
CHECK_EQ(file::findOrCreateDirectory("test1/test1", 0777), true);
|
||||
// TODO: delete dirs
|
||||
}
|
||||
|
||||
TEST_CASE("findOrCreateDirectory can create a directory tree with prefix") {
|
||||
CHECK_EQ(file::findOrCreateDirectory("/tmp/test2", 0777), true);
|
||||
// TODO: delete dirs
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
#include "doctest.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/dts.h>
|
||||
#include <tactility/kernel_init.h>
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
int result;
|
||||
} TestTaskData;
|
||||
|
||||
// From the relevant platform
|
||||
extern "C" struct Module platform_posix_module;
|
||||
|
||||
void test_task(void* parameter) {
|
||||
auto* data = (TestTaskData*)parameter;
|
||||
|
||||
doctest::Context context;
|
||||
|
||||
context.applyCommandLine(data->argc, data->argv);
|
||||
|
||||
// overrides
|
||||
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||
|
||||
Module* dts_modules[] = { &platform_posix_module, nullptr };
|
||||
DtsDevice dts_devices[] = { DTS_DEVICE_TERMINATOR };
|
||||
check(kernel_init(dts_modules, dts_devices) == ERROR_NONE);
|
||||
|
||||
data->result = context.run();
|
||||
|
||||
vTaskEndScheduler();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TestTaskData data = {
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.result = 0
|
||||
};
|
||||
|
||||
BaseType_t task_result = xTaskCreate(
|
||||
test_task,
|
||||
"test_task",
|
||||
8192,
|
||||
&data,
|
||||
1,
|
||||
nullptr
|
||||
);
|
||||
assert(task_result == pdPASS);
|
||||
|
||||
vTaskStartScheduler();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/file/ObjectFile.h>
|
||||
|
||||
using tt::file::ObjectFileWriter;
|
||||
using tt::file::ObjectFileReader;
|
||||
|
||||
constexpr const char* TEMP_FILE = "test.tmp";
|
||||
|
||||
struct TestStruct {
|
||||
uint32_t value;
|
||||
};
|
||||
|
||||
TEST_CASE("Writing and reading multiple records to a file") {
|
||||
ObjectFileWriter writer = ObjectFileWriter(TEMP_FILE, sizeof(TestStruct), 1, false);
|
||||
|
||||
TestStruct record_out_1 = { .value = 0xAAAAAAAA };
|
||||
TestStruct record_out_2 = { .value = 0xBBBBBBBB };
|
||||
|
||||
CHECK_EQ(writer.open(), true);
|
||||
CHECK_EQ(writer.write(&record_out_1), true);
|
||||
CHECK_EQ(writer.write(&record_out_2), true);
|
||||
writer.close();
|
||||
|
||||
TestStruct record_in;
|
||||
ObjectFileReader reader = ObjectFileReader(TEMP_FILE, sizeof(TestStruct));
|
||||
CHECK_EQ(reader.open(), true);
|
||||
CHECK_EQ(reader.hasNext(), true);
|
||||
CHECK_EQ(reader.readNext(&record_in), true);
|
||||
CHECK_EQ(reader.hasNext(), true);
|
||||
CHECK_EQ(reader.readNext(&record_in), true);
|
||||
CHECK_EQ(reader.hasNext(), false);
|
||||
reader.close();
|
||||
|
||||
remove(TEMP_FILE);
|
||||
}
|
||||
|
||||
TEST_CASE("Appending records to a file") {
|
||||
remove(TEMP_FILE);
|
||||
|
||||
ObjectFileWriter writer = ObjectFileWriter(TEMP_FILE, sizeof(TestStruct), 1, false);
|
||||
|
||||
TestStruct record_out_1 = { .value = 0xAAAAAAAA };
|
||||
TestStruct record_out_2 = { .value = 0xBBBBBBBB };
|
||||
|
||||
CHECK_EQ(writer.open(), true);
|
||||
CHECK_EQ(writer.write(&record_out_1), true);
|
||||
writer.close();
|
||||
|
||||
ObjectFileWriter appender = ObjectFileWriter(TEMP_FILE, sizeof(TestStruct), 1, true);
|
||||
CHECK_EQ(appender.open(), true);
|
||||
CHECK_EQ(appender.write(&record_out_2), true);
|
||||
appender.close();
|
||||
|
||||
TestStruct record_in;
|
||||
ObjectFileReader reader = ObjectFileReader(TEMP_FILE, sizeof(TestStruct));
|
||||
CHECK_EQ(reader.open(), true);
|
||||
CHECK_EQ(reader.hasNext(), true);
|
||||
CHECK_EQ(reader.readNext(&record_in), true);
|
||||
CHECK_EQ(record_in.value, 0xAAAAAAAA);
|
||||
CHECK_EQ(reader.hasNext(), true);
|
||||
CHECK_EQ(reader.readNext(&record_in), true);
|
||||
CHECK_EQ(record_in.value, 0xBBBBBBBB);
|
||||
CHECK_EQ(reader.hasNext(), false);
|
||||
reader.close();
|
||||
|
||||
remove(TEMP_FILE);
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/StringUtils.h>
|
||||
|
||||
// region split
|
||||
|
||||
TEST_CASE("splitting an empty string results in an empty vector") {
|
||||
auto result = tt::string::split("", ".");
|
||||
CHECK_EQ(result.empty(), true);
|
||||
}
|
||||
|
||||
TEST_CASE("splitting a string with a single token results in a vector with that token") {
|
||||
auto result = tt::string::split("token", ".");
|
||||
CHECK_EQ(result.size(), 1);
|
||||
CHECK_EQ(result.front(), "token");
|
||||
}
|
||||
|
||||
TEST_CASE("splitting a string with multiple tokens results in a vector with those tokens") {
|
||||
auto result = tt::string::split("token1;token2;token3;", ";");
|
||||
CHECK_EQ(result.size(), 3);
|
||||
CHECK_EQ(result[0], "token1");
|
||||
CHECK_EQ(result[1], "token2");
|
||||
CHECK_EQ(result[2], "token3");
|
||||
}
|
||||
|
||||
// endregion split
|
||||
|
||||
// region join
|
||||
|
||||
TEST_CASE("joining an empty vector results in an empty string") {
|
||||
std::vector<std::string> tokens = {};
|
||||
auto result = tt::string::join(tokens, ".");
|
||||
CHECK_EQ(result, "");
|
||||
}
|
||||
|
||||
TEST_CASE("joining a single token results in a string with that value") {
|
||||
std::vector<std::string> tokens = {
|
||||
"token"
|
||||
};
|
||||
auto result = tt::string::join(tokens, ".");
|
||||
CHECK_EQ(result, "token");
|
||||
}
|
||||
|
||||
TEST_CASE("joining multiple tokens results in a string with all the tokens and the delimiter") {
|
||||
std::vector<std::string> tokens = {
|
||||
"token1",
|
||||
"token2",
|
||||
"token3",
|
||||
};
|
||||
auto result = tt::string::join(tokens, ".");
|
||||
CHECK_EQ(result, "token1.token2.token3");
|
||||
}
|
||||
|
||||
TEST_CASE("joining with empty tokens leads to an extra delimiter") {
|
||||
std::vector<std::string> tokens = {
|
||||
"token1",
|
||||
"",
|
||||
"token2",
|
||||
};
|
||||
auto result = tt::string::join(tokens, ".");
|
||||
CHECK_EQ(result, "token1..token2");
|
||||
}
|
||||
|
||||
// endregion join
|
||||
@@ -1,42 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "doctest.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
/**
|
||||
* A class for creating test files that can automatically clean themselves up.
|
||||
*/
|
||||
class TestFile {
|
||||
const char* path;
|
||||
bool autoClean;
|
||||
|
||||
public:
|
||||
|
||||
TestFile(const char* path, bool autoClean = true) : path(path), autoClean(autoClean) {
|
||||
if (autoClean && exists()) {
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
~TestFile() {
|
||||
if (autoClean && exists()) {
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
const char* getPath() const { return path; }
|
||||
|
||||
void writeData(const char* data) const {
|
||||
CHECK_EQ(tt::file::writeString(path, data), true);
|
||||
}
|
||||
|
||||
bool exists() const {
|
||||
return access(path, F_OK) == 0;
|
||||
}
|
||||
|
||||
void remove() const {
|
||||
::remove(path);
|
||||
}
|
||||
};
|
||||
@@ -1,61 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/network/Url.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("parseUrlQuery can handle a single key-value pair") {
|
||||
auto map = network::parseUrlQuery("?key=value");
|
||||
CHECK_EQ(map.size(), 1);
|
||||
CHECK_EQ(map["key"], "value");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery can handle empty value in the middle") {
|
||||
auto map = network::parseUrlQuery("?a=1&b=&c=3");
|
||||
CHECK_EQ(map.size(), 3);
|
||||
CHECK_EQ(map["a"], "1");
|
||||
CHECK_EQ(map["b"], "");
|
||||
CHECK_EQ(map["c"], "3");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery can handle empty value at the end") {
|
||||
auto map = network::parseUrlQuery("?a=1&b=");
|
||||
CHECK_EQ(map.size(), 2);
|
||||
CHECK_EQ(map["a"], "1");
|
||||
CHECK_EQ(map["b"], "");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery returns empty map when query s questionmark with a key without a value") {
|
||||
auto map = network::parseUrlQuery("?a");
|
||||
CHECK_EQ(map.size(), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery returns empty map when query is a questionmark") {
|
||||
auto map = network::parseUrlQuery("?");
|
||||
CHECK_EQ(map.size(), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery should url-decode the value") {
|
||||
auto map = network::parseUrlQuery("?key=Test%21Test");
|
||||
CHECK_EQ(map.size(), 1);
|
||||
CHECK_EQ(map["key"], "Test!Test");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery should url-decode the key") {
|
||||
auto map = network::parseUrlQuery("?Test%21Test=value");
|
||||
CHECK_EQ(map.size(), 1);
|
||||
CHECK_EQ(map["Test!Test"], "value");
|
||||
}
|
||||
|
||||
TEST_CASE("urlDecode") {
|
||||
auto input = std::string("prefix!*'();:@&=+$,/?#[]<>%-.^_`{}|~ \\");
|
||||
auto expected = std::string("prefix%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D%3C%3E%25-.%5E_%60%7B%7D%7C~+%5C");
|
||||
auto encoded = network::urlEncode(input);
|
||||
CHECK_EQ(encoded, expected);
|
||||
}
|
||||
|
||||
TEST_CASE("urlDecode") {
|
||||
auto input = std::string("prefix%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D%3C%3E%25-.%5E_%60%7B%7D%7C~+%5C");
|
||||
auto expected = std::string("prefix!*'();:@&=+$,/?#[]<>%-.^_`{}|~ \\");
|
||||
auto decoded = network::urlDecode(input);
|
||||
CHECK_EQ(decoded, expected);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
project(TactilityFreeRtosTests)
|
||||
|
||||
enable_language(C CXX ASM)
|
||||
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
|
||||
add_executable(TactilityFreeRtosTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(TactilityFreeRtosTests PRIVATE ${DOCTESTINC})
|
||||
|
||||
add_test(NAME TactilityFreeRtosTests COMMAND TactilityFreeRtosTests)
|
||||
|
||||
target_link_libraries(TactilityFreeRtosTests PUBLIC
|
||||
TactilityFreeRtos
|
||||
freertos_kernel
|
||||
)
|
||||
@@ -1,195 +0,0 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/Dispatcher.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("dispatcher should not call callback if consume isn't called") {
|
||||
int counter = 0;
|
||||
Dispatcher dispatcher;
|
||||
dispatcher.dispatch([&counter]() { counter++; });
|
||||
kernel::delayTicks(10);
|
||||
|
||||
CHECK_EQ(counter, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("dispatcher should be able to dealloc when message is not consumed") {
|
||||
auto* dispatcher = new Dispatcher();
|
||||
auto context = std::make_shared<uint32_t>();
|
||||
dispatcher->dispatch([]() { /* NO-OP */ });
|
||||
delete dispatcher;
|
||||
}
|
||||
|
||||
TEST_CASE("dispatcher should call callback when consume is called") {
|
||||
int counter = 0;
|
||||
Dispatcher dispatcher;
|
||||
|
||||
CHECK_EQ(dispatcher.dispatch([&counter] { counter++; }), true);
|
||||
CHECK_EQ(dispatcher.consume(100), 1);
|
||||
|
||||
CHECK_EQ(counter, 1);
|
||||
}
|
||||
|
||||
TEST_CASE("message should be passed on correctly") {
|
||||
Dispatcher dispatcher;
|
||||
|
||||
dispatcher.dispatch([]() { /* NO-OP */ });
|
||||
dispatcher.consume(100);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/DispatcherThread.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("DispatcherThread state test") {
|
||||
DispatcherThread thread("test");
|
||||
CHECK_EQ(thread.isStarted(), false);
|
||||
|
||||
thread.start();
|
||||
CHECK_EQ(thread.isStarted(), true);
|
||||
|
||||
thread.stop();
|
||||
CHECK_EQ(thread.isStarted(), false);
|
||||
}
|
||||
|
||||
TEST_CASE("DispatcherThread should consume jobs") {
|
||||
DispatcherThread thread("test");
|
||||
thread.start();
|
||||
int counter = 0;
|
||||
|
||||
thread.dispatch([&counter]() { counter++; });
|
||||
|
||||
tt::kernel::delayTicks(10);
|
||||
|
||||
CHECK_EQ(counter, 1);
|
||||
thread.stop();
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/Semaphore.h>
|
||||
#include <Tactility/Lock.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("withLock() locks correctly on Semaphore") {
|
||||
auto semaphore = std::make_shared<Semaphore>(2U);
|
||||
semaphore->withLock([semaphore](){
|
||||
CHECK_EQ(semaphore->getAvailable(), 1);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_CASE("withLock() unlocks correctly on Semaphore") {
|
||||
auto semaphore = std::make_shared<Semaphore>(2U);
|
||||
semaphore->withLock([=](){
|
||||
// NO-OP
|
||||
});
|
||||
|
||||
CHECK_EQ(semaphore->getAvailable(), 2);
|
||||
}
|
||||
|
||||
TEST_CASE("withLock() locks correctly on Mutex") {
|
||||
auto mutex = std::make_shared<Mutex>();
|
||||
mutex->withLock([mutex](){
|
||||
CHECK_EQ(mutex->lock(1), false);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_CASE("withLock() unlocks correctly on Mutex") {
|
||||
auto mutex = std::make_shared<Mutex>();
|
||||
mutex->withLock([=](){
|
||||
// NO-OP
|
||||
});
|
||||
|
||||
CHECK_EQ(mutex->lock(1), true);
|
||||
mutex->unlock();
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
#include "doctest.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
int result;
|
||||
} TestTaskData;
|
||||
|
||||
void test_task(void* parameter) {
|
||||
auto* data = (TestTaskData*)parameter;
|
||||
|
||||
doctest::Context context;
|
||||
|
||||
context.applyCommandLine(data->argc, data->argv);
|
||||
|
||||
// overrides
|
||||
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||
|
||||
data->result = context.run();
|
||||
|
||||
vTaskEndScheduler();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TestTaskData data = {
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.result = 0
|
||||
};
|
||||
|
||||
BaseType_t task_result = xTaskCreate(
|
||||
test_task,
|
||||
"test_task",
|
||||
8192,
|
||||
&data,
|
||||
1,
|
||||
nullptr
|
||||
);
|
||||
assert(task_result == pdPASS);
|
||||
|
||||
vTaskStartScheduler();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
|
||||
// NOTE: This is normally provided by the platform kernel module, but that's not loaded for TactilityCore
|
||||
extern "C" {
|
||||
// Required for FreeRTOS
|
||||
void vAssertCalled(unsigned long line, const char* const file) {
|
||||
__assert_fail("assert failed", file, line, "");
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/MessageQueue.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("message queue initial count should be 0") {
|
||||
MessageQueue queue(10, 1);
|
||||
|
||||
uint32_t count = queue.getCount();
|
||||
CHECK_EQ(count, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("message queue count should increase when message is added") {
|
||||
MessageQueue queue(10, sizeof(uint32_t));
|
||||
|
||||
uint32_t message = 123;
|
||||
queue.put(&message, 100);
|
||||
uint32_t count = queue.getCount();
|
||||
CHECK_EQ(count, 1);
|
||||
}
|
||||
|
||||
TEST_CASE("message queue count should be 0 when message is added and queue is reset") {
|
||||
MessageQueue queue(10, sizeof(uint32_t));
|
||||
|
||||
uint32_t message = 123;
|
||||
|
||||
queue.put(&message, 100);
|
||||
queue.reset();
|
||||
uint32_t count = queue.getCount();
|
||||
CHECK_EQ(count, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("message queue consumption should work") {
|
||||
MessageQueue queue(10, sizeof(uint32_t));
|
||||
|
||||
uint32_t out_message = 123;
|
||||
queue.put(&out_message, 100);
|
||||
|
||||
uint32_t in_message = 0;
|
||||
queue.get(&in_message, 100);
|
||||
CHECK_EQ(in_message, 123);
|
||||
}
|
||||
|
||||
TEST_CASE("message queue count should decrease when message is consumed") {
|
||||
MessageQueue queue(10, sizeof(uint32_t));
|
||||
|
||||
uint32_t out_message = 123;
|
||||
queue.put(&out_message, 100);
|
||||
|
||||
uint32_t in_message = 0;
|
||||
queue.get(&in_message, 100);
|
||||
uint32_t count = queue.getCount();
|
||||
CHECK_EQ(count, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("message queue should make copy of data") {
|
||||
// Given a number that we can later delete
|
||||
MessageQueue queue(1, sizeof(int32_t));
|
||||
const int32_t test_value = 123;
|
||||
auto* number = new int32_t();
|
||||
*number = test_value;
|
||||
|
||||
// When we put the number in the queue and then delete it
|
||||
queue.put(number, 100);
|
||||
delete number;
|
||||
|
||||
// We want to verify that the value was copied into the queue and retrieved properly
|
||||
int32_t queue_number = 0;
|
||||
CHECK_EQ(queue.get(&queue_number, 100), true);
|
||||
CHECK_EQ(queue_number, test_value);
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/kernel/Kernel.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
#include <Tactility/Thread.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("a Mutex can block a thread") {
|
||||
auto mutex = Mutex();
|
||||
CHECK_EQ(mutex.lock(kernel::FREERTOS_MAX_TICKS), true);
|
||||
|
||||
Thread thread = Thread(
|
||||
"thread",
|
||||
1024,
|
||||
[&mutex] {
|
||||
mutex.lock(kernel::FREERTOS_MAX_TICKS);
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
thread.start();
|
||||
|
||||
kernel::delayMillis(5);
|
||||
CHECK_EQ(thread.getState(), Thread::State::Running);
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
kernel::delayMillis(5);
|
||||
CHECK_EQ(thread.getState(), Thread::State::Stopped);
|
||||
|
||||
thread.join();
|
||||
}
|
||||
|
||||
TEST_CASE("a Mutex can be locked exactly once") {
|
||||
Mutex mutex;
|
||||
CHECK_EQ(mutex.lock(0), true);
|
||||
CHECK_EQ(mutex.lock(0), false);
|
||||
mutex.unlock();
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/PubSub.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("PubSub publishing with no subscriptions should not crash") {
|
||||
PubSub<int> pubsub;
|
||||
pubsub.publish(1);
|
||||
}
|
||||
|
||||
TEST_CASE("PubSub subscription receives published data") {
|
||||
PubSub<int> pubsub;
|
||||
int value = 0;
|
||||
|
||||
auto subscription = pubsub.subscribe([&value](auto newValue) {
|
||||
value = newValue;
|
||||
});
|
||||
pubsub.publish(1);
|
||||
|
||||
pubsub.unsubscribe(subscription);
|
||||
|
||||
CHECK_EQ(value, 1);
|
||||
}
|
||||
|
||||
TEST_CASE("PubSub unsubscribed subscription does not receive published data") {
|
||||
PubSub<int> pubsub;
|
||||
int value = 0;
|
||||
|
||||
auto subscription = pubsub.subscribe([&value](auto newValue) {
|
||||
value = newValue;
|
||||
});
|
||||
pubsub.unsubscribe(subscription);
|
||||
pubsub.publish(1);
|
||||
|
||||
CHECK_EQ(value, 0);
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/kernel/Kernel.h>
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <Tactility/Thread.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("a RecursiveMutex can block a thread") {
|
||||
auto mutex = RecursiveMutex();
|
||||
CHECK_EQ(mutex.lock(kernel::FREERTOS_MAX_TICKS), true);
|
||||
|
||||
Thread thread = Thread(
|
||||
"thread",
|
||||
1024,
|
||||
[&mutex] {
|
||||
mutex.lock(kernel::FREERTOS_MAX_TICKS);
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
thread.start();
|
||||
|
||||
kernel::delayMillis(5);
|
||||
CHECK_EQ(thread.getState(), Thread::State::Running);
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
kernel::delayMillis(5);
|
||||
CHECK_EQ(thread.getState(), Thread::State::Stopped);
|
||||
|
||||
thread.join();
|
||||
}
|
||||
|
||||
TEST_CASE("a RecursiveMutex can be locked more than once from the same context") {
|
||||
RecursiveMutex mutex;
|
||||
CHECK_EQ(mutex.lock(0), true);
|
||||
CHECK_EQ(mutex.lock(0), true);
|
||||
mutex.unlock();
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/Semaphore.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
// We want a distinct test for 1 item, because it creates the Semaphore differently
|
||||
TEST_CASE("a Semaphore with max count of 1 can be acquired exactly once") {
|
||||
auto semaphore = Semaphore(1);
|
||||
CHECK_EQ(semaphore.acquire(0), true);
|
||||
CHECK_EQ(semaphore.getAvailable(), 0);
|
||||
CHECK_EQ(semaphore.acquire(0), false);
|
||||
CHECK_EQ(semaphore.release(), true);
|
||||
CHECK_EQ(semaphore.getAvailable(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE("a Semaphore with max count of 2 can be acquired exactly twice") {
|
||||
auto semaphore = Semaphore(2);
|
||||
CHECK_EQ(semaphore.acquire(0), true);
|
||||
CHECK_EQ(semaphore.getAvailable(), 1);
|
||||
CHECK_EQ(semaphore.acquire(0), true);
|
||||
CHECK_EQ(semaphore.getAvailable(), 0);
|
||||
CHECK_EQ(semaphore.acquire(0), false);
|
||||
CHECK_EQ(semaphore.release(), true);
|
||||
CHECK_EQ(semaphore.getAvailable(), 1);
|
||||
CHECK_EQ(semaphore.release(), true);
|
||||
CHECK_EQ(semaphore.getAvailable(), 2);
|
||||
}
|
||||
|
||||
TEST_CASE("the semaphore count should be correct initially") {
|
||||
auto semaphore_a = Semaphore(2);
|
||||
CHECK_EQ(semaphore_a.getAvailable(), 2);
|
||||
|
||||
auto semaphore_b = Semaphore(2, 0);
|
||||
CHECK_EQ(semaphore_b.getAvailable(), 0);
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/Thread.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("when a thread is started then its callback should be called") {
|
||||
bool has_called = false;
|
||||
auto* thread = new Thread(
|
||||
"immediate return task",
|
||||
4096,
|
||||
[&has_called]() {
|
||||
has_called = true;
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
|
||||
CHECK(!has_called);
|
||||
thread->start();
|
||||
thread->join();
|
||||
delete thread;
|
||||
CHECK(has_called);
|
||||
}
|
||||
|
||||
TEST_CASE("a thread can be started and stopped") {
|
||||
bool interrupted = false;
|
||||
auto* thread = new Thread(
|
||||
"interruptable thread",
|
||||
4096,
|
||||
[&interrupted]() {
|
||||
while (!interrupted) {
|
||||
kernel::delayMillis(5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
|
||||
CHECK(thread);
|
||||
thread->start();
|
||||
interrupted = true;
|
||||
thread->join();
|
||||
delete thread;
|
||||
}
|
||||
|
||||
TEST_CASE("thread id should only be set at when thread is started") {
|
||||
bool interrupted = false;
|
||||
auto* thread = new Thread(
|
||||
"interruptable thread",
|
||||
4096,
|
||||
[&interrupted]() {
|
||||
while (!interrupted) {
|
||||
kernel::delayMillis(5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
CHECK_EQ(thread->getTaskHandle(), nullptr);
|
||||
thread->start();
|
||||
CHECK_NE(thread->getTaskHandle(), nullptr);
|
||||
interrupted = true;
|
||||
thread->join();
|
||||
CHECK_EQ(thread->getTaskHandle(), nullptr);
|
||||
delete thread;
|
||||
}
|
||||
|
||||
TEST_CASE("thread state should be correct") {
|
||||
bool interrupted = false;
|
||||
auto* thread = new Thread(
|
||||
"interruptable thread",
|
||||
4096,
|
||||
[&interrupted]() {
|
||||
while (!interrupted) {
|
||||
kernel::delayMillis(5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
);
|
||||
CHECK_EQ(thread->getState(), Thread::State::Stopped);
|
||||
thread->start();
|
||||
Thread::State state = thread->getState();
|
||||
CHECK((state == Thread::State::Starting || state == Thread::State::Running));
|
||||
interrupted = true;
|
||||
thread->join();
|
||||
CHECK_EQ(thread->getState(), Thread::State::Stopped);
|
||||
delete thread;
|
||||
}
|
||||
|
||||
TEST_CASE("thread id should only be set at when thread is started") {
|
||||
int code = 123;
|
||||
auto* thread = new Thread(
|
||||
"return code",
|
||||
4096,
|
||||
[&code]() { return code; }
|
||||
);
|
||||
thread->start();
|
||||
thread->join();
|
||||
CHECK_EQ(thread->getReturnCode(), code);
|
||||
delete thread;
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/Timer.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("TimerType::Periodic timers can be stopped and restarted") {
|
||||
int counter = 0;
|
||||
auto* timer = new Timer(Timer::Type::Periodic, 1, [&counter] { counter++; });
|
||||
CHECK_EQ(timer->start(), true);
|
||||
kernel::delayTicks(10);
|
||||
CHECK_EQ(timer->stop(), true);
|
||||
CHECK_EQ(timer->start(), true);
|
||||
kernel::delayTicks(10);
|
||||
CHECK_EQ(timer->stop(), true);
|
||||
delete timer;
|
||||
|
||||
CHECK_GE(counter, 2);
|
||||
}
|
||||
|
||||
TEST_CASE("TimerType::Periodic calls the callback periodically") {
|
||||
int ticks_to_run = 10;
|
||||
int counter = 0;
|
||||
auto* timer = new Timer(Timer::Type::Periodic, 1, [&counter] { counter++; });
|
||||
CHECK_EQ(timer->start(), true);
|
||||
kernel::delayTicks(ticks_to_run);
|
||||
CHECK_EQ(timer->stop(), true);
|
||||
delete timer;
|
||||
|
||||
CHECK_EQ(counter, ticks_to_run);
|
||||
}
|
||||
|
||||
TEST_CASE("restarting TimerType::Once timers calls the callback again") {
|
||||
int counter = 0;
|
||||
auto* timer = new Timer(Timer::Type::Once, 1, [&counter] { counter++; });
|
||||
CHECK_EQ(timer->start(), true);
|
||||
kernel::delayTicks(10);
|
||||
CHECK_EQ(timer->stop(), true);
|
||||
CHECK_EQ(timer->start(), true);
|
||||
kernel::delayTicks(10);
|
||||
CHECK_EQ(timer->stop(), true);
|
||||
delete timer;
|
||||
|
||||
CHECK_EQ(counter, 2);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
project(TactilityKernelTests)
|
||||
|
||||
enable_language(C CXX ASM)
|
||||
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
|
||||
add_executable(TactilityKernelTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(TactilityKernelTests PRIVATE ${DOCTESTINC})
|
||||
|
||||
add_test(NAME TactilityKernelTests COMMAND TactilityKernelTests)
|
||||
|
||||
target_link_libraries(TactilityKernelTests PUBLIC
|
||||
TactilityKernel
|
||||
platform-posix
|
||||
service-module
|
||||
)
|
||||
@@ -1,195 +0,0 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/bundle.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
TEST_CASE("bundle_alloc/bundle_free round-trip") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
CHECK_NE(bundle, nullptr);
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("bool can be stored and retrieved") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
bundle_put_bool(bundle, "key", true);
|
||||
|
||||
CHECK(bundle_has_bool(bundle, "key"));
|
||||
CHECK_EQ(bundle_get_bool(bundle, "key"), true);
|
||||
|
||||
bool out = false;
|
||||
CHECK(bundle_opt_bool(bundle, "key", &out));
|
||||
CHECK_EQ(out, true);
|
||||
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("int32 can be stored and retrieved") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
bundle_put_int32(bundle, "key", -42);
|
||||
|
||||
CHECK(bundle_has_int32(bundle, "key"));
|
||||
CHECK_EQ(bundle_get_int32(bundle, "key"), -42);
|
||||
|
||||
int32_t out = 0;
|
||||
CHECK(bundle_opt_int32(bundle, "key", &out));
|
||||
CHECK_EQ(out, -42);
|
||||
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("int64 can be stored and retrieved") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
bundle_put_int64(bundle, "key", 123456789012345LL);
|
||||
|
||||
CHECK(bundle_has_int64(bundle, "key"));
|
||||
CHECK_EQ(bundle_get_int64(bundle, "key"), 123456789012345LL);
|
||||
|
||||
int64_t out = 0;
|
||||
CHECK(bundle_opt_int64(bundle, "key", &out));
|
||||
CHECK_EQ(out, 123456789012345LL);
|
||||
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("string can be stored and retrieved") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
bundle_put_string(bundle, "key", "hello world");
|
||||
|
||||
CHECK(bundle_has_string(bundle, "key"));
|
||||
|
||||
char buffer[32];
|
||||
CHECK_EQ(bundle_get_string(bundle, "key", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "hello world"), 0);
|
||||
|
||||
char tiny[4];
|
||||
CHECK_EQ(bundle_get_string(bundle, "key", tiny, sizeof(tiny)), ERROR_BUFFER_OVERFLOW);
|
||||
|
||||
char out[32];
|
||||
CHECK_EQ(bundle_opt_string(bundle, "key", out, sizeof(out)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(out, "hello world"), 0);
|
||||
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("has_*/opt_* reject a key stored with a different type") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
bundle_put_bool(bundle, "key", true);
|
||||
|
||||
CHECK_FALSE(bundle_has_int32(bundle, "key"));
|
||||
CHECK_FALSE(bundle_has_int64(bundle, "key"));
|
||||
CHECK_FALSE(bundle_has_string(bundle, "key"));
|
||||
|
||||
int32_t out_int32 = 0;
|
||||
CHECK_FALSE(bundle_opt_int32(bundle, "key", &out_int32));
|
||||
|
||||
char out_string[8];
|
||||
CHECK_EQ(bundle_opt_string(bundle, "key", out_string, sizeof(out_string)), ERROR_NOT_FOUND);
|
||||
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("opt_string reports ERROR_NOT_FOUND for a missing key") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
char out[8];
|
||||
CHECK_EQ(bundle_opt_string(bundle, "missing", out, sizeof(out)), ERROR_NOT_FOUND);
|
||||
bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("bundle_clone makes an independent deep copy") {
|
||||
Bundle* original = bundle_alloc();
|
||||
bundle_put_bool(original, "bool", true);
|
||||
bundle_put_int32(original, "int32", 123);
|
||||
bundle_put_string(original, "string", "text");
|
||||
|
||||
Bundle* clone = bundle_clone(original);
|
||||
bundle_free(original); // clone must not be affected
|
||||
|
||||
CHECK_EQ(bundle_get_bool(clone, "bool"), true);
|
||||
CHECK_EQ(bundle_get_int32(clone, "int32"), 123);
|
||||
|
||||
char buffer[16];
|
||||
CHECK_EQ(bundle_get_string(clone, "string", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "text"), 0);
|
||||
|
||||
// Mutating the clone must not affect a re-clone of the (already-freed) original's data.
|
||||
bundle_put_int32(clone, "int32", 456);
|
||||
CHECK_EQ(bundle_get_int32(clone, "int32"), 456);
|
||||
|
||||
bundle_free(clone);
|
||||
}
|
||||
|
||||
TEST_CASE("put overwrites a previously stored value, including across types") {
|
||||
Bundle* bundle = bundle_alloc();
|
||||
bundle_put_int32(bundle, "key", 1);
|
||||
bundle_put_string(bundle, "key", "now a string");
|
||||
|
||||
CHECK_FALSE(bundle_has_int32(bundle, "key"));
|
||||
CHECK(bundle_has_string(bundle, "key"));
|
||||
|
||||
char buffer[32];
|
||||
CHECK_EQ(bundle_get_string(bundle, "key", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "now a string"), 0);
|
||||
|
||||
bundle_free(bundle);
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include <tactility/concurrent/thread.h>
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
namespace {
|
||||
|
||||
Module module = {
|
||||
.name = "device_get_put_test_module",
|
||||
.start = nullptr,
|
||||
.stop = nullptr
|
||||
};
|
||||
|
||||
int start(Device*) { return ERROR_NONE; }
|
||||
int stop(Device*) { return ERROR_NONE; }
|
||||
|
||||
Driver test_driver = {
|
||||
.name = "device_get_put_test_driver",
|
||||
.compatible = (const char*[]) { "device_get_put_test", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &module,
|
||||
.internal = nullptr,
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("device_get should succeed even when the device is not started") {
|
||||
Device device = { .name = "get_not_started", .config = nullptr, .parent = nullptr };
|
||||
|
||||
CHECK_EQ(driver_construct_add(&test_driver), ERROR_NONE);
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
device_set_driver(&device, &test_driver);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
|
||||
// Ref-counting brackets construct/destruct, not start/stop.
|
||||
CHECK_EQ(device_get(&device), ERROR_NONE);
|
||||
device_put(&device);
|
||||
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
CHECK_EQ(driver_remove_destruct(&test_driver), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_get should fail with ERROR_INVALID_STATE once the device has been destructed") {
|
||||
Device device = { .name = "get_after_destruct", .config = nullptr, .parent = nullptr };
|
||||
|
||||
CHECK_EQ(driver_construct_add(&test_driver), ERROR_NONE);
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
device_set_driver(&device, &test_driver);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_get(&device), ERROR_INVALID_STATE);
|
||||
|
||||
CHECK_EQ(driver_remove_destruct(&test_driver), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_get should succeed once started, and device_put should release it") {
|
||||
Device device = { .name = "get_started", .config = nullptr, .parent = nullptr };
|
||||
|
||||
CHECK_EQ(driver_construct_add(&test_driver), ERROR_NONE);
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
device_set_driver(&device, &test_driver);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_start(&device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_get(&device), ERROR_NONE);
|
||||
device_put(&device);
|
||||
|
||||
CHECK_EQ(device_stop(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
CHECK_EQ(driver_remove_destruct(&test_driver), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_stop should succeed while a reference is held, but device_destruct should fail with ERROR_RESOURCE_BUSY until it is released") {
|
||||
static Device device = { .name = "get_put_concurrent", .config = nullptr, .parent = nullptr };
|
||||
static std::atomic<bool> acquired { false };
|
||||
static std::atomic<bool> release { false };
|
||||
|
||||
CHECK_EQ(driver_construct_add(&test_driver), ERROR_NONE);
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
device_set_driver(&device, &test_driver);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_start(&device), ERROR_NONE);
|
||||
|
||||
acquired = false;
|
||||
release = false;
|
||||
|
||||
auto* thread = thread_alloc_full(
|
||||
"device_get_put_worker",
|
||||
4096,
|
||||
[](void*) -> int32_t {
|
||||
if (device_get(&device) != ERROR_NONE) {
|
||||
return 1;
|
||||
}
|
||||
acquired = true;
|
||||
while (!release.load()) {
|
||||
delay_millis(1);
|
||||
}
|
||||
device_put(&device);
|
||||
return 0;
|
||||
},
|
||||
nullptr,
|
||||
-1
|
||||
);
|
||||
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
|
||||
while (!acquired.load()) {
|
||||
delay_millis(1);
|
||||
}
|
||||
|
||||
// Held by the worker thread right now - device_stop() is independent of ref-counting, so it
|
||||
// still succeeds; only device_destruct() gates on outstanding refs.
|
||||
CHECK_EQ(device_stop(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_RESOURCE_BUSY);
|
||||
|
||||
release = true;
|
||||
CHECK_EQ(thread_join(thread, 200, 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
|
||||
// Reference released - device_destruct() now succeeds.
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
CHECK_EQ(driver_remove_destruct(&test_driver), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_get_by_name should find and reference an added device regardless of started state, or fail if not found") {
|
||||
Device device = { .name = "get_by_name_device", .config = nullptr, .parent = nullptr };
|
||||
|
||||
CHECK_EQ(driver_construct_add(&test_driver), ERROR_NONE);
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
device_set_driver(&device, &test_driver);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
|
||||
Device* out = nullptr;
|
||||
CHECK_EQ(device_get_by_name("does_not_exist", &out), ERROR_NOT_FOUND);
|
||||
|
||||
// Not started yet - lookup still succeeds, since it only requires the device to be added.
|
||||
CHECK_EQ(device_get_by_name("get_by_name_device", &out), ERROR_NONE);
|
||||
CHECK_EQ(out, &device);
|
||||
device_put(out);
|
||||
|
||||
CHECK_EQ(device_start(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_get_by_name("get_by_name_device", &out), ERROR_NONE);
|
||||
CHECK_EQ(out, &device);
|
||||
device_put(out);
|
||||
|
||||
CHECK_EQ(device_stop(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
CHECK_EQ(driver_remove_destruct(&test_driver), ERROR_NONE);
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <tactility/device_listener.h>
|
||||
|
||||
// Declared in device_listener.cpp's private header; forward-declared here rather than
|
||||
// including the private header, matching the pattern used for other internal-only hooks.
|
||||
extern "C" void device_listener_notify(Device* dev, DeviceEvent event);
|
||||
|
||||
static std::vector<std::pair<void*, DeviceEvent>> calls_a;
|
||||
static std::vector<std::pair<void*, DeviceEvent>> calls_b;
|
||||
|
||||
static void listener_a(Device* dev, DeviceEvent event, void* context) {
|
||||
calls_a.push_back({ context, event });
|
||||
}
|
||||
|
||||
static void listener_b(Device* dev, DeviceEvent event, void* context) {
|
||||
calls_b.push_back({ context, event });
|
||||
}
|
||||
|
||||
static void reset_calls() {
|
||||
calls_a.clear();
|
||||
calls_b.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("device_listener_notify invokes every registered listener with its own context") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
int context_b = 2;
|
||||
|
||||
device_listener_add(listener_a, &context_a);
|
||||
device_listener_add(listener_b, &context_b);
|
||||
|
||||
auto* fake_device = reinterpret_cast<Device*>(0x1000);
|
||||
device_listener_notify(fake_device, DEVICE_EVENT_STARTED);
|
||||
|
||||
CHECK_EQ(calls_a.size(), 1);
|
||||
CHECK_EQ(calls_a[0].first, &context_a);
|
||||
CHECK_EQ(calls_a[0].second, DEVICE_EVENT_STARTED);
|
||||
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
CHECK_EQ(calls_b[0].first, &context_b);
|
||||
CHECK_EQ(calls_b[0].second, DEVICE_EVENT_STARTED);
|
||||
|
||||
device_listener_remove(listener_a);
|
||||
device_listener_remove(listener_b);
|
||||
}
|
||||
|
||||
TEST_CASE("device_listener_remove stops further notifications for that callback only") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
int context_b = 2;
|
||||
|
||||
device_listener_add(listener_a, &context_a);
|
||||
device_listener_add(listener_b, &context_b);
|
||||
|
||||
device_listener_remove(listener_a);
|
||||
|
||||
auto* fake_device = reinterpret_cast<Device*>(0x1000);
|
||||
device_listener_notify(fake_device, DEVICE_EVENT_STOPPED);
|
||||
|
||||
CHECK_EQ(calls_a.size(), 0);
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
|
||||
device_listener_remove(listener_b);
|
||||
}
|
||||
|
||||
TEST_CASE("device_listener_remove on an unregistered callback is a no-op") {
|
||||
reset_calls();
|
||||
int context_b = 2;
|
||||
device_listener_add(listener_b, &context_b);
|
||||
|
||||
// listener_a was never added, so removing it must not disturb listener_b.
|
||||
device_listener_remove(listener_a);
|
||||
|
||||
auto* fake_device = reinterpret_cast<Device*>(0x1000);
|
||||
device_listener_notify(fake_device, DEVICE_EVENT_STARTED);
|
||||
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
|
||||
device_listener_remove(listener_b);
|
||||
}
|
||||
|
||||
static bool reentrant_add_triggered = false;
|
||||
|
||||
static void reentrant_listener(Device* dev, DeviceEvent event, void* context) {
|
||||
calls_a.push_back({ context, event });
|
||||
if (!reentrant_add_triggered) {
|
||||
reentrant_add_triggered = true;
|
||||
// Adding a listener from within a notification must not deadlock: notify() takes a
|
||||
// snapshot of the listener list under the lock, then invokes callbacks after unlocking.
|
||||
device_listener_add(listener_b, context);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("device_listener_notify is safe when a listener adds another listener during notification") {
|
||||
reset_calls();
|
||||
reentrant_add_triggered = false;
|
||||
int context_a = 1;
|
||||
|
||||
device_listener_add(reentrant_listener, &context_a);
|
||||
|
||||
auto* fake_device = reinterpret_cast<Device*>(0x1000);
|
||||
device_listener_notify(fake_device, DEVICE_EVENT_STARTED);
|
||||
|
||||
// The listener added during this round of notification was not part of the snapshot,
|
||||
// so it should not have been invoked yet.
|
||||
CHECK_EQ(calls_a.size(), 1);
|
||||
CHECK_EQ(calls_b.size(), 0);
|
||||
|
||||
// A second round picks up the newly-added listener.
|
||||
device_listener_notify(fake_device, DEVICE_EVENT_STOPPED);
|
||||
CHECK_EQ(calls_a.size(), 2);
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
|
||||
device_listener_remove(reentrant_listener);
|
||||
device_listener_remove(listener_b);
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
static Module module = {
|
||||
.name = "test_module",
|
||||
.start = nullptr,
|
||||
.stop = nullptr
|
||||
};
|
||||
|
||||
TEST_CASE("device_construct and device_destruct should set and unset the constructed state") {
|
||||
Device device = { 0 };
|
||||
|
||||
error_t error = device_construct(&device);
|
||||
CHECK_EQ(error, ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_is_constructed(&device), true);
|
||||
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_is_constructed(&device), false);
|
||||
}
|
||||
|
||||
TEST_CASE("device_construct should be reusable after device_destruct on the same Device") {
|
||||
Device device = { 0 };
|
||||
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
|
||||
// Reconstruct on the same static Device: fresh allocation, behaves like a new device.
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_constructed(&device), true);
|
||||
CHECK_EQ(device_is_added(&device), false);
|
||||
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_add should add the device to the list of all devices") {
|
||||
Device device = {
|
||||
.name = "device",
|
||||
.config = nullptr,
|
||||
.parent = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
|
||||
// Gather all devices
|
||||
std::vector<Device*> devices;
|
||||
device_for_each(&devices, [](auto* device, auto* context) {
|
||||
auto* devices_ptr = static_cast<std::vector<Device*>*>(context);
|
||||
devices_ptr->push_back(device);
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK_EQ(devices.size(), 1);
|
||||
CHECK_EQ(devices[0], &device);
|
||||
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_add should add the device to its parent") {
|
||||
Device parent = {
|
||||
.name = "parent",
|
||||
.config = nullptr,
|
||||
.parent = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
Device child = {
|
||||
.name = "child",
|
||||
.config = nullptr,
|
||||
.parent = &parent,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(device_construct(&parent), ERROR_NONE);
|
||||
CHECK_EQ(device_add(&parent), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_construct(&child), ERROR_NONE);
|
||||
CHECK_EQ(device_add(&child), ERROR_NONE);
|
||||
|
||||
// Gather all child devices
|
||||
std::vector<Device*> children;
|
||||
device_for_each_child(&parent, &children, [](auto* child_device, auto* context) {
|
||||
auto* children_ptr = (std::vector<Device*>*)context;
|
||||
children_ptr->push_back(child_device);
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK_EQ(children.size(), 1);
|
||||
CHECK_EQ(children[0], &child);
|
||||
|
||||
CHECK_EQ(device_remove(&child), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&child), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_remove(&parent), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&parent), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_add should set the state to 'added'") {
|
||||
Device device = {
|
||||
.name = "device",
|
||||
.config = nullptr,
|
||||
.parent = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_is_added(&device), false);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_added(&device), true);
|
||||
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_remove should remove it from the list of all devices") {
|
||||
Device device = {
|
||||
.name = "device",
|
||||
.config = nullptr,
|
||||
.parent = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
|
||||
// Gather all devices
|
||||
std::vector<Device*> devices;
|
||||
device_for_each(&devices, [](auto* device, auto* context) {
|
||||
auto* devices_ptr = (std::vector<Device*>*)context;
|
||||
devices_ptr->push_back(device);
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK_EQ(devices.size(), 0);
|
||||
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_remove should remove the device from its parent") {
|
||||
Device parent = {
|
||||
.name = "parent",
|
||||
.config = nullptr,
|
||||
.parent = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
Device child = {
|
||||
.name = "child",
|
||||
.config = nullptr,
|
||||
.parent = &parent,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(device_construct(&parent), ERROR_NONE);
|
||||
CHECK_EQ(device_add(&parent), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_construct(&child), ERROR_NONE);
|
||||
CHECK_EQ(device_add(&child), ERROR_NONE);
|
||||
CHECK_EQ(device_remove(&child), ERROR_NONE);
|
||||
|
||||
// Gather all child devices
|
||||
std::vector<Device*> children;
|
||||
device_for_each_child(&parent, &children, [](auto* child_device, auto* context) {
|
||||
auto* children_ptr = (std::vector<Device*>*)context;
|
||||
children_ptr->push_back(child_device);
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK_EQ(children.size(), 0);
|
||||
|
||||
CHECK_EQ(device_destruct(&child), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_remove(&parent), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&parent), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_remove should clear the state 'added'") {
|
||||
Device device = {
|
||||
.name = "device",
|
||||
.config = nullptr,
|
||||
.parent = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_added(&device), true);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_added(&device), false);
|
||||
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("device_is_ready should return true only when it is started") {
|
||||
const char* compatible[] = { "test_compatible", nullptr };
|
||||
Driver driver = {
|
||||
.name = "test_driver",
|
||||
.compatible = compatible,
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
Device device = { 0 };
|
||||
|
||||
CHECK_EQ(driver_construct_add(&driver), ERROR_NONE);
|
||||
CHECK_EQ(device_construct(&device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_is_ready(&device), false);
|
||||
device_set_driver(&device, &driver);
|
||||
CHECK_EQ(device_is_ready(&device), false);
|
||||
CHECK_EQ(device_add(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_ready(&device), false);
|
||||
CHECK_EQ(device_start(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_ready(&device), true);
|
||||
CHECK_EQ(device_stop(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_ready(&device), false);
|
||||
CHECK_EQ(device_remove(&device), ERROR_NONE);
|
||||
CHECK_EQ(device_is_ready(&device), false);
|
||||
|
||||
CHECK_EQ(device_destruct(&device), ERROR_NONE);
|
||||
CHECK_EQ(driver_remove_destruct(&driver), ERROR_NONE);
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/freertos/task.h>
|
||||
#include <tactility/concurrent/dispatcher.h>
|
||||
|
||||
TEST_CASE("dispatcher test") {
|
||||
DispatcherHandle_t dispatcher = dispatcher_alloc();
|
||||
CHECK_NE(dispatcher, nullptr);
|
||||
|
||||
int count = 0;
|
||||
auto error = dispatcher_dispatch(dispatcher, &count, [](void* context) {
|
||||
int* count_ptr = static_cast<int*>(context);
|
||||
(*count_ptr)++;
|
||||
});
|
||||
|
||||
CHECK_EQ(error, ERROR_NONE);
|
||||
vTaskDelay(1);
|
||||
|
||||
CHECK_EQ(count, 0);
|
||||
|
||||
CHECK_EQ(dispatcher_consume(dispatcher), ERROR_NONE);
|
||||
CHECK_EQ(count, 1);
|
||||
|
||||
dispatcher_free(dispatcher);
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
static Module module = {
|
||||
.name = "test_module",
|
||||
.start = nullptr,
|
||||
.stop = nullptr
|
||||
};
|
||||
|
||||
struct IntegrationDriverConfig {
|
||||
int startResult;
|
||||
int stopResult;
|
||||
};
|
||||
|
||||
static int startCalled = 0;
|
||||
static int stopCalled = 0;
|
||||
|
||||
#define integration_data(device) static_cast<IntegrationDriverData*>(device_get_driver_data(device))
|
||||
#define integration_config(device) static_cast<const IntegrationDriverConfig*>(device->config)
|
||||
|
||||
static int start(Device* device) {
|
||||
startCalled++;
|
||||
return integration_config(device)->startResult;
|
||||
}
|
||||
|
||||
static int stop(Device* device) {
|
||||
stopCalled++;
|
||||
return integration_config(device)->stopResult;
|
||||
}
|
||||
|
||||
static Driver integration_driver = {
|
||||
.name = "integration_test_driver",
|
||||
.compatible = (const char*[]) { "integration", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &module,
|
||||
.internal = nullptr,
|
||||
};
|
||||
|
||||
TEST_CASE("driver with with start success and stop success should start and stop a device") {
|
||||
startCalled = 0;
|
||||
stopCalled = 0;
|
||||
static const IntegrationDriverConfig config {
|
||||
.startResult = 0,
|
||||
.stopResult = 0
|
||||
};
|
||||
|
||||
static Device integration_device {
|
||||
.name = "integration_device",
|
||||
.config = &config,
|
||||
.parent = nullptr,
|
||||
};
|
||||
|
||||
CHECK_EQ(driver_construct_add(&integration_driver), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(device_construct(&integration_device), ERROR_NONE);
|
||||
device_add(&integration_device);
|
||||
CHECK_EQ(startCalled, 0);
|
||||
CHECK_EQ(driver_bind(&integration_driver, &integration_device), ERROR_NONE);
|
||||
CHECK_EQ(startCalled, 1);
|
||||
CHECK_EQ(stopCalled, 0);
|
||||
CHECK_EQ(driver_unbind(&integration_driver, &integration_device), ERROR_NONE);
|
||||
CHECK_EQ(stopCalled, 1);
|
||||
CHECK_EQ(device_remove(&integration_device), ERROR_NONE);
|
||||
CHECK_EQ(device_destruct(&integration_device), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(driver_remove_destruct(&integration_driver), ERROR_NONE);
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
static Module module = {
|
||||
.name = "test_module",
|
||||
.start = nullptr,
|
||||
.stop = nullptr
|
||||
};
|
||||
|
||||
TEST_CASE("driver_construct and driver_destruct should set and unset the correct fields") {
|
||||
Driver driver = { 0 };
|
||||
driver.owner = &module;
|
||||
|
||||
CHECK_EQ(driver_construct(&driver), ERROR_NONE);
|
||||
CHECK_EQ(driver_add(&driver), ERROR_NONE);
|
||||
CHECK_NE(driver.internal, nullptr);
|
||||
CHECK_EQ(driver_remove(&driver), ERROR_NONE);
|
||||
CHECK_EQ(driver_destruct(&driver), ERROR_NONE);
|
||||
CHECK_EQ(driver.internal, nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("a driver without a module should not be destructible") {
|
||||
Driver driver = { 0 };
|
||||
|
||||
CHECK_EQ(driver_construct(&driver), ERROR_NONE);
|
||||
CHECK_EQ(driver_destruct(&driver), ERROR_NOT_ALLOWED);
|
||||
driver.owner = &module;
|
||||
CHECK_EQ(driver_destruct(&driver), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("driver_is_compatible should return true if a compatible value is found") {
|
||||
const char* compatible[] = { "test_compatible", nullptr };
|
||||
Driver driver = {
|
||||
.name = "test_driver",
|
||||
.compatible = compatible,
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &module,
|
||||
.internal = nullptr
|
||||
};
|
||||
CHECK_EQ(driver_is_compatible(&driver, "test_compatible"), true);
|
||||
CHECK_EQ(driver_is_compatible(&driver, "nope"), false);
|
||||
CHECK_EQ(driver_is_compatible(&driver, nullptr), false);
|
||||
}
|
||||
|
||||
TEST_CASE("driver_find should only find a compatible driver when the driver was constructed") {
|
||||
const char* compatible[] = { "test_compatible", nullptr };
|
||||
Driver driver = {
|
||||
.name = "test_driver",
|
||||
.compatible = compatible,
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
Driver* found_driver = driver_find_compatible("test_compatible");
|
||||
CHECK_EQ(found_driver, nullptr);
|
||||
|
||||
CHECK_EQ(driver_construct(&driver), ERROR_NONE);
|
||||
CHECK_EQ(driver_add(&driver), ERROR_NONE);
|
||||
|
||||
found_driver = driver_find_compatible("test_compatible");
|
||||
CHECK_EQ(found_driver, &driver);
|
||||
|
||||
CHECK_EQ(driver_remove(&driver), ERROR_NONE);
|
||||
CHECK_EQ(driver_destruct(&driver), ERROR_NONE);
|
||||
|
||||
found_driver = driver_find_compatible("test_compatible");
|
||||
CHECK_EQ(found_driver, nullptr);
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/filesystem/file_mutex.h>
|
||||
|
||||
namespace {
|
||||
|
||||
int lock_calls = 0;
|
||||
int unlock_calls = 0;
|
||||
int try_lock_calls = 0;
|
||||
bool try_lock_result = true;
|
||||
uint32_t try_lock_timeout_seen = 0;
|
||||
|
||||
void mock_lock() { lock_calls++; }
|
||||
void mock_unlock() { unlock_calls++; }
|
||||
bool mock_try_lock(uint32_t timeout) {
|
||||
try_lock_calls++;
|
||||
try_lock_timeout_seen = timeout;
|
||||
return try_lock_result;
|
||||
}
|
||||
|
||||
int lock_a_calls = 0;
|
||||
int lock_b_calls = 0;
|
||||
void mock_lock_a() { lock_a_calls++; }
|
||||
void mock_lock_b() { lock_b_calls++; }
|
||||
|
||||
void reset_mocks() {
|
||||
lock_calls = 0;
|
||||
unlock_calls = 0;
|
||||
try_lock_calls = 0;
|
||||
try_lock_result = true;
|
||||
try_lock_timeout_seen = 0;
|
||||
lock_a_calls = 0;
|
||||
lock_b_calls = 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("file_mutex_get with zero registrations returns a no-op mutex") {
|
||||
FileMutex mutex;
|
||||
file_mutex_get(&mutex, "/nowhere/file.txt");
|
||||
|
||||
CHECK_EQ(mutex.lock, nullptr);
|
||||
CHECK_EQ(mutex.try_lock, nullptr);
|
||||
CHECK_EQ(mutex.unlock, nullptr);
|
||||
|
||||
// Calling through a no-op mutex must be safe, and try_lock must report success.
|
||||
file_mutex_lock(&mutex);
|
||||
CHECK_EQ(file_mutex_try_lock(&mutex, 123), true);
|
||||
file_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
TEST_CASE("file_mutex_register/get with a single registration") {
|
||||
reset_mocks();
|
||||
FileMutex registered = { .lock = mock_lock, .try_lock = mock_try_lock, .unlock = mock_unlock };
|
||||
file_mutex_register(®istered, "/mock1");
|
||||
|
||||
FileMutex mutex;
|
||||
|
||||
// Exact mount path match.
|
||||
file_mutex_get(&mutex, "/mock1");
|
||||
CHECK_EQ(mutex.lock, mock_lock);
|
||||
CHECK_EQ(mutex.try_lock, mock_try_lock);
|
||||
CHECK_EQ(mutex.unlock, mock_unlock);
|
||||
|
||||
// Descendant path match.
|
||||
file_mutex_get(&mutex, "/mock1/nested/file.txt");
|
||||
CHECK_EQ(mutex.lock, mock_lock);
|
||||
|
||||
// Unrelated path falls back to no-op.
|
||||
FileMutex unrelated;
|
||||
file_mutex_get(&unrelated, "/other/file.txt");
|
||||
CHECK_EQ(unrelated.lock, nullptr);
|
||||
|
||||
// Prefix-but-not-descendant path (e.g. "/mock1x") must not match "/mock1".
|
||||
FileMutex prefix_only;
|
||||
file_mutex_get(&prefix_only, "/mock1x/file.txt");
|
||||
CHECK_EQ(prefix_only.lock, nullptr);
|
||||
|
||||
// Exercise the resolved callbacks.
|
||||
file_mutex_get(&mutex, "/mock1");
|
||||
file_mutex_lock(&mutex);
|
||||
CHECK_EQ(lock_calls, 1);
|
||||
CHECK_EQ(file_mutex_try_lock(&mutex, 42), true);
|
||||
CHECK_EQ(try_lock_calls, 1);
|
||||
CHECK_EQ(try_lock_timeout_seen, 42);
|
||||
file_mutex_unlock(&mutex);
|
||||
CHECK_EQ(unlock_calls, 1);
|
||||
|
||||
// Re-registering the same path is a no-op: original callbacks remain in place.
|
||||
FileMutex replacement = { .lock = nullptr, .try_lock = nullptr, .unlock = nullptr };
|
||||
file_mutex_register(&replacement, "/mock1");
|
||||
file_mutex_get(&mutex, "/mock1");
|
||||
CHECK_EQ(mutex.lock, mock_lock);
|
||||
}
|
||||
|
||||
TEST_CASE("file_mutex_register/get with two registrations resolves to the matching path") {
|
||||
reset_mocks();
|
||||
|
||||
FileMutex mutex_a = { .lock = mock_lock_a, .try_lock = nullptr, .unlock = nullptr };
|
||||
FileMutex mutex_b = { .lock = mock_lock_b, .try_lock = nullptr, .unlock = nullptr };
|
||||
|
||||
file_mutex_register(&mutex_a, "/mock2a");
|
||||
file_mutex_register(&mutex_b, "/mock2b");
|
||||
|
||||
FileMutex resolved;
|
||||
|
||||
file_mutex_get(&resolved, "/mock2a/file.txt");
|
||||
CHECK_EQ(resolved.lock, mock_lock_a);
|
||||
|
||||
file_mutex_get(&resolved, "/mock2b/file.txt");
|
||||
CHECK_EQ(resolved.lock, mock_lock_b);
|
||||
|
||||
// Path matching neither registration falls back to no-op.
|
||||
file_mutex_get(&resolved, "/mock2c/file.txt");
|
||||
CHECK_EQ(resolved.lock, nullptr);
|
||||
|
||||
// Registration order matters: the first matching entry wins, not the longest
|
||||
// prefix. A mount nested under an earlier one is shadowed by it.
|
||||
FileMutex mutex_nested = { .lock = nullptr, .try_lock = nullptr, .unlock = nullptr };
|
||||
file_mutex_register(&mutex_nested, "/mock2a/nested");
|
||||
file_mutex_get(&resolved, "/mock2a/nested/file.txt");
|
||||
CHECK_EQ(resolved.lock, mock_lock_a); // still /mock2a, registered first
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#include <tactility/filesystem/file_system.h>
|
||||
|
||||
static int mount_called = 0;
|
||||
static int unmount_called = 0;
|
||||
static bool mounted_state = false;
|
||||
static error_t mount_result = ERROR_NONE;
|
||||
static error_t unmount_result = ERROR_NONE;
|
||||
|
||||
static error_t test_mount(void*) {
|
||||
mount_called++;
|
||||
mounted_state = true;
|
||||
return mount_result;
|
||||
}
|
||||
|
||||
static error_t test_unmount(void*) {
|
||||
unmount_called++;
|
||||
mounted_state = false;
|
||||
return unmount_result;
|
||||
}
|
||||
|
||||
static bool test_is_mounted(void*) {
|
||||
return mounted_state;
|
||||
}
|
||||
|
||||
static error_t test_get_path(void* data, char* out_path, size_t out_path_size) {
|
||||
const char* path = static_cast<const char*>(data);
|
||||
if (std::strlen(path) + 1 > out_path_size) {
|
||||
return ERROR_BUFFER_OVERFLOW;
|
||||
}
|
||||
std::strcpy(out_path, path);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static const FileSystemApi test_api = {
|
||||
.mount = test_mount,
|
||||
.unmount = test_unmount,
|
||||
.is_mounted = test_is_mounted,
|
||||
.get_path = test_get_path
|
||||
};
|
||||
|
||||
static void reset_counters() {
|
||||
mount_called = 0;
|
||||
unmount_called = 0;
|
||||
mounted_state = false;
|
||||
mount_result = ERROR_NONE;
|
||||
unmount_result = ERROR_NONE;
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_mount/unmount delegate to the api and reflect is_mounted state") {
|
||||
reset_counters();
|
||||
char path_data[] = "some/path";
|
||||
FileSystem* fs = file_system_add(&test_api, path_data);
|
||||
|
||||
CHECK_EQ(file_system_is_mounted(fs), false);
|
||||
CHECK_EQ(file_system_mount(fs), ERROR_NONE);
|
||||
CHECK_EQ(mount_called, 1);
|
||||
CHECK_EQ(file_system_is_mounted(fs), true);
|
||||
|
||||
CHECK_EQ(file_system_unmount(fs), ERROR_NONE);
|
||||
CHECK_EQ(unmount_called, 1);
|
||||
CHECK_EQ(file_system_is_mounted(fs), false);
|
||||
|
||||
file_system_remove(fs);
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_mount propagates api failure without changing state on its own") {
|
||||
reset_counters();
|
||||
mount_result = ERROR_RESOURCE;
|
||||
char path_data[] = "some/path";
|
||||
FileSystem* fs = file_system_add(&test_api, path_data);
|
||||
|
||||
CHECK_EQ(file_system_mount(fs), ERROR_RESOURCE);
|
||||
// The fake api still flips mounted_state; file_system itself has no independent state,
|
||||
// it always defers to the api's is_mounted().
|
||||
CHECK_EQ(file_system_is_mounted(fs), true);
|
||||
|
||||
mounted_state = false;
|
||||
file_system_remove(fs);
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_get_path forwards to the api with the caller's buffer size") {
|
||||
reset_counters();
|
||||
char path_data[] = "mount/point";
|
||||
FileSystem* fs = file_system_add(&test_api, path_data);
|
||||
|
||||
char small_buffer[4];
|
||||
CHECK_EQ(file_system_get_path(fs, small_buffer, sizeof(small_buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
|
||||
char big_buffer[32];
|
||||
CHECK_EQ(file_system_get_path(fs, big_buffer, sizeof(big_buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(big_buffer, "mount/point"), 0);
|
||||
|
||||
file_system_remove(fs);
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_set_owner/get_owner round-trip and default to null") {
|
||||
reset_counters();
|
||||
char path_data[] = "some/path";
|
||||
FileSystem* fs = file_system_add(&test_api, path_data);
|
||||
|
||||
CHECK_EQ(file_system_get_owner(fs), nullptr);
|
||||
|
||||
auto* fake_owner = reinterpret_cast<Device*>(0x1234);
|
||||
file_system_set_owner(fs, fake_owner);
|
||||
CHECK_EQ(file_system_get_owner(fs), fake_owner);
|
||||
|
||||
file_system_set_owner(fs, nullptr);
|
||||
CHECK_EQ(file_system_get_owner(fs), nullptr);
|
||||
|
||||
file_system_remove(fs);
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_for_each visits every registered file system") {
|
||||
reset_counters();
|
||||
char path_a[] = "a";
|
||||
char path_b[] = "b";
|
||||
char path_c[] = "c";
|
||||
FileSystem* fs_a = file_system_add(&test_api, path_a);
|
||||
FileSystem* fs_b = file_system_add(&test_api, path_b);
|
||||
FileSystem* fs_c = file_system_add(&test_api, path_c);
|
||||
|
||||
std::vector<FileSystem*> visited;
|
||||
file_system_for_each(&visited, [](FileSystem* fs, void* context) {
|
||||
static_cast<std::vector<FileSystem*>*>(context)->push_back(fs);
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK_EQ(visited.size(), 3);
|
||||
CHECK(std::find(visited.begin(), visited.end(), fs_a) != visited.end());
|
||||
CHECK(std::find(visited.begin(), visited.end(), fs_b) != visited.end());
|
||||
CHECK(std::find(visited.begin(), visited.end(), fs_c) != visited.end());
|
||||
|
||||
file_system_remove(fs_a);
|
||||
file_system_remove(fs_b);
|
||||
file_system_remove(fs_c);
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_for_each stops early when the callback returns false") {
|
||||
reset_counters();
|
||||
char path_a[] = "a";
|
||||
char path_b[] = "b";
|
||||
FileSystem* fs_a = file_system_add(&test_api, path_a);
|
||||
FileSystem* fs_b = file_system_add(&test_api, path_b);
|
||||
|
||||
int visit_count = 0;
|
||||
file_system_for_each(&visit_count, [](FileSystem*, void* context) {
|
||||
(*static_cast<int*>(context))++;
|
||||
return false; // stop after the first entry
|
||||
});
|
||||
|
||||
CHECK_EQ(visit_count, 1);
|
||||
|
||||
file_system_remove(fs_a);
|
||||
file_system_remove(fs_b);
|
||||
}
|
||||
|
||||
TEST_CASE("file_system_remove drops the file system from subsequent iteration") {
|
||||
reset_counters();
|
||||
char path_a[] = "a";
|
||||
char path_b[] = "b";
|
||||
FileSystem* fs_a = file_system_add(&test_api, path_a);
|
||||
FileSystem* fs_b = file_system_add(&test_api, path_b);
|
||||
|
||||
file_system_remove(fs_a);
|
||||
|
||||
std::vector<FileSystem*> visited;
|
||||
file_system_for_each(&visited, [](FileSystem* fs, void* context) {
|
||||
static_cast<std::vector<FileSystem*>*>(context)->push_back(fs);
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK_EQ(visited.size(), 1);
|
||||
CHECK_EQ(visited[0], fs_b);
|
||||
|
||||
file_system_remove(fs_b);
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
#include "doctest.h"
|
||||
#include <cassert>
|
||||
#include <tactility/check.h>
|
||||
|
||||
#include <tactility/dts.h>
|
||||
#include <tactility/freertos/task.h>
|
||||
#include <tactility/kernel_init.h>
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
int result;
|
||||
} TestTaskData;
|
||||
|
||||
// From the relevant platform
|
||||
extern "C" struct Module platform_posix_module;
|
||||
|
||||
void test_task(void* parameter) {
|
||||
auto* data = (TestTaskData*)parameter;
|
||||
|
||||
doctest::Context context;
|
||||
|
||||
context.applyCommandLine(data->argc, data->argv);
|
||||
|
||||
// overrides
|
||||
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||
|
||||
Module* dts_modules[] = { &platform_posix_module, nullptr };
|
||||
DtsDevice dts_devices[] = { DTS_DEVICE_TERMINATOR };
|
||||
check(kernel_init(dts_modules, dts_devices) == ERROR_NONE);
|
||||
|
||||
data->result = context.run();
|
||||
|
||||
vTaskEndScheduler();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TestTaskData data = {
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.result = 0
|
||||
};
|
||||
|
||||
BaseType_t task_result = xTaskCreate(
|
||||
test_task,
|
||||
"test_task",
|
||||
8192,
|
||||
&data,
|
||||
1,
|
||||
nullptr
|
||||
);
|
||||
assert(task_result == pdPASS);
|
||||
|
||||
vTaskStartScheduler();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/memory.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
TEST_CASE("MEMORY_POLICY_DEFAULT should have no requirements") {
|
||||
CHECK_EQ(MEMORY_POLICY_DEFAULT.required, 0);
|
||||
CHECK_EQ(MEMORY_POLICY_DEFAULT.desired, 0);
|
||||
CHECK_EQ(MEMORY_POLICY_DEFAULT.alignment, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_alloc should return usable memory") {
|
||||
void* ptr = memory_alloc(64);
|
||||
REQUIRE_NE(ptr, nullptr);
|
||||
memset(ptr, 0xAB, 64);
|
||||
CHECK_EQ(static_cast<uint8_t*>(ptr)[0], 0xAB);
|
||||
CHECK_EQ(static_cast<uint8_t*>(ptr)[63], 0xAB);
|
||||
memory_free(ptr);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_calloc should zero-initialize memory") {
|
||||
auto* ptr = static_cast<uint8_t*>(memory_calloc(16, sizeof(uint8_t)));
|
||||
REQUIRE_NE(ptr, nullptr);
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
CHECK_EQ(ptr[i], 0);
|
||||
}
|
||||
memory_free(ptr);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_realloc should preserve contents when growing") {
|
||||
auto* ptr = static_cast<uint8_t*>(memory_alloc(8));
|
||||
REQUIRE_NE(ptr, nullptr);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
ptr[i] = i;
|
||||
}
|
||||
|
||||
auto* grown = static_cast<uint8_t*>(memory_realloc(ptr, 32));
|
||||
REQUIRE_NE(grown, nullptr);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
CHECK_EQ(grown[i], i);
|
||||
}
|
||||
|
||||
memory_free(grown);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_realloc with a NULL pointer should behave like an allocation") {
|
||||
void* ptr = memory_realloc(nullptr, 32);
|
||||
REQUIRE_NE(ptr, nullptr);
|
||||
memset(ptr, 0, 32);
|
||||
memory_free(ptr);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_free with a NULL pointer should be a no-op") {
|
||||
memory_free(nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_alloc_with_policy should honor a power-of-2 alignment") {
|
||||
MemoryPolicy policy = MEMORY_POLICY_DEFAULT;
|
||||
policy.alignment = 64;
|
||||
|
||||
void* ptr = memory_alloc_with_policy(128, &policy);
|
||||
REQUIRE_NE(ptr, nullptr);
|
||||
CHECK_EQ(reinterpret_cast<uintptr_t>(ptr) % 64, 0);
|
||||
memory_free(ptr);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_calloc_with_policy should honor alignment and zero-initialize") {
|
||||
MemoryPolicy policy = MEMORY_POLICY_DEFAULT;
|
||||
policy.alignment = 32;
|
||||
|
||||
auto* ptr = static_cast<uint8_t*>(memory_calloc_with_policy(8, sizeof(uint32_t), &policy));
|
||||
REQUIRE_NE(ptr, nullptr);
|
||||
CHECK_EQ(reinterpret_cast<uintptr_t>(ptr) % 32, 0);
|
||||
for (size_t i = 0; i < 8 * sizeof(uint32_t); i++) {
|
||||
CHECK_EQ(ptr[i], 0);
|
||||
}
|
||||
memory_free(ptr);
|
||||
}
|
||||
|
||||
TEST_CASE("memory_print_stats should not crash") {
|
||||
memory_print_stats();
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/module.h>
|
||||
|
||||
static void symbol_test_function() { /* NO-OP */ }
|
||||
|
||||
static error_t test_start_result = ERROR_NONE;
|
||||
static bool start_called = false;
|
||||
static struct Module* start_add_order_check_module = nullptr;
|
||||
static error_t test_start() {
|
||||
start_called = true;
|
||||
if (start_add_order_check_module != nullptr) {
|
||||
// If the module was already added to the ledger before start() runs,
|
||||
// a duplicate module_add() must report that it already exists.
|
||||
CHECK_EQ(module_add(start_add_order_check_module), ERROR_INVALID_STATE);
|
||||
}
|
||||
return test_start_result;
|
||||
}
|
||||
|
||||
static error_t test_stop_result = ERROR_NONE;
|
||||
static bool stop_called = false;
|
||||
static error_t test_stop() {
|
||||
stop_called = true;
|
||||
return test_stop_result;
|
||||
}
|
||||
|
||||
TEST_CASE("Module construction and destruction") {
|
||||
struct Module module = {
|
||||
.name = "test",
|
||||
.start = test_start,
|
||||
.stop = test_stop,
|
||||
.symbols = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
// Test successful construction
|
||||
CHECK_EQ(module_construct(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_is_started(&module), false);
|
||||
|
||||
// Test successful destruction
|
||||
CHECK_EQ(module_destruct(&module), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("Module registration") {
|
||||
struct Module module = {
|
||||
.name = "test",
|
||||
.start = test_start,
|
||||
.stop = test_stop,
|
||||
.symbols = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
// module_add should succeed
|
||||
CHECK_EQ(module_add(&module), ERROR_NONE);
|
||||
|
||||
// module_remove should succeed
|
||||
CHECK_EQ(module_remove(&module), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("Module lifecycle") {
|
||||
start_called = false;
|
||||
stop_called = false;
|
||||
test_start_result = ERROR_NONE;
|
||||
test_stop_result = ERROR_NONE;
|
||||
|
||||
struct Module module = {
|
||||
.name = "test",
|
||||
.start = test_start,
|
||||
.stop = test_stop,
|
||||
.symbols = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(module_construct(&module), ERROR_NONE);
|
||||
|
||||
// 1. Successful start (no parent required anymore)
|
||||
CHECK_EQ(module_start(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_is_started(&module), true);
|
||||
CHECK_EQ(start_called, true);
|
||||
|
||||
// Start when already started (should return ERROR_NONE)
|
||||
start_called = false;
|
||||
CHECK_EQ(module_start(&module), ERROR_NONE);
|
||||
CHECK_EQ(start_called, false); // start() function should NOT be called again
|
||||
|
||||
// Stop successful
|
||||
CHECK_EQ(module_stop(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_is_started(&module), false);
|
||||
CHECK_EQ(stop_called, true);
|
||||
|
||||
// Stop when already stopped (should return ERROR_NONE)
|
||||
stop_called = false;
|
||||
CHECK_EQ(module_stop(&module), ERROR_NONE);
|
||||
CHECK_EQ(stop_called, false); // stop() function should NOT be called again
|
||||
|
||||
// Test failed start
|
||||
test_start_result = ERROR_NOT_FOUND;
|
||||
start_called = false;
|
||||
CHECK_EQ(module_start(&module), ERROR_NOT_FOUND);
|
||||
CHECK_EQ(module_is_started(&module), false);
|
||||
CHECK_EQ(start_called, true);
|
||||
|
||||
// Test failed stop
|
||||
test_start_result = ERROR_NONE;
|
||||
CHECK_EQ(module_start(&module), ERROR_NONE);
|
||||
|
||||
test_stop_result = ERROR_NOT_SUPPORTED;
|
||||
stop_called = false;
|
||||
CHECK_EQ(module_stop(&module), ERROR_NOT_SUPPORTED);
|
||||
CHECK_EQ(module_is_started(&module), true); // Should still be started if stop failed
|
||||
CHECK_EQ(stop_called, true);
|
||||
|
||||
// Clean up: fix stop result so we can stop it
|
||||
test_stop_result = ERROR_NONE;
|
||||
CHECK_EQ(module_stop(&module), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(module_destruct(&module), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("Global symbol resolution") {
|
||||
static const struct ModuleSymbol test_symbols[] = {
|
||||
DEFINE_MODULE_SYMBOL(symbol_test_function),
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
|
||||
struct Module module = {
|
||||
.name = "test_sym",
|
||||
.start = test_start,
|
||||
.stop = test_stop,
|
||||
.symbols = test_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
REQUIRE_EQ(module_construct(&module), ERROR_NONE);
|
||||
|
||||
uintptr_t addr;
|
||||
// Should fail as it is not added or started
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), false);
|
||||
REQUIRE_EQ(module_add(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), false);
|
||||
REQUIRE_EQ(module_start(&module), ERROR_NONE);
|
||||
// Still fails as symbols are null
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), true);
|
||||
// Cleanup
|
||||
CHECK_EQ(module_remove(&module), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(module_destruct(&module), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("module_ensure_started adds module to global ledger") {
|
||||
start_called = false;
|
||||
stop_called = false;
|
||||
test_start_result = ERROR_NONE;
|
||||
test_stop_result = ERROR_NONE;
|
||||
|
||||
static const struct ModuleSymbol test_symbols[] = {
|
||||
DEFINE_MODULE_SYMBOL(symbol_test_function),
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
|
||||
struct Module module = {
|
||||
.name = "test_ensure_started",
|
||||
.start = test_start,
|
||||
.stop = test_stop,
|
||||
.symbols = test_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
uintptr_t addr;
|
||||
// Not resolvable before module_ensure_started is called
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), false);
|
||||
|
||||
// test_start() asserts module_add(&module) is already ERROR_INVALID_STATE by the
|
||||
// time start() runs, proving module_add happens before module_start (not after).
|
||||
start_add_order_check_module = &module;
|
||||
CHECK_EQ(module_ensure_started(&module), ERROR_NONE);
|
||||
start_add_order_check_module = nullptr;
|
||||
CHECK_EQ(module_is_started(&module), true);
|
||||
CHECK_EQ(start_called, true);
|
||||
|
||||
// Module must be both added to the ledger and started to be resolvable
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), true);
|
||||
|
||||
// Calling again should be idempotent: no duplicate start, still resolvable
|
||||
start_called = false;
|
||||
CHECK_EQ(module_ensure_started(&module), ERROR_NONE);
|
||||
CHECK_EQ(start_called, false);
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), true);
|
||||
|
||||
// Cleanup
|
||||
CHECK_EQ(module_stop(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_remove(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_destruct(&module), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("module_ensure_destructed removes module from global ledger") {
|
||||
start_called = false;
|
||||
stop_called = false;
|
||||
test_start_result = ERROR_NONE;
|
||||
test_stop_result = ERROR_NONE;
|
||||
|
||||
static const struct ModuleSymbol test_symbols[] = {
|
||||
DEFINE_MODULE_SYMBOL(symbol_test_function),
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
|
||||
struct Module module = {
|
||||
.name = "test_ensure_destructed",
|
||||
.start = test_start,
|
||||
.stop = test_stop,
|
||||
.symbols = test_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
CHECK_EQ(module_ensure_started(&module), ERROR_NONE);
|
||||
|
||||
uintptr_t addr;
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), true);
|
||||
|
||||
CHECK_EQ(module_ensure_destructed(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_is_started(&module), false);
|
||||
CHECK_EQ(stop_called, true);
|
||||
|
||||
// Module must no longer be resolvable once destructed. Note: this alone doesn't
|
||||
// prove removal from the ledger, since module_resolve_symbol_global() also skips
|
||||
// non-started modules — a leaked-but-stopped ledger entry would look the same.
|
||||
CHECK_EQ(module_resolve_symbol_global("symbol_test_function", &addr), false);
|
||||
|
||||
// Directly prove detachment from the ledger: if module_ensure_destructed had left
|
||||
// the module in place, this module_add() would return ERROR_INVALID_STATE.
|
||||
CHECK_EQ(module_add(&module), ERROR_NONE);
|
||||
CHECK_EQ(module_remove(&module), ERROR_NONE);
|
||||
|
||||
// Calling again on an already-destructed module should be a no-op
|
||||
CHECK_EQ(module_ensure_destructed(&module), ERROR_NONE);
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
|
||||
TEST_CASE("mutex_construct and mutex_destruct should properly set the handle") {
|
||||
Mutex mutex = { 0 };
|
||||
mutex_construct(&mutex);
|
||||
CHECK_NE(mutex.handle, nullptr);
|
||||
mutex_destruct(&mutex);
|
||||
CHECK_EQ(mutex.handle, nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("mutex_is_locked should return true only when the mutex is locked") {
|
||||
Mutex mutex = { 0 };
|
||||
mutex_construct(&mutex);
|
||||
|
||||
CHECK_EQ(mutex_is_locked(&mutex), false);
|
||||
mutex_lock(&mutex);
|
||||
CHECK_EQ(mutex_is_locked(&mutex), true);
|
||||
mutex_unlock(&mutex);
|
||||
CHECK_EQ(mutex_is_locked(&mutex), false);
|
||||
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
TEST_CASE("mutex_try_lock should succeed on first lock but not on second") {
|
||||
Mutex mutex = { 0 };
|
||||
mutex_construct(&mutex);
|
||||
|
||||
CHECK_EQ(mutex_try_lock(&mutex, 0), true);
|
||||
CHECK_EQ(mutex_try_lock(&mutex, 0), false);
|
||||
mutex_unlock(&mutex);
|
||||
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
TEST_CASE("mutex_lock in another task should block when a lock is active") {
|
||||
static int task_lock_counter = 0;
|
||||
Mutex mutex = { 0 };
|
||||
task_lock_counter = 0;
|
||||
|
||||
mutex_construct(&mutex);
|
||||
mutex_lock(&mutex);
|
||||
|
||||
TaskHandle_t task_handle;
|
||||
auto task_create_result = xTaskCreate(
|
||||
[](void* input) {
|
||||
Mutex* mutex_ptr = static_cast<Mutex*>(input);
|
||||
mutex_lock(mutex_ptr);
|
||||
task_lock_counter++;
|
||||
vTaskDelete(nullptr);
|
||||
},
|
||||
"mutex_test",
|
||||
2048,
|
||||
&mutex,
|
||||
0,
|
||||
&task_handle
|
||||
);
|
||||
|
||||
CHECK_EQ(task_create_result, pdPASS);
|
||||
CHECK_EQ(task_lock_counter, 0);
|
||||
|
||||
mutex_unlock(&mutex);
|
||||
vTaskDelay(2); // 1 is sufficient most of the time, but not always
|
||||
CHECK_EQ(task_lock_counter, 1);
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <tactility/paths.h>
|
||||
|
||||
// The simulator target is never built with ESP_PLATFORM, so paths_get_user_data_path()
|
||||
// always takes the fixed "data" path branch here, guarded by a buffer-size check.
|
||||
|
||||
TEST_CASE("paths_get_user_data_path succeeds when the buffer exactly fits") {
|
||||
char buffer[16] = { 0 };
|
||||
CHECK_EQ(paths_get_user_data_path(buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "data"), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("paths_get_user_data_path succeeds with a buffer sized to exactly fit the string and terminator") {
|
||||
char buffer[5] = { 0 }; // strlen("data") + 1
|
||||
CHECK_EQ(paths_get_user_data_path(buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "data"), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("paths_get_user_data_path reports a buffer overflow when the buffer is one byte too small") {
|
||||
char buffer[4] = { 0 }; // strlen("data"), no room for the terminator
|
||||
CHECK_EQ(paths_get_user_data_path(buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
}
|
||||
|
||||
TEST_CASE("paths_get_user_data_path reports a buffer overflow for a zero-size buffer") {
|
||||
char buffer[1] = { 'x' };
|
||||
CHECK_EQ(paths_get_user_data_path(buffer, 0), ERROR_BUFFER_OVERFLOW);
|
||||
CHECK_EQ(buffer[0], 'x'); // untouched
|
||||
}
|
||||
@@ -1,244 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/preferences.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
|
||||
const char* TEST_PATH = "/tmp/tactility_kernel_preferences_test.properties";
|
||||
|
||||
struct ScratchFile {
|
||||
ScratchFile() { std::remove(TEST_PATH); }
|
||||
~ScratchFile() { std::remove(TEST_PATH); }
|
||||
};
|
||||
|
||||
bool file_exists(const char* path) {
|
||||
FILE* file = std::fopen(path, "r");
|
||||
if (file == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::fclose(file);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_directory(const char* path) {
|
||||
struct stat info {};
|
||||
return stat(path, &info) == 0 && (info.st_mode & S_IFMT) == S_IFDIR;
|
||||
}
|
||||
|
||||
// Writes a raw properties file directly (bypassing preferences_put_*()) so a test can exercise
|
||||
// a hand-crafted/corrupted payload that preferences_put_*() itself would never produce.
|
||||
void write_raw(const char* path, const char* content) {
|
||||
FILE* file = std::fopen(path, "w");
|
||||
std::fputs(content, file);
|
||||
std::fclose(file);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("preferences_open_path on a missing file starts out empty, without creating it") {
|
||||
ScratchFile scratch;
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
CHECK_NE(preferences, nullptr);
|
||||
CHECK_FALSE(preferences_has_bool(preferences, "key"));
|
||||
CHECK_FALSE(file_exists(TEST_PATH));
|
||||
|
||||
preferences_close(preferences);
|
||||
}
|
||||
|
||||
TEST_CASE("put_*/has_*/opt_* round-trip all four types") {
|
||||
ScratchFile scratch;
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
preferences_put_bool(preferences, "flag", true);
|
||||
preferences_put_int32(preferences, "count", -42);
|
||||
preferences_put_int64(preferences, "big", 123456789012345LL);
|
||||
preferences_put_string(preferences, "text", "hello world");
|
||||
|
||||
CHECK(preferences_has_bool(preferences, "flag"));
|
||||
bool bool_out = false;
|
||||
CHECK(preferences_opt_bool(preferences, "flag", &bool_out));
|
||||
CHECK_EQ(bool_out, true);
|
||||
|
||||
CHECK(preferences_has_int32(preferences, "count"));
|
||||
int32_t int32_out = 0;
|
||||
CHECK(preferences_opt_int32(preferences, "count", &int32_out));
|
||||
CHECK_EQ(int32_out, -42);
|
||||
|
||||
CHECK(preferences_has_int64(preferences, "big"));
|
||||
int64_t int64_out = 0;
|
||||
CHECK(preferences_opt_int64(preferences, "big", &int64_out));
|
||||
CHECK_EQ(int64_out, 123456789012345LL);
|
||||
|
||||
CHECK(preferences_has_string(preferences, "text"));
|
||||
char buffer[32];
|
||||
CHECK_EQ(preferences_opt_string(preferences, "text", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "hello world"), 0);
|
||||
|
||||
preferences_close(preferences);
|
||||
}
|
||||
|
||||
TEST_CASE("opt_string reports ERROR_BUFFER_OVERFLOW and ERROR_NOT_FOUND") {
|
||||
ScratchFile scratch;
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
preferences_put_string(preferences, "text", "hello world");
|
||||
|
||||
char tiny[4];
|
||||
CHECK_EQ(preferences_opt_string(preferences, "text", tiny, sizeof(tiny)), ERROR_BUFFER_OVERFLOW);
|
||||
|
||||
char buffer[32];
|
||||
CHECK_EQ(preferences_opt_string(preferences, "missing", buffer, sizeof(buffer)), ERROR_NOT_FOUND);
|
||||
|
||||
preferences_close(preferences);
|
||||
}
|
||||
|
||||
TEST_CASE("has_*/opt_* reject a key stored with a different type") {
|
||||
ScratchFile scratch;
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
preferences_put_bool(preferences, "key", true);
|
||||
|
||||
CHECK_FALSE(preferences_has_int32(preferences, "key"));
|
||||
CHECK_FALSE(preferences_has_int64(preferences, "key"));
|
||||
CHECK_FALSE(preferences_has_string(preferences, "key"));
|
||||
|
||||
int32_t out = 0;
|
||||
CHECK_FALSE(preferences_opt_int32(preferences, "key", &out));
|
||||
|
||||
preferences_close(preferences);
|
||||
}
|
||||
|
||||
TEST_CASE("has_*/opt_* reject malformed scalar payloads instead of misparsing them") {
|
||||
ScratchFile scratch;
|
||||
write_raw(TEST_PATH,
|
||||
"bad_bool=b:garbage\n"
|
||||
"bad_bool_2=b:2\n"
|
||||
"trailing_junk=i32:42abc\n"
|
||||
"int32_overflow=i32:5000000000\n"
|
||||
"int64_overflow=i64:99999999999999999999\n"
|
||||
"empty_int=i32:\n");
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
|
||||
// "b:garbage" must not silently read back as false - has_bool()/opt_bool() must agree it's
|
||||
// not a valid bool at all.
|
||||
CHECK_FALSE(preferences_has_bool(preferences, "bad_bool"));
|
||||
bool bool_out = true;
|
||||
CHECK_FALSE(preferences_opt_bool(preferences, "bad_bool", &bool_out));
|
||||
CHECK_FALSE(preferences_has_bool(preferences, "bad_bool_2"));
|
||||
CHECK_FALSE(preferences_opt_bool(preferences, "bad_bool_2", &bool_out));
|
||||
|
||||
// "42abc" must not silently parse as 42 - the full payload must be consumed.
|
||||
CHECK_FALSE(preferences_has_int32(preferences, "trailing_junk"));
|
||||
int32_t int32_out = 0;
|
||||
CHECK_FALSE(preferences_opt_int32(preferences, "trailing_junk", &int32_out));
|
||||
|
||||
// Fits in a (64-bit, on this platform) `long` but overflows int32_t - must not silently
|
||||
// truncate on the narrowing cast.
|
||||
CHECK_FALSE(preferences_has_int32(preferences, "int32_overflow"));
|
||||
CHECK_FALSE(preferences_opt_int32(preferences, "int32_overflow", &int32_out));
|
||||
|
||||
// Overflows even a 64-bit integer - strtoll() itself reports ERANGE.
|
||||
CHECK_FALSE(preferences_has_int64(preferences, "int64_overflow"));
|
||||
int64_t int64_out = 0;
|
||||
CHECK_FALSE(preferences_opt_int64(preferences, "int64_overflow", &int64_out));
|
||||
|
||||
CHECK_FALSE(preferences_has_int32(preferences, "empty_int"));
|
||||
CHECK_FALSE(preferences_opt_int32(preferences, "empty_int", &int32_out));
|
||||
|
||||
preferences_close(preferences);
|
||||
}
|
||||
|
||||
TEST_CASE("a string value with embedded newlines and backslashes survives a reopen") {
|
||||
ScratchFile scratch;
|
||||
|
||||
{
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
preferences_put_string(preferences, "text", "line1\nline2 with \\ backslash");
|
||||
preferences_close(preferences);
|
||||
}
|
||||
{
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
char buffer[64];
|
||||
CHECK_EQ(preferences_opt_string(preferences, "text", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "line1\nline2 with \\ backslash"), 0);
|
||||
preferences_close(preferences);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("preferences_close persists changes, and only close persists them") {
|
||||
ScratchFile scratch;
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
preferences_put_bool(preferences, "flag", true);
|
||||
|
||||
// Not persisted yet - only preferences_close() writes to disk.
|
||||
CHECK_FALSE(file_exists(TEST_PATH));
|
||||
|
||||
preferences_close(preferences);
|
||||
CHECK(file_exists(TEST_PATH));
|
||||
|
||||
Preferences* reopened = preferences_open(TEST_PATH);
|
||||
CHECK(preferences_has_bool(reopened, "flag"));
|
||||
preferences_close(reopened);
|
||||
}
|
||||
|
||||
TEST_CASE("put_* on an already-closed value is visible without reopening") {
|
||||
ScratchFile scratch;
|
||||
|
||||
Preferences* preferences = preferences_open(TEST_PATH);
|
||||
preferences_put_int32(preferences, "count", 1);
|
||||
preferences_close(preferences);
|
||||
|
||||
Preferences* reopened = preferences_open(TEST_PATH);
|
||||
preferences_put_int32(reopened, "count", 2);
|
||||
int32_t out = 0;
|
||||
CHECK(preferences_opt_int32(reopened, "count", &out));
|
||||
CHECK_EQ(out, 2);
|
||||
preferences_close(reopened);
|
||||
|
||||
Preferences* final_instance = preferences_open(TEST_PATH);
|
||||
CHECK(preferences_opt_int32(final_instance, "count", &out));
|
||||
CHECK_EQ(out, 2);
|
||||
preferences_close(final_instance);
|
||||
}
|
||||
|
||||
TEST_CASE("preferences_open creates missing parent directories (recursively) and persists into them") {
|
||||
const char* nested_dir_a = "/tmp/tactility_kernel_preferences_test_nested";
|
||||
const char* nested_dir_b = "/tmp/tactility_kernel_preferences_test_nested/a";
|
||||
const char* nested_dir_c = "/tmp/tactility_kernel_preferences_test_nested/a/b";
|
||||
const char* nested_path = "/tmp/tactility_kernel_preferences_test_nested/a/b/settings.properties";
|
||||
|
||||
std::remove(nested_path);
|
||||
rmdir(nested_dir_c);
|
||||
rmdir(nested_dir_b);
|
||||
rmdir(nested_dir_a);
|
||||
REQUIRE_FALSE(is_directory(nested_dir_a));
|
||||
|
||||
Preferences* preferences = preferences_open(nested_path);
|
||||
REQUIRE_NE(preferences, nullptr);
|
||||
CHECK(is_directory(nested_dir_a));
|
||||
CHECK(is_directory(nested_dir_b));
|
||||
CHECK(is_directory(nested_dir_c));
|
||||
|
||||
preferences_put_int32(preferences, "count", 7);
|
||||
preferences_close(preferences);
|
||||
CHECK(file_exists(nested_path));
|
||||
|
||||
Preferences* reopened = preferences_open(nested_path);
|
||||
REQUIRE_NE(reopened, nullptr);
|
||||
int32_t out = 0;
|
||||
CHECK(preferences_opt_int32(reopened, "count", &out));
|
||||
CHECK_EQ(out, 7);
|
||||
preferences_close(reopened);
|
||||
|
||||
std::remove(nested_path);
|
||||
rmdir(nested_dir_c);
|
||||
rmdir(nested_dir_b);
|
||||
rmdir(nested_dir_a);
|
||||
}
|
||||
@@ -1,258 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/properties_file.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
const char* TEST_PATH = "/tmp/tactility_kernel_properties_file_test.properties";
|
||||
|
||||
struct ScratchFile {
|
||||
ScratchFile() { std::remove(TEST_PATH); }
|
||||
~ScratchFile() { std::remove(TEST_PATH); }
|
||||
};
|
||||
|
||||
bool file_exists(const char* path) {
|
||||
FILE* file = std::fopen(path, "r");
|
||||
if (file == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::fclose(file);
|
||||
return true;
|
||||
}
|
||||
|
||||
void write_raw(const char* path, const char* content) {
|
||||
FILE* file = std::fopen(path, "w");
|
||||
std::fputs(content, file);
|
||||
std::fclose(file);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("properties_file_open on a missing file starts out empty, without creating it") {
|
||||
ScratchFile scratch;
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
CHECK_NE(file, nullptr);
|
||||
CHECK_FALSE(properties_file_has(file, "key"));
|
||||
CHECK_FALSE(file_exists(TEST_PATH));
|
||||
|
||||
properties_file_close(file);
|
||||
}
|
||||
|
||||
TEST_CASE("properties_file_open returns NULL when a genuine I/O error interrupts reading") {
|
||||
// fopen() on a directory succeeds on Linux, but the first read fails with EISDIR and sets
|
||||
// the stream's error indicator - a deterministic way to exercise load_from_file()'s
|
||||
// ferror() check without needing real storage-hardware fault injection.
|
||||
const char* dir_path = "/tmp/tactility_kernel_properties_file_test_is_a_directory";
|
||||
rmdir(dir_path);
|
||||
REQUIRE_EQ(mkdir(dir_path, 0777), 0);
|
||||
|
||||
CHECK_EQ(properties_file_open(dir_path), nullptr);
|
||||
|
||||
rmdir(dir_path);
|
||||
}
|
||||
|
||||
TEST_CASE("set/has/get round-trip, and close persists while unclosed changes don't") {
|
||||
ScratchFile scratch;
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
properties_file_set(file, "key", "value");
|
||||
|
||||
CHECK(properties_file_has(file, "key"));
|
||||
char buffer[32];
|
||||
CHECK_EQ(properties_file_get(file, "key", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "value"), 0);
|
||||
|
||||
// Not persisted yet - only properties_file_close() writes to disk.
|
||||
CHECK_FALSE(file_exists(TEST_PATH));
|
||||
|
||||
properties_file_close(file);
|
||||
CHECK(file_exists(TEST_PATH));
|
||||
|
||||
PropertiesFile* reopened = properties_file_open(TEST_PATH);
|
||||
CHECK(properties_file_has(reopened, "key"));
|
||||
properties_file_close(reopened);
|
||||
}
|
||||
|
||||
TEST_CASE("properties_file_get reports ERROR_BUFFER_OVERFLOW and ERROR_NOT_FOUND") {
|
||||
ScratchFile scratch;
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
properties_file_set(file, "key", "value");
|
||||
|
||||
char tiny[3];
|
||||
CHECK_EQ(properties_file_get(file, "key", tiny, sizeof(tiny)), ERROR_BUFFER_OVERFLOW);
|
||||
|
||||
char buffer[32];
|
||||
CHECK_EQ(properties_file_get(file, "missing", buffer, sizeof(buffer)), ERROR_NOT_FOUND);
|
||||
|
||||
properties_file_close(file);
|
||||
}
|
||||
|
||||
TEST_CASE("set overwrites a previously stored value") {
|
||||
ScratchFile scratch;
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
properties_file_set(file, "key", "first");
|
||||
properties_file_set(file, "key", "second");
|
||||
|
||||
char buffer[32];
|
||||
CHECK_EQ(properties_file_get(file, "key", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "second"), 0);
|
||||
|
||||
properties_file_close(file);
|
||||
}
|
||||
|
||||
TEST_CASE("comments and blank lines are skipped, keys and values are trimmed") {
|
||||
ScratchFile scratch;
|
||||
write_raw(TEST_PATH,
|
||||
"# Comment\n"
|
||||
" \t# Indented comment\n"
|
||||
"\n"
|
||||
"key1=value1\n"
|
||||
" \tkey 2\t = \tvalue 2\t \n");
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
|
||||
char buffer[32];
|
||||
CHECK_EQ(properties_file_get(file, "key1", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "value1"), 0);
|
||||
|
||||
// Only leading/trailing whitespace is trimmed - the internal space in "key 2"/"value 2"
|
||||
// survives.
|
||||
CHECK_EQ(properties_file_get(file, "key 2", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "value 2"), 0);
|
||||
|
||||
properties_file_close(file);
|
||||
}
|
||||
|
||||
TEST_CASE("a malformed line (no '=') is skipped without aborting the rest of the file") {
|
||||
ScratchFile scratch;
|
||||
write_raw(TEST_PATH, "not_a_key_value_pair\nkey=value\n");
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
CHECK(properties_file_has(file, "key"));
|
||||
CHECK_FALSE(properties_file_has(file, "not_a_key_value_pair"));
|
||||
properties_file_close(file);
|
||||
}
|
||||
|
||||
TEST_CASE("a [section] line prefixes every following key until the next section") {
|
||||
ScratchFile scratch;
|
||||
write_raw(TEST_PATH,
|
||||
"[app]\n"
|
||||
"id=one.tactility.helloworld\n"
|
||||
"name=Hello\n"
|
||||
"[other]\n"
|
||||
"id=x\n");
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
|
||||
char buffer[64];
|
||||
CHECK_EQ(properties_file_get(file, "[app]id", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "one.tactility.helloworld"), 0);
|
||||
|
||||
CHECK_EQ(properties_file_get(file, "[app]name", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "Hello"), 0);
|
||||
|
||||
CHECK_EQ(properties_file_get(file, "[other]id", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "x"), 0);
|
||||
|
||||
CHECK_FALSE(properties_file_has(file, "id"));
|
||||
|
||||
properties_file_close(file);
|
||||
}
|
||||
|
||||
TEST_CASE("properties_file_close reports ERROR_NONE on success") {
|
||||
ScratchFile scratch;
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
properties_file_set(file, "key", "value");
|
||||
|
||||
CHECK_EQ(properties_file_close(file), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("properties_file_close reports ERROR_RESOURCE when the parent directory doesn't exist") {
|
||||
const char* path = "/tmp/tactility_kernel_properties_file_test_missing_dir/settings.properties";
|
||||
std::remove(path); // no-op if the directory doesn't exist, which is the point of this test
|
||||
|
||||
// Missing directory is not an error for open() - it starts out empty, same as a missing
|
||||
// file (see the "starts out empty" test above).
|
||||
PropertiesFile* file = properties_file_open(path);
|
||||
REQUIRE_NE(file, nullptr);
|
||||
properties_file_set(file, "key", "value");
|
||||
|
||||
// close()'s save can't create its temp file in a directory that doesn't exist.
|
||||
CHECK_EQ(properties_file_close(file), ERROR_RESOURCE);
|
||||
}
|
||||
|
||||
TEST_CASE("a failed close leaves previously-saved content on disk untouched") {
|
||||
if (geteuid() == 0) {
|
||||
// Root bypasses directory write permissions, so the read-only directory below would
|
||||
// not make save_to_file() fail.
|
||||
return;
|
||||
}
|
||||
|
||||
const char* dir = "/tmp/tactility_kernel_properties_file_readonly_test";
|
||||
const char* path = "/tmp/tactility_kernel_properties_file_readonly_test/settings.properties";
|
||||
|
||||
mkdir(dir, 0700);
|
||||
chmod(dir, 0700);
|
||||
std::remove(path);
|
||||
|
||||
{
|
||||
PropertiesFile* file = properties_file_open(path);
|
||||
properties_file_set(file, "key", "original");
|
||||
REQUIRE_EQ(properties_file_close(file), ERROR_NONE);
|
||||
}
|
||||
|
||||
// Read-only directory - save_to_file()'s temp file can't be created there, so the close
|
||||
// below must fail without disturbing the "original" content already on disk.
|
||||
REQUIRE_EQ(chmod(dir, 0555), 0);
|
||||
|
||||
PropertiesFile* file = properties_file_open(path);
|
||||
properties_file_set(file, "key", "corrupted");
|
||||
CHECK_EQ(properties_file_close(file), ERROR_RESOURCE);
|
||||
|
||||
chmod(dir, 0700); // restore write access for the check below and for cleanup
|
||||
|
||||
PropertiesFile* reloaded = properties_file_open(path);
|
||||
char buffer[32];
|
||||
CHECK_EQ(properties_file_get(reloaded, "key", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::strcmp(buffer, "original"), 0);
|
||||
properties_file_close(reloaded);
|
||||
|
||||
std::remove(path);
|
||||
rmdir(dir);
|
||||
}
|
||||
|
||||
TEST_CASE("properties_file_for_each visits every key exactly once") {
|
||||
ScratchFile scratch;
|
||||
|
||||
PropertiesFile* file = properties_file_open(TEST_PATH);
|
||||
properties_file_set(file, "a", "1");
|
||||
properties_file_set(file, "b", "2");
|
||||
properties_file_set(file, "c", "3");
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> seen;
|
||||
properties_file_for_each(file, [](const char* key, const char* value, void* context) {
|
||||
auto* out = static_cast<std::vector<std::pair<std::string, std::string>>*>(context);
|
||||
out->emplace_back(key, value);
|
||||
}, &seen);
|
||||
|
||||
CHECK_EQ(seen.size(), 3);
|
||||
for (const auto& [key, value] : seen) {
|
||||
if (key == "a") CHECK_EQ(value, "1");
|
||||
else if (key == "b") CHECK_EQ(value, "2");
|
||||
else if (key == "c") CHECK_EQ(value, "3");
|
||||
else FAIL("unexpected key: " << key);
|
||||
}
|
||||
|
||||
properties_file_close(file);
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/concurrent/recursive_mutex.h>
|
||||
|
||||
TEST_CASE("recursive_mutex_construct and mutex_destruct should properly set the handle") {
|
||||
RecursiveMutex mutex = { 0 };
|
||||
recursive_mutex_construct(&mutex);
|
||||
CHECK_NE(mutex.handle, nullptr);
|
||||
recursive_mutex_destruct(&mutex);
|
||||
CHECK_EQ(mutex.handle, nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("recursive_mutex_is_locked should return true only when the mutex is locked") {
|
||||
RecursiveMutex mutex = { 0 };
|
||||
recursive_mutex_construct(&mutex);
|
||||
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), false);
|
||||
recursive_mutex_lock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), true);
|
||||
recursive_mutex_unlock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), false);
|
||||
|
||||
recursive_mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
TEST_CASE("recursive_mutex_is_locked can lock twice from the same thread") {
|
||||
RecursiveMutex mutex = { 0 };
|
||||
recursive_mutex_construct(&mutex);
|
||||
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), false);
|
||||
recursive_mutex_lock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), true);
|
||||
recursive_mutex_lock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), true);
|
||||
recursive_mutex_unlock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), true);
|
||||
recursive_mutex_unlock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), false);
|
||||
|
||||
recursive_mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
TEST_CASE("recursive_mutex_try_lock should lock multiple times from the same thread") {
|
||||
RecursiveMutex mutex = { 0 };
|
||||
recursive_mutex_construct(&mutex);
|
||||
|
||||
CHECK_EQ(recursive_mutex_try_lock(&mutex, 0), true);
|
||||
CHECK_EQ(recursive_mutex_try_lock(&mutex, 0), true);
|
||||
recursive_mutex_unlock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), true);
|
||||
recursive_mutex_unlock(&mutex);
|
||||
CHECK_EQ(recursive_mutex_is_locked(&mutex), false);
|
||||
|
||||
recursive_mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
TEST_CASE("recursive_mutex_lock in another task should block when a lock is active") {
|
||||
static int task_lock_counter = 0;
|
||||
RecursiveMutex mutex = { 0 };
|
||||
task_lock_counter = 0;
|
||||
|
||||
recursive_mutex_construct(&mutex);
|
||||
recursive_mutex_lock(&mutex);
|
||||
|
||||
TaskHandle_t task_handle;
|
||||
auto task_create_result = xTaskCreate(
|
||||
[](void* input) {
|
||||
RecursiveMutex* mutex_ptr = static_cast<RecursiveMutex*>(input);
|
||||
recursive_mutex_lock(mutex_ptr);
|
||||
task_lock_counter++;
|
||||
vTaskDelete(nullptr);
|
||||
},
|
||||
"mutex_test",
|
||||
2048,
|
||||
&mutex,
|
||||
0,
|
||||
&task_handle
|
||||
);
|
||||
|
||||
CHECK_EQ(task_create_result, pdPASS);
|
||||
CHECK_EQ(task_lock_counter, 0);
|
||||
|
||||
recursive_mutex_unlock(&mutex);
|
||||
vTaskDelay(2); // 1 is sufficient most of the time, but not always
|
||||
CHECK_EQ(task_lock_counter, 1);
|
||||
recursive_mutex_destruct(&mutex);
|
||||
}
|
||||
@@ -1,439 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <tactility/concurrent/thread.h>
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/system_event.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
// system_event_emit() snapshots matching subscriptions under the lock, then invokes them
|
||||
// after unlocking (see the @warning on system_event_callback_add() in system_event.h), so a
|
||||
// callback calling system_event_callback_add()/_unsubscribe()/_emit() must not deadlock -
|
||||
// covered below, mirroring DeviceListenerTest.cpp's reentrancy test.
|
||||
|
||||
struct RecordedCall {
|
||||
void* context;
|
||||
SystemEventType type;
|
||||
// Copied out of event->data during the callback - event->data is only valid for the
|
||||
// duration of the callback (it lives in system_event_emit()'s own stack frame), so a bare
|
||||
// pointer/length pair recorded here would dangle by the time a TEST_CASE inspects it.
|
||||
std::vector<uint8_t> data;
|
||||
uint64_t timestamp;
|
||||
};
|
||||
|
||||
static std::vector<RecordedCall> calls_a;
|
||||
static std::vector<RecordedCall> calls_b;
|
||||
|
||||
static void listener_a(SystemEvent* event, void* context) {
|
||||
calls_a.push_back({ context, event->type, std::vector<uint8_t>(event->data, event->data + event->data_len), event->timestamp });
|
||||
}
|
||||
|
||||
static void listener_b(SystemEvent* event, void* context) {
|
||||
calls_b.push_back({ context, event->type, std::vector<uint8_t>(event->data, event->data + event->data_len), event->timestamp });
|
||||
}
|
||||
|
||||
static void reset_calls() {
|
||||
calls_a.clear();
|
||||
calls_b.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit invokes every subscriber registered for that type") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
int context_b = 2;
|
||||
|
||||
CHECK_EQ(system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_a, &context_a), ERROR_NONE);
|
||||
CHECK_EQ(system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_b, &context_b), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0), ERROR_NONE);
|
||||
|
||||
REQUIRE_EQ(calls_a.size(), 1);
|
||||
CHECK_EQ(calls_a[0].context, &context_a);
|
||||
CHECK_EQ(calls_a[0].type, KERNEL_EVENT_BOOT_COMPLETED);
|
||||
|
||||
REQUIRE_EQ(calls_b.size(), 1);
|
||||
CHECK_EQ(calls_b[0].context, &context_b);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a);
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_b);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit only invokes subscribers registered for the emitted type") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_a, &context_a);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_TIME_CHANGED, nullptr, 0);
|
||||
CHECK_EQ(calls_a.size(), 0);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
CHECK_EQ(calls_a.size(), 1);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit copies the data into the delivered event") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
struct Payload { int value; } payload { 42 };
|
||||
|
||||
system_event_callback_add(KERNEL_EVENT_TIME_CHANGED, listener_a, &context_a);
|
||||
system_event_emit(KERNEL_EVENT_TIME_CHANGED, &payload, sizeof(payload));
|
||||
|
||||
REQUIRE_EQ(calls_a.size(), 1);
|
||||
REQUIRE_EQ(calls_a[0].data.size(), sizeof(payload));
|
||||
CHECK_EQ(reinterpret_cast<const Payload*>(calls_a[0].data.data())->value, 42);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_TIME_CHANGED, listener_a);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit with no data delivers an empty payload") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_a, &context_a);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
|
||||
REQUIRE_EQ(calls_a.size(), 1);
|
||||
CHECK(calls_a[0].data.empty());
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_callback_remove stops further notifications for that callback only") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
int context_b = 2;
|
||||
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_a, &context_a);
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_b, &context_b);
|
||||
|
||||
CHECK_EQ(system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a), ERROR_NONE);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
|
||||
CHECK_EQ(calls_a.size(), 0);
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_b);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_callback_remove on an unregistered callback returns ERROR_NOT_FOUND and is a no-op") {
|
||||
reset_calls();
|
||||
int context_b = 2;
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_b, &context_b);
|
||||
|
||||
// listener_a was never added for this type, so removing it must not disturb listener_b.
|
||||
CHECK_EQ(system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a), ERROR_NOT_FOUND);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_b);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_callback_remove matches on (type, callback), not the callback alone") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
|
||||
// Same callback subscribed for two different event types.
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_a, &context_a);
|
||||
system_event_callback_add(KERNEL_EVENT_TIME_CHANGED, listener_a, &context_a);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
CHECK_EQ(calls_a.size(), 0);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_TIME_CHANGED, nullptr, 0);
|
||||
CHECK_EQ(calls_a.size(), 1);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_TIME_CHANGED, listener_a);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit with no subscribers for that type returns ERROR_NONE") {
|
||||
CHECK_EQ(system_event_emit(KERNEL_EVENT_SERVICE_STOPPED, nullptr, 0), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit stamps the event with the current boot-relative time") {
|
||||
reset_calls();
|
||||
int context_a = 1;
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_a, &context_a);
|
||||
|
||||
auto before = static_cast<uint64_t>(get_micros_since_boot());
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
auto after = static_cast<uint64_t>(get_micros_since_boot());
|
||||
|
||||
REQUIRE_EQ(calls_a.size(), 1);
|
||||
CHECK_GE(calls_a[0].timestamp, before);
|
||||
CHECK_LE(calls_a[0].timestamp, after);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_a);
|
||||
}
|
||||
|
||||
static bool reentrant_add_triggered = false;
|
||||
|
||||
static void reentrant_listener(SystemEvent* event, void* context) {
|
||||
calls_a.push_back({ context, event->type, std::vector<uint8_t>(event->data, event->data + event->data_len), event->timestamp });
|
||||
if (!reentrant_add_triggered) {
|
||||
reentrant_add_triggered = true;
|
||||
// Subscribing from within a notification must not deadlock: emit() releases the
|
||||
// lock before invoking callbacks, so this only blocks briefly on the (already
|
||||
// unlocked) mutex.
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, listener_b, context);
|
||||
// Also exercise unsubscribe() and a nested emit() of a different type from within
|
||||
// a callback - all must complete without deadlocking.
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, reentrant_listener);
|
||||
system_event_emit(KERNEL_EVENT_TIME_CHANGED, nullptr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit is safe when a callback subscribes, unsubscribes and emits during notification") {
|
||||
reset_calls();
|
||||
reentrant_add_triggered = false;
|
||||
int context_a = 1;
|
||||
|
||||
system_event_callback_add(KERNEL_EVENT_BOOT_COMPLETED, reentrant_listener, &context_a);
|
||||
system_event_callback_add(KERNEL_EVENT_TIME_CHANGED, listener_b, &context_a);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
|
||||
// reentrant_listener unsubscribed itself and triggered a nested TIME_CHANGED emit,
|
||||
// which the pre-existing listener_b subscription picks up. The listener_b
|
||||
// subscription added *during* this round wasn't part of this round's snapshot, so it
|
||||
// wasn't invoked for BOOT_COMPLETED yet.
|
||||
CHECK_EQ(calls_a.size(), 1);
|
||||
CHECK_EQ(calls_b.size(), 1);
|
||||
|
||||
// A second BOOT_COMPLETED emit must not reach reentrant_listener again (it
|
||||
// unsubscribed itself), but must reach the listener_b subscription added last round.
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
CHECK_EQ(calls_a.size(), 1);
|
||||
CHECK_EQ(calls_b.size(), 2);
|
||||
|
||||
system_event_callback_remove(KERNEL_EVENT_BOOT_COMPLETED, listener_b);
|
||||
system_event_callback_remove(KERNEL_EVENT_TIME_CHANGED, listener_b);
|
||||
}
|
||||
|
||||
// gps.h-style poll subscription: system_event_subscribe()/_await()/_unsubscribe().
|
||||
//
|
||||
// system_event_await() only detects sequence increments that happen *after* it starts
|
||||
// waiting (same as gps_api_event_await()), so the emit must be started from another task
|
||||
// while this one is already blocked in await() - emitting first and awaiting after would
|
||||
// race the notification the same way it would with any FreeRTOS task-notify consumer.
|
||||
|
||||
TEST_CASE("system_event_subscribe/_await deliver the event payload by value") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_NETWORK_CONNECTED;
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
NetworkConnectedEvent connected { .device = nullptr, .ipv4_addr = 0x0A000001, .gateway = 0x0A0000FE };
|
||||
auto* thread = thread_alloc_full(
|
||||
"system-event-emitter",
|
||||
4096,
|
||||
[](void* context) {
|
||||
delay_millis(20);
|
||||
auto* connected_ptr = static_cast<NetworkConnectedEvent*>(context);
|
||||
system_event_emit(KERNEL_EVENT_NETWORK_CONNECTED, connected_ptr, sizeof(*connected_ptr));
|
||||
return 0;
|
||||
},
|
||||
&connected,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(system_event_await(&sub, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
|
||||
NetworkConnectedEvent received {};
|
||||
CHECK_EQ(system_event_get_data(&sub, reinterpret_cast<uint8_t*>(&received), sizeof(received)), ERROR_NONE);
|
||||
CHECK_EQ(received.ipv4_addr, connected.ipv4_addr);
|
||||
CHECK_EQ(received.gateway, connected.gateway);
|
||||
|
||||
CHECK_EQ(thread_join(thread, 2, 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
|
||||
CHECK_EQ(system_event_unsubscribe(&sub), ERROR_NONE);
|
||||
CHECK_EQ(system_event_unsubscribe(&sub), ERROR_NOT_FOUND);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_await returns a matching event that arrived before it started waiting") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_NETWORK_CONNECTED;
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
// Same-thread emit, no background thread needed: unlike the "detects a change after it
|
||||
// starts waiting" tests above, this is exactly the case system_event_await() must handle -
|
||||
// sequence already moved ahead of consumed_sequence before await() is even called.
|
||||
NetworkConnectedEvent connected { .device = nullptr, .ipv4_addr = 0x0A000001, .gateway = 0x0A0000FE };
|
||||
CHECK_EQ(system_event_emit(KERNEL_EVENT_NETWORK_CONNECTED, &connected, sizeof(connected)), ERROR_NONE);
|
||||
|
||||
CHECK_EQ(system_event_await(&sub, 0), ERROR_NONE);
|
||||
|
||||
NetworkConnectedEvent received {};
|
||||
CHECK_EQ(system_event_get_data(&sub, reinterpret_cast<uint8_t*>(&received), sizeof(received)), ERROR_NONE);
|
||||
CHECK_EQ(received.ipv4_addr, connected.ipv4_addr);
|
||||
CHECK_EQ(received.gateway, connected.gateway);
|
||||
|
||||
// The pending event was consumed by the call above - a second await() with no further
|
||||
// emit must time out rather than returning the same event again.
|
||||
CHECK_EQ(system_event_await(&sub, 0), ERROR_TIMEOUT);
|
||||
|
||||
system_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_await times out when no matching event has arrived") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_TIME_CHANGED;
|
||||
system_event_subscribe(&sub);
|
||||
|
||||
CHECK_EQ(system_event_await(&sub, 0), ERROR_TIMEOUT);
|
||||
|
||||
system_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_emit does not notify a poll subscriber of a different type") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_BOOT_COMPLETED;
|
||||
system_event_subscribe(&sub);
|
||||
|
||||
system_event_emit(KERNEL_EVENT_TIME_CHANGED, nullptr, 0);
|
||||
CHECK_EQ(system_event_await(&sub, 0), ERROR_TIMEOUT);
|
||||
|
||||
system_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_get_data reports ERROR_BUFFER_OVERFLOW and leaves the buffer untouched") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_NETWORK_DISCONNECTED;
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
// system_event_await() only detects sequence increments that happen *after* it starts
|
||||
// waiting (see the comment above), so the emit must come from another task while this one
|
||||
// is already blocked in await() - same pattern as the payload-delivery test above.
|
||||
NetworkDisconnectedEvent disconnected { .device = nullptr };
|
||||
auto* thread = thread_alloc_full(
|
||||
"system-event-emitter",
|
||||
4096,
|
||||
[](void* context) {
|
||||
delay_millis(20);
|
||||
auto* disconnected_ptr = static_cast<NetworkDisconnectedEvent*>(context);
|
||||
system_event_emit(KERNEL_EVENT_NETWORK_DISCONNECTED, disconnected_ptr, sizeof(*disconnected_ptr));
|
||||
return 0;
|
||||
},
|
||||
&disconnected,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
CHECK_EQ(system_event_await(&sub, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
CHECK_EQ(thread_join(thread, 2, 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
|
||||
uint8_t tiny[1] = { 0xAA };
|
||||
CHECK_EQ(system_event_get_data(&sub, tiny, sizeof(tiny)), ERROR_BUFFER_OVERFLOW);
|
||||
CHECK_EQ(tiny[0], 0xAA);
|
||||
|
||||
uint8_t exact[sizeof(NetworkDisconnectedEvent)];
|
||||
CHECK_EQ(system_event_get_data(&sub, exact, sizeof(exact)), ERROR_NONE);
|
||||
|
||||
system_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("system_event_get_data on a subscription with no payload copies nothing and succeeds") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_BOOT_COMPLETED;
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
auto* thread = thread_alloc_full(
|
||||
"system-event-emitter",
|
||||
4096,
|
||||
[](void*) {
|
||||
delay_millis(20);
|
||||
system_event_emit(KERNEL_EVENT_BOOT_COMPLETED, nullptr, 0);
|
||||
return 0;
|
||||
},
|
||||
nullptr,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
CHECK_EQ(system_event_await(&sub, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
CHECK_EQ(thread_join(thread, 2, 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
|
||||
uint8_t buffer[1] = { 0x42 };
|
||||
CHECK_EQ(system_event_get_data(&sub, buffer, 0), ERROR_NONE);
|
||||
CHECK_EQ(buffer[0], 0x42); // untouched - nothing to copy
|
||||
|
||||
system_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
// Regression coverage for system_event_unsubscribe() racing a task blocked in
|
||||
// system_event_await() on the same subscription, and for reusing a subscription node after
|
||||
// unsubscribing it - see the @warning on system_event_unsubscribe() in system_event.h.
|
||||
|
||||
TEST_CASE("system_event_unsubscribe wakes a task blocked in system_event_await with ERROR_INVALID_STATE") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_SERVICE_STARTED;
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
auto* thread = thread_alloc_full(
|
||||
"system-event-awaiter",
|
||||
4096,
|
||||
[](void* context) {
|
||||
auto* awaited_sub = static_cast<SystemEventSubscription*>(context);
|
||||
// Long timeout - the point is that unsubscribe() wakes this early, not that it
|
||||
// eventually times out on its own.
|
||||
return static_cast<int32_t>(system_event_await(awaited_sub, pdMS_TO_TICKS(5000)));
|
||||
},
|
||||
&sub,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
|
||||
// Give the awaiter task a moment to actually reach xSemaphoreTake() before unsubscribing -
|
||||
// otherwise this test wouldn't exercise the "already blocked" race at all.
|
||||
delay_millis(20);
|
||||
|
||||
// Must return promptly (nudging the blocked awaiter awake), not by waiting out its timeout.
|
||||
TickType_t before = get_ticks();
|
||||
CHECK_EQ(system_event_unsubscribe(&sub), ERROR_NONE);
|
||||
CHECK_LT(get_ticks() - before, pdMS_TO_TICKS(1000));
|
||||
|
||||
CHECK_EQ(thread_join(thread, pdMS_TO_TICKS(2000), pdMS_TO_TICKS(1)), ERROR_NONE);
|
||||
CHECK_EQ(thread_get_return_code(thread), ERROR_INVALID_STATE);
|
||||
thread_free(thread);
|
||||
|
||||
// A second unsubscribe() has nothing left to do.
|
||||
CHECK_EQ(system_event_unsubscribe(&sub), ERROR_NOT_FOUND);
|
||||
}
|
||||
|
||||
TEST_CASE("a subscription node can be re-subscribed after system_event_unsubscribe") {
|
||||
SystemEventSubscription sub {};
|
||||
sub.event.type = KERNEL_EVENT_SERVICE_STOPPED;
|
||||
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
CHECK_EQ(system_event_unsubscribe(&sub), ERROR_NONE);
|
||||
|
||||
// Re-registering the same node (same storage, not a fresh SystemEventSubscription) must
|
||||
// work as if it were new - a fresh semaphore, and no leftover `cancelled` state from the
|
||||
// unsubscribe() above causing an immediate spurious ERROR_INVALID_STATE below.
|
||||
CHECK_EQ(system_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
auto* thread = thread_alloc_full(
|
||||
"system-event-emitter",
|
||||
4096,
|
||||
[](void*) {
|
||||
delay_millis(20);
|
||||
system_event_emit(KERNEL_EVENT_SERVICE_STOPPED, nullptr, 0);
|
||||
return 0;
|
||||
},
|
||||
nullptr,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
CHECK_EQ(system_event_await(&sub, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
CHECK_EQ(thread_join(thread, pdMS_TO_TICKS(2000), pdMS_TO_TICKS(1)), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
|
||||
CHECK_EQ(system_event_unsubscribe(&sub), ERROR_NONE);
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/concurrent/thread.h>
|
||||
|
||||
TEST_CASE("when a thread is started then its callback should be called") {
|
||||
bool has_called = false;
|
||||
auto* thread = thread_alloc_full(
|
||||
"immediate return task",
|
||||
4096,
|
||||
[](void* context) {
|
||||
auto* has_called_ptr = static_cast<bool*>(context);
|
||||
*has_called_ptr = true;
|
||||
return 0;
|
||||
},
|
||||
&has_called,
|
||||
-1
|
||||
);
|
||||
|
||||
CHECK(!has_called);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
CHECK_EQ(thread_join(thread, 2, 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
CHECK(has_called);
|
||||
}
|
||||
|
||||
TEST_CASE("a thread can be started and stopped") {
|
||||
bool interrupted = false;
|
||||
auto* thread = thread_alloc_full(
|
||||
"interruptable thread",
|
||||
4096,
|
||||
[](void* context) {
|
||||
auto* interrupted_ptr = static_cast<bool*>(context);
|
||||
while (!*interrupted_ptr) {
|
||||
delay_millis(1);
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
&interrupted,
|
||||
-1
|
||||
);
|
||||
|
||||
CHECK(thread);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
interrupted = true;
|
||||
CHECK_EQ(thread_join(thread, 2, 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
}
|
||||
|
||||
TEST_CASE("thread id should only be set at when thread is started") {
|
||||
bool interrupted = false;
|
||||
auto* thread = thread_alloc_full(
|
||||
"interruptable thread",
|
||||
4096,
|
||||
[](void* context) {
|
||||
auto* interrupted_ptr = static_cast<bool*>(context);
|
||||
while (!*interrupted_ptr) {
|
||||
delay_millis(1);
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
&interrupted,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_get_task_handle(thread), nullptr);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
CHECK_NE(thread_get_task_handle(thread), nullptr);
|
||||
interrupted = true;
|
||||
CHECK_EQ(thread_join(thread, 2, 1), ERROR_NONE);
|
||||
CHECK_EQ(thread_get_task_handle(thread), nullptr);
|
||||
thread_free(thread);
|
||||
}
|
||||
|
||||
TEST_CASE("thread state should be correct") {
|
||||
bool interrupted = false;
|
||||
auto* thread = thread_alloc_full(
|
||||
"interruptable thread",
|
||||
4096,
|
||||
[](void* context) {
|
||||
auto* interrupted_ptr = static_cast<bool*>(context);
|
||||
while (!*interrupted_ptr) {
|
||||
delay_millis(1);
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
&interrupted,
|
||||
-1
|
||||
|
||||
);
|
||||
CHECK_EQ(thread_get_state(thread), THREAD_STATE_STOPPED);
|
||||
thread_start(thread);
|
||||
auto state = thread_get_state(thread);
|
||||
CHECK((state == THREAD_STATE_STARTING || state == THREAD_STATE_RUNNING));
|
||||
interrupted = true;
|
||||
CHECK_EQ(thread_join(thread, 10, 1), ERROR_NONE);
|
||||
CHECK_EQ(thread_get_state(thread), THREAD_STATE_STOPPED);
|
||||
thread_free(thread);
|
||||
}
|
||||
|
||||
TEST_CASE("thread id should only be set at when thread is started") {
|
||||
auto* thread = thread_alloc_full(
|
||||
"return code",
|
||||
4096,
|
||||
[](void* context) { return 123; },
|
||||
nullptr,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
CHECK_EQ(thread_join(thread, 1, 1), ERROR_NONE);
|
||||
CHECK_EQ(thread_get_return_code(thread), 123);
|
||||
thread_free(thread);
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
TEST_CASE("delay ticks should be accurate within 1 tick") {
|
||||
auto start_time = get_ticks();
|
||||
delay_ticks(100);
|
||||
auto end_time = get_ticks();
|
||||
auto difference = end_time - start_time;
|
||||
CHECK_EQ(difference >= 100, true);
|
||||
CHECK_EQ(difference <= 101, true);
|
||||
}
|
||||
|
||||
TEST_CASE("delay millis should be accurate within 1 tick") {
|
||||
auto start_time = get_millis();
|
||||
delay_millis(100);
|
||||
auto end_time = get_millis();
|
||||
auto difference = end_time - start_time;
|
||||
CHECK_EQ(difference >= 100, true);
|
||||
CHECK_EQ(difference <= 101, true);
|
||||
}
|
||||
|
||||
TEST_CASE("microsecond time should be accurate within 1 tick") {
|
||||
auto start_time = get_micros_since_boot();
|
||||
delay_millis(100);
|
||||
auto end_time = get_micros_since_boot();
|
||||
auto difference = (end_time - start_time) / 1000;
|
||||
CHECK_EQ(difference >= 99, true);
|
||||
CHECK_EQ(difference <= 101, true);
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include <tactility/concurrent/timer.h>
|
||||
#include <tactility/delay.h>
|
||||
|
||||
TEST_CASE("timer_alloc and timer_free should handle allocation and deallocation") {
|
||||
auto callback = [](void* context) {};
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 10, callback, nullptr);
|
||||
CHECK_NE(timer, nullptr);
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("timer_start and timer_stop should change running state") {
|
||||
auto callback = [](void* context) {};
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 10, callback, nullptr);
|
||||
REQUIRE_NE(timer, nullptr);
|
||||
|
||||
CHECK_EQ(timer_is_running(timer), false);
|
||||
CHECK_EQ(timer_start(timer), ERROR_NONE);
|
||||
CHECK_EQ(timer_is_running(timer), true);
|
||||
CHECK_EQ(timer_stop(timer), ERROR_NONE);
|
||||
CHECK_EQ(timer_is_running(timer), false);
|
||||
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("one-shot timer should fire callback once") {
|
||||
std::atomic<int> call_count{0};
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 10, [](void* context) {
|
||||
auto* count = static_cast<std::atomic<int>*>(context);
|
||||
(*count)++;
|
||||
}, &call_count);
|
||||
REQUIRE_NE(timer, nullptr);
|
||||
|
||||
CHECK_EQ(timer_start(timer), ERROR_NONE);
|
||||
delay_millis(20);
|
||||
|
||||
CHECK_EQ(call_count.load(), 1);
|
||||
CHECK_EQ(timer_is_running(timer), false);
|
||||
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("periodic timer should fire callback multiple times") {
|
||||
std::atomic<int> call_count{0};
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_PERIODIC, 10, [](void* context) {
|
||||
auto* count = static_cast<std::atomic<int>*>(context);
|
||||
(*count)++;
|
||||
}, &call_count);
|
||||
REQUIRE_NE(timer, nullptr);
|
||||
|
||||
CHECK_EQ(timer_start(timer), ERROR_NONE);
|
||||
delay_millis(35); // Should fire around 3 times
|
||||
|
||||
CHECK_GE(call_count.load(), 3);
|
||||
CHECK_EQ(timer_is_running(timer), true);
|
||||
|
||||
timer_stop(timer);
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("timer_reset should restart the timer") {
|
||||
std::atomic<int> call_count{0};
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 20, [](void* context) {
|
||||
auto* count = static_cast<std::atomic<int>*>(context);
|
||||
(*count)++;
|
||||
}, &call_count);
|
||||
REQUIRE_NE(timer, nullptr);
|
||||
|
||||
CHECK_EQ(timer_start(timer), ERROR_NONE);
|
||||
delay_millis(10);
|
||||
CHECK_EQ(call_count.load(), 0);
|
||||
|
||||
// Resetting should push the expiry further
|
||||
CHECK_EQ(timer_reset(timer), ERROR_NONE);
|
||||
delay_millis(15);
|
||||
CHECK_EQ(call_count.load(), 0); // Still shouldn't have fired if reset worked
|
||||
|
||||
delay_millis(10);
|
||||
CHECK_EQ(call_count.load(), 1); // Now it should have fired
|
||||
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("timer_reset_with_interval should change the period") {
|
||||
std::atomic<int> call_count{0};
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 40, [](void* context) {
|
||||
auto* count = static_cast<std::atomic<int>*>(context);
|
||||
(*count)++;
|
||||
}, &call_count);
|
||||
REQUIRE_NE(timer, nullptr);
|
||||
|
||||
CHECK_EQ(timer_start(timer), ERROR_NONE);
|
||||
// Change to a much shorter interval
|
||||
CHECK_EQ(timer_reset_with_interval(timer, 10), ERROR_NONE);
|
||||
|
||||
delay_millis(20);
|
||||
CHECK_EQ(call_count.load(), 1);
|
||||
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("timer_get_expiry_time should return a valid time") {
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 10, [](void* context) {}, nullptr);
|
||||
REQUIRE_NE(timer, nullptr);
|
||||
|
||||
timer_start(timer);
|
||||
TickType_t expiry = timer_get_expiry_time(timer);
|
||||
// Expiry should be in the future
|
||||
CHECK_GT(expiry, xTaskGetTickCount());
|
||||
|
||||
timer_free(timer);
|
||||
}
|
||||
|
||||
TEST_CASE("timer_set_pending_callback should execute callback in timer task") {
|
||||
std::atomic<bool> called{false};
|
||||
struct Context {
|
||||
std::atomic<bool>* called;
|
||||
uint32_t expected_arg;
|
||||
uint32_t received_arg;
|
||||
} context = { &called, 0x12345678, 0 };
|
||||
|
||||
auto pending_cb = [](void* ctx, uint32_t arg) {
|
||||
auto* c = static_cast<Context*>(ctx);
|
||||
c->received_arg = arg;
|
||||
c->called->store(true);
|
||||
};
|
||||
|
||||
// timer_set_pending_callback doesn't actually use the timer object in current implementation
|
||||
// but we need one for the API
|
||||
struct Timer* timer = timer_alloc(TIMER_TYPE_ONCE, 10, [](void* context) {}, nullptr);
|
||||
|
||||
CHECK_EQ(timer_set_pending_callback(timer, pending_cb, &context, context.expected_arg, portMAX_DELAY), ERROR_NONE);
|
||||
|
||||
// Wait for timer task to process the callback
|
||||
int retries = 10;
|
||||
while (!called.load() && retries-- > 0) {
|
||||
delay_millis(10);
|
||||
}
|
||||
|
||||
CHECK(called.load());
|
||||
CHECK_EQ(context.received_arg, context.expected_arg);
|
||||
|
||||
timer_free(timer);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
project(AppModuleTests)
|
||||
|
||||
enable_language(C CXX ASM)
|
||||
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
|
||||
add_executable(AppModuleTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(AppModuleTests PRIVATE ${DOCTESTINC})
|
||||
|
||||
add_test(NAME AppModuleTests COMMAND AppModuleTests)
|
||||
|
||||
target_link_libraries(AppModuleTests PUBLIC
|
||||
TactilityKernel
|
||||
app-module
|
||||
service-module
|
||||
platform-posix
|
||||
freertos_kernel
|
||||
)
|
||||
@@ -1,137 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <app/event.h>
|
||||
|
||||
#include <tactility/concurrent/thread.h>
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
TEST_CASE("app_event_subscribe/_await deliver events in FIFO order") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 1;
|
||||
CHECK_EQ(app_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
for (uint32_t i = 0; i < 3; i++) {
|
||||
AppEvent event { .type = APP_EVENT_RESULT, .timestamp = 0, .result = { .launch_id = i, .result = 0 } };
|
||||
CHECK_EQ(app_event_emit(1, &event), ERROR_NONE);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < 3; i++) {
|
||||
AppEvent out {};
|
||||
CHECK_EQ(app_event_await(&sub, &out, 0), ERROR_NONE);
|
||||
CHECK_EQ(out.type, APP_EVENT_RESULT);
|
||||
CHECK_EQ(out.result.launch_id, i);
|
||||
}
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("app_event_emit only delivers to subscriptions for that app_instance_id") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 10;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
AppEvent event { .type = APP_EVENT_CLOSE, .timestamp = 0, .result = {} };
|
||||
CHECK_EQ(app_event_emit(11, &event), ERROR_NOT_FOUND);
|
||||
|
||||
AppEvent out {};
|
||||
CHECK_EQ(app_event_await(&sub, &out, 0), ERROR_TIMEOUT);
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("app_event_emit returns ERROR_RESOURCE and drops the newest event once a subscription's queue is full") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 20;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
for (uint32_t i = 0; i < APP_EVENT_QUEUE_CAPACITY; i++) {
|
||||
AppEvent event { .type = APP_EVENT_RESULT, .timestamp = 0, .result = { .launch_id = i, .result = 0 } };
|
||||
CHECK_EQ(app_event_emit(20, &event), ERROR_NONE);
|
||||
}
|
||||
|
||||
// Queue is now full; this one should be dropped.
|
||||
AppEvent overflow_event { .type = APP_EVENT_RESULT, .timestamp = 0, .result = { .launch_id = 999, .result = 0 } };
|
||||
CHECK_EQ(app_event_emit(20, &overflow_event), ERROR_RESOURCE);
|
||||
|
||||
// The already-queued events survive, in order, and the dropped one never arrives.
|
||||
for (uint32_t i = 0; i < APP_EVENT_QUEUE_CAPACITY; i++) {
|
||||
AppEvent out {};
|
||||
CHECK_EQ(app_event_await(&sub, &out, 0), ERROR_NONE);
|
||||
CHECK_EQ(out.result.launch_id, i);
|
||||
}
|
||||
AppEvent out {};
|
||||
CHECK_EQ(app_event_await(&sub, &out, 0), ERROR_TIMEOUT);
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("app_event_unsubscribe stops further delivery") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 30;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
CHECK_EQ(app_event_unsubscribe(&sub), ERROR_NONE);
|
||||
CHECK_EQ(app_event_unsubscribe(&sub), ERROR_NOT_FOUND);
|
||||
|
||||
AppEvent event { .type = APP_EVENT_CLOSE, .timestamp = 0, .result = {} };
|
||||
CHECK_EQ(app_event_emit(30, &event), ERROR_NOT_FOUND);
|
||||
}
|
||||
|
||||
TEST_CASE("app_event_await times out when no event has arrived") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 40;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
AppEvent out {};
|
||||
CHECK_EQ(app_event_await(&sub, &out, 0), ERROR_TIMEOUT);
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("app_event_emit stamps the event with the current boot-relative time") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 50;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
auto before = static_cast<uint64_t>(get_micros_since_boot());
|
||||
AppEvent event { .type = APP_EVENT_CLOSE, .timestamp = 0, .result = {} };
|
||||
app_event_emit(50, &event);
|
||||
auto after = static_cast<uint64_t>(get_micros_since_boot());
|
||||
|
||||
AppEvent out {};
|
||||
REQUIRE_EQ(app_event_await(&sub, &out, 0), ERROR_NONE);
|
||||
CHECK_GE(out.timestamp, before);
|
||||
CHECK_LE(out.timestamp, after);
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
}
|
||||
|
||||
TEST_CASE("app_event_await wakes when the event is emitted from another task") {
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = 60;
|
||||
CHECK_EQ(app_event_subscribe(&sub), ERROR_NONE);
|
||||
|
||||
auto* thread = thread_alloc_full(
|
||||
"app-event-emitter",
|
||||
4096,
|
||||
[](void*) -> int32_t {
|
||||
delay_millis(20);
|
||||
AppEvent event { .type = APP_EVENT_CLOSE, .timestamp = 0, .result = {} };
|
||||
app_event_emit(60, &event);
|
||||
return 0;
|
||||
},
|
||||
nullptr,
|
||||
-1
|
||||
);
|
||||
CHECK_EQ(thread_start(thread), ERROR_NONE);
|
||||
|
||||
AppEvent out {};
|
||||
CHECK_EQ(app_event_await(&sub, &out, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
CHECK_EQ(out.type, APP_EVENT_CLOSE);
|
||||
|
||||
CHECK_EQ(thread_join(thread, pdMS_TO_TICKS(2000), 1), ERROR_NONE);
|
||||
thread_free(thread);
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
}
|
||||
@@ -1,456 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <app/event.h>
|
||||
#include <app/loader.h>
|
||||
#include <app/manager.h>
|
||||
|
||||
#include <service/manager.h>
|
||||
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/freertos/task.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
extern ServiceManifest app_internal_loader_service_manifest;
|
||||
|
||||
namespace {
|
||||
|
||||
error_t fake_load(AppLocation, void** out_runtime) {
|
||||
*out_runtime = nullptr;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// Stashed by fake_run() on every call, for tests that need to verify exactly what argc/argv it
|
||||
// received (e.g. that app-module deep-copied the caller's argv) without a getter to query it
|
||||
// through.
|
||||
int last_received_argc = -1;
|
||||
std::vector<std::string> last_received_argv;
|
||||
|
||||
// fake_run() runs on the app's own task; stash_received_arguments() writes
|
||||
// last_received_argc/last_received_argv there while a test thread reads them - wait_for_state()
|
||||
// only establishes that the instance reached APP_INSTANCE_STATE_ACTIVE (set before
|
||||
// AppLoaderApi::run() is even called, i.e. before fake_run() runs at all), not that
|
||||
// stash_received_arguments() has finished writing. This flag is the actual ordering: reset
|
||||
// before starting the app, set (release) as the last step of stash_received_arguments(), waited
|
||||
// on (acquire) before a test reads the stashed values.
|
||||
std::atomic<bool> arguments_stashed { false };
|
||||
|
||||
void stash_received_arguments(int argc, char* argv[]) {
|
||||
last_received_argc = argc;
|
||||
last_received_argv.clear();
|
||||
for (int i = 0; i < argc; i++) {
|
||||
last_received_argv.emplace_back(argv[i]);
|
||||
}
|
||||
arguments_stashed.store(true, std::memory_order_release);
|
||||
}
|
||||
|
||||
// A minimal stand-in for a real app's main(): subscribes to its own app_event stream and exits
|
||||
// as soon as it's asked to close - exactly the contract every app instance (with its own
|
||||
// dedicated task for its whole lifetime) is expected to follow. If launched with a single
|
||||
// parameter (app_manager_start_for_result()), acts as a modal dialog instead: returns the
|
||||
// requested result (argv[0], parsed as an int) immediately (the app's own return value IS the
|
||||
// delivered APP_EVENT_RESULT.result - see app_scheduler.cpp's thread_main()).
|
||||
int32_t fake_run(void*, uint32_t app_instance_id, int argc, char* argv[]) {
|
||||
stash_received_arguments(argc, argv);
|
||||
|
||||
if (argc == 1) {
|
||||
// Single-arg shortcut used by the start_for_result() result-delivery tests: returns
|
||||
// immediately with argv[0] parsed as the result code, instead of running the normal
|
||||
// event loop below. Tests that pass other argc (0, or >1 to check deep-copy) fall
|
||||
// through and run the loop as usual.
|
||||
return static_cast<int32_t>(strtol(argv[0], nullptr, 10));
|
||||
}
|
||||
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = app_instance_id;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
while (true) {
|
||||
AppEvent event {};
|
||||
if (app_event_await(&sub, &event, pdMS_TO_TICKS(5000)) != ERROR_NONE) {
|
||||
break; // safety net so a bug here can't hang the test suite
|
||||
}
|
||||
if (event.type == APP_EVENT_CLOSE) {
|
||||
app_manager_finish(app_instance_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
app_event_unsubscribe(&sub);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fake_unload(void*) {
|
||||
}
|
||||
|
||||
AppLoaderApi fake_loader_api = {
|
||||
.load = fake_load,
|
||||
.run = fake_run,
|
||||
.unload = fake_unload,
|
||||
};
|
||||
|
||||
void* create_loader_service(const ServiceManifest*) {
|
||||
return &fake_loader_api;
|
||||
}
|
||||
|
||||
void destroy_loader_service(const ServiceManifest*, void*) {
|
||||
}
|
||||
|
||||
ServiceManifest fake_loader_manifest = {
|
||||
.id = APP_LOADER_PATH_SERVICE_ID,
|
||||
.create_service = create_loader_service,
|
||||
.destroy_service = destroy_loader_service,
|
||||
.on_start = nullptr,
|
||||
.on_stop = nullptr,
|
||||
};
|
||||
|
||||
void ensure_fake_loader_registered() {
|
||||
static bool registered = false;
|
||||
if (!registered) {
|
||||
CHECK_EQ(service_manager_add(&fake_loader_manifest, /*auto_start=*/true), ERROR_NONE);
|
||||
registered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// app-module's real APP_LOCATION_MEMORY loader (source/app_internal_loader.cpp) - not a fake,
|
||||
// since it has no platform dependency and is exactly what a statically-linked app would go
|
||||
// through.
|
||||
void ensure_memory_loader_registered() {
|
||||
static bool registered = false;
|
||||
if (!registered) {
|
||||
CHECK_EQ(service_manager_add(&app_internal_loader_service_manifest, /*auto_start=*/true), ERROR_NONE);
|
||||
registered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Same subscribe-until-close contract as fake_run() above, but called directly as an AppMainFn -
|
||||
// this is what a real internal app's entry point looks like.
|
||||
int32_t fake_app_main(uint32_t app_instance_id, int argc, char* argv[]) {
|
||||
return fake_run(nullptr, app_instance_id, argc, argv);
|
||||
}
|
||||
|
||||
// Wraps app_manager_get_topmost_instance_id() for terse assertions: 0 if no app is Active.
|
||||
AppInstanceId topmost_instance_id() {
|
||||
AppInstanceId id = 0;
|
||||
return app_manager_get_topmost_instance_id(&id) == ERROR_NONE ? id : 0;
|
||||
}
|
||||
|
||||
bool wait_for_state(uint32_t instance_id, AppInstanceState target, uint32_t timeout_ms) {
|
||||
uint32_t waited = 0;
|
||||
while (waited < timeout_ms) {
|
||||
if (app_manager_get_state(instance_id) == target) {
|
||||
return true;
|
||||
}
|
||||
delay_millis(10);
|
||||
waited += 10;
|
||||
}
|
||||
return app_manager_get_state(instance_id) == target;
|
||||
}
|
||||
|
||||
bool wait_for_arguments_stashed(uint32_t timeout_ms) {
|
||||
uint32_t waited = 0;
|
||||
while (waited < timeout_ms) {
|
||||
if (arguments_stashed.load(std::memory_order_acquire)) {
|
||||
return true;
|
||||
}
|
||||
delay_millis(10);
|
||||
waited += 10;
|
||||
}
|
||||
return arguments_stashed.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("app_manager_start activates an app instance, app_manager_stop terminates it") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
AppManifest manifest { "test.app.a", "Test App A", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest), ERROR_NONE);
|
||||
|
||||
uint32_t instance_id = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.a", &instance_id), ERROR_NONE);
|
||||
CHECK(wait_for_state(instance_id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
CHECK_EQ(app_manager_stop(instance_id), ERROR_NONE);
|
||||
CHECK_EQ(app_manager_get_state(instance_id), APP_INSTANCE_STATE_STOPPED);
|
||||
|
||||
app_manager_remove("test.app.a");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start never touches another already-running app - every instance gets its own task") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
AppManifest manifest_b { "test.app.b", "Test App B", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
AppManifest manifest_c { "test.app.c", "Test App C", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest_b), ERROR_NONE);
|
||||
REQUIRE_EQ(app_manager_add(&manifest_c), ERROR_NONE);
|
||||
|
||||
uint32_t id_b = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.b", &id_b), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_b, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
uint32_t id_c = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.c", &id_c), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_c, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
// b is untouched by c starting - both stay Active at once, each with its own task.
|
||||
CHECK_EQ(app_manager_get_state(id_b), APP_INSTANCE_STATE_ACTIVE);
|
||||
|
||||
app_manager_stop(id_b);
|
||||
app_manager_stop(id_c);
|
||||
app_manager_remove("test.app.b");
|
||||
app_manager_remove("test.app.c");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start always creates a fresh instance, even for the same manifest id twice") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
AppManifest manifest { "test.app.twice", "Test App Twice", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest), ERROR_NONE);
|
||||
|
||||
uint32_t id_first = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.twice", &id_first), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_first, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
uint32_t id_second = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.twice", &id_second), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_second, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
CHECK_NE(id_first, id_second);
|
||||
CHECK_EQ(app_manager_get_state(id_first), APP_INSTANCE_STATE_ACTIVE);
|
||||
|
||||
app_manager_stop(id_first);
|
||||
app_manager_stop(id_second);
|
||||
app_manager_remove("test.app.twice");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_get_state returns STOPPED for an unknown instance id") {
|
||||
CHECK_EQ(app_manager_get_state(999999), APP_INSTANCE_STATE_STOPPED);
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start_with_parameters deep-copies argv before the app instance receives it") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
AppManifest manifest { "test.app.args", "Test App Args", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest), ERROR_NONE);
|
||||
|
||||
uint32_t instance_id = 0;
|
||||
arguments_stashed.store(false, std::memory_order_relaxed);
|
||||
{
|
||||
// Caller's argv is stack-local and goes out of scope immediately after this block -
|
||||
// proves app-module made its own copy rather than aliasing the caller's strings.
|
||||
std::string ssid = "MyNetwork";
|
||||
std::string password = "hunter2";
|
||||
const char* argv[] = { ssid.c_str(), password.c_str() };
|
||||
REQUIRE_EQ(app_manager_start_with_parameters("test.app.args", 2, argv, &instance_id), ERROR_NONE);
|
||||
}
|
||||
CHECK(wait_for_state(instance_id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
REQUIRE(wait_for_arguments_stashed(1000));
|
||||
|
||||
REQUIRE_EQ(last_received_argc, 2);
|
||||
REQUIRE_EQ(last_received_argv.size(), 2u);
|
||||
CHECK_EQ(last_received_argv[0], "MyNetwork");
|
||||
CHECK_EQ(last_received_argv[1], "hunter2");
|
||||
|
||||
app_manager_stop(instance_id);
|
||||
app_manager_remove("test.app.args");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_add rejects a duplicate id") {
|
||||
AppManifest manifest { "test.app.dup", "Test App Dup", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest), ERROR_NONE);
|
||||
CHECK_EQ(app_manager_add(&manifest), ERROR_INVALID_ARGUMENT);
|
||||
app_manager_remove("test.app.dup");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_for_each_manifest visits every registered manifest, including newly added ones") {
|
||||
AppManifest manifest_x { "test.app.foreach.x", "X", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
AppManifest manifest_y { "test.app.foreach.y", "Y", APP_CATEGORY_SETTINGS, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest_x), ERROR_NONE);
|
||||
REQUIRE_EQ(app_manager_add(&manifest_y), ERROR_NONE);
|
||||
|
||||
std::vector<std::string> seen_ids;
|
||||
app_manager_for_each_manifest([](const AppManifest* manifest, void* context) {
|
||||
static_cast<std::vector<std::string>*>(context)->emplace_back(manifest->id);
|
||||
}, &seen_ids);
|
||||
|
||||
CHECK(std::ranges::find(seen_ids, "test.app.foreach.x") != seen_ids.end());
|
||||
CHECK(std::ranges::find(seen_ids, "test.app.foreach.y") != seen_ids.end());
|
||||
|
||||
app_manager_remove("test.app.foreach.x");
|
||||
app_manager_remove("test.app.foreach.y");
|
||||
|
||||
seen_ids.clear();
|
||||
app_manager_for_each_manifest([](const AppManifest* manifest, void* context) {
|
||||
static_cast<std::vector<std::string>*>(context)->emplace_back(manifest->id);
|
||||
}, &seen_ids);
|
||||
CHECK(std::ranges::find(seen_ids, "test.app.foreach.x") == seen_ids.end());
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start fails for an unregistered manifest id") {
|
||||
uint32_t instance_id = 0;
|
||||
CHECK_EQ(app_manager_start("test.app.nonexistent", &instance_id), ERROR_NOT_FOUND);
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start runs an APP_LOCATION_MEMORY app via its function pointer, through the real internal loader") {
|
||||
ensure_memory_loader_registered();
|
||||
|
||||
AppManifest manifest {
|
||||
"test.app.memory",
|
||||
"Test App Memory",
|
||||
APP_CATEGORY_USER,
|
||||
{ APP_LOCATION_MEMORY, reinterpret_cast<void*>(fake_app_main) }
|
||||
};
|
||||
REQUIRE_EQ(app_manager_add(&manifest), ERROR_NONE);
|
||||
|
||||
uint32_t instance_id = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.memory", &instance_id), ERROR_NONE);
|
||||
CHECK(wait_for_state(instance_id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
CHECK_EQ(app_manager_stop(instance_id), ERROR_NONE);
|
||||
CHECK_EQ(app_manager_get_state(instance_id), APP_INSTANCE_STATE_STOPPED);
|
||||
|
||||
app_manager_remove("test.app.memory");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start_for_result delivers APP_EVENT_RESULT to the parent, which stays Active throughout") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
AppManifest parent_manifest { "test.app.parent", "Parent", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
AppManifest child_manifest { "test.app.child", "Child", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&parent_manifest), ERROR_NONE);
|
||||
REQUIRE_EQ(app_manager_add(&child_manifest), ERROR_NONE);
|
||||
|
||||
uint32_t parent_id = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.parent", &parent_id), ERROR_NONE);
|
||||
CHECK(wait_for_state(parent_id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
AppEventSubscription parent_sub {};
|
||||
parent_sub.app_instance_id = parent_id;
|
||||
REQUIRE_EQ(app_event_subscribe(&parent_sub), ERROR_NONE);
|
||||
|
||||
const char* argv[] = { "42" };
|
||||
uint32_t child_id = 0;
|
||||
REQUIRE_EQ(app_manager_start_for_result("test.app.child", parent_id, 1, argv, &child_id), ERROR_NONE);
|
||||
|
||||
// Launching a modal child never touches the parent's own task/state.
|
||||
CHECK_EQ(app_manager_get_state(parent_id), APP_INSTANCE_STATE_ACTIVE);
|
||||
|
||||
AppEvent event {};
|
||||
REQUIRE_EQ(app_event_await(&parent_sub, &event, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
CHECK_EQ(event.type, APP_EVENT_RESULT);
|
||||
CHECK_EQ(event.result.launch_id, child_id);
|
||||
CHECK_EQ(event.result.result, 42);
|
||||
|
||||
app_event_unsubscribe(&parent_sub);
|
||||
app_manager_stop(child_id);
|
||||
app_manager_stop(parent_id);
|
||||
app_manager_remove("test.app.parent");
|
||||
app_manager_remove("test.app.child");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_start_for_result delivers the child's own return value as the result") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
AppManifest parent_manifest { "test.app.parent2", "Parent2", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
AppManifest child_manifest { "test.app.child2", "Child2", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&parent_manifest), ERROR_NONE);
|
||||
REQUIRE_EQ(app_manager_add(&child_manifest), ERROR_NONE);
|
||||
|
||||
uint32_t parent_id = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.parent2", &parent_id), ERROR_NONE);
|
||||
CHECK(wait_for_state(parent_id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
AppEventSubscription parent_sub {};
|
||||
parent_sub.app_instance_id = parent_id;
|
||||
REQUIRE_EQ(app_event_subscribe(&parent_sub), ERROR_NONE);
|
||||
|
||||
uint32_t child_id = 0;
|
||||
// No parameters - fake_run falls through to its normal CLOSE loop instead of acting as a
|
||||
// dialog.
|
||||
REQUIRE_EQ(app_manager_start_for_result("test.app.child2", parent_id, 0, nullptr, &child_id), ERROR_NONE);
|
||||
CHECK(wait_for_state(child_id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
app_manager_stop(child_id); // force-close
|
||||
|
||||
AppEvent event {};
|
||||
REQUIRE_EQ(app_event_await(&parent_sub, &event, pdMS_TO_TICKS(2000)), ERROR_NONE);
|
||||
CHECK_EQ(event.type, APP_EVENT_RESULT);
|
||||
CHECK_EQ(event.result.launch_id, child_id);
|
||||
CHECK_EQ(event.result.result, 0); // fake_run's CLOSE loop always returns 0
|
||||
|
||||
app_event_unsubscribe(&parent_sub);
|
||||
app_manager_stop(parent_id);
|
||||
app_manager_remove("test.app.parent2");
|
||||
app_manager_remove("test.app.child2");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_get_topmost_instance_id returns NOT_FOUND when nothing is active, then tracks who's on top") {
|
||||
ensure_fake_loader_registered();
|
||||
AppInstanceId id = 999999;
|
||||
CHECK_EQ(app_manager_get_topmost_instance_id(&id), ERROR_NOT_FOUND);
|
||||
|
||||
AppManifest manifest_a { "test.app.top_a", "A", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
AppManifest manifest_b { "test.app.top_b", "B", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest_a), ERROR_NONE);
|
||||
REQUIRE_EQ(app_manager_add(&manifest_b), ERROR_NONE);
|
||||
|
||||
uint32_t id_a = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.top_a", &id_a), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_a, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
CHECK_EQ(topmost_instance_id(), id_a);
|
||||
|
||||
// a stays Active - b just has a higher (more recently allocated) instance id, so it becomes
|
||||
// topmost without a superseding/saving.
|
||||
uint32_t id_b = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.top_b", &id_b), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_b, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
CHECK_EQ(topmost_instance_id(), id_b);
|
||||
|
||||
char app_id_buffer[64];
|
||||
REQUIRE_EQ(app_manager_get_topmost_app_id(app_id_buffer, sizeof(app_id_buffer)), ERROR_NONE);
|
||||
CHECK_EQ(std::string(app_id_buffer), "test.app.top_b");
|
||||
|
||||
// A modal child stays Active alongside its parent while shown - the child (started more
|
||||
// recently) must be reported as topmost, not the parent. No parameters, so fake_run() takes
|
||||
// its persistent CLOSE loop branch instead of instantly resolving like a real dialog would -
|
||||
// needed here so there's a reliable window to observe it as topmost.
|
||||
uint32_t id_c = 0;
|
||||
REQUIRE_EQ(app_manager_start_for_result("test.app.top_a", id_b, 0, nullptr, &id_c), ERROR_NONE);
|
||||
CHECK(wait_for_state(id_c, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
CHECK_EQ(topmost_instance_id(), id_c);
|
||||
|
||||
app_manager_stop(id_c);
|
||||
CHECK_EQ(topmost_instance_id(), id_b);
|
||||
|
||||
app_manager_stop(id_a);
|
||||
app_manager_stop(id_b);
|
||||
app_manager_remove("test.app.top_a");
|
||||
app_manager_remove("test.app.top_b");
|
||||
}
|
||||
|
||||
TEST_CASE("app_manager_get_topmost_app_id returns BUFFER_OVERFLOW for a too-small buffer, NOT_FOUND when nothing is active") {
|
||||
ensure_fake_loader_registered();
|
||||
|
||||
char buffer[4];
|
||||
CHECK_EQ(app_manager_get_topmost_app_id(buffer, sizeof(buffer)), ERROR_NOT_FOUND);
|
||||
CHECK_EQ(app_manager_get_topmost_app_id(buffer, 0), ERROR_BUFFER_OVERFLOW);
|
||||
|
||||
AppManifest manifest { "test.app.top_overflow", "Overflow", APP_CATEGORY_USER, { APP_LOCATION_PATH, nullptr } };
|
||||
REQUIRE_EQ(app_manager_add(&manifest), ERROR_NONE);
|
||||
|
||||
uint32_t id = 0;
|
||||
REQUIRE_EQ(app_manager_start("test.app.top_overflow", &id), ERROR_NONE);
|
||||
CHECK(wait_for_state(id, APP_INSTANCE_STATE_ACTIVE, 1000));
|
||||
|
||||
// "test.app.top_overflow" doesn't fit in a 4-byte buffer.
|
||||
CHECK_EQ(app_manager_get_topmost_app_id(buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
|
||||
app_manager_stop(id);
|
||||
app_manager_remove("test.app.top_overflow");
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
#include "doctest.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
int result;
|
||||
} TestTaskData;
|
||||
|
||||
void test_task(void* parameter) {
|
||||
auto* data = (TestTaskData*)parameter;
|
||||
|
||||
doctest::Context context;
|
||||
|
||||
context.applyCommandLine(data->argc, data->argv);
|
||||
|
||||
// overrides
|
||||
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||
|
||||
data->result = context.run();
|
||||
|
||||
vTaskEndScheduler();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TestTaskData data = {
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.result = 0
|
||||
};
|
||||
|
||||
BaseType_t task_result = xTaskCreate(
|
||||
test_task,
|
||||
"test_task",
|
||||
8192,
|
||||
&data,
|
||||
1,
|
||||
nullptr
|
||||
);
|
||||
|
||||
if (task_result != pdPASS) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
vTaskStartScheduler();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
project(CryptModuleTests)
|
||||
|
||||
enable_language(C CXX ASM)
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
|
||||
add_executable(CryptModuleTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(CryptModuleTests PRIVATE ${DOCTESTINC})
|
||||
|
||||
add_test(NAME CryptModuleTests COMMAND CryptModuleTests)
|
||||
|
||||
target_link_libraries(CryptModuleTests PUBLIC
|
||||
crypt-module
|
||||
TactilityKernel
|
||||
freertos_kernel
|
||||
)
|
||||
@@ -1,195 +0,0 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <crypt/crypt.h>
|
||||
#include <cstring>
|
||||
|
||||
TEST_CASE("crypt_encrypt followed by crypt_decrypt returns the original data") {
|
||||
uint8_t iv[16];
|
||||
crypt_get_iv("test-seed", 9, iv);
|
||||
|
||||
uint8_t plaintext[16];
|
||||
memcpy(plaintext, "0123456789abcdef", sizeof(plaintext));
|
||||
|
||||
uint8_t encrypted[16];
|
||||
CHECK_EQ(crypt_encrypt(iv, plaintext, encrypted, sizeof(encrypted)), 0);
|
||||
|
||||
// Re-derive the same IV, as a real caller would when decrypting later
|
||||
uint8_t iv2[16];
|
||||
crypt_get_iv("test-seed", 9, iv2);
|
||||
|
||||
uint8_t decrypted[16];
|
||||
CHECK_EQ(crypt_decrypt(iv2, encrypted, decrypted, sizeof(decrypted)), 0);
|
||||
|
||||
CHECK_EQ(memcmp(plaintext, decrypted, sizeof(plaintext)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_encrypt with a length that isn't a multiple of 16 fails without crashing") {
|
||||
uint8_t iv[16] = {};
|
||||
uint8_t plaintext[15] = {};
|
||||
uint8_t encrypted[15] = {};
|
||||
CHECK_NE(crypt_encrypt(iv, plaintext, encrypted, sizeof(plaintext)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_decrypt with a length that isn't a multiple of 16 fails without crashing") {
|
||||
uint8_t iv[16] = {};
|
||||
uint8_t ciphertext[15] = {};
|
||||
uint8_t decrypted[15] = {};
|
||||
CHECK_NE(crypt_decrypt(iv, ciphertext, decrypted, sizeof(ciphertext)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_encrypt with a zero length fails without crashing") {
|
||||
uint8_t iv[16] = {};
|
||||
uint8_t plaintext[1] = {};
|
||||
uint8_t encrypted[1] = {};
|
||||
CHECK_NE(crypt_encrypt(iv, plaintext, encrypted, 0), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_get_iv is deterministic for the same input") {
|
||||
uint8_t iv1[16];
|
||||
uint8_t iv2[16];
|
||||
crypt_get_iv("same-input", 10, iv1);
|
||||
crypt_get_iv("same-input", 10, iv2);
|
||||
CHECK_EQ(memcmp(iv1, iv2, sizeof(iv1)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_get_iv derives a non-zero IV from non-trivial input") {
|
||||
uint8_t iv[16];
|
||||
crypt_get_iv("test-seed", 9, iv);
|
||||
|
||||
uint8_t zero[16] = {};
|
||||
CHECK_NE(memcmp(iv, zero, sizeof(iv)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_get_iv derives different IVs from different input") {
|
||||
uint8_t iv1[16];
|
||||
uint8_t iv2[16];
|
||||
crypt_get_iv("input-one", 9, iv1);
|
||||
crypt_get_iv("input-two", 9, iv2);
|
||||
CHECK_NE(memcmp(iv1, iv2, sizeof(iv1)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_generate_iv produces a non-zero IV") {
|
||||
uint8_t iv[16];
|
||||
crypt_generate_iv(iv);
|
||||
|
||||
uint8_t zero[16] = {};
|
||||
CHECK_NE(memcmp(iv, zero, sizeof(iv)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_generate_iv is not deterministic across calls") {
|
||||
uint8_t iv1[16];
|
||||
uint8_t iv2[16];
|
||||
crypt_generate_iv(iv1);
|
||||
crypt_generate_iv(iv2);
|
||||
CHECK_NE(memcmp(iv1, iv2, sizeof(iv1)), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("crypt_encrypt followed by crypt_decrypt works with a randomly generated IV") {
|
||||
uint8_t iv[16];
|
||||
crypt_generate_iv(iv);
|
||||
|
||||
uint8_t plaintext[16];
|
||||
memcpy(plaintext, "0123456789abcdef", sizeof(plaintext));
|
||||
|
||||
uint8_t encrypted[16];
|
||||
CHECK_EQ(crypt_encrypt(iv, plaintext, encrypted, sizeof(encrypted)), 0);
|
||||
|
||||
// A random IV must be stored/transmitted alongside the ciphertext and reused as-is for decryption
|
||||
uint8_t decrypted[16];
|
||||
CHECK_EQ(crypt_decrypt(iv, encrypted, decrypted, sizeof(decrypted)), 0);
|
||||
|
||||
CHECK_EQ(memcmp(plaintext, decrypted, sizeof(plaintext)), 0);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#include "doctest.h"
|
||||
#include <crypt/hash.h>
|
||||
#include <cstring>
|
||||
|
||||
TEST_CASE("djb2_str of an empty string returns the DJB2 seed value") {
|
||||
CHECK_EQ(djb2_str(""), 5381u);
|
||||
}
|
||||
|
||||
TEST_CASE("djb2_str produces the well-known DJB2 hash for a string") {
|
||||
CHECK_EQ(djb2_str("hello"), 261238937u);
|
||||
}
|
||||
|
||||
TEST_CASE("djb2_str is deterministic for the same input") {
|
||||
CHECK_EQ(djb2_str("tactility"), djb2_str("tactility"));
|
||||
}
|
||||
|
||||
TEST_CASE("djb2_str produces different hashes for different input") {
|
||||
CHECK_NE(djb2_str("tactility"), djb2_str("Tactility"));
|
||||
}
|
||||
|
||||
TEST_CASE("djb2_data of an empty buffer returns the DJB2 seed value") {
|
||||
CHECK_EQ(djb2_data("", 0), 5381u);
|
||||
}
|
||||
|
||||
TEST_CASE("djb2_data matches djb2_str for the same bytes") {
|
||||
const char* text = "tactility";
|
||||
CHECK_EQ(djb2_data(text, strlen(text)), djb2_str(text));
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
#include "doctest.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
int result;
|
||||
} TestTaskData;
|
||||
|
||||
void test_task(void* parameter) {
|
||||
auto* data = (TestTaskData*)parameter;
|
||||
|
||||
doctest::Context context;
|
||||
|
||||
context.applyCommandLine(data->argc, data->argv);
|
||||
|
||||
// overrides
|
||||
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||
|
||||
data->result = context.run();
|
||||
|
||||
vTaskEndScheduler();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TestTaskData data = {
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.result = 0
|
||||
};
|
||||
|
||||
BaseType_t task_result = xTaskCreate(
|
||||
test_task,
|
||||
"test_task",
|
||||
8192,
|
||||
&data,
|
||||
1,
|
||||
nullptr
|
||||
);
|
||||
assert(task_result == pdPASS);
|
||||
|
||||
vTaskStartScheduler();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
|
||||
// NOTE: This is normally provided by the platform kernel module, but that's not loaded for crypt-module
|
||||
extern "C" {
|
||||
// Required for FreeRTOS
|
||||
void vAssertCalled(unsigned long line, const char* const file) {
|
||||
__assert_fail("assert failed", file, line, "");
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
project(ServiceModuleTests)
|
||||
|
||||
enable_language(C CXX ASM)
|
||||
|
||||
|
||||
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
|
||||
add_executable(ServiceModuleTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
|
||||
|
||||
target_include_directories(ServiceModuleTests PRIVATE ${DOCTESTINC})
|
||||
|
||||
add_test(NAME ServiceModuleTests COMMAND ServiceModuleTests)
|
||||
|
||||
target_link_libraries(ServiceModuleTests PUBLIC
|
||||
TactilityKernel
|
||||
service-module
|
||||
platform-posix
|
||||
freertos_kernel
|
||||
)
|
||||
@@ -1,195 +0,0 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
#include "doctest.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char** argv;
|
||||
int result;
|
||||
} TestTaskData;
|
||||
|
||||
void test_task(void* parameter) {
|
||||
auto* data = (TestTaskData*)parameter;
|
||||
|
||||
doctest::Context context;
|
||||
|
||||
context.applyCommandLine(data->argc, data->argv);
|
||||
|
||||
// overrides
|
||||
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
|
||||
|
||||
data->result = context.run();
|
||||
|
||||
vTaskEndScheduler();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TestTaskData data = {
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.result = 0
|
||||
};
|
||||
|
||||
BaseType_t task_result = xTaskCreate(
|
||||
test_task,
|
||||
"test_task",
|
||||
8192,
|
||||
&data,
|
||||
1,
|
||||
nullptr
|
||||
);
|
||||
assert(task_result == pdPASS);
|
||||
|
||||
vTaskStartScheduler();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <service/paths.h>
|
||||
|
||||
#include <tactility/paths.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("paths_get_user_data_path returns a non-empty path") {
|
||||
char buffer[192];
|
||||
CHECK_EQ(paths_get_user_data_path(buffer, sizeof(buffer)), ERROR_NONE);
|
||||
CHECK_GT(std::strlen(buffer), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("paths_get_user_data_path reports overflow for a too-small buffer") {
|
||||
char buffer[1];
|
||||
CHECK_EQ(paths_get_user_data_path(buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
}
|
||||
|
||||
TEST_CASE("service_paths_get_user_data_directory includes the service id") {
|
||||
char root[192];
|
||||
REQUIRE_EQ(paths_get_user_data_path(root, sizeof(root)), ERROR_NONE);
|
||||
|
||||
char buffer[224];
|
||||
CHECK_EQ(service_paths_get_user_data_directory("my-service", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
|
||||
std::string expected = std::string(root) + "/service/my-service";
|
||||
CHECK_EQ(std::string(buffer), expected);
|
||||
}
|
||||
|
||||
TEST_CASE("service_paths_get_user_data_path appends the child path") {
|
||||
char directory[224];
|
||||
REQUIRE_EQ(service_paths_get_user_data_directory("my-service", directory, sizeof(directory)), ERROR_NONE);
|
||||
|
||||
char buffer[256];
|
||||
CHECK_EQ(service_paths_get_user_data_path("my-service", "settings.properties", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
|
||||
std::string expected = std::string(directory) + "/settings.properties";
|
||||
CHECK_EQ(std::string(buffer), expected);
|
||||
}
|
||||
|
||||
TEST_CASE("service_paths_get_assets_directory is nested under the user data directory") {
|
||||
char directory[224];
|
||||
REQUIRE_EQ(service_paths_get_user_data_directory("my-service", directory, sizeof(directory)), ERROR_NONE);
|
||||
|
||||
char buffer[256];
|
||||
CHECK_EQ(service_paths_get_assets_directory("my-service", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
|
||||
std::string expected = std::string(directory) + "/assets";
|
||||
CHECK_EQ(std::string(buffer), expected);
|
||||
}
|
||||
|
||||
TEST_CASE("service_paths_get_assets_path appends the child path") {
|
||||
char directory[224];
|
||||
REQUIRE_EQ(service_paths_get_assets_directory("my-service", directory, sizeof(directory)), ERROR_NONE);
|
||||
|
||||
char buffer[256];
|
||||
CHECK_EQ(service_paths_get_assets_path("my-service", "icon.png", buffer, sizeof(buffer)), ERROR_NONE);
|
||||
|
||||
std::string expected = std::string(directory) + "/icon.png";
|
||||
CHECK_EQ(std::string(buffer), expected);
|
||||
}
|
||||
|
||||
TEST_CASE("service_paths functions report overflow for a too-small buffer") {
|
||||
char buffer[1];
|
||||
CHECK_EQ(service_paths_get_user_data_directory("my-service", buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
CHECK_EQ(service_paths_get_user_data_path("my-service", "child", buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
CHECK_EQ(service_paths_get_assets_directory("my-service", buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
CHECK_EQ(service_paths_get_assets_path("my-service", "child", buffer, sizeof(buffer)), ERROR_BUFFER_OVERFLOW);
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
#include "doctest.h"
|
||||
|
||||
#include <service/manager.h>
|
||||
|
||||
// Defined in service_instance.cpp. Internal-only, exposed here to test try_get/put gating.
|
||||
extern "C" void service_instance_set_state(ServiceInstance* instance, ServiceState state);
|
||||
|
||||
static int create_called = 0;
|
||||
static int destroy_called = 0;
|
||||
static int on_start_called = 0;
|
||||
static int on_stop_called = 0;
|
||||
static error_t on_start_result = ERROR_NONE;
|
||||
static const ServiceManifest* last_create_manifest = nullptr;
|
||||
static const ServiceManifest* last_destroy_manifest = nullptr;
|
||||
|
||||
static void* test_create_service(const ServiceManifest* manifest) {
|
||||
create_called++;
|
||||
last_create_manifest = manifest;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void test_destroy_service(const ServiceManifest* manifest, void*) {
|
||||
destroy_called++;
|
||||
last_destroy_manifest = manifest;
|
||||
}
|
||||
|
||||
static error_t test_on_start(ServiceInstance*, void*) {
|
||||
on_start_called++;
|
||||
return on_start_result;
|
||||
}
|
||||
|
||||
static void test_on_stop(ServiceInstance*, void*) {
|
||||
on_stop_called++;
|
||||
}
|
||||
|
||||
static void reset_counters() {
|
||||
create_called = 0;
|
||||
destroy_called = 0;
|
||||
on_start_called = 0;
|
||||
on_stop_called = 0;
|
||||
on_start_result = ERROR_NONE;
|
||||
last_create_manifest = nullptr;
|
||||
last_destroy_manifest = nullptr;
|
||||
}
|
||||
|
||||
TEST_CASE("ServiceInstance construction and destruction") {
|
||||
reset_counters();
|
||||
|
||||
static const ServiceManifest manifest = {
|
||||
.id = "instance-test",
|
||||
.create_service = test_create_service,
|
||||
.destroy_service = test_destroy_service,
|
||||
.on_start = test_on_start,
|
||||
.on_stop = test_on_stop
|
||||
};
|
||||
|
||||
ServiceInstance instance = { .manifest = nullptr, .data = nullptr, .internal = nullptr };
|
||||
|
||||
CHECK_EQ(service_instance_construct(&instance, &manifest), ERROR_NONE);
|
||||
CHECK_NE(instance.internal, nullptr);
|
||||
CHECK_EQ(instance.manifest, &manifest);
|
||||
CHECK_EQ(create_called, 1);
|
||||
CHECK_EQ(last_create_manifest, &manifest);
|
||||
CHECK_EQ(service_instance_get_state(&instance), SERVICE_STATE_STOPPED);
|
||||
|
||||
CHECK_EQ(service_instance_destruct(&instance), ERROR_NONE);
|
||||
CHECK_EQ(instance.internal, nullptr);
|
||||
CHECK_EQ(destroy_called, 1);
|
||||
CHECK_EQ(last_destroy_manifest, &manifest);
|
||||
}
|
||||
|
||||
TEST_CASE("service_manager_add rejects duplicate ids") {
|
||||
reset_counters();
|
||||
|
||||
static const ServiceManifest manifest = {
|
||||
.id = "duplicate-test",
|
||||
.create_service = test_create_service,
|
||||
.destroy_service = test_destroy_service
|
||||
};
|
||||
|
||||
CHECK_EQ(service_manager_add(&manifest, false), ERROR_NONE);
|
||||
CHECK_EQ(service_manager_add(&manifest, false), ERROR_INVALID_ARGUMENT);
|
||||
|
||||
CHECK_EQ(service_manager_remove("duplicate-test"), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("service_registration start/stop lifecycle") {
|
||||
reset_counters();
|
||||
|
||||
static const ServiceManifest manifest = {
|
||||
.id = "lifecycle-test",
|
||||
.create_service = test_create_service,
|
||||
.destroy_service = test_destroy_service,
|
||||
.on_start = test_on_start,
|
||||
.on_stop = test_on_stop
|
||||
};
|
||||
|
||||
CHECK_EQ(service_manager_add(&manifest, false), ERROR_NONE);
|
||||
CHECK_EQ(service_manager_get_state("lifecycle-test"), SERVICE_STATE_STOPPED);
|
||||
|
||||
CHECK_EQ(service_manager_start("lifecycle-test"), ERROR_NONE);
|
||||
CHECK_EQ(on_start_called, 1);
|
||||
CHECK_EQ(service_manager_get_state("lifecycle-test"), SERVICE_STATE_STARTED);
|
||||
CHECK_NE(service_manager_find_instance("lifecycle-test"), nullptr);
|
||||
|
||||
// Starting again while already started should fail
|
||||
CHECK_EQ(service_manager_start("lifecycle-test"), ERROR_INVALID_STATE);
|
||||
|
||||
// Removing while running should fail
|
||||
CHECK_EQ(service_manager_remove("lifecycle-test"), ERROR_INVALID_STATE);
|
||||
|
||||
CHECK_EQ(service_manager_stop("lifecycle-test"), ERROR_NONE);
|
||||
CHECK_EQ(on_stop_called, 1);
|
||||
CHECK_EQ(service_manager_get_state("lifecycle-test"), SERVICE_STATE_STOPPED);
|
||||
CHECK_EQ(service_manager_find_instance("lifecycle-test"), nullptr);
|
||||
|
||||
// Stopping again while already stopped should fail
|
||||
CHECK_EQ(service_manager_stop("lifecycle-test"), ERROR_NOT_FOUND);
|
||||
|
||||
CHECK_EQ(service_manager_remove("lifecycle-test"), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("service_manager_add with auto_start") {
|
||||
reset_counters();
|
||||
|
||||
static const ServiceManifest manifest = {
|
||||
.id = "auto-start-test",
|
||||
.create_service = test_create_service,
|
||||
.destroy_service = test_destroy_service,
|
||||
.on_start = test_on_start,
|
||||
.on_stop = test_on_stop
|
||||
};
|
||||
|
||||
CHECK_EQ(service_manager_add(&manifest, true), ERROR_NONE);
|
||||
CHECK_EQ(on_start_called, 1);
|
||||
CHECK_EQ(service_manager_get_state("auto-start-test"), SERVICE_STATE_STARTED);
|
||||
|
||||
CHECK_EQ(service_manager_stop("auto-start-test"), ERROR_NONE);
|
||||
CHECK_EQ(service_manager_remove("auto-start-test"), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("service_manager_start failure leaves service stopped") {
|
||||
reset_counters();
|
||||
on_start_result = ERROR_RESOURCE;
|
||||
|
||||
static const ServiceManifest manifest = {
|
||||
.id = "failing-start-test",
|
||||
.create_service = test_create_service,
|
||||
.destroy_service = test_destroy_service,
|
||||
.on_start = test_on_start,
|
||||
.on_stop = test_on_stop
|
||||
};
|
||||
|
||||
CHECK_EQ(service_manager_add(&manifest, false), ERROR_NONE);
|
||||
CHECK_EQ(service_manager_start("failing-start-test"), ERROR_RESOURCE);
|
||||
CHECK_EQ(service_manager_get_state("failing-start-test"), SERVICE_STATE_STOPPED);
|
||||
CHECK_EQ(service_manager_find_instance("failing-start-test"), nullptr);
|
||||
|
||||
CHECK_EQ(service_manager_remove("failing-start-test"), ERROR_NONE);
|
||||
}
|
||||
|
||||
TEST_CASE("service_registration lookup functions with unknown id") {
|
||||
CHECK_EQ(service_manager_get_state("unknown-service-id"), SERVICE_STATE_STOPPED);
|
||||
CHECK_EQ(service_manager_find_manifest("unknown-service-id"), nullptr);
|
||||
CHECK_EQ(service_manager_find_instance("unknown-service-id"), nullptr);
|
||||
CHECK_EQ(service_manager_start("unknown-service-id"), ERROR_NOT_FOUND);
|
||||
CHECK_EQ(service_manager_stop("unknown-service-id"), ERROR_NOT_FOUND);
|
||||
CHECK_EQ(service_manager_remove("unknown-service-id"), ERROR_NOT_FOUND);
|
||||
}
|
||||
Reference in New Issue
Block a user