Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base45 to a Set up payload #164

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions src/setup_payload/QRCodeSetupPayloadParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
#include "QRCodeSetupPayloadParser.h"
#include "Base45.h"

#include <core/CHIPError.h>
#include <iostream>
#include <vector>

using namespace chip;
using namespace std;

// Populate numberOfBits into dest from buf starting at startIndex
static uint64_t readBits(vector<uint8_t> buf, int & index, size_t numberOfBitsToRead)
static CHIP_ERROR readBits(vector<uint8_t> buf, int & index, uint64_t & dest, size_t numberOfBitsToRead)
{
uint64_t dest = 0;
dest = 0;
if (index + numberOfBitsToRead > buf.size() * 8 || numberOfBitsToRead > sizeof(uint64_t) * 8)
{
fprintf(stderr, "Error parsing QR code. startIndex %d numberOfBitsToLoad %zu buf_len %zu ", index, numberOfBitsToRead,
buf.size());
return 0;
return CHIP_ERROR_INVALID_ARGUMENT;
}

int currentIndex = index;
Expand All @@ -51,30 +52,72 @@ static uint64_t readBits(vector<uint8_t> buf, int & index, size_t numberOfBitsTo
currentIndex++;
}
index += numberOfBitsToRead;
return dest;
return CHIP_NO_ERROR;
}

SetupPayload QRCodeSetupPayloadParser::payload()
CHIP_ERROR QRCodeSetupPayloadParser::populatePayload(SetupPayload & outPayload)
{
vector<uint8_t> buf = vector<uint8_t>();

if (CHIP_NO_ERROR != base45Decode(mBase45StringRepresentation, buf))
CHIP_ERROR result = base45Decode(mBase45Representation, buf);

if (CHIP_NO_ERROR != result)
{
fprintf(stderr, "Decoding of base45 string failed");
return SetupPayload();
return result;
}

SetupPayload payload;

int indexToReadFrom = 0;
uint64_t dest;

result = readBits(buf, indexToReadFrom, dest, kVersionFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.version = dest;

result = readBits(buf, indexToReadFrom, dest, kVendorIDFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.vendorID = dest;

result = readBits(buf, indexToReadFrom, dest, kProductIDFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.productID = dest;

payload.version = readBits(buf, indexToReadFrom, kVersionFieldLengthInBits);
payload.vendorID = readBits(buf, indexToReadFrom, kVendorIDFieldLengthInBits);
payload.productID = readBits(buf, indexToReadFrom, kProductIDFieldLengthInBits);
payload.requiresCustomFlow = readBits(buf, indexToReadFrom, kCustomFlowRequiredFieldLengthInBits);
payload.rendezvousInformation = readBits(buf, indexToReadFrom, kRendezvousInfoFieldLengthInBits);
payload.discriminator = readBits(buf, indexToReadFrom, kPayloadDiscriminatorFieldLengthInBits);
payload.setUpPINCode = readBits(buf, indexToReadFrom, kSetupPINCodeFieldLengthInBits);
result = readBits(buf, indexToReadFrom, dest, kCustomFlowRequiredFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.requiresCustomFlow = dest;

result = readBits(buf, indexToReadFrom, dest, kRendezvousInfoFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.rendezvousInformation = dest;

result = readBits(buf, indexToReadFrom, dest, kPayloadDiscriminatorFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.discriminator = dest;

result = readBits(buf, indexToReadFrom, dest, kSetupPINCodeFieldLengthInBits);
if (result != CHIP_NO_ERROR)
{
return result;
}
outPayload.setUpPINCode = dest;

return payload;
return result;
}
16 changes: 8 additions & 8 deletions src/setup_payload/QRCodeSetupPayloadParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@

#include "SetupPayload.h"

#include <core/CHIPError.h>
#include <string>
using namespace std;

namespace chip
{
namespace chip {

/**
* @class QRCodeSetupPayloadParser
* A class that can be used to convert a base45 encoded payload to a SetupPayload object
* */
class QRCodeSetupPayloadParser
class QRCodeSetupPayloadParser
{
private:
string mBase45StringRepresentation;
private:
string mBase45Representation;

public:
QRCodeSetupPayloadParser(string base45Representation) : mBase45StringRepresentation(base45Representation){};
SetupPayload payload();
public:
QRCodeSetupPayloadParser(string base45Representation) : mBase45Representation(base45Representation){};
CHIP_ERROR populatePayload(SetupPayload & outPayload);
};

}; // namespace chip
31 changes: 21 additions & 10 deletions src/setup_payload/tests/tests_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
using namespace chip;
using namespace std;

#define EXPECT_EQ(x, y) ((x) != (y)) ? cerr << __FILE__ << ":" << __LINE__ << ":error EXPECT_EQ(" << x << ", " << y << ")\n", 1 : 0
#define EXPECT_EQ(x, y) \
((x) != (y)) ? cerr << endl << __FILE__ << ":" << __LINE__ << ":error EXPECT_EQ(" << x << ", " << y << ")\n", 1 : 0
Copy link
Contributor Author

@bhaskar-apple bhaskar-apple Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify this change. Just added a endlto make it easy to read the logged output


int testPayloadByteArrayRep()
{
Expand Down Expand Up @@ -173,12 +174,15 @@ int testSetupPayloadVerify()

int testInvalidQRCodePayload_WrongCharacterSet()
{
int surprises = 0;
string invalidString = "adas12AA";
QRCodeSetupPayloadParser parser = QRCodeSetupPayloadParser(invalidString);
SetupPayload payload = parser.payload();
int surprises = 0;
string invalidString = "adas12AA";

surprises = EXPECT_EQ(payload.isValid(), false);
QRCodeSetupPayloadParser parser = QRCodeSetupPayloadParser(invalidString);
SetupPayload payload;
CHIP_ERROR err = parser.populatePayload(payload);
bool didFail = err != CHIP_NO_ERROR;
surprises = EXPECT_EQ(didFail, true);
surprises = EXPECT_EQ(payload.isValid(), false);
return surprises;
}

Expand All @@ -187,9 +191,11 @@ int testInvalidQRCodePayload_WrongLength()
int surprises = 0;
string invalidString = "AA12";
QRCodeSetupPayloadParser parser = QRCodeSetupPayloadParser(invalidString);
SetupPayload payload = parser.payload();

surprises = EXPECT_EQ(payload.isValid(), false);
SetupPayload payload;
CHIP_ERROR err = parser.populatePayload(payload);
bool didFail = err != CHIP_NO_ERROR;
surprises = EXPECT_EQ(didFail, true);
surprises = EXPECT_EQ(payload.isValid(), false);
return surprises;
}

Expand Down Expand Up @@ -258,8 +264,13 @@ int testQRCodeToPayloadGeneration()
QRCodeSetupPayloadGenerator generator(payload);
string base45Rep = generator.payloadBase45Representation();

SetupPayload resultingPayload;
QRCodeSetupPayloadParser parser(base45Rep);
SetupPayload resultingPayload = parser.payload();

CHIP_ERROR err = parser.populatePayload(resultingPayload);
bool didSucceed = err == CHIP_NO_ERROR;
surprises = EXPECT_EQ(didSucceed, true);
surprises = EXPECT_EQ(resultingPayload.isValid(), true);

bool result = payload == resultingPayload;
surprises += EXPECT_EQ(result, true);
Expand Down