Fix some warnings

Instances of `RegexError` are just asserted as `assert_not_reached` as
they cannot really fail except for allocation failure if the given regex
is valid.
This commit is contained in:
hrxi 2019-09-01 18:18:25 +02:00 committed by fiaxh
parent 9950742bf1
commit d5d305193c
25 changed files with 275 additions and 138 deletions

View file

@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(Dino LANGUAGES C)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
# Prepare Plugins # Prepare Plugins

View file

@ -236,6 +236,7 @@ public class Database : Qlite.Database {
message.fts_rebuild(); message.fts_rebuild();
} }
if (oldVersion < 8) { if (oldVersion < 8) {
try {
exec(""" exec("""
insert into content_item (conversation_id, time, local_time, content_type, foreign_id, hide) insert into content_item (conversation_id, time, local_time, content_type, foreign_id, hide)
select conversation.id, message.time, message.local_time, 1, message.id, 0 select conversation.id, message.time, message.local_time, 1, message.id, 0
@ -257,8 +258,13 @@ public class Database : Qlite.Database {
file_transfer.counterpart_id=conversation.jid_id and file_transfer.counterpart_id=conversation.jid_id and
message.type=conversation.type+1 and message.type=conversation.type+1 and
(message.counterpart_resource=conversation.resource or message.type != 3)"""); (message.counterpart_resource=conversation.resource or message.type != 3)""");
} catch (Error e) {
stderr.printf("Failed to upgrade to database version 8: %s\n", e.message);
Process.exit(-1);
}
} }
if (oldVersion < 9) { if (oldVersion < 9) {
try {
exec(""" exec("""
insert into content_item (conversation_id, time, local_time, content_type, foreign_id, hide) insert into content_item (conversation_id, time, local_time, content_type, foreign_id, hide)
select conversation.id, message.time, message.local_time, 1, message.id, 1 select conversation.id, message.time, message.local_time, 1, message.id, 1
@ -270,6 +276,10 @@ public class Database : Qlite.Database {
where where
message.body in (select info from file_transfer where info not null) or message.body in (select info from file_transfer where info not null) or
message.id in (select info from file_transfer where info not null)"""); message.id in (select info from file_transfer where info not null)""");
} catch (Error e) {
stderr.printf("Failed to upgrade to database version 8: %s\n", e.message);
Process.exit(-1);
}
} }
} }

View file

@ -5,6 +5,7 @@ using Gtk;
using Dino.Entities; using Dino.Entities;
using Xmpp; using Xmpp;
using Xmpp.Util;
namespace Dino.Ui { namespace Dino.Ui {
@ -259,10 +260,10 @@ public class AvatarGenerator {
} }
private static void set_source_hex_color(Context ctx, string hex_color) { private static void set_source_hex_color(Context ctx, string hex_color) {
ctx.set_source_rgba((double) hex_color.substring(0, 2).to_long(null, 16) / 255, ctx.set_source_rgba((double) from_hex(hex_color.substring(0, 2)) / 255,
(double) hex_color.substring(2, 2).to_long(null, 16) / 255, (double) from_hex(hex_color.substring(2, 2)) / 255,
(double) hex_color.substring(4, 2).to_long(null, 16) / 255, (double) from_hex(hex_color.substring(4, 2)) / 255,
hex_color.length > 6 ? (double) hex_color.substring(6, 2).to_long(null, 16) / 255 : 1); hex_color.length > 6 ? (double) from_hex(hex_color.substring(6, 2)) / 255 : 1);
} }
} }

View file

