Page 299 - 《软件学报》2021年第6期
P. 299

王颖  等:面向 ROS 的差分模糊测试方法                                                           1873


                         Algorithm 1 . Input Data Process.
                         1: Input : msg type ← The message type of CLI;
                                  _
                         2: Output :type dict {}⋅←  A dictionary including basic properties of input parameter.
                                   _
                                    _
                                        _
                         3: function ROS TYPE TO DICT
                                            _
                         4:   module importlib=  .import module (msg _type +  '.msg  ')
                         5:   ros  _ msg loader (module )
                                    _
                         6:   if  msg _type =  'array then
                                           '
                                           { module array array size data threshold〉
                         7:       type _ regexp = 〈  〉〈  〉〈  _  〉〈  _    }
                         8:      type _ match = type _ regexp .match (msg type )
                                                          _
                                               '
                         9:   else if  msy  _type =  'complex then
                         10 :    type _ regexp = { complex type type size data threshold〈  〉〈  〉〈  _  〉〈  _  } 〉
                         11:     type _ match =  type _ regexp .match (msg type )
                                                         _
                         12 :  else
                         13:     type _ regexp = { module type type size data threshold〈  〉〈  〉〈  _  〉〈  _  } 〉
                                                          _
                         14 :    type _ match = type _ regexp .match (msg type )
                         15:  end if
                         16 :  type _ dict =  type  _ match
                         17 :end  function
                                      Fig.4    Algorithm of input data process
                                        图 4   输入数据处理算法设计图
         3.1.2    种子生成
             种子生成主要是目标文件按策略处理模块返回的策略进行种子生成,产生测试用例的过程.如图 5 中算法 2
         所示,首先创建策略映射函数,将上一模块返回的 ROS 消息字段映射到策略处理模块,并按策略处理模块定义好
         的具体策略指导种子生成,如后文图 6 所示.模糊实现过程中调用 map_ros_types(⋅)函数生成随机的测试用例,在
         @setting(⋅)装饰器中通过 max_examples 控制随机数的个数.除此之外,完整的测试用例生成之前,文件中的每组
         数据使用全局变量添加唯一的序列标识,方便后续过程的种子分析及回溯.
                                Algorithm 2 .Seed Generation Process.
                               1: Input : ros clsss ←  The ros class to be fuzzed;
                                       _
                               2:    msg  _type ← The message type of CLI;
                               3:Output test data json ←  The generated test case file.
                                            .
                                      :
                                         _
                                          _
                               4: function MAP ROS _TYPES
                                              {}
                               5:   strategy _ dict =⋅
                               6:   slo _ full = list (zip (ros class slot ,ros class .slot types ))
                                                             _
                                      ts
                                                       _
                                                   _
                                                                    _
                               7:   for  s _ name s type in slots  _ full do
                                           , _
                                                          ( _
                               8:      type _ dict =  ros _type to dict s type )
                                                       _
                                                     _
                                                [ _
                               9:      strategy _ dicts s name =  _  ['type ']
                                                     ] type dict
                               10:  end for
                                                      _ pe
                               11:  msg =  map  _ ros types (msg ty )
                                              _
                                                         . ,
                                                                  . )
                                                             . ,
                               12:  test  _ data json =  json .dumps (msg x mag y msg z
                                          .
                               13:end  function
                                       Fig.5    Algorithm of seed generation
                                          图 5   种子生成算法设计图
         3.2   策略处理模块
             策略处理模块基于 Python 中 hypothesis 策略库的 st 模块定义了多个简单策略,如数组、时间戳、字符串
         等以及组合复杂策略来指导种子生成过程,如定义_Time 策略可以指导 time 类型的种子生成,st.text 策略可以指
         导字符串类型的种子生成等.
   294   295   296   297   298   299   300   301   302   303   304