diff options
author | hrxi <hrrrxi@gmail.com> | 2019-09-01 18:18:25 +0200 |
---|---|---|
committer | fiaxh <fiaxh@users.noreply.github.com> | 2019-09-10 19:36:11 +0200 |
commit | d5d305193ce527f1cc3022c406de35d9a85d4ccb (patch) | |
tree | d12efc741319a7d71c13f7bf6c2c7579d25fdabe /xmpp-vala | |
parent | 9950742bf1903291c271619aea101b0e2f81d19c (diff) | |
download | dino-d5d305193ce527f1cc3022c406de35d9a85d4ccb.tar.gz dino-d5d305193ce527f1cc3022c406de35d9a85d4ccb.zip |
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.
Diffstat (limited to 'xmpp-vala')
-rw-r--r-- | xmpp-vala/CMakeLists.txt | 3 | ||||
-rw-r--r-- | xmpp-vala/src/core/stanza_reader.vala | 2 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0166_jingle.vala | 2 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala | 9 | ||||
-rw-r--r-- | xmpp-vala/src/util.vala | 50 | ||||
-rw-r--r-- | xmpp-vala/tests/common.vala | 1 | ||||
-rw-r--r-- | xmpp-vala/tests/util.vala | 24 |
7 files changed, 86 insertions, 5 deletions
diff --git a/xmpp-vala/CMakeLists.txt b/xmpp-vala/CMakeLists.txt index a0c15579..e0f01723 100644 --- a/xmpp-vala/CMakeLists.txt +++ b/xmpp-vala/CMakeLists.txt @@ -79,6 +79,8 @@ SOURCES "src/module/xep/0368_srv_records_tls.vala" "src/module/xep/0380_explicit_encryption.vala" "src/module/xep/pixbuf_storage.vala" + + "src/util.vala" PACKAGES ${ENGINE_PACKAGES} GENERATE_VAPI @@ -110,6 +112,7 @@ if(BUILD_TESTS) "tests/testcase.vala" "tests/stanza.vala" + "tests/util.vala" CUSTOM_VAPIS ${CMAKE_BINARY_DIR}/exports/xmpp-vala_internal.vapi PACKAGES diff --git a/xmpp-vala/src/core/stanza_reader.vala b/xmpp-vala/src/core/stanza_reader.vala index c90390b5..1727847d 100644 --- a/xmpp-vala/src/core/stanza_reader.vala +++ b/xmpp-vala/src/core/stanza_reader.vala @@ -52,8 +52,6 @@ public class StanzaReader { 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."); buffer_pos = 0; - } catch (TlsError e) { - throw new XmlError.TLS("TlsError: %s".printf(e.message)); } catch (GLib.IOError e) { throw new XmlError.IO("GLib.IOError: %s".printf(e.message)); } diff --git a/xmpp-vala/src/module/xep/0166_jingle.vala b/xmpp-vala/src/module/xep/0166_jingle.vala index 06e3d5c8..86396f30 100644 --- a/xmpp-vala/src/module/xep/0166_jingle.vala +++ b/xmpp-vala/src/module/xep/0166_jingle.vala @@ -543,7 +543,7 @@ public class Session { throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method"); } 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)); state = State.CONNECTING; diff --git a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala index 25fe3ce4..951ea7b7 100644 --- a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala +++ b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala @@ -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())))); // TODO(hrxi): Add the mandatory hash field - Jingle.Session 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"? + 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"? + } catch (Jingle.Error e) { + throw new IOError.FAILED(@"couldn't create Jingle session: $(e.message)"); + } session.terminate_on_connection_close = false; yield session.conn.input_stream.close_async(); diff --git a/xmpp-vala/src/util.vala b/xmpp-vala/src/util.vala new file mode 100644 index 00000000..34a05b7a --- /dev/null +++ b/xmpp-vala/src/util.vala @@ -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; +} + +} diff --git a/xmpp-vala/tests/common.vala b/xmpp-vala/tests/common.vala index 01cc7d09..b91bbf7c 100644 --- a/xmpp-vala/tests/common.vala +++ b/xmpp-vala/tests/common.vala @@ -4,6 +4,7 @@ int main(string[] args) { GLib.Test.init(ref args); GLib.Test.set_nonfatal_assertions(); 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(); } diff --git a/xmpp-vala/tests/util.vala b/xmpp-vala/tests/util.vala new file mode 100644 index 00000000..9d893776 --- /dev/null +++ b/xmpp-vala/tests/util.vala @@ -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)); + }); + } +} + +} |