Louis Ke
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Unit test framework for kernel (ktfff) ----------------------------- ## Introduction This framework is a unit test runner which integrates the following open source: 1. [ktf (kernel test framework)](https://github.com/oracle/ktf) 2. [fff (fake function framework)](https://github.com/meekrosoft/fff) A first open source is a kernel unit test framework, but it hasn't covered the mock feature, that will cause some unit test case could not reproduce easy. So, we join the fff framework with ktf, then we could use mock function and could run the unit test in linux kernel. ## Options Usage: ./ktfff.sh [argument...] #### Argument: * ```install```: * install the ktfff required pakage (gtest, ktf). * ```run```: * build all kernel module and run all unittest case. ```--test=<test case>``` * build the specific kernel module and run the specific unit test case. ```--suite=<test file>``` * build the specific kernel module and run the specific unit test file. * ```clean```: * remove all file generated by ktfff. * ```uninstall```: * uninstall ktfff framework. * ```-h, --help```: * display this help text and exit. ## Usage **ktfff setup** ktfff install will build the gtest and ktf in ~/ktfff. ``` ./ktfff.sh install ``` **Run all unit test** ``` ./ktfff.sh run ``` **Run specific unit test case** ``` ./ktfff.sh run --test=hello_ok --test=hello_fail ``` **Run specific unit test file** ``` ./ktfff.sh run --suite=test_hello.c ``` **Run with other ktfrun options** (Hasn't implement) ``` ``` **Clean env** ``` ./ktfff.sh clean ``` **ktfff uninstall** ``` ./ktfff.sh uninstall ``` ## Unit test example #### Coding rule ``` KTF_INIT(); TEST(unit_name, test_name) { EXPECT_TRUE(true); } ADD_TEST(test_name); ``` ``` Prep: KTF_INIT(); DECLARE_F(fixture_name) <attributes> }; INIT_F(fixture_name,setup,teardown); Usage: SETUP_F(fixture_name,setup) { <setup code, set ctx->ok to true to have the test executed> } TEARDOWN_F(fixture_name,teardown) { <teardown code> } TEST_F(fixture_name,unit_name,test_name) { <test code> } ``` example: [link](https://github.com/oracle/ktf/blob/master/examples/h3.c) #### Assersion [ktfff Assertion docs](https://github.com/oracle/ktf/blob/master/kernel/ktf.h#L213) ## Mock function #### How to Mock If you want to mock the function in ktfff, could reference the following template, you should add the comment that `// MOCK_FILE: <mock_filename>` in your test file, the `./ktfff.sh` will through this comment to exclude the mock file in makefile. Due to `fff.h` will reference `FAKE_VOID_FUNC` or `FAKE_VALUE_FUNC` to redefine the function which you want to mock, so we couldn't include this file to compile together. **Step**: 1. Include the fff header `#include "fff.h"` 2. Init fff `DEFINE_FFF_GLOBALS;` 3. Comment your mock function `// MOCK_FILE: mock filename` 4. Using fff macro to mock your function `FAKE_VOID_FUNC(void function);` or `FAKE_VALUE_FUNC(return type, function);` *Example in ktfff_unittest/unittest/test_hello.c* ``` #include "../lib/fff.h" DEFINE_FFF_GLOBALS; // MOCK_FILE: drv_main.c FAKE_VALUE_FUNC(int32_t, init_drv_component); FAKE_VALUE_FUNC(int32_t, rel_drv_component); RESET_FAKE(init_drv_component); RESET_FAKE(rel_drv_component); ``` ##### fff Macro Table: | Macro | Description | Example | |-------|-------------|---------| | `FAKE_VOID_FUNC(fn [,arg_types*]);` |Define a fake function named fn returning void with n arguments | `FAKE_VOID_FUNC(DISPLAY_output_message, const char*);` | | `FAKE_VALUE_FUNC(return_type, fn [,arg_types*]);` | Define a fake function returning a value with type return_type taking n arguments | `FAKE_VALUE_FUNC(int, DISPLAY_get_line_insert_index);` | | `RESET_FAKE(fn);` | Reset the state of fake function called fn | `RESET_FAKE(DISPLAY_init);` | #### Mock function structure fff will also defines a struct `"function_name"_fake` which contains all the information about the fake, and support some method for test. For instance: |Usage|Description|Referencee| |-----|-----------|----------| |`"function_name"_fake.call_count`| will incremented every time the faked function is called |[more..](https://github.com/meekrosoft/fff/blob/master/README.md#hello-fake-world) |`"function_name"_fake.return_val`| define a mock function that returns a value|[more..](https://github.com/meekrosoft/fff/blob/master/README.md#return-values)| More mock function information: [fff docs](https://github.com/meekrosoft/fff/blob/master/README.md) --- ## ktfff implemention ![](https://i.imgur.com/RmIpEre.png) ## ktfff file structure ``` . ├── ... ├── unittest-mk-temp # Each unit test makefiles folder ├── unittest-module-temp # Each test case kernel module temporary folder ├── unittest-output # Unit test result temporary folder ├── unittest # Unit test files ├── lib # Library for ktfff │ ├── config.sh # Define all global variable │ ├── fff.h # fff header file │ ├── install.sh # Install function of ktfff │ ├── kerN_ver_patch.sh # Patch dms_kernel_version.h with different linux kernel version │ ├── makefile.sh # Generate && build Makefile function │ ├── message.sh # Message information ouput for ktfff runner │ └── unittest.sh # About unit test trigger function and info message function ├── gtest2html # A template which transfer xml to html ├── ktfff.sh # ktfff runner main script file └── Makefile_ktfff # Template Makefile for ktfff ``` ## Run unit test #### ktfrun **Step**: 1. Insert kernel module - ktf.ko ``` sudo insmod ktfff/build/4.4.0-134-generic/ktf/kernel/ktf.ko ``` 2. Insert unit test kernel modlue - test_hello.ko ``` sudo insmod unittest-module-temp/4.4.0-134-generic/test_hello.ko ``` 3. Run ktfrun ``` ktfrun [==========] Running 2 tests from 1 test case. [----------] Global test environment set-up. [----------] 2 tests from examples [ RUN ] examples./hello_fail [ OK ] examples./hello_fail (1 ms) [ RUN ] examples./hello_ok [ OK ] examples./hello_ok (0 ms) [----------] 2 tests from examples (1 ms total) [----------] Global test environment tear-down [==========] 2 tests from 1 test case ran. (1 ms total) [ PASSED ] 2 tests. ``` #### ktfff Runner ``` ./ktfff.sh run [INFO] BUILDING KERNEL MODULE ... ... [INFO] UNNITEST START ====================================================================================== ktfff_unittest :: kernel version - 4.4.0-134-generic Test ====================================================================================== [==========] Running 2 tests from 1 test case. [----------] Global test environment set-up. [----------] 2 tests from examples [ RUN ] examples./hello_fail [ OK ] examples./hello_fail (0 ms) [ RUN ] examples./hello_ok [ OK ] examples./hello_ok (0 ms) [----------] 2 tests from examples (0 ms total) [----------] Global test environment tear-down [==========] 2 tests from 1 test case ran. (1 ms total) [ PASSED ] 2 tests. -------------------------------------------------------------------------------------- Output: /home/louis/clientnode-uf/BlockDeviceDriver2/ktfff_unittest/unittest-output/4.4.0-134-generic/output.xml Report: /home/louis/clientnode-uf/BlockDeviceDriver2/ktfff_unittest/unittest-output/4.4.0-134-generic/report.html ``` #### ktfff result ![](https://i.imgur.com/GJmk2SS.png) ## Resources ktf: [link](https://github.com/oracle/ktf/) fff: [link](https://github.com/meekrosoft/fff)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully