Beispiel #1
0
static void test_cr_handling()
{
	unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
	unsigned char c7_expected[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r' };
	unsigned char c8[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r' };
	unsigned char c8_expected[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r', '\r' };

	unsigned char *packed;
	unsigned char *unpacked;
	long packed_size;
	long unpacked_size;

	packed = pack_7bit(c8, 8, 0, TRUE, &packed_size, 0);
	g_assert(packed != NULL);
	g_assert(packed_size == 8);
	g_assert(((packed[6] & 0xfe) >> 1) == '\r');
	g_assert((packed[7] & 0x7f) == '\r');

	unpacked = unpack_7bit(packed, 8, 0, TRUE, -1, &unpacked_size, 0);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 9);
	g_assert(memcmp(c8_expected, unpacked, 9) == 0);

	g_free(unpacked);
	g_free(packed);

	packed = pack_7bit(c7, 7, 0, TRUE, &packed_size, 0);
	g_assert(packed != NULL);
	g_assert(packed_size == 7);
	g_assert(((packed[6] & 0xfe) >> 1) == '\r');

	unpacked = unpack_7bit(packed, 7, 0, TRUE, -1, &unpacked_size, 0);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 7);
	g_assert(memcmp(c7, unpacked, 7) == 0);

	g_free(unpacked);
	g_free(packed);

	/* As above, but now unpack using SMS style, we should now have cr at
	 * the end of the stream
	 */
	packed = pack_7bit(c7, 7, 0, TRUE, &packed_size, 0);
	unpacked = unpack_7bit(packed, 7, 0, FALSE, 8, &unpacked_size, 0);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 8);
	g_assert(memcmp(c7_expected, unpacked, 8) == 0);

	g_free(unpacked);
	g_free(packed);
}
Beispiel #2
0
static void ril_ussd_request(struct ofono_ussd *ussd, int dcs,
			const unsigned char *pdu, int len,
			ofono_ussd_cb_t cb, void *data)
{
	struct ussd_data *ud = ofono_ussd_get_data(ussd);
	struct cb_data *cbd = cb_data_new(cb, data, ussd);
	enum sms_charset charset;
	char *text = NULL;
	int ret = 0;

	if (cbs_dcs_decode(dcs, NULL, NULL, &charset, NULL, NULL, NULL)) {

		if (charset == SMS_CHARSET_7BIT) {
			long written;

			text = (char *) unpack_7bit(pdu, len, 0, TRUE,
							0, &written, 1);
			if (text != NULL)
				*(text + written) = '\0';

		} else if (charset == SMS_CHARSET_UCS2) {
			text = g_convert((char *) pdu, len,
					"UTF-8//TRANSLIT", "UCS-2BE",
					NULL, NULL, NULL);
		} else {
			ofono_error("%s: No support for charset %d",
					__func__, charset);
		}
	}

	if (text) {
		struct parcel rilp;

		g_ril_request_send_ussd(ud->ril, text, &rilp);

		ret = g_ril_send(ud->ril, RIL_REQUEST_SEND_USSD,
					&rilp, ril_ussd_cb, ussd, NULL);
		g_free(text);
	}

	/*
	 * We do not wait for the SEND_USSD reply to do the callback, as some
	 * networks send it after sending one or more ON_USSD events. From the
	 * ofono core perspective, Initiate() does not return until one ON_USSD
	 * event is received: making here a successful callback just makes the
	 * core wait for that event.
	 */
	if (ret <= 0) {
		g_free(cbd);
		CALLBACK_WITH_FAILURE(cb, data);
	} else {
		g_idle_add(request_success, cbd);
	}
}
Beispiel #3
0
static void test_sms_handling()
{
	unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

	unsigned char *packed;
	unsigned char *unpacked;
	long packed_size;
	long unpacked_size;

	packed = pack_7bit(c7, 7, 0, FALSE, &packed_size, 0);
	g_assert(packed != NULL);
	g_assert(packed_size == 7);

	unpacked = unpack_7bit(packed, 7, 0, FALSE, 8, &unpacked_size, 0xff);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 8);
	g_assert(unpacked[7] == 0);
	g_assert(unpacked[8] == 0xff);

	g_free(unpacked);
	g_free(packed);

	packed = pack_7bit(c7, 7, 0, FALSE, &packed_size, 0);
	g_assert(packed != NULL);
	g_assert(packed_size == 7);

	unpacked = unpack_7bit(packed, 7, 0, FALSE, 7, &unpacked_size, 0xff);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 7);
	g_assert(unpacked[7] == 0xff);

	g_free(unpacked);
	g_free(packed);
}
Beispiel #4
0
static void ril_ussd_request(struct ofono_ussd *ussd, int dcs,
			const unsigned char *pdu, int len,
			ofono_ussd_cb_t cb, void *data)
{
	struct ussd_data *ud = ofono_ussd_get_data(ussd);
	struct cb_data *cbd = cb_data_new(cb, data, ussd);
	enum sms_charset charset;
	int ret = 0;

	if (cbs_dcs_decode(dcs, NULL, NULL, &charset, NULL, NULL, NULL)) {
		/* TODO: send other alphabets (maybe needed by STK) */
		if (charset == SMS_CHARSET_7BIT) {
			unsigned char *unpacked_buf;
			long written;

			unpacked_buf = unpack_7bit(pdu, len, 0, TRUE, 0,
							&written, 1);

			if (written >= 1) {
				struct parcel rilp;

				*(unpacked_buf + written) = '\0';
				g_ril_request_send_ussd(ud->ril,
							(char *) unpacked_buf,
							&rilp);

				ret = g_ril_send(ud->ril, RIL_REQUEST_SEND_USSD,
							&rilp, ril_ussd_cb,
							ussd, NULL);
			}

			g_free(unpacked_buf);
		} else {
			ofono_error("%s: No support for charset %d",
					__func__, charset);
		}
	}

	/*
	 * We do not wait for the SEND_USSD reply to do the callback, as some
	 * networks send it after sending one or more ON_USSD events. From the
	 * ofono core perspective, Initiate() does not return until one ON_USSD
	 * event is received: making here a successful callback just makes the
	 * core wait for that event.
	 */
	if (ret <= 0) {
		g_free(cbd);
		CALLBACK_WITH_FAILURE(cb, data);
	} else {
		g_idle_add(request_success, cbd);
	}
}
Beispiel #5
0
static void test_simple_deliver()
{
	struct sms sms;
	unsigned char *decoded_pdu;
	long pdu_len;
	gboolean ret;
	int data_len;
	unsigned char *unpacked;
	char *utf8;

	decoded_pdu = decode_hex(simple_deliver, -1, &pdu_len, 0);

	g_assert(decoded_pdu);
	g_assert(pdu_len == (long)strlen(simple_deliver) / 2);

	ret = sms_decode(decoded_pdu, pdu_len, FALSE, 30, &sms);

	g_free(decoded_pdu);

	g_assert(ret);
	g_assert(sms.type == SMS_TYPE_DELIVER);

	if (g_test_verbose()) {
		g_print("SMSC Address number_type: %d, number_plan: %d, %s\n",
			(int)sms.sc_addr.number_type,
			(int)sms.sc_addr.numbering_plan, sms.sc_addr.address);

		g_print("SMS type: %d\n", (int)sms.type);

		g_print("Originator-Address: %d, %d, %s\n",
			(int)sms.deliver.oaddr.number_type,
			(int)sms.deliver.oaddr.numbering_plan,
			sms.deliver.oaddr.address);

		g_print("PID: %d\n", (int)sms.deliver.pid);
		g_print("DCS: %d\n", (int)sms.deliver.dcs);

		print_scts(&sms.deliver.scts, "Timezone");
	}

	g_assert(sms.sc_addr.number_type == SMS_NUMBER_TYPE_INTERNATIONAL);
	g_assert(sms.sc_addr.numbering_plan == SMS_NUMBERING_PLAN_ISDN);
	g_assert(strcmp(sms.sc_addr.address, "31624000000") == 0);

	g_assert(sms.deliver.oaddr.number_type ==
			SMS_NUMBER_TYPE_INTERNATIONAL);
	g_assert(sms.deliver.oaddr.numbering_plan ==
			SMS_NUMBERING_PLAN_ISDN);
	g_assert(strcmp(sms.deliver.oaddr.address, "31641600986") == 0);

	g_assert(sms.deliver.pid == 0);
	g_assert(sms.deliver.dcs == 0);

	g_assert(sms.deliver.scts.year == 2);
	g_assert(sms.deliver.scts.month == 8);
	g_assert(sms.deliver.scts.day == 26);
	g_assert(sms.deliver.scts.hour == 19);
	g_assert(sms.deliver.scts.minute == 37);
	g_assert(sms.deliver.scts.second == 41);
	g_assert(sms.deliver.scts.timezone == -4);

	g_assert(sms.deliver.udl == 12);

	data_len = sms_udl_in_bytes(sms.deliver.udl, sms.deliver.dcs);

	g_assert(data_len == 11);

	unpacked = unpack_7bit(sms.deliver.ud, data_len, 0, FALSE,
				sms.deliver.udl, NULL, 0xff);

	g_assert(unpacked);

	utf8 = convert_gsm_to_utf8(unpacked, -1, NULL, NULL, 0xff);

	g_free(unpacked);

	g_assert(utf8);

	if (g_test_verbose())
		g_print("Decoded user data is: %s\n", utf8);

	g_assert(strcmp(utf8, "How are you?") == 0);

	g_free(utf8);
}
Beispiel #6
0
static void test_simple_submit()
{
	struct sms sms;
	unsigned char *decoded_pdu;
	long pdu_len;
	gboolean ret;
	int data_len;
	unsigned char *unpacked;
	char *utf8;

	decoded_pdu = decode_hex(simple_submit, -1, &pdu_len, 0);

	g_assert(decoded_pdu);
	g_assert(pdu_len == (long)strlen(simple_submit) / 2);

	ret = sms_decode(decoded_pdu, pdu_len, TRUE, 23, &sms);

	g_free(decoded_pdu);

	g_assert(ret);
	g_assert(sms.type == SMS_TYPE_SUBMIT);

	if (g_test_verbose()) {
		if (sms.sc_addr.address[0] == '\0')
			g_print("SMSC Address absent, default will be used\n");
		else
			g_print("SMSC Address number_type: %d,"
				" number_plan: %d, %s\n",
				(int)sms.sc_addr.number_type,
				(int)sms.sc_addr.numbering_plan,
				sms.sc_addr.address);

		g_print("SMS type: %d\n", (int)sms.type);

		g_print("Message Reference: %u\n", (int)sms.submit.mr);

		g_print("Destination-Address: %d, %d, %s\n",
			(int)sms.submit.daddr.number_type,
			(int)sms.submit.daddr.numbering_plan,
			sms.submit.daddr.address);

		g_print("PID: %d\n", (int)sms.submit.pid);
		g_print("DCS: %d\n", (int)sms.submit.dcs);

		print_vpf(sms.submit.vpf, &sms.submit.vp);
	}

	g_assert(strlen(sms.sc_addr.address) == 0);

	g_assert(sms.submit.mr == 0);

	g_assert(sms.submit.daddr.number_type ==
			SMS_NUMBER_TYPE_INTERNATIONAL);
	g_assert(sms.submit.daddr.numbering_plan ==
			SMS_NUMBERING_PLAN_ISDN);
	g_assert(strcmp(sms.submit.daddr.address, "46708251358") == 0);

	g_assert(sms.submit.pid == 0);
	g_assert(sms.submit.dcs == 0);

	g_assert(sms.submit.vpf == SMS_VALIDITY_PERIOD_FORMAT_RELATIVE);
	g_assert(sms.submit.vp.relative == 0xAA);

	g_assert(sms.submit.udl == 10);

	data_len = sms_udl_in_bytes(sms.submit.udl, sms.submit.dcs);

	g_assert(data_len == 9);

	unpacked = unpack_7bit(sms.submit.ud, data_len, 0, FALSE,
				sms.submit.udl, NULL, 0xff);

	g_assert(unpacked);

	utf8 = convert_gsm_to_utf8(unpacked, -1, NULL, NULL, 0xff);

	g_free(unpacked);

	g_assert(utf8);

	if (g_test_verbose())
		g_print("Decoded user data is: %s\n", utf8);

	g_assert(strcmp(utf8, "hellohello") == 0);

	g_free(utf8);
}
Beispiel #7
0
static void test_offset_handling()
{
	unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
	unsigned char c8[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
	unsigned char c9[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
	unsigned char *packed;
	unsigned char *unpacked;
	long packed_size;
	long unpacked_size;

	/* Pack at offset = 2 bytes, e.g. starting with 21st bit */
	packed = pack_7bit(c7, 6, 2, FALSE, &packed_size, 0);

	if (g_test_verbose())
		g_print("Packed to size: %ld\n", packed_size);

	g_assert(packed != NULL);
	g_assert(packed_size == 6);

	unpacked = unpack_7bit(packed, 6, 2, FALSE, 6, &unpacked_size, 0xff);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 6);
	g_assert(unpacked[6] == 0xff);
	g_assert(unpacked[0] == 'a');
	g_assert(unpacked[5] == 'f');

	g_free(unpacked);
	g_free(packed);

	/* Pack at offset = 6 bytes, we should be able to fit one character
	 * into the first byte, and the other 7 characters into the following
	 * 7 bytes.  The 7 MSB bits of the last byte should be 0 since
	 * we're not using CBS packing
	 */
	packed = pack_7bit(c8, 8, 6, FALSE, &packed_size, 0);

	if (g_test_verbose())
		g_print("Packed to size: %ld\n", packed_size);

	g_assert(packed != NULL);
	g_assert(packed_size == 8);

	unpacked = unpack_7bit(packed, 8, 6, FALSE, 8, &unpacked_size, 0xff);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 8);
	g_assert(unpacked[8] == 0xff);
	g_assert(unpacked[0] == 'a');
	g_assert(unpacked[7] == 'h');

	g_free(unpacked);
	g_free(packed);

	/* Same as above, but instead pack in 9 characters */
	packed = pack_7bit(c9, 9, 6, FALSE, &packed_size, 0);

	if (g_test_verbose())
		g_print("Packed to size: %ld\n", packed_size);

	g_assert(packed != NULL);
	g_assert(packed_size == 8);

	unpacked = unpack_7bit(packed, 8, 6, FALSE, 9, &unpacked_size, 0xff);
	if (g_test_verbose())
		g_print("Unpacked to size: %ld\n", unpacked_size);

	g_assert(unpacked != NULL);
	g_assert(unpacked_size == 9);
	g_assert(unpacked[9] == 0xff);
	g_assert(unpacked[0] == 'a');
	g_assert(unpacked[8] == 'i');

	g_free(unpacked);
	g_free(packed);
}
Beispiel #8
0
static void test_decode_encode()
{
	const char *sms = hex_packed;
	unsigned char *decoded, *packed;
	char *utf8, *hex_packed;
	unsigned char *gsm, *gsm_encoded;
	long hex_decoded_size;
	long unpacked_size, packed_size;
	long gsm_encoded_size;
	long i;

	if (g_test_verbose())
		g_print("Size of the orig string: %u\n",
			(unsigned int)strlen(sms));

	decoded = decode_hex(sms, -1, &hex_decoded_size, 0);

	g_assert(decoded != NULL);

	if (g_test_verbose())
		g_print("Decode to %ld bytes\n", hex_decoded_size);

	if (g_test_verbose()) {
		g_print("%s\n", sms);

		for (i = 0; i < hex_decoded_size; i++)
			g_print("%02X", decoded[i]);
		g_print("\n");
	}

	gsm = unpack_7bit(decoded, hex_decoded_size, 0, FALSE,
				reported_text_size, &unpacked_size, 0xff);

	g_assert(gsm != NULL);

	if (g_test_verbose())
		g_print("String unpacked to %ld bytes\n", unpacked_size);

	utf8 = convert_gsm_to_utf8(gsm, -1, NULL, NULL, 0xff);

	g_assert(utf8 != NULL);

	if (g_test_verbose())
		g_print("String is: -->%s<--\n", utf8);

	g_assert(strcmp(utf8, expected) == 0);

	gsm_encoded = convert_utf8_to_gsm(utf8, -1, NULL,
						&gsm_encoded_size, 0xff);

	g_assert(gsm_encoded != NULL);

	if (g_test_verbose())
		g_print("Converted back to GSM string of %ld bytes\n",
				gsm_encoded_size);

	g_assert(gsm_encoded[gsm_encoded_size] == 0xff);
	g_assert(gsm_encoded_size == unpacked_size);
	g_assert(memcmp(gsm_encoded, gsm, gsm_encoded_size) == 0);

	g_free(utf8);
	g_free(gsm);

	packed = pack_7bit(gsm_encoded, -1, 0, FALSE, &packed_size, 0xff);

	g_free(gsm_encoded);

	g_assert(packed != NULL);

	if (g_test_verbose())
		g_print("Packed GSM to size of %ld bytes\n", packed_size);

	if (g_test_verbose()) {
		for (i = 0; i < packed_size; i++)
			g_print("%02X", packed[i]);
		g_print("\n");
	}

	g_assert(packed_size == hex_decoded_size);
	g_assert(memcmp(packed, decoded, packed_size) == 0);

	g_free(decoded);

	hex_packed = encode_hex(packed, packed_size, 0);

	g_assert(hex_packed != NULL);

	g_free(packed);

	if (g_test_verbose())
		g_print("Hex encoded packed to size %ld bytes\n",
				(long)strlen(hex_packed));

	g_assert(strlen(hex_packed) == strlen(sms));
	g_assert(strcmp(hex_packed, sms) == 0);

	g_free(hex_packed);
}
Beispiel #9
0
static void test_simple_submit(void)
{
	struct sms sms;
	unsigned char *decoded_pdu;
	long pdu_len;
	gboolean ret;
	int data_len;
	unsigned char *unpacked;
	char *utf8;

	decoded_pdu = decode_hex(simple_submit, -1, &pdu_len, 0);

	g_assert(decoded_pdu);
	g_assert(pdu_len == (long)strlen(simple_submit) / 2);

	ret = sms_decode(decoded_pdu, pdu_len, TRUE, 23, &sms);

	g_free(decoded_pdu);

	g_assert(ret);
	g_assert(sms.type == SMS_TYPE_SUBMIT);

	if (g_test_verbose())
		dump_details(&sms);

	g_assert(strlen(sms.sc_addr.address) == 0);

	g_assert(sms.submit.mr == 0);

	g_assert(sms.submit.daddr.number_type ==
			SMS_NUMBER_TYPE_INTERNATIONAL);
	g_assert(sms.submit.daddr.numbering_plan ==
			SMS_NUMBERING_PLAN_ISDN);
	g_assert(strcmp(sms.submit.daddr.address, "46708251358") == 0);

	g_assert(sms.submit.pid == 0);
	g_assert(sms.submit.dcs == 0);

	g_assert(sms.submit.vpf == SMS_VALIDITY_PERIOD_FORMAT_RELATIVE);
	g_assert(sms.submit.vp.relative == 0xAA);

	g_assert(sms.submit.udl == 10);

	data_len = sms_udl_in_bytes(sms.submit.udl, sms.submit.dcs);

	g_assert(data_len == 9);

	unpacked = unpack_7bit(sms.submit.ud, data_len, 0, FALSE,
				sms.submit.udl, NULL, 0xff);

	g_assert(unpacked);

	utf8 = convert_gsm_to_utf8(unpacked, -1, NULL, NULL, 0xff);

	g_free(unpacked);

	g_assert(utf8);

	if (g_test_verbose())
		g_print("Decoded user data is: %s\n", utf8);

	g_assert(strcmp(utf8, "hellohello") == 0);

	g_free(utf8);
}
Beispiel #10
0
static void test_alnum_sender(void)
{
	struct sms sms;
	unsigned char *decoded_pdu;
	long pdu_len;
	gboolean ret;
	int data_len;
	unsigned char *unpacked;
	char *utf8;

	decoded_pdu = decode_hex(alnum_sender, -1, &pdu_len, 0);

	g_assert(decoded_pdu);
	g_assert(pdu_len == (long)strlen(alnum_sender) / 2);

	ret = sms_decode(decoded_pdu, pdu_len, FALSE, 27, &sms);

	g_free(decoded_pdu);

	g_assert(ret);
	g_assert(sms.type == SMS_TYPE_DELIVER);

	if (g_test_verbose())
		dump_details(&sms);

	g_assert(sms.sc_addr.number_type == SMS_NUMBER_TYPE_INTERNATIONAL);
	g_assert(sms.sc_addr.numbering_plan == SMS_NUMBERING_PLAN_ISDN);
	g_assert(strcmp(sms.sc_addr.address, "447785016005") == 0);

	g_assert(sms.deliver.oaddr.number_type ==
			SMS_NUMBER_TYPE_ALPHANUMERIC);
	g_assert(sms.deliver.oaddr.numbering_plan ==
			SMS_NUMBERING_PLAN_UNKNOWN);
	g_assert(strcmp(sms.deliver.oaddr.address, "sipgate") == 0);

	g_assert(sms.deliver.pid == 0);
	g_assert(sms.deliver.dcs == 0);

	g_assert(sms.deliver.scts.year == 8);
	g_assert(sms.deliver.scts.month == 8);
	g_assert(sms.deliver.scts.day == 13);
	g_assert(sms.deliver.scts.hour == 12);
	g_assert(sms.deliver.scts.minute == 07);
	g_assert(sms.deliver.scts.second == 22);
	g_assert(sms.deliver.scts.timezone == 4);

	g_assert(sms.deliver.udl == 8);

	data_len = sms_udl_in_bytes(sms.deliver.udl, sms.deliver.dcs);

	g_assert(data_len == 7);

	unpacked = unpack_7bit(sms.deliver.ud, data_len, 0, FALSE,
				sms.deliver.udl, NULL, 0xff);

	g_assert(unpacked);

	utf8 = convert_gsm_to_utf8(unpacked, -1, NULL, NULL, 0xff);

	g_free(unpacked);

	g_assert(utf8);

	if (g_test_verbose())
		g_print("Decoded user data is: %s\n", utf8);

	g_assert(strcmp(utf8, "Testmail") == 0);

	g_free(utf8);
}