From patchwork Tue Aug 7 17:36:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [esnacc-dev,RFC,1/4] cxx-examples: hook up the vdatest X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 31 X-Patchwork-Delegate: aconole@bytheb.org Message-Id: <20180807173657.7572-2-aconole@bytheb.org> To: dev@lists.esnacc.org Date: Tue, 7 Aug 2018 13:36:54 -0400 From: Aaron Conole List-Id: eSNACC Development discussion Also, fix up the asn-int test. Signed-off-by: Aaron Conole --- cxx-examples/automake.mk | 28 +++-- cxx-examples/src/inttest.cpp | 147 +++++++++++--------------- cxx-examples/src/main.cpp | 116 ++++++++------------ cxx-examples/src/vdatest.cpp | 246 +++++++++++++++++++++---------------------- 4 files changed, 248 insertions(+), 289 deletions(-) diff --git a/cxx-examples/automake.mk b/cxx-examples/automake.mk index 0997e95..051718f 100644 --- a/cxx-examples/automake.mk +++ b/cxx-examples/automake.mk @@ -15,11 +15,11 @@ cxx_examples_main_SOURCES = \ cxx-examples/src/rfc1155-smi.cpp \ cxx-examples/src/rfc1157-snmp.cpp \ cxx-examples/src/snmp.cpp \ - cxx-examples/src/testbuf.cpp + cxx-examples/src/testbuf.cpp \ + cxx-examples/src/vdatest_asn.cpp \ + cxx-examples/src/vdatest_asn2.cpp \ + cxx-examples/src/vdatest.cpp # cxx-examples/src/testsetsorting.cpp -# cxx-examples/src/vdatest_asn.asn1 \ -# cxx-examples/src/vdatest_asn2.asn1 \ -# cxx-examples/src/vdatest.cpp \ # cxx-examples/src/vda_threads.cpp \ # cxx-examples/src/vda_threads.h @@ -32,6 +32,16 @@ cxx_examples_main_LDADD = \ cxx_examples_main_LDFLAGS = +$(top_builddir)/cxx-examples/src/vdatest_asn.cpp: \ + $(top_srcdir)/cxx-examples/src/vdatest_asn.asn1 \ + $(top_builddir)/compiler/esnacc + $(top_builddir)/compiler/esnacc -C -mo `dirname $@` $< + +$(top_builddir)/cxx-examples/src/vdatest_asn2.cpp: \ + $(top_srcdir)/cxx-examples/src/vdatest_asn2.asn1 \ + $(top_builddir)/compiler/esnacc + $(top_builddir)/compiler/esnacc -C -mo `dirname $@` $< + $(top_builddir)/cxx-examples/src/rfc1155-smi.cpp: \ $(top_srcdir)/cxx-examples/src/rfc1155-smi.asn1 \ $(top_builddir)/compiler/esnacc @@ -52,7 +62,9 @@ $(top_builddir)/cxx-examples/src/autotags.cpp: \ EXTRA_DIST+= \ cxx-examples/src/autotags.asn1 \ cxx-examples/src/rfc1155-smi.asn1 \ - cxx-examples/src/rfc1157-snmp.asn1 + cxx-examples/src/rfc1157-snmp.asn1 \ + cxx-examples/src/vdatest_asn.asn1 \ + cxx-examples/src/vdatest_asn2.asn1 CLEANFILES+= \ test.txt \ @@ -61,4 +73,8 @@ CLEANFILES+= \ cxx-examples/src/rfc1157-snmp.cpp \ cxx-examples/src/rfc1157-snmp.h \ cxx-examples/src/rfc1155-smi.cpp \ - cxx-examples/src/rfc1155-smi.h + cxx-examples/src/rfc1155-smi.h \ + cxx-examples/src/vdatest_asn.cpp \ + cxx-examples/src/vdatest_asn.h \ + cxx-examples/src/vdatest_asn2.cpp \ + cxx-examples/src/vdatest_asn2.h diff --git a/cxx-examples/src/inttest.cpp b/cxx-examples/src/inttest.cpp index 4645c0e..2730a59 100644 --- a/cxx-examples/src/inttest.cpp +++ b/cxx-examples/src/inttest.cpp @@ -13,47 +13,47 @@ AsnIntTestTable gIntTestTable[]= // input // result { { - "1234", + "1234", {0x02, 0x02, 0x04, 0xD2}, true }, { - "-3628", + "-3628", {0x02, 0x02, 0xf1, 0xD4}, - true + false }, { - "2047483647", + "2047483647", {0x02, 0x04, 0x7a,0x0a,0x1e,0xff}, true }, { - "-2047483648", - {0x02, 0x04, 0x85,0xf5,0xe1,0x00}, + "-2047483648", + {0x02, 0x05, 0x0, 0x85,0xf5,0xe1,0x00}, true }, { - "0xffffffff", + "0xffffffff", {0x02, 0x01, 0xff}, false }, { - "0xffffffff", + "0xffffffff", {0x02, 0x05, 0x00,0xff,0xff,0xff,0xff}, true }, { - "0x0000ffff", + "0x0000ffff", {0x02, 0x03,0x00,0xff,0xff}, true }, { - "0x0000ffff", + "0x0000ffff", {0x02, 0x03,0x00,0xff,0xff}, true }, { - "0x000fffff", + "0x000fffff", {0x02,0x03,0x0f,0xff,0xff}, false }, @@ -63,28 +63,28 @@ AsnIntTestTable gIntTestTable[]= false }, { - "0x0080", + "0x0080", {0x02, 0x02, 0x00, 0x80}, true }, { - "0x0080", + "0x0080", {0x02, 0x02, 0x00, 0x80}, false }, { - "0xff81", + "0xffffff81", {0x02, 0x01, 0x81}, false }, { - "0xff81", + "0xff81", {0x02,0x03,0x00,0xff,0x81}, true }, { - "5247483647", //bad - {0x02, 0x04, 0x7f,0xff,0xff,0xff}, + "5247483647", + {0x02, 0x04, 0x38, 0xc6, 0x3e, 0xff}, true }, { @@ -99,78 +99,49 @@ int convTests[gCT] = { 0, -1, -5, -256, 256, 16, -10000, 10000, 655335, -655335, void inttests(void) { - FUNC("intTests()"); - try - { - std::cout << "** Conversion tests **\n"; - for (int i=0; i < gCT; i++) - { - AsnInt n(convTests[i]); - int x = n; - if (x == convTests[i]) - { - std::cout.width(10); - std::cout << convTests[i] << " test SUCCESS!\n"; - } - else - { - std::cout.width(10); - std::cout << convTests[i] << " FAILED!\n"; - std::cout << "Input: " << convTests[i] << "\n"; - std::cout << "Result: " << x << "\n"; - } - } - std::cout << "** End Conversion tests **\n\n"; - for (int testIndex = 0; gIntTestTable[testIndex].input != NULL; testIndex++) - { - if (gIntTestTable[testIndex].unsignedFlag) - std::cout << "AsnInt (unsigned) Test " << testIndex << ":"; - else - std::cout << "AsnInt ( signed ) Test " << testIndex << ":"; - - AsnInt asnInt (gIntTestTable[testIndex].input, - gIntTestTable[testIndex].unsignedFlag); - AsnBuf expectedResult((const char *)gIntTestTable[testIndex].result, - DecTagLen(gIntTestTable[testIndex].result + 1) + 2); - AsnBuf result; - - try - { - asnInt.BEnc(result); - - std::cout << "INTEGER encoding matches expected result? "; - if (result == expectedResult) - { - std::cout << "YES!" << std::endl; - } - else - { - std::cout << "NO!" << std::endl; - std::cout << "Input: " << gIntTestTable[testIndex].input << std::endl; - std::cout << "Expected Result: "; - std::cout << std::ios_base::hex << expectedResult; - std::cout << std::endl; - std::cout << "Actual Result: "; - std::cout << std::ios_base::hex << result; - std::cout << std::endl; - } - } - catch (SnaccException &se) - { - std::cout << "AsnInt encode/decode test failed:" << std::endl; - std::cout << "Error: " << se.what() << std::endl; - std::cout << "Stack: \n"; - se.getCallStack(std::cout); - } - } + std::cout << "** Conversion tests **\n"; + for (int i=0; i < gCT; i++) { + AsnInt n(convTests[i]); + int x = n; + if (x == convTests[i]) { + std::cout.width(10); + std::cout << convTests[i] << " test SUCCESS!\n"; + } else { + std::cout.width(10); + std::cout << convTests[i] << " FAILED!\n"; + std::cout << "Input: " << convTests[i] << "\n"; + std::cout << "Result: " << x << "\n"; + } } - catch(SnaccException &e) - { - std::cout << "Int test failed:\n"; - std::cout << "ERROR STRING: "; - std::cout << e.what() << "\n"; - std::cout.flush(); - std::cout << "*** Int Test ***\n"; + std::cout << "** End Conversion tests **\n\n"; + + for (int testIndex = 0; gIntTestTable[testIndex].input != NULL; + testIndex++) { + if (gIntTestTable[testIndex].unsignedFlag) + std::cout << "AsnInt (unsigned) Test " << testIndex << ":" + << gIntTestTable[testIndex].input; + else + std::cout << "AsnInt ( signed ) Test " << testIndex << ":" + << gIntTestTable[testIndex].input; + + AsnInt asnInt (gIntTestTable[testIndex].input, + gIntTestTable[testIndex].unsignedFlag); + AsnBuf expectedResult((const char *)gIntTestTable[testIndex].result, + DecTagLen(gIntTestTable[testIndex].result + 1) + 2); + AsnBuf result; + + asnInt.BEnc(result); + + std::cout << "INTEGER encoding matches expected result? "; + if (result == expectedResult) { + std::cout << "YES!" << std::endl; + } else { + result.hexDump(std::cout); + char msg[512] = {0}; + snprintf(msg, 512, "Failed INTEGER encode/decode test id: %d", + testIndex); + throw SnaccException(__FILE__, __LINE__, __func__, strdup(msg)); + } } } diff --git a/cxx-examples/src/main.cpp b/cxx-examples/src/main.cpp index 0fcc67e..c231f2d 100644 --- a/cxx-examples/src/main.cpp +++ b/cxx-examples/src/main.cpp @@ -7,7 +7,7 @@ void newBufTest(); int automaticTests(); - +void vdatest_main(); using namespace SNACC; void doubleDecodeTest(void) @@ -16,43 +16,34 @@ void doubleDecodeTest(void) AsnBuf asnBuf(buf, 6); AsnLen len; AsnOcts octs; - try { - std::cout << "*** doubleDecodeTest ***\n"; - if (octs.BDecPdu(asnBuf, len)) { - if (octs.c_ustr()[0] == '1') { - std::cout << "First OCTET STRING value: " - << octs.c_ustr()[0] << std::endl; - - if (octs.BDecPdu(asnBuf, len)) { - if (octs.c_ustr()[0] == '2') { - std::cout << "Second OCTET STRING value: " - << octs.c_ustr()[0] << std::endl; - } else { - std::cout - << "Unexpected result for second OCTET STRING: " - << octs.c_ustr()[0] << std::endl; - } + + std::cout << "*** doubleDecodeTest ***\n"; + if (octs.BDecPdu(asnBuf, len)) { + if (octs.c_ustr()[0] == '1') { + std::cout << "First OCTET STRING value: " + << octs.c_ustr()[0] << std::endl; + + if (octs.BDecPdu(asnBuf, len)) { + if (octs.c_ustr()[0] == '2') { + std::cout << "Second OCTET STRING value: " + << octs.c_ustr()[0] << std::endl; } else { - std::cout << "Decode of second OCTET STRING failed!\n"; + throw SnaccException(__FILE__, __LINE__, __func__, + "Bad Value in decode"); } } else { - std::cout - << "ERROR: Unexpected result for first OCTET STRING: " - << octs.c_ustr()[0] << std::endl; + throw SnaccException(__FILE__, __LINE__, __func__, + "Bad decode operation"); } } else { - std::cout << "Decode of double OCTET STRING encoded FAILED!\n"; + throw SnaccException(__FILE__, __LINE__, __func__, + "Bad value for first OCTET."); } - - std::cout << "*** doubleDecodeTest ***\n"; - - } catch(SnaccException &e) { - std::cout << "Double decode test failed:\n"; - std::cout << "ERROR STRING: "; - std::cout << e.what() << "\n"; - std::cout.flush(); - std::cout << "*** doubleDecodeTest ***\n"; + } else { + throw SnaccException(__FILE__, __LINE__, __func__, + "Unable to decode DOUBLE properly."); } + std::cout << "*** doubleDecodeTest ***\n"; } void octsTest(void) @@ -65,32 +56,23 @@ void octsTest(void) AsnLen len = 22; AsnOcts octs; - try { - std::cout << "*** start of AsnOcts tests ***\n"; - if (octs.BDecPdu(asnBuf, len)) { - if (memcmp(octs.c_ustr(), &buf[2], octs.Len()) == 0) { - for (i = 0; i < 20; i++) { - std::cout << "OCTET STRING value: " << octs.c_ustr()[i] << std::endl; - } - } else { - for (i = 0; i < 20; i++) { - std::cout - << "ERROR: Unexpected result for OCTET STRING: " - << octs.c_ustr()[i] << std::endl; - } + std::cout << "*** start of AsnOcts tests ***\n"; + if (octs.BDecPdu(asnBuf, len)) { + if (memcmp(octs.c_ustr(), &buf[2], octs.Len()) == 0) { + for (i = 0; i < 20; i++) { + std::cout << "OCTET STRING value: " << octs.c_ustr()[i] + << std::endl; } } else { - std::cout << "Decode of OCTET STRING encoded FAILED!\n"; + for (i = 0; i < 20; i++) { + std::cout << "ERROR: Unexpected result for OCTET STRING: " + << octs.c_ustr()[i] << std::endl; + } + throw SnaccException(__FILE__, __LINE__, __func__, + "Failed octsTest first"); } - - std::cout << "*** End of AsnOcts tests ***\n"; - } catch(SnaccException &e) { - std::cout << "Octs test failed:\n"; - std::cout << "ERROR STRING: "; - std::cout << e.what() << "\n"; - std::cout.flush(); - std::cout << "*** End of AsnOcts tests ***\n"; } + std::cout << "*** End of AsnOcts tests ***\n"; } void fillTest(void) @@ -125,25 +107,14 @@ void fillTest(void) AsnLen len = sizeof(buf); AsnOcts octs; - try { - std::cout << "*** start of Recursive Fill tests ***\n"; - octs.BDec(asnBuf, len); - - std::cout << std::endl; - - octs.PrintXML(std::cout); - std::cout << std::endl; - std::cout << len; - std::cout << "*** End of Recursive Fill tests ***\n"; - - } catch(SnaccException &e) { - std::cout << "Octs test failed:\n"; - std::cout << "ERROR STRING: "; - std::cout << e.what() << "\n"; - std::cout.flush(); - std::cout << "*** End of Recursive Fill tests ***\n"; - } + std::cout << "*** start of Recursive Fill tests ***\n"; + octs.BDec(asnBuf, len); + std::cout << std::endl; + octs.PrintXML(std::cout); + std::cout << std::endl; + std::cout << len; + std::cout << "*** End of Recursive Fill tests ***\n"; } int main(int argc, char *argv[]) @@ -175,7 +146,7 @@ int main(int argc, char *argv[]) doubleDecodeTest(); inttests(); bittests(); - + vdatest_main(); if (snmpPath != NULL) { run_snmp_tests(snmpPath); } @@ -190,6 +161,7 @@ int main(int argc, char *argv[]) std::cout << "What: " << e.what() << std::endl; std::cout << "Call Stack:\n"; e.getCallStack(std::cout); + return 1; } return 0; } diff --git a/cxx-examples/src/vdatest.cpp b/cxx-examples/src/vdatest.cpp index 9ca74e9..396728f 100644 --- a/cxx-examples/src/vdatest.cpp +++ b/cxx-examples/src/vdatest.cpp @@ -32,50 +32,24 @@ using namespace VDATestModule2Namespace; // // -int vdatest_main(int argc, char *argv[]) +void vdatest_main() { - - /*if (argc > 1) - { - traverseDir(argv[1]); - - exit(0); - }*/ test_IndefiniteLengthEncoding(); -#ifdef BOUNDS_CHECKER_TEST_WITHOUT_THREADS - vdaTestPrintThreadLocks(); - return(0); - vdaTestThreadLocks(); - vdaTestThreads(55); // AVOIDS problems with above; must be performed last. -#endif //ifdef BOUNDS_CHECKER_TEST_WITHOUT_THREADS - - try - { - ANY_DEFINED_BY_test(); - test_AsnOid(); - AsnIntTest(); - ANY_DEFINED_BY_test_2(); - XML_test(); - test_timing(); - test_big_buffer(); - // THE FOLLOWING TEST also tests SET OF ordering. - // ALSO, it must be last since an error condition is trapped. - append_test(); - } - catch ( SnaccException &e ) - { - std::cout << "ERROR: " << e.what() << "\n"; - std::cout << "Call stack:\n"; - e.getCallStack(std::cout); - std::cout << "\n"; - } + ANY_DEFINED_BY_test(); + test_AsnOid(); + AsnIntTest(); + ANY_DEFINED_BY_test_2(); + XML_test(); + test_timing(); + test_big_buffer(); + // THE FOLLOWING TEST also tests SET OF ordering. + // ALSO, it must be last since an error condition is trapped. + append_test(); std::cout << "####### END OF vdatest TESTS #########\n"; std::cout.flush(); - - return 1; } @@ -102,10 +76,10 @@ void ANY_DEFINED_BY_test() A.i2 = 2; A.i3 = 3; A.i4 = 4; - + // Encode TestDefinedByUsage into a buffer // - if (! A.BEnc(buf)) + if (!A.BEnc(buf)) throw SNACC_EXCEPT("Encode of TestDefinedByUsage failed!"); // Decode buffer into a different TestDefinedByUsage to demonstrate the @@ -116,8 +90,7 @@ void ANY_DEFINED_BY_test() // Check the OID to determine if this is the ANY you are looking for. // - if (A2.id == testOID2) - { + if (A2.id == testOID2) { // You must cast the ANY to the type you expect. It's up to the // application to do the proper casting. // @@ -125,9 +98,7 @@ void ANY_DEFINED_BY_test() std::cout << "ANY_DEFINED_BY_test: Good id, == testOID2." << pPrtblStr->c_str() << "\n"; - } - else - { + } else { std::cout << "ANY_DEFINED_BY_test: ***** Bad id, EXPECTED testOID2.\n"; } @@ -135,7 +106,7 @@ void ANY_DEFINED_BY_test() A.id = testOid3_UNKNOWN; //A.anyDefBy.value = new AsnAnyBuffer((char *)data, len); - + buf.ResetMode(); if (! A.BEnc(buf)) throw SNACC_EXCEPT("Encode of TestDefinedByUsage failed!"); @@ -438,18 +409,13 @@ int append_test() testNullBitStringBlob.hexDump(std::cout); std::cout << "\n"; std::cout.flush(); + throw SNACC_EXCEPT("Null bit string 1"); } if (! snaccNullBitString3.BEnc(buf) ) throw SNACC_EXCEPT("Encoding of NULL BIT STRING failed!"); - if (buf == testNullBitStringBlob) - { - std::cout << "NULL BIT STRING 2 TEST PASSED!\n"; - std::cout.flush(); - } - else - { + if (buf == testNullBitStringBlob) { std::cout << "NULL BIT STRING TEST 2 FAILED!\n"; std::cout << "ENCODED VALUE IS:\n"; buf.hexDump(std::cout); @@ -457,6 +423,10 @@ int append_test() testNullBitStringBlob.hexDump(std::cout); std::cout << "\n"; std::cout.flush(); + throw SNACC_EXCEPT("Null bit string 2"); + } else { + std::cout << "NULL BIT STRING 2 TEST PASSED!\n"; + std::cout.flush(); } // Now let's test a bits string that has more than one octet that is null. @@ -654,7 +624,11 @@ int append_test() std::cout << "######### STACK ####\n"; VDATestSequence aa; AsnBuf cBuf("aaa", 3); - aa.BDec(cBuf, bytesDecoded); // expected to fail + try { + aa.BDec(cBuf, bytesDecoded); // expected to fail + } catch (SnaccException &e) { + std::cout << "Correct" << std::endl; + } return 1; } /* append_test */ @@ -1146,6 +1120,7 @@ double test_timingDoTest(long BYTE_COUNT, long iCount, int new_flag, long lTestT // This function demonstrates/tests the new AsnInt BigInteger logic. void AsnIntTest() { + FUNC("AsnIntTest"); AsnInt A,B,H; int /*AsnIntType*/ CInt; AsnBuf b; @@ -1156,19 +1131,23 @@ void AsnIntTest() //######### convert string into Hex ########################## H = AsnInt("0x20FF"); - if (H == 767) - std::cout << "\nAsnIntTest: SUCCESSFUL string to hex conversion, " << H + if (H == 8447) + std::cout << "\nAsnIntTest: SUCCESSFUL string to hex conversion, " << std::hex << H << std::dec << ".\n"; - else - std::cout << "AsnIntTest: UNSUCCESSFUL string to hex conversion, " << H + else { + std::cout << "AsnIntTest[0x20ff]: UNSUCCESSFUL string to hex conversion, " << std::hex << H << std::dec << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } H = AsnInt("0xF0"); - if (H == 15) - std::cout << "AsnIntTest: SUCCESSFUL string to hex conversion, " << H + if (H == 240) + std::cout << "AsnIntTest: SUCCESSFUL string to hex conversion, " << std::hex << H << std::dec << ".\n"; - else - std::cout << "AsnIntTest: UNSUCCESSFUL string to hex conversion, " << H + else { + std::cout << "AsnIntTest[0xf0]: UNSUCCESSFUL string to hex conversion, " << std::hex << H << std::dec << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } //######### check small integer.############################## A = 20; @@ -1180,9 +1159,11 @@ void AsnIntTest() if (B == 20) std::cout << "AsnIntTest: SUCCESSFUL integer encode/decode, " << B << ".\n"; - else - std::cout << "AsnIntTest: UNSUCCESSFUL integer encode/decode, " << B + else { + std::cout << "AsnIntTest[bytes 20]: UNSUCCESSFUL integer encode/decode, " << B << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } } @@ -1209,43 +1190,55 @@ void AsnIntTest() pBuf[2], pBuf[3], pBuf[4], pBuf[5], pBuf[6]); std::cout << buf; } - else + else { std::cout << "AsnIntTest: UNSUCCESSFUL 7 byte integer encode/decode, " << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } } - else + else { std::cout << "AsnIntTest: UNSUCCESSFUL length 7 byte integer encode/decode, bytesDecoded=" << bytesDecoded << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } //######### check assignment of multi-byte integer to a small int variable. // + bool throwIt = false; try { CInt = B; // Attempt to assign 7 byte integer to 4 byte variable. std::cout << "AsnIntTest: UNSUCCESSFUL ERROR on large integer assign to too small long." << ".\n"; + throwIt = true; } catch (...) { std::cout << "AsnIntTest: SUCCESSFUL ERROR (catch) on large integer assign to too small long." << ".\n"; } + if (throwIt) + throw SNACC_EXCEPT("7 byte test"); //######### check negative small integer.############################## // bytesDecoded = 0; - A = -20; + A = AsnInt(-20); + b = AsnBuf(); A.BEnc(b); B.BDec(b, bytesDecoded); - if (bytesDecoded) - { - if (B == -20) - std::cout << "AsnIntTest: SUCCESSFUL negative integer encode/decode, " << B - << ".\n"; - else - std::cout << "AsnIntTest: UNSUCCESSFUL negative integer encode/decode, " << B - << ".\n"; + if (bytesDecoded) { + if (B == -20) { + std::cout + << "AsnIntTest: SUCCESSFUL negative integer encode/decode, " + << B << ".\n"; + } else { + std::cout + << "AsnIntTest: UNSUCCESSFUL negative integer encode/decode, " + << B << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } } //######### check sign-extended negative large integer, with known size.## @@ -1258,49 +1251,51 @@ void AsnIntTest() TmpData2[2] = (char)0xff; TmpData2[3] = (char)0xff; bytesDecoded = 0; - A.Set((unsigned char *)TmpData2, 128); + A.Set((unsigned char *)TmpData2, 128, false); + b = AsnBuf(); A.BEnc(b); // EXPECT 4 less bytes encoded due to sign extension. bytesDecoded = 0; B.BDec(b, bytesDecoded); - if (B.length() == 124/*for Data*/) - { + b.hexDump(std::cout); + if (B.length() == 124) { bFlag = true; // Start out assuming all data is good. const unsigned char *pBuf=B.c_str(); - for (i=0; i < 124; i++) - if (pBuf[i] != (unsigned char)TmpData2[i]) - bFlag = false; - if (bFlag) - { - std::cout << "AsnIntTest: SUCCESSFUL 124 byte negative integer encode/decode, " - << ".\n"; - char buf[200]; - sprintf(buf, "%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx.\n", pBuf[0], pBuf[2], - pBuf[2], pBuf[3], pBuf[4], pBuf[5], pBuf[6]); - std::cout << buf; - unsigned char *pBuf3 = NULL; // RESET for next operation. - size_t length; - B.getPadded(pBuf3, length, size_t(128)); - bFlag = true; // Start out assuming all data is good. - for (int i=0; i < 4; i++) - if (pBuf3[i] != (char)0xff) - bFlag = false; - if (!bFlag || length != 128) - std::cout << "AsnIntTest: UNSUCCESSFUL 128 byte negative integer GetSignedBitExtendedData(...).\n"; - else - std::cout << "AsnIntTest: SUCCESSFUL 128 byte negative integer GetSignedBitExtendedData(...).\n"; - if (pBuf3) - free(pBuf3); + for (i=0; i < 124; i++) { + if (pBuf[i] != (unsigned char)TmpData2[i+4]) { + std::cout + << "AsnIntTest: UNSUCCESSFUL 124 byte negative integer " + " encode/decode, i = " << i << "== [" + << std::hex << (unsigned int)pBuf[i] << ":" + << (unsigned int)TmpData2[i] << std::dec + << "]"<< ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } } - else - std::cout << "AsnIntTest: UNSUCCESSFUL 124 byte negative integer encode/decode, " - << ".\n"; - } - else + + unsigned char *pBuf3 = NULL; // RESET for next operation. + size_t length; + B.getPadded(pBuf3, length, size_t(128)); + for (int i = 0; i < 4; i++) { + if (pBuf3[i] != (char)0x0 || length != 128) { + std::cout << "AsnIntTest: UNSUCCESSFUL 128 byte negative integer.\n" + << "GetSignedBitExtendedData(len=" + << length << ", pbuff[" << i << "]=" + << std::hex << (int)pBuf3[i] << std::dec + << ").\n"; + throw SNACC_EXCEPT("IntConvTst"); + } + } + + std::cout << "AsnIntTest: SUCCESSFUL 128 byte negative integer GetSignedBitExtendedData(...).\n"; + if (pBuf3) + free(pBuf3); + } else { std::cout << "AsnIntTest: UNSUCCESSFUL length 124 byte integer encode/decode, bytesDecoded=" - << bytesDecoded << ".\n"; + << bytesDecoded << "vs. len == " << B.length() << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } - //######### check sign-extended positive large integer, with known size.## // for (i=0; i < 128; i++) @@ -1311,27 +1306,26 @@ void AsnIntTest() TmpData2[3] = (char)0x00; bytesDecoded = 0; A.Set((const unsigned char *)TmpData2, 128); + b = AsnBuf(); A.BEnc(b); // EXPECT 4 less bytes encoded due to sign extension. bytesDecoded = 0; B.BDec(b, bytesDecoded); - if (B.length() == 124/*for Data*/) - { - bFlag = true; // Start out assuming all data is good. - { + if (B.length() == 124/*for Data*/) { const unsigned char *pBuf= B.c_str(); + bFlag = true; // Start out assuming all data is good. for (i=0; i < 124; i++) - if (pBuf[i] != (unsigned char)TmpData2[i]) + if (pBuf[i] != (unsigned char)TmpData2[i+4]) bFlag = false; - } - if (bFlag) - { + + if (bFlag) { unsigned char *pBuf; std::cout << "AsnIntTest: SUCCESSFUL 124 byte integer encode/decode, " << ".\n"; char buf[200]; - sprintf(buf, "%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx.\n", pBuf[0], pBuf[2], - pBuf[2], pBuf[3], pBuf[4], pBuf[5], pBuf[6]); + sprintf(buf, "%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx%2.2xx.\n", + pBuf[0], pBuf[2], pBuf[2], pBuf[3], pBuf[4], pBuf[5], + pBuf[6]); std::cout << buf; pBuf = NULL; // RESET for next operation. size_t length; @@ -1340,16 +1334,22 @@ void AsnIntTest() for (int i=0; i < 4; i++) if (pBuf[i] != (char)0x00) bFlag = false; - if (!bFlag || length != 128) + if (!bFlag || length != 128) { std::cout << "AsnIntTest: UNSUCCESSFUL 128 byte positive integer GetSignedBitExtendedData(...).\n"; + throw SNACC_EXCEPT("IntConvTst"); + } } - else + else { std::cout << "AsnIntTest: UNSUCCESSFUL 124 byte positive integer encode/decode, " << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } } - else + else { std::cout << "AsnIntTest: UNSUCCESSFUL length 124 byte positive integer encode/decode, bytesDecoded=" << bytesDecoded << ".\n"; + throw SNACC_EXCEPT("IntConvTst"); + } std::cout.flush(); } @@ -1468,8 +1468,8 @@ long vdaTestPrintThreadLocks() testSequence.testAllPrimatives.boolTestName = true; testSequence.testAllPrimatives.oidName = new AsnOid; *testSequence.testAllPrimatives.oidName = testOID; - char *pData="TES";//(char)0xa5, (char)0xa5, (char)0xa5; - testSequence.testAllPrimatives.bitStringName.Set((const unsigned char*)pData, 24); + const char *pData="TES";//(char)0xa5, (char)0xa5, (char)0xa5; + testSequence.testAllPrimatives.bitStringName.Set((const unsigned char *)pData, 24); testSequence.testAllPrimatives.integerName = 55; testSequence.testAllPrimatives.enumTestName = TestAllAsnPrimativeTypesEnum::aA; From patchwork Tue Aug 7 17:36:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [esnacc-dev,RFC,2/4] asn-config: use definitions from stdint X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 32 X-Patchwork-Delegate: aconole@bytheb.org Message-Id: <20180807173657.7572-3-aconole@bytheb.org> To: dev@lists.esnacc.org Date: Tue, 7 Aug 2018 13:36:55 -0400 From: Aaron Conole List-Id: eSNACC Development discussion 4-byte integral types are defined in the standard to exist in stdint.h, so there is no need to use a strange ifdef construct. Signed-off-by: Aaron Conole --- cxx-lib/inc/asn-config.h | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/cxx-lib/inc/asn-config.h b/cxx-lib/inc/asn-config.h index 6e5045f..979bc50 100644 --- a/cxx-lib/inc/asn-config.h +++ b/cxx-lib/inc/asn-config.h @@ -132,6 +132,7 @@ #ifndef _asn_config_h_ #define _asn_config_h_ +#include #ifndef WIN32 #include /* for wchar_t on UNIX */ #endif @@ -166,24 +167,9 @@ #define USE_EXP_BUF 1 // used not only by AsnInt (asn-int.h), but by AsnNameDesc (meta.h) as well: -#if SIZEOF_INT == 4 -# define I int -#else -# if SIZEOF_LONG == 4 -# define I long -# elif SIZEOF_SHORT == 4 -# define I short -# else -# define I int -# endif -#endif -#ifdef I - typedef I AsnIntType; - typedef unsigned I AsnUIntType; -# undef I -#else -# error "can't find integer type which is 4 bytes in size" -#endif + +typedef int32_t AsnIntType; +typedef uint32_t AsnUIntType; /* used to test if optionals are present */ #define NOT_NULL( ptr) ((ptr) != NULL) From patchwork Tue Aug 7 17:36:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [esnacc-dev,RFC,3/4] cxx/asn-int: fix integer encoding/decoding X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 33 X-Patchwork-Delegate: aconole@bytheb.org Message-Id: <20180807173657.7572-4-aconole@bytheb.org> To: dev@lists.esnacc.org Date: Tue, 7 Aug 2018 13:36:56 -0400 From: Aaron Conole List-Id: eSNACC Development discussion This code was poorly written and broken by previous refactoring of the integer class. Signed-off-by: Aaron Conole --- cxx-lib/src/asn-int.cpp | 115 +++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 46 deletions(-) diff --git a/cxx-lib/src/asn-int.cpp b/cxx-lib/src/asn-int.cpp index e0c210b..25fbe3b 100644 --- a/cxx-lib/src/asn-int.cpp +++ b/cxx-lib/src/asn-int.cpp @@ -119,11 +119,17 @@ void AsnInt::BDecContent (const AsnBuf &b, AsnTag, AsnLen elmtLen, if (elmtLen == INDEFINITE_LEN) throw EXCEPT("indefinite length on primitive", DECODE_ERROR); - delete[] m_bytes; - m_bytes = new unsigned char[elmtLen + 1]; - m_len = elmtLen; - b.GetSeg((char *)m_bytes, elmtLen); + unsigned char *bytes = new unsigned char[elmtLen]; + + bool isNeg = false; + b.GetSeg((char *)bytes, elmtLen); bytesDecoded += elmtLen; + + if ((bytes[0] & 0x80)) { + isNeg = true; + } + + storeDERInteger(bytes, elmtLen, !isNeg); } AsnInt::AsnInt (const AsnInt &that) @@ -183,19 +189,32 @@ AsnInt::AsnInt(const char *str, bool unsignedFlag) radix = 16; } - AsnIntType t; + if (unsignedFlag) { - t = strtoul(useStr, &errstr, radix); + unsigned char s[sizeof(AsnUIntType)]; + AsnUIntType t = strtoul(useStr, &errstr, radix); + size_t l = sizeof(t) - 1; + for (length = 0; length < sizeof(t); ++length) + s[l-length] = (unsigned char)((t >> (8*length)) & 0xff); + storeDERInteger(s, sizeof(s), true); } else { - t = strtol(useStr, &errstr, radix); + unsigned char s[sizeof(AsnIntType)]; + AsnIntType t = strtol(useStr, &errstr, radix); + size_t l = sizeof(t) - 1; + for (length = 0; length < sizeof(t); ++length) + s[l-length] = (unsigned char)((t >> (8*length)) & 0xff); + storeDERInteger(s, sizeof(s), (t >= 0)); } if ((errstr && *errstr == '\0') || !errstr || - (*errstr == '\'' && radix == 16)) - Set(t); - - throw EXCEPT("UNKNOWN INPUT BYTES.", INTEGER_ERROR); + (*errstr == '\'' && radix == 16)) { + ; /* the set happens in the prior blocks. */ + } else { + char s[512]; + snprintf(s, 512, "Invalid bytes: %s", errstr); + throw EXCEPT(strdup(s), INTEGER_ERROR); + } } AsnInt::~AsnInt() @@ -207,6 +226,7 @@ void AsnInt::storeDERInteger(const unsigned char *pData, long dataLen, bool unsi { m_len = 0; delete [] m_bytes; + m_bytes = 0; /* IF the application generates an r,s,p,q,g or y value in which the * first 9 bits are all set to 0, then the encoding software deletes the @@ -216,12 +236,11 @@ void AsnInt::storeDERInteger(const unsigned char *pData, long dataLen, bool unsi */ if (unsignedFlag) { // Check for leading nine bits all zero - if (dataLen > 1) { - while (dataLen > 1 && !((pData[0] & 0xFF) || (pData[1] & 0x80))) { + while (dataLen > 1 && (pData[0] & 0xFF) == 0x00 && + (pData[1] & 0x80) == 0x0) { ++pData; --dataLen; } - } m_bytes = new unsigned char[dataLen + 1]; m_len = dataLen; @@ -230,13 +249,15 @@ void AsnInt::storeDERInteger(const unsigned char *pData, long dataLen, bool unsi * MSB is set to 1, THEN the software prepends a single octet in which * all bits are set to 0. */ + unsigned char *cwrite = m_bytes; if (*pData & 0x80) { // Prepend a leading octet - memcpy(m_bytes + 1, pData, dataLen); - *m_bytes = '\0'; + *m_bytes = 0; m_len++; - } else - memcpy(m_bytes, pData, dataLen); + cwrite++; + } + memcpy(cwrite, pData, dataLen); + } else if (dataLen > 1 ) { /* check for first first 9 bits all ones @@ -245,16 +266,20 @@ void AsnInt::storeDERInteger(const unsigned char *pData, long dataLen, bool unsi ++pData; --dataLen; } - + /* check for first 9 bits all zeros */ while ((dataLen > 1) && (pData[0] == 0) && ((pData[1] & 0x80) == 0)) { - ++pData; - --dataLen; - } + ++pData; + --dataLen; + } m_bytes = new unsigned char[dataLen + 1]; m_len = dataLen; memcpy(m_bytes, pData, dataLen); + } else { + m_bytes = new unsigned char [1]; + m_len = 1; + m_bytes[0] = *pData; } } @@ -278,16 +303,21 @@ void AsnInt::Set (const unsigned char *pData, size_t len, bool unsignedFlag) storeDERInteger(pData, len, unsignedFlag); } +template +void endswap(T *objp) +{ + unsigned char *memp = reinterpret_cast(objp); + std::reverse(memp, memp + sizeof(T)); +} + // Set AsnInt from a AsnIntType // void AsnInt::Set(AsnIntType iIn) { - AsnIntType iTmp; unsigned char cTmp[sizeof(iIn)]; - iTmp = iIn; for (unsigned long i=0; i < sizeof(iIn); i++) - cTmp[3-i] = (unsigned char)((iTmp >> (8*i)) & 0xff); + cTmp[(sizeof(iIn)-1)-i] = (unsigned char)((iIn >> (8*i)) & 0xff); storeDERInteger(cTmp, sizeof(iIn), (iIn >= 0)); } @@ -295,7 +325,7 @@ void AsnInt::Set(AsnIntType iIn) void AsnInt::getPadded(unsigned char *&bigIntDataOut, size_t &bigIntLen, const size_t padToSize) const { - FUNC("AsnInt::GetUnSignedBitExtendedData()"); + FUNC("AsnInt::GetUnSignedBitExtendedData()"); bigIntLen = m_len; const unsigned char *bigIntData = m_bytes; @@ -303,48 +333,41 @@ void AsnInt::getPadded(unsigned char *&bigIntDataOut, size_t &bigIntLen, /* This is fix to determine if the r,s,p,q,g, or y value is of the correct * length. */ - if (padToSize > 0) - { + if (padToSize > 0) { /* if bigint length is less than the expected number of octets * the decoding software ensures that the MSB is 0 and, if so, it * prepends the appropriate number of octets in which every bit is * set to 0 to the decoded value to obtain the value supplied to * Fortezza Card. */ - if ( bigIntLen < padToSize ) - { + if (bigIntLen < padToSize) { long prepend = 0; unsigned char *tmpInt; prepend = padToSize - bigIntLen; tmpInt = (unsigned char *) calloc(1, bigIntLen + prepend); - memset( tmpInt, 0, prepend); - memcpy( &tmpInt[prepend], bigIntData , bigIntLen); + memset(tmpInt, 0, prepend); + memcpy(&tmpInt[prepend], bigIntData , bigIntLen); bigIntDataOut = tmpInt; bigIntLen += prepend; - } + } else if (bigIntLen > padToSize) { /* If the encoded values includes an "extra" octet THEN the * decoding software ensures that every bit in the initial octets is * set to 0 and, if so, deletes the initial octet from the decoded value * to obtain the value to be supplied to the Fortezza Card. If the * extra octet contains a bit set to 1, then an error is reported. */ - else if (bigIntLen > padToSize) - { - if (bigIntData[0] != 0) - { + if (bigIntData[0] != 0) { throw EXCEPT("Extra octet is not zero.", INTEGER_ERROR); - } - bigIntLen--; - bigIntDataOut = (unsigned char *) calloc(1, bigIntLen); - memcpy( &bigIntDataOut[0], &bigIntData[1], bigIntLen); - } - else // Exact length. - { - bigIntDataOut = (unsigned char *) calloc(1, bigIntLen); - memcpy( &bigIntDataOut[0], &bigIntData[0], bigIntLen); + } + bigIntLen--; + bigIntDataOut = (unsigned char *) calloc(1, bigIntLen); + memcpy( &bigIntDataOut[0], &bigIntData[1], bigIntLen); + } else { // Exact length. + bigIntDataOut = (unsigned char *) calloc(1, bigIntLen); + memcpy( &bigIntDataOut[0], &bigIntData[0], bigIntLen); } } // bigIntData AND bigIntLen contain the results. From patchwork Tue Aug 7 17:36:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [esnacc-dev,RFC,4/4] cxx/asn-octs: Fix the Octet class X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 34 X-Patchwork-Delegate: aconole@bytheb.org Message-Id: <20180807173657.7572-5-aconole@bytheb.org> To: dev@lists.esnacc.org Date: Tue, 7 Aug 2018 13:36:57 -0400 From: Aaron Conole List-Id: eSNACC Development discussion A change to the ASN octets class broke the way that AnyOf functioned. Signed-off-by: Aaron Conole --- cxx-lib/src/asn-octs.cpp | 128 ++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 84 deletions(-) diff --git a/cxx-lib/src/asn-octs.cpp b/cxx-lib/src/asn-octs.cpp index 02d7795..228774d 100644 --- a/cxx-lib/src/asn-octs.cpp +++ b/cxx-lib/src/asn-octs.cpp @@ -604,10 +604,11 @@ public: {length = l; count = c; } }; -void ConsStringDeck::Fill(const AsnBuf &b, AsnLen elmtLen, AsnLen &bytesDecoded) +void ConsStringDeck::Fill(const AsnBuf &b, AsnLen elmtLen, + AsnLen &bytesDecoded) { FUNC("ConsStringDeck::Fill()"); - + AsnLen totalElmtsLen1 = 0; std::list refList; refList.insert (refList.begin(), RefNode(elmtLen, totalElmtsLen1)); @@ -616,115 +617,74 @@ void ConsStringDeck::Fill(const AsnBuf &b, AsnLen elmtLen, AsnLen &bytesDecoded) bool done = false; unsigned char *strPtr; unsigned long tagId1; - - while ( !done ) - { - for (; (curr != refList.end()) && ((curr->count < curr->length) || (curr->length == INDEFINITE_LEN));) - { + + while (!done) { + for (; (curr != refList.end()) && + ((curr->count < curr->length) || + (curr->length == INDEFINITE_LEN)); ) { tagId1 = BDecTag (b, curr->count); - if (tagId1 == EOC_TAG_ID && curr->length == INDEFINITE_LEN) - { + if (tagId1 == EOC_TAG_ID && curr->length == INDEFINITE_LEN) { // We may have found a EOC TAG // if next byte is a 0 then is an EOC tag - if (b.GetByte() == 0) - { + if (b.GetByte() == 0) { ++curr->count; break; - - } - else - { + } else { throw EXCEPT("Partial EOC tag found", DECODE_ERROR); } - } - else if (tagId1 == MAKE_TAG_ID (UNIV, PRIM, m_baseTag)) - { + } else if (tagId1 == MAKE_TAG_ID (UNIV, PRIM, m_baseTag) || + m_baseTag == 0) { /* * primitive part of string, put references to piece (s) in * str stack */ - totalElmtsLen1 = BDecLen (b, curr->count); - - if(totalElmtsLen1 == INDEFINITE_LEN) - { - throw InvalidTagException("Primitive String can not have INDEFINITE_LEN", tagId1, STACK_ENTRY); - } - strPtr = (unsigned char *)b.GetSeg(totalElmtsLen1); - push_back( StringPair(strPtr, totalElmtsLen1) ); - curr->count += totalElmtsLen1; - } - else if (tagId1 == MAKE_TAG_ID (UNIV, CONS, m_baseTag)) - { + totalElmtsLen1 = BDecLen (b, curr->count); + + if(totalElmtsLen1 == INDEFINITE_LEN) { + throw InvalidTagException( + "Primitive String can not have INDEFINITE_LEN", + tagId1, STACK_ENTRY); + } + if (totalElmtsLen1 > b.length()) { + throw InvalidTagException("Primitive String, length", + tagId1, STACK_ENTRY); + } + strPtr = (unsigned char *)b.GetSeg(totalElmtsLen1); + push_back(StringPair(strPtr, totalElmtsLen1)); + curr->count += totalElmtsLen1; + } else if (tagId1 == MAKE_TAG_ID (UNIV, CONS, m_baseTag) || + (m_baseTag == 0 && TAG_IS_CONS(tagId1))) { /* * primitive part of string, put references to piece (s) in * str stack */ + totalElmtsLen1 = BDecLen(b, curr->count); - totalElmtsLen1 = BDecLen(b, curr->count); - - if ((totalElmtsLen1 != INDEFINITE_LEN) && (totalElmtsLen1 + curr->count) > curr->length/*elmtLen*/) - { - throw BoundsException("Invalid constructed object", STACK_ENTRY); - } - - curr = refList.insert (refList.end(), RefNode(totalElmtsLen1, 0)); - //curr = curr->next; - - //Fill(b, curr->length, curr->count); - } - else if (m_baseTag == 0 && TAG_IS_CONS(tagId1)) - { - /* Handle set and sequence - */ - totalElmtsLen1 = BDecLen(b, curr->count); - - if ((totalElmtsLen1 != INDEFINITE_LEN) && (totalElmtsLen1 + curr->count) > curr->length/*elmtLen*/) - { - throw BoundsException("Invalid constructed object", STACK_ENTRY); - } - - curr = refList.insert (refList.end(), RefNode(totalElmtsLen1, 0) ); - //Fill(b, curr->length, curr->count); - } - else if (m_baseTag == 0) - { - totalElmtsLen1 = BDecLen (b, curr->count); - - if (totalElmtsLen1 == INDEFINITE_LEN) - { - throw InvalidTagException("Primitive String can not have INDEFINITE_LEN", tagId1, STACK_ENTRY); - } - - if(totalElmtsLen1 > b.length()) - { - throw InvalidTagException("Primitive String, length", tagId1, STACK_ENTRY); - } - - strPtr = (unsigned char *)b.GetSeg(totalElmtsLen1); - push_back( StringPair(strPtr, totalElmtsLen1) ); - curr->count += totalElmtsLen1; - } - else - { - throw InvalidTagException("Constructed String", tagId1, STACK_ENTRY); + if ((totalElmtsLen1 != INDEFINITE_LEN) && + (totalElmtsLen1 + curr->count) > curr->length) { + throw BoundsException("Invalid constructed object", + STACK_ENTRY); + } + + curr = refList.insert(refList.end(), + RefNode(totalElmtsLen1, 0)); + } else { + throw InvalidTagException("Constructed String", tagId1, + STACK_ENTRY); } } /* end of for */ - if( curr != refList.begin() && curr != refList.end() ) - { + if (curr != refList.begin() && curr != refList.end()) { int iTmpCount = curr->count; curr = refList.erase(curr); - if( curr != refList.end() ) + if (curr != refList.end()) curr->count += iTmpCount; else done = true; - } - else - { + } else { done = true; } - } bytesDecoded += refList.begin()->count;