Beispiel #1
0
static void test_add_abunch_to_md_store(void) {
    grpc_credentials_md_store *store = grpc_credentials_md_store_create(4);
    size_t num_entries = 1000;
    const char *key_str = "hello";
    const char *value_str = "there blah blah blah blah blah blah blah";
    size_t i;
    for (i = 0; i < num_entries; i++) {
        grpc_credentials_md_store_add_cstrings(store, key_str, value_str);
    }
    for (i = 0; i < num_entries; i++) {
        GPR_ASSERT(gpr_slice_str_cmp(store->entries[i].key, key_str) == 0);
        GPR_ASSERT(gpr_slice_str_cmp(store->entries[i].value, value_str) == 0);
    }
    grpc_credentials_md_store_unref(store);
}
Beispiel #2
0
static void test_format_post_request(void) {
  grpc_http_header hdr = {"x-yz", "abc"};
  grpc_httpcli_request req;
  gpr_slice slice;
  char body_bytes[] = "fake body";
  size_t body_len = 9;

  memset(&req, 0, sizeof(req));
  req.host = "example.com";
  req.http.path = "/index.html";
  req.http.hdr_count = 1;
  req.http.hdrs = &hdr;

  slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);

  GPR_ASSERT(0 == gpr_slice_str_cmp(slice,
                                    "POST /index.html HTTP/1.0\r\n"
                                    "Host: example.com\r\n"
                                    "Connection: close\r\n"
                                    "User-Agent: " GRPC_HTTPCLI_USER_AGENT
                                    "\r\n"
                                    "x-yz: abc\r\n"
                                    "Content-Type: text/plain\r\n"
                                    "Content-Length: 9\r\n"
                                    "\r\n"
                                    "fake body"));

  gpr_slice_unref(slice);
}
Beispiel #3
0
static void test_format_post_request_content_type_override(void) {
  grpc_http_header hdrs[2];
  grpc_httpcli_request req;
  gpr_slice slice;
  char body_bytes[] = "fake%20body";
  size_t body_len = 11;

  hdrs[0].key = "x-yz";
  hdrs[0].value = "abc";
  hdrs[1].key = "Content-Type";
  hdrs[1].value = "application/x-www-form-urlencoded";
  memset(&req, 0, sizeof(req));
  req.host = "example.com";
  req.http.path = "/index.html";
  req.http.hdr_count = 2;
  req.http.hdrs = hdrs;

  slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);

  GPR_ASSERT(0 == gpr_slice_str_cmp(
                      slice,
                      "POST /index.html HTTP/1.0\r\n"
                      "Host: example.com\r\n"
                      "Connection: close\r\n"
                      "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"
                      "x-yz: abc\r\n"
                      "Content-Type: application/x-www-form-urlencoded\r\n"
                      "Content-Length: 11\r\n"
                      "\r\n"
                      "fake%20body"));

  gpr_slice_unref(slice);
}
Beispiel #4
0
static void test_oauth2_token_fetcher_creds_parsing_ok(void) {
    grpc_credentials_md_store *token_md = NULL;
    gpr_timespec token_lifetime;
    grpc_httpcli_response response =
        http_response(200, valid_oauth2_json_response);
    GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
                   &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_OK);
    GPR_ASSERT(token_lifetime.tv_sec == 3599);
    GPR_ASSERT(token_lifetime.tv_nsec == 0);
    GPR_ASSERT(token_md->num_entries == 1);
    GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].key, "Authorization") == 0);
    GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].value,
                                 "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") ==
               0);
    grpc_credentials_md_store_unref(token_md);
}
Beispiel #5
0
static void check_metadata(expected_md *expected, grpc_credentials_md *md_elems,
                           size_t num_md) {
    size_t i;
    for (i = 0; i < num_md; i++) {
        size_t j;
        for (j = 0; j < num_md; j++) {
            if (0 == gpr_slice_str_cmp(md_elems[j].key, expected[i].key)) {
                GPR_ASSERT(gpr_slice_str_cmp(md_elems[j].value, expected[i].value) ==
                           0);
                break;
            }
        }
        if (j == num_md) {
            gpr_log(GPR_ERROR, "key %s not found", expected[i].key);
            GPR_ASSERT(0);
        }
    }
}
Beispiel #6
0
static void test_create_string(void) {
  grpc_mdstr *s1, *s2, *s3;

  LOG_TEST("test_create_string");

  grpc_init();
  s1 = grpc_mdstr_from_string("hello");
  s2 = grpc_mdstr_from_string("hello");
  s3 = grpc_mdstr_from_string("very much not hello");
  GPR_ASSERT(s1 == s2);
  GPR_ASSERT(s3 != s1);
  GPR_ASSERT(gpr_slice_str_cmp(s1->slice, "hello") == 0);
  GPR_ASSERT(gpr_slice_str_cmp(s3->slice, "very much not hello") == 0);
  GRPC_MDSTR_UNREF(s1);
  GRPC_MDSTR_UNREF(s2);
  GRPC_MDSTR_UNREF(s3);
  grpc_shutdown();
}
Beispiel #7
0
static void test_create_string(void) {
  grpc_mdctx *ctx;
  grpc_mdstr *s1, *s2, *s3;

  LOG_TEST("test_create_string");

  ctx = grpc_mdctx_create();
  s1 = grpc_mdstr_from_string(ctx, "hello");
  s2 = grpc_mdstr_from_string(ctx, "hello");
  s3 = grpc_mdstr_from_string(ctx, "very much not hello");
  GPR_ASSERT(s1 == s2);
  GPR_ASSERT(s3 != s1);
  GPR_ASSERT(gpr_slice_str_cmp(s1->slice, "hello") == 0);
  GPR_ASSERT(gpr_slice_str_cmp(s3->slice, "very much not hello") == 0);
  GRPC_MDSTR_UNREF(s1);
  GRPC_MDSTR_UNREF(s2);
  grpc_mdctx_unref(ctx);
  GRPC_MDSTR_UNREF(s3);
}
Beispiel #8
0
static void test_create_metadata(void) {
  grpc_mdelem *m1, *m2, *m3;

  LOG_TEST("test_create_metadata");

  grpc_init();
  m1 = grpc_mdelem_from_strings("a", "b");
  m2 = grpc_mdelem_from_strings("a", "b");
  m3 = grpc_mdelem_from_strings("a", "c");
  GPR_ASSERT(m1 == m2);
  GPR_ASSERT(m3 != m1);
  GPR_ASSERT(m3->key == m1->key);
  GPR_ASSERT(m3->value != m1->value);
  GPR_ASSERT(gpr_slice_str_cmp(m1->key->slice, "a") == 0);
  GPR_ASSERT(gpr_slice_str_cmp(m1->value->slice, "b") == 0);
  GPR_ASSERT(gpr_slice_str_cmp(m3->value->slice, "c") == 0);
  GRPC_MDELEM_UNREF(m1);
  GRPC_MDELEM_UNREF(m2);
  GRPC_MDELEM_UNREF(m3);
  grpc_shutdown();
}
Beispiel #9
0
static void test_format_post_request_no_body(void) {
  grpc_http_header hdr = {"x-yz", "abc"};
  grpc_httpcli_request req;
  gpr_slice slice;

  memset(&req, 0, sizeof(req));
  req.host = "example.com";
  req.http.path = "/index.html";
  req.http.hdr_count = 1;
  req.http.hdrs = &hdr;

  slice = grpc_httpcli_format_post_request(&req, NULL, 0);

  GPR_ASSERT(0 == gpr_slice_str_cmp(slice,
                                    "POST /index.html HTTP/1.0\r\n"
                                    "Host: example.com\r\n"
                                    "Connection: close\r\n"
                                    "User-Agent: " GRPC_HTTPCLI_USER_AGENT
                                    "\r\n"
                                    "x-yz: abc\r\n"
                                    "\r\n"));

  gpr_slice_unref(slice);
}
Beispiel #10
0
static void test_strsplit(void) {
  gpr_slice_buffer *parts;
  gpr_slice str;

  LOG_TEST_NAME("test_strsplit");

  parts = gpr_malloc(sizeof(gpr_slice_buffer));
  gpr_slice_buffer_init(parts);

  str = gpr_slice_from_copied_string("one, two, three, four");
  gpr_slice_split(str, ", ", parts);
  GPR_ASSERT(4 == parts->count);
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one"));
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "two"));
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[2], "three"));
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[3], "four"));
  gpr_slice_buffer_reset_and_unref(parts);
  gpr_slice_unref(str);

  /* separator not present in string */
  str = gpr_slice_from_copied_string("one two three four");
  gpr_slice_split(str, ", ", parts);
  GPR_ASSERT(1 == parts->count);
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one two three four"));
  gpr_slice_buffer_reset_and_unref(parts);
  gpr_slice_unref(str);

  /* separator at the end */
  str = gpr_slice_from_copied_string("foo,");
  gpr_slice_split(str, ",", parts);
  GPR_ASSERT(2 == parts->count);
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "foo"));
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], ""));
  gpr_slice_buffer_reset_and_unref(parts);
  gpr_slice_unref(str);

  /* separator at the beginning */
  str = gpr_slice_from_copied_string(",foo");
  gpr_slice_split(str, ",", parts);
  GPR_ASSERT(2 == parts->count);
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "foo"));
  gpr_slice_buffer_reset_and_unref(parts);
  gpr_slice_unref(str);

  /* standalone separator */
  str = gpr_slice_from_copied_string(",");
  gpr_slice_split(str, ",", parts);
  GPR_ASSERT(2 == parts->count);
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], ""));
  gpr_slice_buffer_reset_and_unref(parts);
  gpr_slice_unref(str);

  /* empty input */
  str = gpr_slice_from_copied_string("");
  gpr_slice_split(str, ", ", parts);
  GPR_ASSERT(1 == parts->count);
  GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
  gpr_slice_buffer_reset_and_unref(parts);
  gpr_slice_unref(str);

  gpr_slice_buffer_destroy(parts);
  gpr_free(parts);
}
Beispiel #11
0
static void assert_str(const grpc_chttp2_hptbl *tbl, grpc_mdstr *mdstr,
                       const char *str) {
  GPR_ASSERT(gpr_slice_str_cmp(mdstr->slice, str) == 0);
}
Beispiel #12
0
static grpc_resolver *sockaddr_create(
    grpc_resolver_args *args, const char *default_lb_policy_name,
    int parse(grpc_uri *uri, struct sockaddr_storage *dst, size_t *len)) {
  size_t i;
  int errors_found = 0; /* GPR_FALSE */
  sockaddr_resolver *r;
  gpr_slice path_slice;
  gpr_slice_buffer path_parts;

  if (0 != strcmp(args->uri->authority, "")) {
    gpr_log(GPR_ERROR, "authority based uri's not supported by the %s scheme",
            args->uri->scheme);
    return NULL;
  }

  r = gpr_malloc(sizeof(sockaddr_resolver));
  memset(r, 0, sizeof(*r));

  r->lb_policy_name = NULL;
  if (0 != strcmp(args->uri->query, "")) {
    gpr_slice query_slice;
    gpr_slice_buffer query_parts;

    query_slice =
        gpr_slice_new(args->uri->query, strlen(args->uri->query), do_nothing);
    gpr_slice_buffer_init(&query_parts);
    gpr_slice_split(query_slice, "=", &query_parts);
    GPR_ASSERT(query_parts.count == 2);
    if (0 == gpr_slice_str_cmp(query_parts.slices[0], "lb_policy")) {
      r->lb_policy_name = gpr_dump_slice(query_parts.slices[1], GPR_DUMP_ASCII);
    }
    gpr_slice_buffer_destroy(&query_parts);
    gpr_slice_unref(query_slice);
  }
  if (r->lb_policy_name == NULL) {
    r->lb_policy_name = gpr_strdup(default_lb_policy_name);
  }

  path_slice =
      gpr_slice_new(args->uri->path, strlen(args->uri->path), do_nothing);
  gpr_slice_buffer_init(&path_parts);

  gpr_slice_split(path_slice, ",", &path_parts);
  r->num_addrs = path_parts.count;
  r->addrs = gpr_malloc(sizeof(struct sockaddr_storage) * r->num_addrs);
  r->addrs_len = gpr_malloc(sizeof(*r->addrs_len) * r->num_addrs);

  for (i = 0; i < r->num_addrs; i++) {
    grpc_uri ith_uri = *args->uri;
    char *part_str = gpr_dump_slice(path_parts.slices[i], GPR_DUMP_ASCII);
    ith_uri.path = part_str;
    if (!parse(&ith_uri, &r->addrs[i], &r->addrs_len[i])) {
      errors_found = 1; /* GPR_TRUE */
    }
    gpr_free(part_str);
    if (errors_found) break;
  }

  gpr_slice_buffer_destroy(&path_parts);
  gpr_slice_unref(path_slice);
  if (errors_found) {
    gpr_free(r->lb_policy_name);
    gpr_free(r->addrs);
    gpr_free(r->addrs_len);
    gpr_free(r);
    return NULL;
  }

  gpr_ref_init(&r->refs, 1);
  gpr_mu_init(&r->mu);
  grpc_resolver_init(&r->base, &sockaddr_resolver_vtable);
  r->subchannel_factory = args->subchannel_factory;
  grpc_subchannel_factory_ref(r->subchannel_factory);

  return &r->base;
}