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>
This commit is contained in:
David Benjamin 2016-08-04 16:45:15 -04:00 committed by Adam Langley
parent db0c693b76
commit ea655fa33f

View File

@ -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;
}