about summary refs log blame commit diff stats
path: root/016dilated_reagent.cc
blob: 1d89ba8d13771419ed17d73a8787989cd0fcb360 (plain) (tree)
a796831fpre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include "config.h"

#ifdef HAVE_LIBOTR
#include <libotr/proto.h>
#include "otr/otr.h"
#endif

#include "config/preferences.h"

#include "command/cmd_defs.h"
#include "command/cmd_funcs.h"
#include "ui/window_list.h"
#include "xmpp/xmpp.h"

#include "ui/ui.h"
#include "ui/stub_ui.h"

#define CMD_OTR "/otr"

#ifdef HAVE_LIBOTR
void
cmd_otr_log_shows_usage_when_no_args(void** state)
{
    gchar* args[] = { "log", NULL };

    expect_string(cons_bad_cmd_usage, cmd, CMD_OTR);

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    assert_true(result);
}

void
cmd_otr_log_shows_usage_when_invalid_subcommand(void** state)
{
    gchar* args[] = { "log", "wrong", NULL };

    expect_string(cons_bad_cmd_usage, cmd, CMD_OTR);

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    assert_true(result);
}

void
cmd_otr_log_on_enables_logging(void** state)
{
    gchar* args[] = { "log", "on", NULL };
    prefs_set_string(PREF_OTR_LOG, "off");
    prefs_set_boolean(PREF_CHLOG, TRUE);

    expect_cons_show("OTR messages will be logged as plaintext.");

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    char* pref_otr_log = prefs_get_string(PREF_OTR_LOG);

    assert_true(result);
    assert_string_equal("on", pref_otr_log);
    g_free(pref_otr_log);
}

void
cmd_otr_log_on_shows_warning_when_chlog_disabled(void** state)
{
    gchar* args[] = { "log", "on", NULL };
    prefs_set_string(PREF_OTR_LOG, "off");
    prefs_set_boolean(PREF_CHLOG, FALSE);

    expect_cons_show("OTR messages will be logged as plaintext.");
    expect_cons_show("Chat logging is currently disabled, use '/logging chat on' to enable.");

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    assert_true(result);
}

void
cmd_otr_log_off_disables_logging(void** state)
{
    gchar* args[] = { "log", "off", NULL };
    prefs_set_string(PREF_OTR_LOG, "on");
    prefs_set_boolean(PREF_CHLOG, TRUE);

    expect_cons_show("OTR message logging disabled.");

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    char* pref_otr_log = prefs_get_string(PREF_OTR_LOG);

    assert_true(result);
    assert_string_equal("off", pref_otr_log);
    g_free(pref_otr_log);
}

void
cmd_otr_redact_redacts_logging(void** state)
{
    gchar* args[] = { "log", "redact", NULL };
    prefs_set_string(PREF_OTR_LOG, "on");
    prefs_set_boolean(PREF_CHLOG, TRUE);

    expect_cons_show("OTR messages will be logged as '[redacted]'.");

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    char* pref_otr_log = prefs_get_string(PREF_OTR_LOG);

    assert_true(result);
    assert_string_equal("redact", pref_otr_log);
    g_free(pref_otr_log);
}

void
cmd_otr_log_redact_shows_warning_when_chlog_disabled(void** state)
{
    gchar* args[] = { "log", "redact", NULL };
    prefs_set_string(PREF_OTR_LOG, "off");
    prefs_set_boolean(PREF_CHLOG, FALSE);

    expect_cons_show("OTR messages will be logged as '[redacted]'.");
    expect_cons_show("Chat logging is currently disabled, use '/logging chat on' to enable.");

    gboolean result = cmd_otr_log(NULL, CMD_OTR, args);
    assert_true(result);
}

void
cmd_otr_libver_shows_libotr_version(void** state)
{
    gchar* args[] = { "libver", NULL };
    char* version = "9.9.9";
    GString* message = g_string_new("Using libotr version ");
    g_string_append(message, version);

    will_return(otr_libotr_version, version);

    expect_cons_show(message->str);

    gboolean result = cmd_otr_libver(NULL, CMD_OTR, args);
    assert_true(result);

    g_string_free(message, TRUE);
}

void
cmd_otr_gen_shows_message_when_not_connected(void** state)
{
    gchar* args[] = { "gen", NULL };

    will_return(connection_get_status, JABBER_DISCONNECTED);

    expect_cons_show("You must be connected with an account to load OTR information.");

    gboolean result = cmd_otr_gen(NULL, CMD_OTR, args);
    assert_true(result);
}

static void
test_with_command_and_connection_status(char* command, void* cmd_func, jabber_conn_status_t status)
{
    gchar* args[] = { command, NULL };

    will_return(connection_get_status, status);

    expect_cons_show("You must be connected with an account to load OTR information.");

    gboolean (*func)(ProfWin * window, const char* const comma
//: An alternative syntax for reagents that permits whitespace in properties,
//: grouped by brackets. We'll use this ability in the next layer, when we
//: generalize types from lists to trees of properties.

:(scenarios load)
:(scenario dilated_reagent)
def main [
  {1: number, foo: bar} <- copy 34
]
+parse:   product: {1: "number", "foo": "bar"}

:(scenario load_trailing_space_after_curly_bracket)
def main [
  # line below has a space at the end
  { 
]
# successfully parsed

:(scenario dilated_reagent_with_comment)
def main [
  {1: number, foo: bar} <- copy 34  # test comment
]
+parse:   product: {1: "number", "foo": "bar"}
$error: 0

:(scenario dilated_reagent_with_comment_immediately_following)
def main [
  1:number <- copy {34: literal}  # test comment
]
$error: 0

//: First augment next_word to group balanced brackets together.

:(before "End next_word Special-cases")
if (in.peek() == '(')
  return slurp_balanced_bracket(in);
// treat curlies mostly like parens, but don't mess up labels
if (start_of_dilated_reagent(in))
  return slurp_balanced_bracket(in);

:(code)
// A curly is considered a label if it's the last thing on a line. Dilated
// reagents should remain all on one line.
bool start_of_dilated_reagent(istream& in) {
  if (in.peek() != '{') return false;
  int pos = in.tellg();
  in.get();  // slurp '{'
  skip_whitespace_but_not_newline(in);
  char next = in.peek();
  in.seekg(pos);
  return next != '\n';
}

// Assume the first letter is an open bracket, and read everything until the
// matching close bracket.
// We balance {} () and [].
string slurp_balanced_bracket(istream& in) {
  ostringstream result;
  char c;
  list<char> open_brackets;
  while (in >> c) {
    if (c == '(') open_brackets.push_back(c);
    if (c == ')') {
      if (open_brackets.empty() || open_brackets.back() != '(') {
        raise << "unbalanced ')'\n" << end();
        continue;
      }
      assert(open_brackets.back() == '(');
      open_brackets.pop_back();
    }
    if (c == '[') open_brackets.push_back(c);
    if (c == ']') {
      if (open_brackets.empty() || open_brackets.back() != '[') {
        raise << "unbalanced ']'\n" << end();
        continue;
      }
      open_brackets.pop_back();
    }
    if (c == '{') open_brackets.push_back(c);
    if (c == '}') {
      if (open_brackets.empty() || open_brackets.back() != '{') {
        raise << "unbalanced '}'\n" << end();
        continue;
      }
      open_brackets.pop_back();
    }
    result << c;
    if (open_brackets.empty()) break;
  }
  skip_whitespace_and_comments_but_not_newline(in);
  return result.str();
}

:(after "Parsing reagent(string s)")
if (starts_with(s, "{")) {
  assert(properties.empty());
  istringstream in(s);
  in >> std::noskipws;
  in.get();  // skip '{'
  name = slurp_key(in);
  if (name.empty()) {
    raise << "invalid reagent '" << s << "' without a name\n" << end();
    return;
  }
  if (name == "}") {
    raise << "invalid empty reagent '" << s << "'\n" << end();
    return;
  }
  {
    string s = next_word(in);
    if (s.empty()) {
      assert(!has_data(in));
      raise << "incomplete dilated reagent at end of file (0)\n" << end();
      return;
    }
    string_tree* type_names = new string_tree(s);
    // End Parsing Dilated Reagent Type Property(type_names)
    type = new_type_tree(type_names);
    delete type_names;
  }
  while (has_data(in)) {
    string key = slurp_key(in);
    if (key.empty()) continue;
    if (key == "}") continue;
    string s = next_word(in);
    if (s.empty()) {
      assert(!has_data(in));
      raise << "incomplete dilated reagent at end of file (1)\n" << end();
      return;
    }
    string_tree* value = new string_tree(s);
    // End Parsing Dilated Reagent Property(value)
    properties.push_back(pair<string, string_tree*>(key, value));
  }
  return;
}

:(code)
string slurp_key(istream& in) {
  string result = next_word(in);
  if (result.empty()) {
    assert(!has_data(in));
    raise << "incomplete dilated reagent at end of file (2)\n" << end();
    return result;
  }
  while (!result.empty() && *result.rbegin() == ':')
    strip_last(result);
  while (isspace(in.peek()) || in.peek() == ':')
    in.get();
  return result;
}
an class="n">window.type = WIN_CHAT; window.layout = NULL; window.urls_ac = NULL; ProfChatWin chatwin; chatwin.window = window; chatwin.barejid = recipient; chatwin.memcheck = PROFCHATWIN_MEMCHECK; chatwin.pgp_send = FALSE; chatwin.is_otr = TRUE; expect_win_println("You are already in an OTR session."); gboolean result = cmd_otr_start((ProfWin*)&chatwin, CMD_OTR, args); assert_true(result); } void cmd_otr_start_shows_message_when_no_key(void** state) { char* recipient = "someone@server.org"; gchar* args[] = { "start", NULL }; will_return(connection_get_status, JABBER_CONNECTED); will_return(otr_key_loaded, FALSE); ProfWin window; window.type = WIN_CHAT; window.layout = NULL; window.urls_ac = NULL; ProfChatWin chatwin; chatwin.window = window; chatwin.barejid = recipient; chatwin.memcheck = PROFCHATWIN_MEMCHECK; chatwin.pgp_send = FALSE; chatwin.is_otr = FALSE; expect_win_println("You have not generated or loaded a private key, use '/otr gen'"); gboolean result = cmd_otr_start((ProfWin*)&chatwin, CMD_OTR, args); assert_true(result); } void cmd_otr_start_sends_otr_query_message_to_current_recipeint(void** state) { char* recipient = "buddy@chat.com"; char* query_message = "?OTR?"; gchar* args[] = { "start", NULL }; ProfWin window; window.type = WIN_CHAT; window.layout = NULL; window.urls_ac = NULL; ProfChatWin chatwin; chatwin.window = window; chatwin.barejid = recipient; chatwin.memcheck = PROFCHATWIN_MEMCHECK; chatwin.pgp_send = FALSE; chatwin.is_otr = FALSE; will_return(connection_get_status, JABBER_CONNECTED); will_return(otr_key_loaded, TRUE); will_return(otr_start_query, query_message); expect_string(message_send_chat_otr, barejid, recipient); expect_string(message_send_chat_otr, msg, query_message); gboolean result = cmd_otr_start((ProfWin*)&chatwin, CMD_OTR, args); assert_true(result); } #else void cmd_otr_shows_message_when_otr_unsupported(void** state) { gchar* args[] = { "gen", NULL }; expect_cons_show("This version of Profanity has not been built with OTR support enabled"); gboolean result = cmd_otr_gen(NULL, CMD_OTR, args); assert_true(result); } #endif