initial commit

This commit is contained in:
2021-10-02 11:40:46 +02:00
commit 4d119fdce3
34 changed files with 3410 additions and 0 deletions

View File

@@ -0,0 +1,222 @@
#include <Wire.h>
#include <PN532.h>
#include <NdefMessage.h>
#include <NdefRecord.h>
#include <ArduinoUnit.h>
void leakCheck(void (*callback)())
{
int start = freeMemory();
(*callback)();
int end = freeMemory();
Serial.println((end - start), DEC);
}
// Custom Assertion
void assertNoLeak(void (*callback)())
{
int start = freeMemory();
(*callback)();
int end = freeMemory();
assertEqual(0, (start - end));
}
void record()
{
NdefRecord* r = new NdefRecord();
delete r;
}
void emptyRecord()
{
NdefRecord* r = new NdefRecord();
#ifdef NDEF_USE_SERIAL
r->print();
#endif
delete r;
}
void textRecord()
{
NdefRecord* r = new NdefRecord();
r->setTnf(0x1);
uint8_t type[] = { 0x54 };
r->setType(type, sizeof(type));
uint8_t payload[] = { 0x1A, 0x1B, 0x1C };
r->setPayload(payload, sizeof(payload));
#ifdef NDEF_USE_SERIAL
r->print();
#endif
delete r;
}
void recordMallocZero()
{
NdefRecord r = NdefRecord();
String type = r.getType();
String id = r.getId();
byte payload[r.getPayloadLength()];
r.getPayload(payload);
}
// this is OK
void emptyMessage()
{
NdefMessage* m = new NdefMessage();
delete m;
}
// this is OK
void printEmptyMessage()
{
NdefMessage* m = new NdefMessage();
#ifdef NDEF_USE_SERIAL
m->print();
#endif
delete m;
}
// this is OK
void printEmptyMessageNoNew()
{
NdefMessage m = NdefMessage();
#ifdef NDEF_USE_SERIAL
m.print();
#endif
}
void messageWithTextRecord()
{
NdefMessage m = NdefMessage();
m.addTextRecord("foo");
#ifdef NDEF_USE_SERIAL
m.print();
#endif
}
void messageWithEmptyRecord()
{
NdefMessage m = NdefMessage();
NdefRecord r = NdefRecord();
m.addRecord(r);
#ifdef NDEF_USE_SERIAL
m.print();
#endif
}
void messageWithoutHelper()
{
NdefMessage m = NdefMessage();
NdefRecord r = NdefRecord();
r.setTnf(1);
uint8_t type[] = { 0x54 };
r.setType(type, sizeof(type));
uint8_t payload[] = { 0x02, 0x65, 0x6E, 0x66, 0x6F, 0x6F };
r.setPayload(payload, sizeof(payload));
m.addRecord(r);
#ifdef NDEF_USE_SERIAL
m.print();
#endif
}
void messageWithId()
{
NdefMessage m = NdefMessage();
NdefRecord r = NdefRecord();
r.setTnf(1);
uint8_t type[] = { 0x54 };
r.setType(type, sizeof(type));
uint8_t payload[] = { 0x02, 0x65, 0x6E, 0x66, 0x6F, 0x6F };
r.setPayload(payload, sizeof(payload));
uint8_t id[] = { 0x0, 0x0, 0x0 };
r.setId(id, sizeof(id));
m.addRecord(r);
#ifdef NDEF_USE_SERIAL
m.print();
#endif
}
void message80()
{
NdefMessage message = NdefMessage();
message.addTextRecord("This record is 80 characters.X01234567890123456789012345678901234567890123456789");
#ifdef NDEF_USE_SERIAL
//message.print();
#endif
}
void message100()
{
NdefMessage message = NdefMessage();
message.addTextRecord("This record is 100 characters.0123456789012345678901234567890123456789012345678901234567890123456789");
#ifdef NDEF_USE_SERIAL
//message.print();
#endif
}
void message120()
{
NdefMessage message = NdefMessage();
message.addTextRecord("This record is 120 characters.012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
#ifdef NDEF_USE_SERIAL
//message.print();
#endif
}
void setup() {
Serial.begin(9600);
Serial.println("\n");
Serial.println(F("========="));
Serial.println(freeMemory());
Serial.println(F("========="));
}
test(memoryKludgeEnd)
{
// TODO ensure the output matches start
Serial.println(F("========="));
Serial.print("End ");Serial.println(freeMemory());
Serial.println(F("========="));
}
test(recordLeaks)
{
assertNoLeak(&record);
assertNoLeak(&emptyRecord);
assertNoLeak(&textRecord);
}
test(recordAccessorLeaks)
{
assertNoLeak(&recordMallocZero);
}
test(messageLeaks)
{
assertNoLeak(&emptyMessage);
assertNoLeak(&printEmptyMessage);
assertNoLeak(&printEmptyMessageNoNew);
assertNoLeak(&messageWithTextRecord);
assertNoLeak(&messageWithEmptyRecord);
assertNoLeak(&messageWithoutHelper);
assertNoLeak(&messageWithId);
}
test(messageOneBigRecord)
{
assertNoLeak(&message80);
// The next 2 fail. Maybe out of memory? Look into helper methods
//assertNoLeak(&message100);
//assertNoLeak(&message120);
}
test(memoryKludgeStart)
{
Serial.println(F("---------"));
Serial.print("Start ");Serial.println(freeMemory());
Serial.println(F("---------"));
}
void loop() {
Test::run();
}

View File

@@ -0,0 +1,238 @@
#include <Wire.h>
#include <PN532.h>
#include <NdefMessage.h>
#include <NdefRecord.h>
#include <ArduinoUnit.h>
// Custom Assertion
void assertNoLeak(void (*callback)())
{
int start = freeMemory();
(*callback)();
int end = freeMemory();
assertEqual(0, (start - end));
}
void assertBytesEqual(const uint8_t* expected, const uint8_t* actual, int size) {
for (int i = 0; i < size; i++) {
// Serial.print("> ");Serial.print(expected[i]);Serial.print(" ");Serial.println(actual[i]);
assertEqual(expected[i], actual[i]);
}
}
void setup() {
Serial.begin(9600);
}
test(messageDelete)
{
int start = freeMemory();
NdefMessage* m1 = new NdefMessage();
m1->addTextRecord("Foo");
delete m1;
int end = freeMemory();
// Serial.print("Start ");Serial.println(start);
// Serial.print("End ");Serial.println(end);
assertEqual(0, (start-end));
}
test(assign)
{
int start = freeMemory();
if (true) // bogus block so automatic storage duration objects are deleted
{
NdefMessage* m1 = new NdefMessage();
m1->addTextRecord("We the People of the United States, in Order to form a more perfect Union...");
NdefMessage* m2 = new NdefMessage();
*m2 = *m1;
NdefRecord r1 = m1->getRecord(0);
NdefRecord r2 = m2->getRecord(0);
assertEqual(r1.getTnf(), r2.getTnf());
assertEqual(r1.getTypeLength(), r2.getTypeLength());
assertEqual(r1.getPayloadLength(), r2.getPayloadLength());
assertEqual(r1.getIdLength(), r2.getIdLength());
byte p1[r1.getPayloadLength()];
byte p2[r2.getPayloadLength()];
r1.getPayload(p1);
r2.getPayload(p2);
int size = r1.getPayloadLength();
assertBytesEqual(p1, p2, size);
delete m2;
delete m1;
}
int end = freeMemory();
assertEqual(0, (start-end));
}
test(assign2)
{
int start = freeMemory();
if (true) // bogus block so automatic storage duration objects are deleted
{
NdefMessage m1 = NdefMessage();
m1.addTextRecord("We the People of the United States, in Order to form a more perfect Union...");
NdefMessage m2 = NdefMessage();
m2 = m1;
NdefRecord r1 = m1.getRecord(0);
NdefRecord r2 = m2.getRecord(0);
assertEqual(r1.getTnf(), r2.getTnf());
assertEqual(r1.getTypeLength(), r2.getTypeLength());
assertEqual(r1.getPayloadLength(), r2.getPayloadLength());
assertEqual(r1.getIdLength(), r2.getIdLength());
// TODO check type
byte p1[r1.getPayloadLength()];
byte p2[r2.getPayloadLength()];
r1.getPayload(p1);
r2.getPayload(p2);
int size = r1.getPayloadLength();
assertBytesEqual(p1, p2, size);
}
int end = freeMemory();
assertEqual(0, (start-end));
}
test(assign3)
{
int start = freeMemory();
if (true) // bogus block so automatic storage duration objects are deleted
{
NdefMessage* m1 = new NdefMessage();
m1->addTextRecord("We the People of the United States, in Order to form a more perfect Union...");
NdefMessage* m2 = new NdefMessage();
*m2 = *m1;
delete m1;
NdefRecord r = m2->getRecord(0);
assertEqual(TNF_WELL_KNOWN, r.getTnf());
assertEqual(1, r.getTypeLength());
assertEqual(79, r.getPayloadLength());
assertEqual(0, r.getIdLength());
::String s = "We the People of the United States, in Order to form a more perfect Union...";
byte payload[s.length() + 1];
s.getBytes(payload, sizeof(payload));
byte p[r.getPayloadLength()];
r.getPayload(p);
assertBytesEqual(payload, p+3, s.length());
delete m2;
}
int end = freeMemory();
assertEqual(0, (start-end));
}
test(assign4)
{
int start = freeMemory();
if (true) // bogus block so automatic storage duration objects are deleted
{
NdefMessage* m1 = new NdefMessage();
m1->addTextRecord("We the People of the United States, in Order to form a more perfect Union...");
NdefMessage* m2 = new NdefMessage();
m2->addTextRecord("Record 1");
m2->addTextRecord("RECORD 2");
m2->addTextRecord("Record 3");
assertEqual(3, m2->getRecordCount());
*m2 = *m1;
assertEqual(1, m2->getRecordCount());
// NdefRecord ghost = m2->getRecord(1);
// ghost.print();
//
// NdefRecord ghost2 = m2->getRecord(3);
// ghost2.print();
//
// delete m1;
//
// NdefRecord r = m2->getRecord(0);
//
// assertEqual(TNF_WELL_KNOWN, r.getTnf());
// assertEqual(1, r.getTypeLength());
// assertEqual(79, r.getPayloadLength());
// assertEqual(0, r.getIdLength());
//
// String s = "We the People of the United States, in Order to form a more perfect Union...";
// byte payload[s.length() + 1];
// s.getBytes(payload, sizeof(payload));
//
// uint8_t* p = r.getPayload();
// int size = r.getPayloadLength();
// assertBytesEqual(payload, p+3, s.length());
// free(p);
delete m1;
delete m2;
}
int end = freeMemory();
assertEqual(0, (start-end));
}
// really a record test
test(doublePayload)
{
int start = freeMemory();
NdefRecord* r = new NdefRecord();
uint8_t p1[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
r->setPayload(p1, sizeof(p1));
r->setPayload(p1, sizeof(p1));
delete r;
int end = freeMemory();
assertEqual(0, (start-end));
}
test(aaa_printFreeMemoryAtStart) // warning: relies on fact tests are run in alphabetical order
{
Serial.println(F("---------------------"));
Serial.print("Free Memory Start ");Serial.println(freeMemory());
Serial.println(F("---------------------"));
}
test(zzz_printFreeMemoryAtEnd) // warning: relies on fact tests are run in alphabetical order
{
// unfortunately the user needs to manually check this matches the start value
Serial.println(F("====================="));
Serial.print("Free Memory End ");Serial.println(freeMemory());
Serial.println(F("====================="));
}
void loop() {
Test::run();
}

View File

@@ -0,0 +1,173 @@
#include <Wire.h>
#include <PN532.h>
#include <NdefRecord.h>
#include <ArduinoUnit.h>
void assertBytesEqual(const uint8_t* expected, const uint8_t* actual, uint8_t size) {
for (int i = 0; i < size; i++) {
assertEqual(expected[i], actual[i]);
}
}
void setup() {
Serial.begin(9600);
}
test(accessors) {
NdefRecord record = NdefRecord();
record.setTnf(TNF_WELL_KNOWN);
uint8_t recordType[] = { 0x54 }; // "T" Text Record
assertEqual(0x54, recordType[0]);
record.setType(recordType, sizeof(recordType));
// 2 + "en" + "Unit Test"
uint8_t payload[] = { 0x02, 0x65, 0x6e, 0x55, 0x6e, 0x69, 0x74, 0x20, 0x54, 0x65, 0x73, 0x74 };
record.setPayload(payload, sizeof(payload));
uint8_t id[] = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x64}; // testid
record.setId(id, sizeof(id));
assertEqual(TNF_WELL_KNOWN, record.getTnf());
assertEqual(sizeof(recordType), record.getTypeLength());
assertEqual(1, record.getTypeLength());
assertEqual(sizeof(payload), record.getPayloadLength());
assertEqual(12, record.getPayloadLength());
assertEqual(sizeof(id), record.getIdLength());
assertEqual(6, record.getIdLength());
uint8_t typeCheck[record.getTypeLength()];
record.getType(typeCheck);
assertEqual(0x54, typeCheck[0]);
assertBytesEqual(recordType, typeCheck, sizeof(recordType));
uint8_t payloadCheck[record.getPayloadLength()];
record.getPayload(&payloadCheck[0]);
assertBytesEqual(payload, payloadCheck, sizeof(payload));
uint8_t idCheck[record.getIdLength()];
record.getId(&idCheck[0]);
assertBytesEqual(id, idCheck, sizeof(id));
}
test(newaccessors) {
NdefRecord record = NdefRecord();
record.setTnf(TNF_WELL_KNOWN);
uint8_t recordType[] = { 0x54 }; // "T" Text Record
assertEqual(0x54, recordType[0]);
record.setType(recordType, sizeof(recordType));
// 2 + "en" + "Unit Test"
uint8_t payload[] = { 0x02, 0x65, 0x6e, 0x55, 0x6e, 0x69, 0x74, 0x20, 0x54, 0x65, 0x73, 0x74 };
record.setPayload(payload, sizeof(payload));
uint8_t id[] = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x64}; // testid
record.setId(id, sizeof(id));
assertEqual(TNF_WELL_KNOWN, record.getTnf());
assertEqual(sizeof(recordType), record.getTypeLength());
assertEqual(1, record.getTypeLength());
assertEqual(sizeof(payload), record.getPayloadLength());
assertEqual(12, record.getPayloadLength());
assertEqual(sizeof(id), record.getIdLength());
assertEqual(6, record.getIdLength());
::String typeCheck = record.getType();
assertTrue(typeCheck.equals("T"));
byte payloadCheck[record.getPayloadLength()];
record.getPayload(payloadCheck);
assertBytesEqual(payload, payloadCheck, sizeof(payload));
byte idCheck[record.getIdLength()];
record.getId(idCheck);
assertBytesEqual(id, idCheck, sizeof(id));
}
test(assignment)
{
NdefRecord record = NdefRecord();
record.setTnf(TNF_WELL_KNOWN);
uint8_t recordType[] = { 0x54 }; // "T" Text Record
assertEqual(0x54, recordType[0]);
record.setType(recordType, sizeof(recordType));
// 2 + "en" + "Unit Test"
uint8_t payload[] = { 0x02, 0x65, 0x6e, 0x55, 0x6e, 0x69, 0x74, 0x20, 0x54, 0x65, 0x73, 0x74 };
record.setPayload(payload, sizeof(payload));
uint8_t id[] = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x64}; // testid
record.setId(id, sizeof(id));
NdefRecord record2 = NdefRecord();
record2 = record;
assertEqual(TNF_WELL_KNOWN, record.getTnf());
assertEqual(sizeof(recordType), record2.getTypeLength());
assertEqual(sizeof(payload), record2.getPayloadLength());
assertEqual(sizeof(id), record2.getIdLength());
::String typeCheck = record.getType();
assertTrue(typeCheck.equals("T"));
byte payload2[record2.getPayloadLength()];
record2.getPayload(payload2);
assertBytesEqual(payload, payload2, sizeof(payload));
byte id2[record.getIdLength()];
record2.getId(id2);
assertBytesEqual(id, id2, sizeof(id));
}
test(getEmptyPayload)
{
NdefRecord r = NdefRecord();
assertEqual(TNF_EMPTY, r.getTnf());
assertEqual(0, r.getPayloadLength());
byte payload[r.getPayloadLength()];
r.getPayload(payload);
byte id[r.getIdLength()];
r.getId(id);
byte empty[0];
assertBytesEqual(empty, payload, sizeof(payload));
assertBytesEqual(empty, id, sizeof(id));
}
test(encoding_without_record_id) {
NdefRecord record = NdefRecord();
record.setTnf(TNF_WELL_KNOWN);
uint8_t recordType[] = { 0x54 }; // "T" Text Record
assertEqual(0x54, recordType[0]);
record.setType(recordType, sizeof(recordType));
// 2 + "en" + "Unit Test"
uint8_t payload[] = { 0x02, 0x65, 0x6e, 0x55, 0x6e, 0x69, 0x74, 0x20, 0x54, 0x65, 0x73, 0x74 };
record.setPayload(payload, sizeof(payload));
uint8_t encodedBytes[record.getEncodedSize()];
record.encode(encodedBytes, true, true);
uint8_t expectedBytes[] = { 209, 1, 12, 84, 2, 101, 110, 85, 110, 105, 116, 32, 84, 101, 115, 116 };
assertBytesEqual(encodedBytes, expectedBytes, sizeof(encodedBytes));
}
// https://github.com/don/NDEF/issues/30
test(encoding_with_record_id) {
NdefRecord record = NdefRecord();
record.setTnf(TNF_WELL_KNOWN);
uint8_t recordType[] = { 0x54 }; // "T" Text Record
assertEqual(0x54, recordType[0]);
record.setType(recordType, sizeof(recordType));
// 2 + "en" + "Unit Test"
uint8_t payload[] = { 0x02, 0x65, 0x6e, 0x55, 0x6e, 0x69, 0x74, 0x20, 0x54, 0x65, 0x73, 0x74 };
record.setPayload(payload, sizeof(payload));
// testid
uint8_t id[] = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x64};
record.setId(id, sizeof(id));
uint8_t encodedBytes[record.getEncodedSize()];
record.encode(encodedBytes, true, true);
uint8_t expectedBytes[] = { 217, 1, 12, 6, 84, 116, 101, 115, 116, 105, 100, 2, 101, 110, 85, 110, 105, 116, 32, 84, 101, 115, 116 };
assertBytesEqual(encodedBytes, expectedBytes, sizeof(encodedBytes));
}
void loop() {
Test::run();
}

View File

@@ -0,0 +1,37 @@
#include <Wire.h>
#include <PN532.h>
#include <NfcTag.h>
#include <ArduinoUnit.h>
void setup() {
Serial.begin(9600);
}
// Test for pull requests #14 and #16
test(getUid)
{
byte uid[4] = { 0x00, 0xFF, 0xAA, 0x17 };
byte uidFromTag[sizeof(uid)];
NfcTag tag = NfcTag(uid, sizeof(uid));
assertEqual(sizeof(uid), tag.getUidLength());
tag.getUid(uidFromTag, sizeof(uidFromTag));
// make sure the 2 uids are the same
for (int i = 0; i < sizeof(uid); i++) {
assertEqual(uid[i], uidFromTag[i]);
}
// check contents, to ensure the original uid wasn't overwritten
assertEqual(0x00, uid[0]);
assertEqual(0xFF, uid[1]);
assertEqual(0xAA, uid[2]);
assertEqual(0x17, uid[3]);
}
void loop() {
Test::run();
}