~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/golang.org/x/crypto/otr/libotr_test_helper.c

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
#include <proto.h>
15
15
#include <message.h>
 
16
#include <privkey.h>
16
17
 
17
18
static int g_session_established = 0;
18
19
 
20
21
  return OTRL_POLICY_ALWAYS;
21
22
}
22
23
 
23
 
int is_logged_in(void *opdata, const char *accountname, const char *protocol, const char *recipient) {
 
24
int is_logged_in(void *opdata, const char *accountname, const char *protocol,
 
25
                 const char *recipient) {
24
26
  return 1;
25
27
}
26
28
 
27
 
void inject_message(void *opdata, const char *accountname, const char *protocol, const char *recipient, const char *message) {
 
29
void inject_message(void *opdata, const char *accountname, const char *protocol,
 
30
                    const char *recipient, const char *message) {
28
31
  printf("%s\n", message);
29
32
  fflush(stdout);
30
33
  fprintf(stderr, "libotr helper sent: %s\n", message);
31
34
}
32
35
 
33
 
void notify(void *opdata, OtrlNotifyLevel level, const char *accountname, const char *protocol, const char *username, const char *title, const char *primary, const char *secondary) {
34
 
  fprintf(stderr, "NOTIFY: %s %s %s %s\n", username, title, primary, secondary);
35
 
}
36
 
 
37
 
int display_otr_message(void *opdata, const char *accountname, const char *protocol, const char *username, const char *msg) {
38
 
  fprintf(stderr, "MESSAGE: %s %s\n", username, msg);
39
 
  return 1;
40
 
}
41
 
 
42
 
void update_context_list(void *opdata) {
43
 
}
44
 
 
45
 
const char *protocol_name(void *opdata, const char *protocol) {
46
 
        return "PROTOCOL";
47
 
}
48
 
 
49
 
void protocol_name_free(void *opdata, const char *protocol_name) {
50
 
}
51
 
 
52
 
void new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, const char *protocol, const char *username, unsigned char fingerprint[20]) {
53
 
        fprintf(stderr, "NEW FINGERPRINT\n");
54
 
        g_session_established = 1;
55
 
}
56
 
 
57
 
void write_fingerprints(void *opdata) {
58
 
}
59
 
 
60
 
void gone_secure(void *opdata, ConnContext *context) {
61
 
}
62
 
 
63
 
void gone_insecure(void *opdata, ConnContext *context) {
64
 
}
65
 
 
66
 
void still_secure(void *opdata, ConnContext *context, int is_reply) {
67
 
}
68
 
 
69
 
void log_message(void *opdata, const char *message) {
70
 
        fprintf(stderr, "MESSAGE: %s\n", message);
71
 
}
72
 
 
73
 
int max_message_size(void *opdata, ConnContext *context) {
74
 
  return 99999;
75
 
}
76
 
 
77
 
const char *account_name(void *opdata, const char *account, const char *protocol) {
78
 
        return "ACCOUNT";
79
 
}
80
 
 
81
 
void account_name_free(void *opdata, const char *account_name) {
 
36
void update_context_list(void *opdata) {}
 
37
 
 
38
void new_fingerprint(void *opdata, OtrlUserState us, const char *accountname,
 
39
                     const char *protocol, const char *username,
 
40
                     unsigned char fingerprint[20]) {
 
41
  fprintf(stderr, "NEW FINGERPRINT\n");
 
42
  g_session_established = 1;
 
43
}
 
44
 
 
45
void write_fingerprints(void *opdata) {}
 
46
 
 
47
void gone_secure(void *opdata, ConnContext *context) {}
 
48
 
 
49
void gone_insecure(void *opdata, ConnContext *context) {}
 
50
 
 
51
void still_secure(void *opdata, ConnContext *context, int is_reply) {}
 
52
 
 
53
int max_message_size(void *opdata, ConnContext *context) { return 99999; }
 
54
 
 
55
const char *account_name(void *opdata, const char *account,
 
56
                         const char *protocol) {
 
57
  return "ACCOUNT";
 
58
}
 
59
 
 
60
void account_name_free(void *opdata, const char *account_name) {}
 
61
 
 
62
const char *error_message(void *opdata, ConnContext *context,
 
63
                          OtrlErrorCode err_code) {
 
64
  return "ERR";
 
65
}
 
66
 
 
67
void error_message_free(void *opdata, const char *msg) {}
 
68
 
 
69
void resent_msg_prefix_free(void *opdata, const char *prefix) {}
 
70
 
 
71
void handle_smp_event(void *opdata, OtrlSMPEvent smp_event,
 
72
                      ConnContext *context, unsigned short progress_event,
 
73
                      char *question) {}
 
74
 
 
75
void handle_msg_event(void *opdata, OtrlMessageEvent msg_event,
 
76
                      ConnContext *context, const char *message,
 
77
                      gcry_error_t err) {
 
78
  fprintf(stderr, "msg event: %d %s\n", msg_event, message);
82
79
}
83
80
 
84
81
OtrlMessageAppOps uiops = {
85
 
  policy,
86
 
  NULL,
87
 
  is_logged_in,
88
 
  inject_message,
89
 
  notify,
90
 
  display_otr_message,
91
 
  update_context_list,
92
 
  protocol_name,
93
 
  protocol_name_free,
94
 
  new_fingerprint,
95
 
  write_fingerprints,
96
 
  gone_secure,
97
 
  gone_insecure,
98
 
  still_secure,
99
 
  log_message,
100
 
  max_message_size,
101
 
  account_name,
102
 
  account_name_free,
 
82
    policy,
 
83
    NULL,
 
84
    is_logged_in,
 
85
    inject_message,
 
86
    update_context_list,
 
87
    new_fingerprint,
 
88
    write_fingerprints,
 
89
    gone_secure,
 
90
    gone_insecure,
 
91
    still_secure,
 
92
    max_message_size,
 
93
    account_name,
 
94
    account_name_free,
 
95
    NULL, /* received_symkey */
 
96
    error_message,
 
97
    error_message_free,
 
98
    NULL, /* resent_msg_prefix */
 
99
    resent_msg_prefix_free,
 
100
    handle_smp_event,
 
101
    handle_msg_event,
 
102
    NULL /* create_instag */,
 
103
    NULL /* convert_msg */,
 
104
    NULL /* convert_free */,
 
105
    NULL /* timer_control */,
103
106
};
104
107
 
105
 
static const char kPrivateKeyData[] = "(privkeys (account (name \"account\") (protocol proto) (private-key (dsa (p #00FC07ABCF0DC916AFF6E9AE47BEF60C7AB9B4D6B2469E436630E36F8A489BE812486A09F30B71224508654940A835301ACC525A4FF133FC152CC53DCC59D65C30A54F1993FE13FE63E5823D4C746DB21B90F9B9C00B49EC7404AB1D929BA7FBA12F2E45C6E0A651689750E8528AB8C031D3561FECEE72EBB4A090D450A9B7A857#) (q #00997BD266EF7B1F60A5C23F3A741F2AEFD07A2081#) (g #535E360E8A95EBA46A4F7DE50AD6E9B2A6DB785A66B64EB9F20338D2A3E8FB0E94725848F1AA6CC567CB83A1CC517EC806F2E92EAE71457E80B2210A189B91250779434B41FC8A8873F6DB94BEA7D177F5D59E7E114EE10A49CFD9CEF88AE43387023B672927BA74B04EB6BBB5E57597766A2F9CE3857D7ACE3E1E3BC1FC6F26#) (y #0AC8670AD767D7A8D9D14CC1AC6744CD7D76F993B77FFD9E39DF01E5A6536EF65E775FCEF2A983E2A19BD6415500F6979715D9FD1257E1FE2B6F5E1E74B333079E7C880D39868462A93454B41877BE62E5EF0A041C2EE9C9E76BD1E12AE25D9628DECB097025DD625EF49C3258A1A3C0FF501E3DC673B76D7BABF349009B6ECF#) (x #14D0345A3562C480A039E3C72764F72D79043216#)))))\n";
 
108
static const char kPrivateKeyData[] =
 
109
    "(privkeys (account (name \"account\") (protocol proto) (private-key (dsa "
 
110
    "(p "
 
111
    "#00FC07ABCF0DC916AFF6E9AE47BEF60C7AB9B4D6B2469E436630E36F8A489BE812486A09F"
 
112
    "30B71224508654940A835301ACC525A4FF133FC152CC53DCC59D65C30A54F1993FE13FE63E"
 
113
    "5823D4C746DB21B90F9B9C00B49EC7404AB1D929BA7FBA12F2E45C6E0A651689750E8528AB"
 
114
    "8C031D3561FECEE72EBB4A090D450A9B7A857#) (q "
 
115
    "#00997BD266EF7B1F60A5C23F3A741F2AEFD07A2081#) (g "
 
116
    "#535E360E8A95EBA46A4F7DE50AD6E9B2A6DB785A66B64EB9F20338D2A3E8FB0E94725848F"
 
117
    "1AA6CC567CB83A1CC517EC806F2E92EAE71457E80B2210A189B91250779434B41FC8A8873F"
 
118
    "6DB94BEA7D177F5D59E7E114EE10A49CFD9CEF88AE43387023B672927BA74B04EB6BBB5E57"
 
119
    "597766A2F9CE3857D7ACE3E1E3BC1FC6F26#) (y "
 
120
    "#0AC8670AD767D7A8D9D14CC1AC6744CD7D76F993B77FFD9E39DF01E5A6536EF65E775FCEF"
 
121
    "2A983E2A19BD6415500F6979715D9FD1257E1FE2B6F5E1E74B333079E7C880D39868462A93"
 
122
    "454B41877BE62E5EF0A041C2EE9C9E76BD1E12AE25D9628DECB097025DD625EF49C3258A1A"
 
123
    "3C0FF501E3DC673B76D7BABF349009B6ECF#) (x "
 
124
    "#14D0345A3562C480A039E3C72764F72D79043216#)))))\n";
106
125
 
107
 
int
108
 
main() {
 
126
int main() {
109
127
  OTRL_INIT;
110
128
 
111
129
  // We have to write the private key information to a file because the libotr
116
134
  }
117
135
 
118
136
  char private_key_file[256];
119
 
  snprintf(private_key_file, sizeof(private_key_file), "%s/libotr_test_helper_privatekeys-XXXXXX", tmpdir);
 
137
  snprintf(private_key_file, sizeof(private_key_file),
 
138
           "%s/libotr_test_helper_privatekeys-XXXXXX", tmpdir);
120
139
  int fd = mkstemp(private_key_file);
121
140
  if (fd == -1) {
122
141
    perror("creating temp file");
123
142
  }
124
 
  write(fd, kPrivateKeyData, sizeof(kPrivateKeyData)-1);
 
143
  write(fd, kPrivateKeyData, sizeof(kPrivateKeyData) - 1);
125
144
  close(fd);
126
145
 
127
146
  OtrlUserState userstate = otrl_userstate_create();
133
152
  char buf[4096];
134
153
 
135
154
  for (;;) {
136
 
    char* message = fgets(buf, sizeof(buf), stdin);
 
155
    char *message = fgets(buf, sizeof(buf), stdin);
137
156
    if (strlen(message) == 0) {
138
157
      break;
139
158
    }
142
161
 
143
162
    char *newmessage = NULL;
144
163
    OtrlTLV *tlvs;
145
 
    int ignore_message = otrl_message_receiving(userstate, &uiops, NULL, "account", "proto", "peer", message, &newmessage, &tlvs, NULL, NULL);
 
164
    int ignore_message = otrl_message_receiving(
 
165
        userstate, &uiops, NULL, "account", "proto", "peer", message,
 
166
        &newmessage, &tlvs, NULL, NULL, NULL);
146
167
    if (tlvs) {
147
 
            otrl_tlv_free(tlvs);
 
168
      otrl_tlv_free(tlvs);
148
169
    }
149
170
 
150
171
    if (newmessage != NULL) {
154
175
      gcry_error_t err;
155
176
      char *newmessage = NULL;
156
177
 
157
 
      err = otrl_message_sending(userstate, &uiops, NULL, "account", "proto", "peer", "test message", NULL, &newmessage, NULL, NULL);
 
178
      err = otrl_message_sending(userstate, &uiops, NULL, "account", "proto",
 
179
                                 "peer", 0, "test message", NULL, &newmessage,
 
180
                                 OTRL_FRAGMENT_SEND_SKIP, NULL, NULL, NULL);
158
181
      if (newmessage == NULL) {
159
182
        fprintf(stderr, "libotr didn't encrypt message\n");
160
183
        return 1;
161
184
      }
162
185
      write(1, newmessage, strlen(newmessage));
163
186
      write(1, "\n", 1);
 
187
      fprintf(stderr, "libotr sent: %s\n", newmessage);
 
188
      otrl_message_free(newmessage);
 
189
 
164
190
      g_session_established = 0;
165
 
      otrl_message_free(newmessage);
166
191
      write(1, "?OTRv2?\n", 8);
 
192
      fprintf(stderr, "libotr sent: ?OTRv2\n");
167
193
    }
168
194
  }
169
195