gobot.io/x/gobot/v2@v2.1.0/drivers/common/mfrc522/mfrc522_pcd_test.go (about)

     1  package mfrc522
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gobot.io/x/gobot/v2/gobottest"
     7  )
     8  
     9  type busConnMock struct {
    10  	written []byte
    11  	readIdx int
    12  	simRead []byte
    13  	fifoIdx int
    14  	simFifo []byte
    15  }
    16  
    17  func (c *busConnMock) ReadByteData(reg uint8) (uint8, error) {
    18  	c.written = append(c.written, reg)
    19  
    20  	switch reg {
    21  	case regFIFOLevel:
    22  		return uint8(len(c.simFifo)), nil
    23  	case regFIFOData:
    24  		c.fifoIdx++
    25  		return c.simFifo[c.fifoIdx-1], nil
    26  	default:
    27  		if len(c.simRead) > 0 {
    28  			c.readIdx++
    29  			return c.simRead[c.readIdx-1], nil
    30  		}
    31  		return 0, nil
    32  	}
    33  }
    34  
    35  func (c *busConnMock) WriteByteData(reg uint8, data byte) error {
    36  	c.written = append(c.written, reg)
    37  	c.written = append(c.written, data)
    38  	return nil
    39  }
    40  
    41  func initTestMFRC522CommonWithStubbedConnector() (*MFRC522Common, *busConnMock) {
    42  	c := &busConnMock{}
    43  	d := NewMFRC522Common()
    44  	d.connection = c
    45  	return d, c
    46  }
    47  
    48  func TestNewMFRC522Common(t *testing.T) {
    49  	// act
    50  	d := NewMFRC522Common()
    51  	// assert
    52  	gobottest.Refute(t, d, nil)
    53  }
    54  
    55  func TestInitialize(t *testing.T) {
    56  	// arrange
    57  	wantSoftReset := []byte{0x01, 0x0F, 0x01}
    58  	wantInit := []byte{0x12, 0x00, 0x13, 0x00, 0x24, 0x26, 0x2A, 0x8F, 0x2B, 0xFF, 0x2D, 0xE8, 0x2C, 0x03, 0x15, 0x40, 0x11, 0x29}
    59  	wantAntennaOn := []byte{0x14, 0x14, 0x03}
    60  	wantGain := []byte{0x26, 0x50}
    61  	c := &busConnMock{}
    62  	d := NewMFRC522Common()
    63  	// act
    64  	err := d.Initialize(c)
    65  	// assert
    66  	gobottest.Assert(t, err, nil)
    67  	gobottest.Assert(t, d.connection, c)
    68  	gobottest.Assert(t, c.written[:3], wantSoftReset)
    69  	gobottest.Assert(t, c.written[3:21], wantInit)
    70  	gobottest.Assert(t, c.written[21:24], wantAntennaOn)
    71  	gobottest.Assert(t, c.written[24:], wantGain)
    72  }
    73  
    74  func Test_getVersion(t *testing.T) {
    75  	// arrange
    76  	d, c := initTestMFRC522CommonWithStubbedConnector()
    77  	wantWritten := []byte{0x37}
    78  	const want = uint8(5)
    79  	c.simRead = []byte{want}
    80  	// act
    81  	got, err := d.getVersion()
    82  	// assert
    83  	gobottest.Assert(t, err, nil)
    84  	gobottest.Assert(t, got, want)
    85  	gobottest.Assert(t, c.written, wantWritten)
    86  }
    87  
    88  func Test_switchAntenna(t *testing.T) {
    89  	var tests = map[string]struct {
    90  		target      bool
    91  		simRead     byte
    92  		wantWritten []byte
    93  	}{
    94  		"switch_on": {
    95  			target:      true,
    96  			simRead:     0xFD,
    97  			wantWritten: []byte{0x14, 0x14, 0xFF},
    98  		},
    99  		"is_already_on": {
   100  			target:      true,
   101  			simRead:     0x03,
   102  			wantWritten: []byte{0x14},
   103  		},
   104  		"switch_off": {
   105  			target:      false,
   106  			simRead:     0x03,
   107  			wantWritten: []byte{0x14, 0x14, 0x00},
   108  		},
   109  		"is_already_off": {
   110  			target:      false,
   111  			simRead:     0xFD,
   112  			wantWritten: []byte{0x14},
   113  		},
   114  	}
   115  	for name, tc := range tests {
   116  		t.Run(name, func(t *testing.T) {
   117  			// arrange
   118  			d, c := initTestMFRC522CommonWithStubbedConnector()
   119  			c.simRead = []byte{tc.simRead}
   120  			// act
   121  			err := d.switchAntenna(tc.target)
   122  			// assert
   123  			gobottest.Assert(t, err, nil)
   124  			gobottest.Assert(t, c.written, tc.wantWritten)
   125  		})
   126  	}
   127  }
   128  
   129  func Test_stopCrypto1(t *testing.T) {
   130  	// arrange
   131  	d, c := initTestMFRC522CommonWithStubbedConnector()
   132  	c.simRead = []byte{0xFF}
   133  	wantWritten := []byte{0x08, 0x08, 0xF7}
   134  	// act
   135  	err := d.stopCrypto1()
   136  	// assert
   137  	gobottest.Assert(t, err, nil)
   138  	gobottest.Assert(t, c.written, wantWritten)
   139  }
   140  
   141  func Test_communicateWithPICC(t *testing.T) {
   142  	// arrange
   143  	d, c := initTestMFRC522CommonWithStubbedConnector()
   144  	dataToFifo := []byte{0xF1, 0xF2}
   145  	writtenPrepare := []byte{0x02, 0xF7, 0x04, 0x7F, 0x0A, 0x80, 0x01, 0x00}
   146  	writtenWriteFifo := []byte{0x09, 0xF1, 0x09, 0xF2}
   147  	writtenTransceive := []byte{0x0D, 0x00, 0x01, 0x0C}
   148  	writtenBitFramingStart := []byte{0x0D, 0x0D, 0x80}
   149  	writtenWaitAndFinish := []byte{0x04, 0x0D, 0x0D, 0x7F, 0x06}
   150  	writtenReadFifo := []byte{0x0A, 0x09, 0x09, 0x0C}
   151  	// read: bit framing register set, simulate calculation done, bit framing register clear, simulate no error,
   152  	// simulate control register 0x00
   153  	c.simRead = []byte{0x00, 0x30, 0xFF, 0x00, 0x00}
   154  	c.simFifo = []byte{0x11, 0x22}
   155  
   156  	backData := []byte{0x00, 0x00}
   157  	// act
   158  	// transceive, all 8 bits, no CRC
   159  	err := d.communicateWithPICC(0x0C, dataToFifo, backData, 0x00, false)
   160  	// assert
   161  	gobottest.Assert(t, err, nil)
   162  	gobottest.Assert(t, c.written[:8], writtenPrepare)
   163  	gobottest.Assert(t, c.written[8:12], writtenWriteFifo)
   164  	gobottest.Assert(t, c.written[12:16], writtenTransceive)
   165  	gobottest.Assert(t, c.written[16:19], writtenBitFramingStart)
   166  	gobottest.Assert(t, c.written[19:24], writtenWaitAndFinish)
   167  	gobottest.Assert(t, c.written[24:], writtenReadFifo)
   168  	gobottest.Assert(t, backData, []byte{0x11, 0x22})
   169  }
   170  
   171  func Test_calculateCRC(t *testing.T) {
   172  	// arrange
   173  	d, c := initTestMFRC522CommonWithStubbedConnector()
   174  	dataToFifo := []byte{0x02, 0x03}
   175  	writtenPrepare := []byte{0x01, 0x00, 0x05, 0x04, 0x0A, 0x80}
   176  	writtenFifo := []byte{0x09, 0x02, 0x09, 0x03}
   177  	writtenCalc := []byte{0x01, 0x03, 0x05, 0x01, 0x00}
   178  	writtenGetResult := []byte{0x22, 0x21}
   179  	c.simRead = []byte{0x04, 0x11, 0x22} // calculation done, crcL, crcH
   180  	gotCrcBack := []byte{0x00, 0x00}
   181  	// act
   182  	err := d.calculateCRC(dataToFifo, gotCrcBack)
   183  	// assert
   184  	gobottest.Assert(t, err, nil)
   185  	gobottest.Assert(t, c.written[:6], writtenPrepare)
   186  	gobottest.Assert(t, c.written[6:10], writtenFifo)
   187  	gobottest.Assert(t, c.written[10:15], writtenCalc)
   188  	gobottest.Assert(t, c.written[15:], writtenGetResult)
   189  	gobottest.Assert(t, gotCrcBack, []byte{0x11, 0x22})
   190  }
   191  
   192  func Test_writeFifo(t *testing.T) {
   193  	// arrange
   194  	d, c := initTestMFRC522CommonWithStubbedConnector()
   195  	dataToFifo := []byte{0x11, 0x22, 0x33}
   196  	wantWritten := []byte{0x09, 0x11, 0x09, 0x22, 0x09, 0x33}
   197  	// act
   198  	err := d.writeFifo(dataToFifo)
   199  	// assert
   200  	gobottest.Assert(t, err, nil)
   201  	gobottest.Assert(t, c.written, wantWritten)
   202  }
   203  
   204  func Test_readFifo(t *testing.T) {
   205  	// arrange
   206  	d, c := initTestMFRC522CommonWithStubbedConnector()
   207  	c.simFifo = []byte{0x30, 0x20, 0x10}
   208  	wantWritten := []byte{0x0A, 0x09, 0x09, 0x09, 0x0C}
   209  	backData := []byte{0x00, 0x00, 0x00}
   210  	// act
   211  	_, err := d.readFifo(backData)
   212  	// assert
   213  	gobottest.Assert(t, err, nil)
   214  	gobottest.Assert(t, c.written, wantWritten)
   215  	gobottest.Assert(t, backData, c.simFifo)
   216  }