@ -1,6 +1,7 @@
using Gtk; using Gtk;
using Dino.Entities; using Dino.Entities;
using Xmpp; using Xmpp;
using Xmpp.Util;
namespace Dino.Ui { namespace Dino.Ui {
@ -74,10 +75,10 @@ public class AvatarImage : Misc {
} }
private static void set_source_hex_color(Cairo.Context ctx, string hex_color) { private static void set_source_hex_color(Cairo.Context ctx, string hex_color) {
ctx.set_source_rgba((double) hex_color.substring(0, 2).to_long(null, 16) / 255, ctx.set_source_rgba((double) from_hex(hex_color.substring(0, 2)) / 255,
(double) hex_color.substring(2, 2).to_long(null, 16) / 255, (double) from_hex(hex_color.substring(2, 2)) / 255,
(double) hex_color.substring(4, 2).to_long(null, 16) / 255, (double) from_hex(hex_color.substring(4, 2)) / 255,
hex_color.length > 6 ? (double) hex_color.substring(6, 2).to_long(null, 16) / 255 : 1); hex_color.length > 6 ? (double) from_hex(hex_color.substring(6, 2)) / 255 : 1);
} }
public override bool draw(Cairo.Context ctx_in) { public override bool draw(Cairo.Context ctx_in) {

View file

@ -136,7 +136,11 @@ public class ConversationSelectorRow : ListBoxRow {
nick_label.label = last_message.direction == Message.DIRECTION_SENT ? _("Me") + ": " : ""; nick_label.label = last_message.direction == Message.DIRECTION_SENT ? _("Me") + ": " : "";
} }
try {
message_label.label = Markup.escape_text((/\s+/).replace_literal(last_message.body, -1, 0, " ")); message_label.label = Markup.escape_text((/\s+/).replace_literal(last_message.body, -1, 0, " "));
} catch (RegexError e) {
assert_not_reached();
}
break; break;
case FileItem.TYPE: case FileItem.TYPE:
FileItem file_item = last_content_item as FileItem; FileItem file_item = last_content_item as FileItem;

View file

@ -206,6 +206,7 @@ public class GlobalSearch : Overlay {
// Color the keywords // Color the keywords
int elongated_by = 0; int elongated_by = 0;
try {
Regex highlight_regex = new Regex(regex_str); Regex highlight_regex = new Regex(regex_str);
MatchInfo match_info; MatchInfo match_info;
string markup_text_bak = markup_text.down(); string markup_text_bak = markup_text.down();
@ -227,6 +228,9 @@ public class GlobalSearch : Overlay {
}); });
button.add(grid); button.add(grid);
return button; return button;
} catch (RegexError e) {
assert_not_reached();
}
} }
private Grid get_context_message_widget(MessageItem item) { private Grid get_context_message_widget(MessageItem item) {

View file

@ -125,8 +125,10 @@ public class Notifications : Object {
string body = _("%s invited you to %s").printf(display_name, display_room); string body = _("%s invited you to %s").printf(display_name, display_room);
notification.set_body(body); notification.set_body(body);
try {
Cairo.ImageSurface jid_avatar = yield (new AvatarGenerator(40, 40)).draw_jid(stream_interactor, from_jid, account); Cairo.ImageSurface jid_avatar = yield (new AvatarGenerator(40, 40)).draw_jid(stream_interactor, from_jid, account);
notification.set_icon(get_pixbuf_icon(jid_avatar)); notification.set_icon(get_pixbuf_icon(jid_avatar));
} catch (Error e) { }
Conversation conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(room_jid, account, Conversation.Type.GROUPCHAT); Conversation conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(room_jid, account, Conversation.Type.GROUPCHAT);
notification.set_default_action_and_target_value("app.open-muc-join", new Variant.int32(conversation.id)); notification.set_default_action_and_target_value("app.open-muc-join", new Variant.int32(conversation.id));

View file

@ -153,11 +153,19 @@ public class UnifiedWindowController : Object {
private void update_conversation_topic(string? subtitle = null) { private void update_conversation_topic(string? subtitle = null) {
if (subtitle != null) { if (subtitle != null) {
try {
conversation_topic = (/\s+/).replace_literal(subtitle, -1, 0, " "); conversation_topic = (/\s+/).replace_literal(subtitle, -1, 0, " ");
} catch (RegexError e) {
assert_not_reached();
}
} else if (conversation.type_ == Conversation.Type.GROUPCHAT) { } else if (conversation.type_ == Conversation.Type.GROUPCHAT) {
string? subject = stream_interactor.get_module(MucManager.IDENTITY).get_groupchat_subject(conversation.counterpart, conversation.account); string? subject = stream_interactor.get_module(MucManager.IDENTITY).get_groupchat_subject(conversation.counterpart, conversation.account);
if (subject != null) { if (subject != null) {
try {
conversation_topic = (/\s+/).replace_literal(subject, -1, 0, " "); conversation_topic = (/\s+/).replace_literal(subject, -1, 0, " ");
} catch (RegexError e) {
assert_not_reached();
}
} else { } else {
conversation_topic = null; conversation_topic = null;
} }

View file

@ -188,6 +188,7 @@ public static string parse_add_markup(string s_, string? highlight_word, bool pa
} }
if (highlight_word != null) { if (highlight_word != null) {
try {
Regex highlight_regex = new Regex("\\b" + Regex.escape_string(highlight_word.down()) + "\\b"); Regex highlight_regex = new Regex("\\b" + Regex.escape_string(highlight_word.down()) + "\\b");
MatchInfo match_info; MatchInfo match_info;
highlight_regex.match(s.down(), 0, out match_info); highlight_regex.match(s.down(), 0, out match_info);
@ -198,6 +199,9 @@ public static string parse_add_markup(string s_, string? highlight_word, bool pa
"<b>" + s[start:end] + "</b>" + "<b>" + s[start:end] + "</b>" +
parse_add_markup(s[end:s.length], highlight_word, parse_links, parse_text_markup, already_escaped); parse_add_markup(s[end:s.length], highlight_word, parse_links, parse_text_markup, already_escaped);
} }
} catch (RegexError e) {
assert_not_reached();
}
} }
if (parse_text_markup) { if (parse_text_markup) {
@ -206,6 +210,7 @@ public static string parse_add_markup(string s_, string? highlight_word, bool pa
for (int i = 0; i < markup_string.length; i++) { for (int i = 0; i < markup_string.length; i++) {
string markup_esc = Regex.escape_string(markup_string[i]); string markup_esc = Regex.escape_string(markup_string[i]);
try {
Regex regex = new Regex("(^|\\s)" + markup_esc + "(\\S.*?\\S|\\S)" + markup_esc + "($|\\s)"); Regex regex = new Regex("(^|\\s)" + markup_esc + "(\\S.*?\\S|\\S)" + markup_esc + "($|\\s)");
MatchInfo match_info; MatchInfo match_info;
regex.match(s.down(), 0, out match_info); regex.match(s.down(), 0, out match_info);
@ -216,6 +221,9 @@ public static string parse_add_markup(string s_, string? highlight_word, bool pa
@"<$(convenience_tag[i])>" + s[start:end] + @"</$(convenience_tag[i])>" + @"<$(convenience_tag[i])>" + s[start:end] + @"</$(convenience_tag[i])>" +
parse_add_markup(s[end:s.length], highlight_word, parse_links, parse_text_markup, already_escaped); parse_add_markup(s[end:s.length], highlight_word, parse_links, parse_text_markup, already_escaped);
} }
} catch (RegexError e) {
assert_not_reached();
}
} }
} }

View file

@ -38,7 +38,8 @@ public class OmemoFileDecryptor : FileDecryptor, Object {
return this.url_regex.match(http_file_receive.url) || (receive_data as OmemoHttpFileReceiveData) != null; return this.url_regex.match(http_file_receive.url) || (receive_data as OmemoHttpFileReceiveData) != null;
} }
public async InputStream decrypt_file(InputStream encrypted_stream, Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) { public async InputStream decrypt_file(InputStream encrypted_stream, Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) throws FileReceiveError {
try {
OmemoHttpFileReceiveData? omemo_http_receive_data = receive_data as OmemoHttpFileReceiveData; OmemoHttpFileReceiveData? omemo_http_receive_data = receive_data as OmemoHttpFileReceiveData;
if (omemo_http_receive_data == null) assert(false); if (omemo_http_receive_data == null) assert(false);
@ -68,6 +69,9 @@ public class OmemoFileDecryptor : FileDecryptor, Object {
uint8[] cleartext = Signal.aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data); uint8[] cleartext = Signal.aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data);
file_transfer.encryption = Encryption.OMEMO; file_transfer.encryption = Encryption.OMEMO;
return new MemoryInputStream.from_data(cleartext); return new MemoryInputStream.from_data(cleartext);
} catch (Error e) {
throw new FileReceiveError.DECRYPTION_FAILED("OMEMO file decryption error: %s".printf(e.message));
}
} }
private uint8[] hex_to_bin(string hex) { private uint8[] hex_to_bin(string hex) {

View file

@ -326,22 +326,22 @@ public class Manager : StreamInteractionModule, Object {
store.identity_key_store.local_registration_id = Random.int_range(1, int32.MAX); store.identity_key_store.local_registration_id = Random.int_range(1, int32.MAX);
Signal.ECKeyPair key_pair = Plugin.get_context().generate_key_pair(); Signal.ECKeyPair key_pair = Plugin.get_context().generate_key_pair();
store.identity_key_store.identity_key_private = key_pair.private.serialize(); store.identity_key_store.identity_key_private = new Bytes(key_pair.private.serialize());
store.identity_key_store.identity_key_public = key_pair.public.serialize(); store.identity_key_store.identity_key_public = new Bytes(key_pair.public.serialize());
identity_id = (int) db.identity.insert().or("REPLACE") identity_id = (int) db.identity.insert().or("REPLACE")
.value(db.identity.account_id, account.id) .value(db.identity.account_id, account.id)
.value(db.identity.device_id, (int) store.local_registration_id) .value(db.identity.device_id, (int) store.local_registration_id)
.value(db.identity.identity_key_private_base64, Base64.encode(store.identity_key_store.identity_key_private)) .value(db.identity.identity_key_private_base64, Base64.encode(store.identity_key_store.identity_key_private.get_data()))
.value(db.identity.identity_key_public_base64, Base64.encode(store.identity_key_store.identity_key_public)) .value(db.identity.identity_key_public_base64, Base64.encode(store.identity_key_store.identity_key_public.get_data()))
.perform(); .perform();
} catch (Error e) { } catch (Error e) {
// Ignore error // Ignore error
} }
} else { } else {
store.identity_key_store.local_registration_id = ((!)row)[db.identity.device_id]; store.identity_key_store.local_registration_id = ((!)row)[db.identity.device_id];
store.identity_key_store.identity_key_private = Base64.decode(((!)row)[db.identity.identity_key_private_base64]); store.identity_key_store.identity_key_private = new Bytes(Base64.decode(((!)row)[db.identity.identity_key_private_base64]));
store.identity_key_store.identity_key_public = Base64.decode(((!)row)[db.identity.identity_key_public_base64]); store.identity_key_store.identity_key_public = new Bytes(Base64.decode(((!)row)[db.identity.identity_key_public_base64]));
identity_id = ((!)row)[db.identity.id]; identity_id = ((!)row)[db.identity.id];
} }

View file

@ -1,3 +1,5 @@
using Xmpp.Util;
namespace Dino.Plugins.Omemo { namespace Dino.Plugins.Omemo {
public static string fingerprint_from_base64(string b64) { public static string fingerprint_from_base64(string b64) {
@ -19,7 +21,7 @@ public static string fingerprint_markup(string s) {
for (int i = 0; i < s.length; i += 4) { for (int i = 0; i < s.length; i += 4) {
string four_chars = s.substring(i, 4).down(); string four_chars = s.substring(i, 4).down();
int raw = (int) four_chars.to_long(null, 16); int raw = (int) from_hex(four_chars);
uint8[] bytes = {(uint8) ((raw >> 8) & 0xff - 128), (uint8) (raw & 0xff - 128)}; uint8[] bytes = {(uint8) ((raw >> 8) & 0xff - 128), (uint8) (raw & 0xff - 128)};
Checksum checksum = new Checksum(ChecksumType.SHA1); Checksum checksum = new Checksum(ChecksumType.SHA1);

View file

@ -35,7 +35,7 @@ public class PgpFileDecryptor : FileDecryptor, Object {
} }
return new MemoryInputStream.from_data(clear_data.data, GLib.free); return new MemoryInputStream.from_data(clear_data.data, GLib.free);
} catch (Error e) { } catch (Error e) {
throw new FileReceiveError.DECRYPTION_FAILED("PGP file decrypt error: %s".printf(e.message)); throw new FileReceiveError.DECRYPTION_FAILED("PGP file decryption error: %s".printf(e.message));
} }
} }
} }

View file

@ -1,6 +1,7 @@
using Gtk; using Gtk;
using Dino.Entities; using Dino.Entities;
using Xmpp.Util;
namespace Dino.Plugins.OpenPgp { namespace Dino.Plugins.OpenPgp {
@ -10,7 +11,7 @@ public static string markup_colorize_id(string s, bool is_fingerprint) {
for (int i = 0; i < s.length; i += 4) { for (int i = 0; i < s.length; i += 4) {
string four_chars = s.substring(i, 4).down(); string four_chars = s.substring(i, 4).down();
int raw = (int) four_chars.to_long(null, 16); int raw = (int) from_hex(four_chars);
uint8[] bytes = {(uint8) ((raw >> 8) & 0xff - 128), (uint8) (raw & 0xff - 128)}; uint8[] bytes = {(uint8) ((raw >> 8) & 0xff - 128), (uint8) (raw & 0xff - 128)};
Checksum checksum = new Checksum(ChecksumType.SHA1); Checksum checksum = new Checksum(ChecksumType.SHA1);

View file

@ -3,8 +3,8 @@ using Gee;
namespace Signal { namespace Signal {
public class SimpleIdentityKeyStore : IdentityKeyStore { public class SimpleIdentityKeyStore : IdentityKeyStore {
public override uint8[] identity_key_private { get; set; } public override Bytes identity_key_private { get; set; }
public override uint8[] identity_key_public { get; set; } public override Bytes identity_key_public { get; set; }
public override uint32 local_registration_id { get; set; } public override uint32 local_registration_id { get; set; }
private Map<string, Map<int, IdentityKeyStore.TrustedIdentity>> trusted_identities = new HashMap<string, Map<int, IdentityKeyStore.TrustedIdentity>>(); private Map<string, Map<int, IdentityKeyStore.TrustedIdentity>> trusted_identities = new HashMap<string, Map<int, IdentityKeyStore.TrustedIdentity>>();

View file

@ -1,8 +1,8 @@
namespace Signal { namespace Signal {
public abstract class IdentityKeyStore : Object { public abstract class IdentityKeyStore : Object {
public abstract uint8[] identity_key_private { get; set; } public abstract Bytes identity_key_private { get; set; }
public abstract uint8[] identity_key_public { get; set; } public abstract Bytes identity_key_public { get; set; }
public abstract uint32 local_registration_id { get; set; } public abstract uint32 local_registration_id { get; set; }
public signal void trusted_identity_added(TrustedIdentity id); public signal void trusted_identity_added(TrustedIdentity id);
@ -112,8 +112,8 @@ public class Store : Object {
static int iks_get_identity_key_pair(out Buffer public_data, out Buffer private_data, void* user_data) { static int iks_get_identity_key_pair(out Buffer public_data, out Buffer private_data, void* user_data) {
Store store = (Store) user_data; Store store = (Store) user_data;
public_data = new Buffer.from(store.identity_key_store.identity_key_public); public_data = new Buffer.from(store.identity_key_store.identity_key_public.get_data());
private_data = new Buffer.from(store.identity_key_store.identity_key_private); private_data = new Buffer.from(store.identity_key_store.identity_key_private.get_data());
return 0; return 0;
} }

View file

@ -15,8 +15,8 @@ Store setup_test_store_context(Context global_context) {
store.identity_key_store.local_registration_id = (Random.next_int() % 16380) + 1; store.identity_key_store.local_registration_id = (Random.next_int() % 16380) + 1;
ECKeyPair key_pair = global_context.generate_key_pair(); ECKeyPair key_pair = global_context.generate_key_pair();
store.identity_key_store.identity_key_private = key_pair.private.serialize(); store.identity_key_store.identity_key_private = new Bytes(key_pair.private.serialize());
store.identity_key_store.identity_key_public = key_pair.public.serialize(); store.identity_key_store.identity_key_public = new Bytes(key_pair.public.serialize());
} catch (Error e) { } catch (Error e) {
fail_if_reached(); fail_if_reached();
} }

View file

@ -218,9 +218,14 @@ namespace Signal {
[CCode (instance_pos = 1, cname = "ec_public_key_serialize")] [CCode (instance_pos = 1, cname = "ec_public_key_serialize")]
private int serialize_([CCode (pos = 0)] out Buffer buffer); private int serialize_([CCode (pos = 0)] out Buffer buffer);
[CCode (cname = "ec_public_key_serialize_")] [CCode (cname = "ec_public_key_serialize_")]
public uint8[] serialize() throws GLib.Error { public uint8[] serialize() {
Buffer buffer; Buffer buffer;
try {
throw_by_code(serialize_(out buffer)); throw_by_code(serialize_(out buffer));
} catch (GLib.Error e) {
// Can only throw for invalid arguments or out of memory.
GLib.assert_not_reached();
}
return buffer.data; return buffer.data;
} }
public int compare(ECPublicKey other); public int compare(ECPublicKey other);
@ -235,7 +240,12 @@ namespace Signal {
[CCode (cname = "ec_private_key_serialize_")] [CCode (cname = "ec_private_key_serialize_")]
public uint8[] serialize() throws GLib.Error { public uint8[] serialize() throws GLib.Error {
Buffer buffer; Buffer buffer;
try {
throw_by_code(serialize_(out buffer)); throw_by_code(serialize_(out buffer));
} catch (GLib.Error e) {
// Can only throw for invalid arguments or out of memory.
GLib.assert_not_reached();
}
return buffer.data; return buffer.data;
} }
public int compare(ECPublicKey other); public int compare(ECPublicKey other);

View file

@ -79,6 +79,8 @@ SOURCES
"src/module/xep/0368_srv_records_tls.vala" "src/module/xep/0368_srv_records_tls.vala"
"src/module/xep/0380_explicit_encryption.vala" "src/module/xep/0380_explicit_encryption.vala"
"src/module/xep/pixbuf_storage.vala" "src/module/xep/pixbuf_storage.vala"
"src/util.vala"
PACKAGES PACKAGES
${ENGINE_PACKAGES} ${ENGINE_PACKAGES}
GENERATE_VAPI GENERATE_VAPI
@ -110,6 +112,7 @@ if(BUILD_TESTS)
"tests/testcase.vala" "tests/testcase.vala"
"tests/stanza.vala" "tests/stanza.vala"
"tests/util.vala"
CUSTOM_VAPIS CUSTOM_VAPIS
${CMAKE_BINARY_DIR}/exports/xmpp-vala_internal.vapi ${CMAKE_BINARY_DIR}/exports/xmpp-vala_internal.vapi
PACKAGES PACKAGES

View file

@ -52,8 +52,6 @@ public class StanzaReader {
buffer_fill = (int) yield ((!)input).read_async(buffer, GLib.Priority.DEFAULT, cancellable); buffer_fill = (int) yield ((!)input).read_async(buffer, GLib.Priority.DEFAULT, cancellable);
if (buffer_fill == 0) throw new XmlError.EOF("End of input stream reached."); if (buffer_fill == 0) throw new XmlError.EOF("End of input stream reached.");
buffer_pos = 0; buffer_pos = 0;
} catch (TlsError e) {
throw new XmlError.TLS("TlsError: %s".printf(e.message));
} catch (GLib.IOError e) { } catch (GLib.IOError e) {
throw new XmlError.IO("GLib.IOError: %s".printf(e.message)); throw new XmlError.IO("GLib.IOError: %s".printf(e.message));
} }

View file

@ -543,7 +543,7 @@ public class Session {
throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method"); throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method");
} }
transport.on_transport_accept(content.transport); transport.on_transport_accept(content.transport);
StanzaNode description = content.description; // TODO(hrxi): handle this :P // TODO(hrxi): handle content.description :)
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
state = State.CONNECTING; state = State.CONNECTING;

View file

@ -56,8 +56,13 @@ public class Module : Jingle.ContentType, XmppStreamModule {
.put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(size.to_string())))); .put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(size.to_string()))));
// TODO(hrxi): Add the mandatory hash field // TODO(hrxi): Add the mandatory hash field
Jingle.Session session = stream.get_module(Jingle.Module.IDENTITY) Jingle.Session session;
try {
session = stream.get_module(Jingle.Module.IDENTITY)
.create_session(stream, Jingle.TransportType.STREAMING, receiver_full_jid, Jingle.Senders.INITIATOR, "a-file-offer", description); // TODO(hrxi): Why "a-file-offer"? .create_session(stream, Jingle.TransportType.STREAMING, receiver_full_jid, Jingle.Senders.INITIATOR, "a-file-offer", description); // TODO(hrxi): Why "a-file-offer"?
} catch (Jingle.Error e) {
throw new IOError.FAILED(@"couldn't create Jingle session: $(e.message)");
}
session.terminate_on_connection_close = false; session.terminate_on_connection_close = false;
yield session.conn.input_stream.close_async(); yield session.conn.input_stream.close_async();

50
xmpp-vala/src/util.vala Normal file
View file

@ -0,0 +1,50 @@
namespace Xmpp.Util {
// Parse a number from a hexadecimal representation.
//
// Skips any whitespace at the start of the string, parses as many valid
// characters as hexadecimal digits as possible (possibly zero) and returns
// them as an integer value.
//
// ```
// // 0x0
// print("0x%lx\n", from_hex(""));
//
// // 0x123abc
// print("0x%lx\n", from_hex("123abc"));
//
// // 0x0
// print("0x%lx\n", from_hex("0x123abc"));
//
// // 0xa
// print("0x%lx\n", from_hex("A quick brown fox jumps over the lazy dog."));
//
// // 0xfeed
// print("0x%lx\n", from_hex(" FEED ME "));
// ```
public long from_hex(string numeral) {
long result = 0;
bool skipping_whitespace = true;
foreach (uint8 byte in numeral.data) {
char c = (char)byte;
if (skipping_whitespace && c.isspace()) {
continue;
}
skipping_whitespace = false;
int digit;
if ('0' <= c && c <= '9') {
digit = c - '0';
} else if ('A' <= c && c <= 'F') {
digit = c - 'A' + 10;
} else if ('a' <= c && c <= 'f') {
digit = c - 'a' + 10;
} else {
break;
}
result = (result << 4) | digit;
}
return result;
}
}

View file

@ -4,6 +4,7 @@ int main(string[] args) {
GLib.Test.init(ref args); GLib.Test.init(ref args);
GLib.Test.set_nonfatal_assertions(); GLib.Test.set_nonfatal_assertions();
TestSuite.get_root().add_suite(new Xmpp.Test.StanzaTest().get_suite()); TestSuite.get_root().add_suite(new Xmpp.Test.StanzaTest().get_suite());
TestSuite.get_root().add_suite(new Xmpp.Test.UtilTest().get_suite());
return GLib.Test.run(); return GLib.Test.run();
} }

24
xmpp-vala/tests/util.vala Normal file
View file

@ -0,0 +1,24 @@
using Xmpp.Util;
namespace Xmpp.Test {
class UtilTest : Gee.TestCase {
public UtilTest() {
base("util");
add_hex_test(0x0, "");
add_hex_test(0x123abc, "123abc");
add_hex_test(0x0, "0x123abc");
add_hex_test(0xa, "A quick brown fox jumps over the lazy dog.");
add_hex_test(0xfeed, " FEED ME ");
}
private void add_hex_test(int expected, string str) {
string test_name = @"from_hex(\"$(str)\")";
add_test(test_name, () => {
fail_if_not_eq_int(expected, (int)from_hex(str));
});
}
}
}