github.com/zondax/ledger-go@v0.14.3/ledger_mock.go (about)

     1  //+build ledger_mock
     2  
     3  /*******************************************************************************
     4  *   (c) 2018 - 2022 ZondaX AG
     5  *
     6  *  Licensed under the Apache License, Version 2.0 (the "License");
     7  *  you may not use this file except in compliance with the License.
     8  *  You may obtain a copy of the License at
     9  *
    10  *      http://www.apache.org/licenses/LICENSE-2.0
    11  *
    12  *  Unless required by applicable law or agreed to in writing, software
    13  *  distributed under the License is distributed on an "AS IS" BASIS,
    14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  *  See the License for the specific language governing permissions and
    16  *  limitations under the License.
    17  ********************************************************************************/
    18  
    19  package ledger_go
    20  
    21  import (
    22  	"bytes"
    23  	"encoding/hex"
    24  	"log"
    25  )
    26  
    27  type LedgerAdminMock struct{}
    28  
    29  type LedgerDeviceMock struct{}
    30  
    31  func NewLedgerAdmin() *LedgerAdminMock {
    32  	return &LedgerAdminMock{}
    33  }
    34  
    35  func (admin *LedgerAdminMock) ListDevices() ([]string, error) {
    36  	x := []string{"Mock device"}
    37  	return x, nil
    38  }
    39  
    40  func (admin *LedgerAdminMock) CountDevices() int {
    41  	return 1
    42  }
    43  
    44  func (admin *LedgerAdminMock) Connect(deviceIndex int) (*LedgerDeviceMock, error) {
    45  	return &LedgerDeviceMock{}, nil
    46  }
    47  
    48  func (ledger *LedgerDeviceMock) Exchange(command []byte) ([]byte, error) {
    49  	// Some predetermined command/replies
    50  	infoCommand := []byte{0xE0, 0x01, 0, 0, 0}
    51  	infoReply, _ := hex.DecodeString("311000040853706563756c6f73000b53706563756c6f734d4355")
    52  
    53  	reply := []byte{}
    54  
    55  	log.Printf("exchange [mock] >>> %s", hex.EncodeToString(command))
    56  
    57  	if bytes.Equal(command, infoCommand) {
    58  		reply = infoReply
    59  	}
    60  	// always return the same
    61  
    62  	log.Printf("exchange [mock] <<< %s", hex.EncodeToString(reply))
    63  
    64  	return reply, nil
    65  }
    66  
    67  func (ledger *LedgerDeviceMock) Close() error {
    68  	// Nothing to do here
    69  	return nil
    70  }