"Fossies" - the Fresh Open Source Software Archive

Member "mattermost-server-6.0.1/vendor/github.com/mschoch/smat/actionseq.go" (18 Oct 2021, 1736 Bytes) of package /linux/www/mattermost-server-6.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 //  Copyright (c) 2016 Marty Schoch
    2 
    3 //  Licensed under the Apache License, Version 2.0 (the "License");
    4 //  you may not use this file except in compliance with the
    5 //  License. You may obtain a copy of the License at
    6 //    http://www.apache.org/licenses/LICENSE-2.0
    7 //  Unless required by applicable law or agreed to in writing,
    8 //  software distributed under the License is distributed on an "AS
    9 //  IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
   10 //  express or implied. See the License for the specific language
   11 //  governing permissions and limitations under the License.
   12 
   13 package smat
   14 
   15 // ActionSeq represents a sequence of actions, used for populating a corpus
   16 // of byte sequences for the corresponding fuzz tests
   17 type ActionSeq []ActionID
   18 
   19 // ByteEncoding runs the FSM to produce a byte sequence to trigger the
   20 // desired action
   21 func (a ActionSeq) ByteEncoding(ctx Context, setup, teardown ActionID, actionMap ActionMap) ([]byte, error) {
   22     setupFunc, teardownFunc, err := actionMap.findSetupTeardown(setup, teardown)
   23     if err != nil {
   24         return nil, err
   25     }
   26     state, err := setupFunc(ctx)
   27     if err != nil {
   28         return nil, err
   29     }
   30     defer func() {
   31         _, _ = teardownFunc(ctx)
   32     }()
   33 
   34     var rv []byte
   35     for _, actionID := range a {
   36         b, err := probeStateForAction(state, actionID)
   37         if err != nil {
   38             return nil, err
   39         }
   40         rv = append(rv, b)
   41         action, ok := actionMap[actionID]
   42         if !ok {
   43             continue
   44         }
   45         state, err = action(ctx)
   46         if err != nil {
   47             return nil, err
   48         }
   49     }
   50     return rv, nil
   51 }
   52 
   53 func probeStateForAction(state State, actionID ActionID) (byte, error) {
   54     for i := 0; i < 256; i++ {
   55         nextActionID := state(byte(i))
   56         if nextActionID == actionID {
   57             return byte(i), nil
   58         }
   59     }
   60     return 0, ErrActionNotPossible
   61 }