aboutsummaryrefslogtreecommitdiff
path: root/crypto/obj/obj_test.cc
diff options
context:
space:
mode:
authorDavid Benjamin <davidben@google.com>2016-08-04 16:45:15 -0400
committerAdam Langley <agl@google.com>2016-08-05 18:32:30 +0000
commitea655fa33f08221e209a8865f4610156c7794184 (patch)
treebd9fc439143e2413dcc7cf3b4d0fbbafb1cfdca1 /crypto/obj/obj_test.cc
parentdb0c693b762ea412c9eaa3a2dc2708444dfd4f0a (diff)
downloadboringssl-ea655fa33f08221e209a8865f4610156c7794184.zip
boringssl-ea655fa33f08221e209a8865f4610156c7794184.tar.gz
boringssl-ea655fa33f08221e209a8865f4610156c7794184.tar.bz2
Write a test for OBJ_obj2txt.
OBJ_obj2txt's implementation is kind of scary. Also it casts between int and size_t a lot. In preparation for rewriting it, add a test. Change-Id: Iefb1d0cddff58d67e5b04ec332477aab8aa687b6 Reviewed-on: https://boringssl-review.googlesource.com/9130 Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'crypto/obj/obj_test.cc')
-rw-r--r--crypto/obj/obj_test.cc95
1 files changed, 94 insertions, 1 deletions
diff --git a/crypto/obj/obj_test.cc b/crypto/obj/obj_test.cc
index 2948941..9f610bd 100644
--- a/crypto/obj/obj_test.cc
+++ b/crypto/obj/obj_test.cc
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <string.h>
+#include <openssl/asn1.h>
#include <openssl/bytestring.h>
#include <openssl/crypto.h>
#include <openssl/obj.h>
@@ -93,11 +94,103 @@ static bool TestSignatureAlgorithms() {
return true;
}
+static bool ExpectObj2Txt(const uint8_t *der, size_t der_len,
+ bool dont_return_name,
+ const char *expected) {
+ ASN1_OBJECT obj;
+ memset(&obj, 0, sizeof(obj));
+ obj.data = der;
+ obj.length = static_cast<int>(der_len);
+
+ int expected_len = static_cast<int>(strlen(expected));
+
+ int len = OBJ_obj2txt(nullptr, 0, &obj, dont_return_name);
+ if (len != expected_len) {
+ fprintf(stderr,
+ "OBJ_obj2txt of %s with out_len = 0 returned %d, wanted %d.\n",
+ expected, len, expected_len);
+ return false;
+ }
+
+ char short_buf[1];
+ memset(short_buf, 0xff, sizeof(short_buf));
+ len = OBJ_obj2txt(short_buf, sizeof(short_buf), &obj, dont_return_name);
+ if (len != expected_len) {
+ fprintf(stderr,
+ "OBJ_obj2txt of %s with out_len = 1 returned %d, wanted %d.\n",
+ expected, len, expected_len);
+ return false;
+ }
+
+ if (memchr(short_buf, '\0', sizeof(short_buf)) == nullptr) {
+ fprintf(stderr,
+ "OBJ_obj2txt of %s with out_len = 1 did not NUL-terminate the "
+ "output.\n",
+ expected);
+ return false;
+ }
+
+ char buf[256];
+ len = OBJ_obj2txt(buf, sizeof(buf), &obj, dont_return_name);
+ if (len != expected_len) {
+ fprintf(stderr,
+ "OBJ_obj2txt of %s with out_len = 256 returned %d, wanted %d.\n",
+ expected, len, expected_len);
+ return false;
+ }
+
+ if (strcmp(buf, expected) != 0) {
+ fprintf(stderr, "OBJ_obj2txt returned \"%s\"; wanted \"%s\".\n", buf,
+ expected);
+ return false;
+ }
+
+ return true;
+}
+
+static bool TestObj2Txt() {
+ // kSHA256WithRSAEncryption is the DER representation of
+ // 1.2.840.113549.1.1.11, id-sha256WithRSAEncryption.
+ static const uint8_t kSHA256WithRSAEncryption[] = {
+ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
+ };
+
+ // kBasicConstraints is the DER representation of 2.5.29.19,
+ // id-basicConstraints.
+ static const uint8_t kBasicConstraints[] = {
+ 0x55, 0x1d, 0x13,
+ };
+
+ // kTestOID is the DER representation of 1.2.840.113554.4.1.72585.0,
+ // from https://davidben.net/oid.
+ static const uint8_t kTestOID[] = {
+ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00,
+ };
+
+ if (!ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
+ true /* don't return name */, "1.2.840.113549.1.1.11") ||
+ !ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
+ false /* return name */, "sha256WithRSAEncryption") ||
+ !ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
+ true /* don't return name */, "2.5.29.19") ||
+ !ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
+ false /* return name */, "X509v3 Basic Constraints") ||
+ !ExpectObj2Txt(kTestOID, sizeof(kTestOID), true /* don't return name */,
+ "1.2.840.113554.4.1.72585.0") ||
+ !ExpectObj2Txt(kTestOID, sizeof(kTestOID), false /* return name */,
+ "1.2.840.113554.4.1.72585.0")) {
+ return false;
+ }
+
+ return true;
+}
+
int main() {
CRYPTO_library_init();
if (!TestBasic() ||
- !TestSignatureAlgorithms()) {
+ !TestSignatureAlgorithms() ||
+ !TestObj2Txt()) {
return 1;
}