TEST_FIXTURE(page_blob_test_base, existing_page_blob_write_stream)
    {
        CHECK_THROW(m_blob.open_write(azure::storage::access_condition(), azure::storage::blob_request_options(), m_context), azure::storage::storage_exception);

        azure::storage::blob_request_options options;
        options.set_store_blob_content_md5(true);

        std::vector<uint8_t> buffer;
        buffer.resize(16 * 1024);
        fill_buffer_and_get_md5(buffer);
        m_blob.upload_from_stream(concurrency::streams::bytestream::open_istream(buffer), 0, azure::storage::access_condition(), options, m_context);

        CHECK_THROW(m_blob.open_write(azure::storage::access_condition(), options, m_context), std::logic_error);

        options.set_store_blob_content_md5(false);
        auto stream = m_blob.open_write(azure::storage::access_condition(), options, m_context);
        stream.seek(512);
        stream.streambuf().putn_nocopy(buffer.data(), 512).wait();
        stream.close().wait();

        concurrency::streams::container_buffer<std::vector<uint8_t>> downloaded_blob;
        CHECK_THROW(m_blob.download_to_stream(downloaded_blob.create_ostream(), azure::storage::access_condition(), options, m_context), azure::storage::storage_exception);

        downloaded_blob.seekpos(0, std::ios_base::out);
        options.set_disable_content_md5_validation(true);
        m_blob.download_to_stream(downloaded_blob.create_ostream(), azure::storage::access_condition(), options, m_context);

        CHECK_ARRAY_EQUAL(buffer.data(), downloaded_blob.collection().data(), 512);
        CHECK_ARRAY_EQUAL(buffer.data(), downloaded_blob.collection().data() + 512, 512);
        CHECK_ARRAY_EQUAL(buffer.data() + 1024, downloaded_blob.collection().data() + 1024, (int)(buffer.size()) - 1024);
    }
 TEST_FIXTURE(ConstructorFixture, BlockingSendAccumulatesOverMultipleCalls)
 {
   uint8_t expected[20] = {
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
   spark_protocol.blocking_send(expected, 20);
   CHECK_ARRAY_EQUAL(expected, sent_buf_0, 20);
 }
 TEST_FIXTURE(ConstructorFixture, HandshakeSendsExpectedHello)
 {
   const uint8_t expected[18] = {
     0x00, 0x10,
     0xde, 0x89, 0x60, 0x27, 0xba, 0x11, 0x1a, 0x95,
     0xbe, 0x39, 0x6f, 0x4d, 0xfc, 0x30, 0x9b, 0x6b };
   spark_protocol.handshake();
   CHECK_ARRAY_EQUAL(expected, sent_buf_1, 18);
 }
Example #4
0
    TEST_FIXTURE(StrModFixture, CopyENTerminateTest)
    {
        char *cc = fslc_strncpy_e(testString,"Hello, World!", 5);
        strncpy(expectedString,"Hello, World!", 5);

        CHECK_EQUAL((uintptr_t)testString+5, (uintptr_t)cc);

        CHECK_ARRAY_EQUAL(expectedString, testString, MAXLEN);
    }
Example #5
0
    TEST_FIXTURE(StrModFixture, Copy0TerminateTest)
    {
        char *cc = fslc_strncpy(testString,"Hello, World!", 30);
        strncpy(expectedString,"Hello, World!", 30);

        CHECK_EQUAL((uintptr_t)testString, (uintptr_t)cc);

        CHECK_ARRAY_EQUAL(expectedString, testString, strlen(expectedString)+1);
    }
 TEST_FIXTURE(ConstructorFixture, BlockingReceiveAccumulatesOverMultipleCalls)
 {
   uint8_t expected[20] = {
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
   uint8_t receive_buf[20];
   memcpy(message_to_receive, expected, 20);
   spark_protocol.blocking_receive(receive_buf, 20);
   CHECK_ARRAY_EQUAL(expected, receive_buf, 20);
 }
TEST_FIXTURE(HandshakeFixture, HMACMatchesOpenSSLExample)
{
  uint8_t hmac[20];
  uint8_t hmac_key[40];
  decipher_aes_credentials(private_key,
                           encrypted_aes_credentials,
                           hmac_key);
  calculate_ciphertext_hmac(encrypted_aes_credentials, hmac_key, hmac);
  CHECK_ARRAY_EQUAL(expected_hmac, hmac, 20);
}
Example #8
0
 TEST_FIXTURE(CoAPFixture, VariableValueDoubleMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0x00, 0x06, 0x3d, 0xb5, 0xcc, 0xda, 0xf9, 0xce,
     0x81, 0x26, 0x01, 0x97, 0xbb, 0x64, 0x8d, 0x97 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.variable_value(buf, 0x5d, 0xab, 0xce,-104.858);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #9
0
 TEST_FIXTURE(CoAPFixture, ChunkReceivedMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0x26, 0x03, 0x6a, 0x64, 0x8b, 0xba, 0xc7, 0x42,
     0xc9, 0xfa, 0x78, 0x7b, 0xf9, 0x25, 0x51, 0x01 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.chunk_received(buf, 0x01, ChunkReceivedCode::OK);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #10
0
 TEST_FIXTURE(CoAPFixture, UpdateReadyMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0xf8, 0xe2, 0xf5, 0xc5, 0x50, 0xa3, 0x12, 0x03,
     0xd0, 0xa0, 0xf9, 0x98, 0xe5, 0x48, 0x58, 0x71 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.update_ready(buf, 0x10);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #11
0
 TEST_FIXTURE(CoAPFixture, VariableValueBoolMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0xae, 0x35, 0x85, 0xd5, 0x6b, 0xc0, 0x6d, 0xd0,
     0xd6, 0x25, 0x75, 0xb9, 0xd2, 0x9c, 0x36, 0x42 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.variable_value(buf, 0x5f, 0xf6, 0x49, false);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #12
0
 TEST_FIXTURE(CoAPFixture, VariableValueIntMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0xbe, 0x47, 0x34, 0x62, 0x85, 0xc0, 0x7b, 0x92,
     0x21, 0xc8, 0x9a, 0xc4, 0x91, 0x96, 0xf5, 0xf6 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.variable_value(buf, 0x88, 0x97, 0xb2, -98765);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #13
0
 TEST_FIXTURE(CoAPFixture, FunctionReturnIntMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0x73, 0x9b, 0xa0, 0xad, 0xa9, 0xff, 0x01, 0x3d,
     0xcd, 0xc3, 0x18, 0xe4, 0x3e, 0x5a, 0x92, 0xe6 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.function_return(buf, 0xc3, 42);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #14
0
 TEST_FIXTURE(CoAPFixture, KeyChangedMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0x67, 0x64, 0x33, 0xa9, 0x58, 0x12, 0xca, 0x37,
     0x5f, 0x12, 0x59, 0x3b, 0x3e, 0x32, 0x64, 0xd3 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.key_changed(buf, 0x99);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #15
0
 TEST_FIXTURE(CoAPFixture, HelloMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0x3d, 0xc4, 0xa4, 0x21, 0xb8, 0x30, 0x9b, 0x9d,
     0xc8, 0x4b, 0x70, 0x07, 0x33, 0xc1, 0xff, 0x3e };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.hello(buf, false);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #16
0
 TEST_FIXTURE(CoAPFixture, VariableValueStringMatchesOpenSSL)
 {
   uint8_t expected[16] = {
     0x89, 0x21, 0x18, 0xc6, 0x35, 0x2f, 0xe7, 0xa0,
     0xeb, 0x6f, 0x7b, 0x0b, 0xb6, 0x31, 0xa2, 0x12 };
   unsigned char buf[16];
   memset(buf, 0, 16);
   init();
   spark_protocol.variable_value(buf, 0x5c, 0xf6, 0x49, "woot", 4);
   CHECK_ARRAY_EQUAL(expected, buf, 16);
 }
Example #17
0
 TEST_FIXTURE(CoAPFixture, PresenceAnnouncementMatchesExpected)
 {
   uint8_t expected[19] = {
     0x50, 0x02, 0x00, 0x00, 0xb1, 'h', 0xff,
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
   const char id[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
   unsigned char buf[19];
   memset(buf, 0, 19);
   init();
   spark_protocol.presence_announcement(buf, id);
   CHECK_ARRAY_EQUAL(expected, buf, 19);
 }
    TEST_FIXTURE(page_blob_test_base, page_blob_write_stream_seek_without_md5)
    {
        std::vector<uint8_t> buffer;
        std::vector<uint8_t> final_blob_contents;
        final_blob_contents.resize(16 * 1024);

        azure::storage::blob_request_options options;
        options.set_store_blob_content_md5(false);

        auto stream = m_blob.open_write(final_blob_contents.size(), 0, azure::storage::access_condition(), options, m_context);
        CHECK(stream.can_seek());

        // Because container create and blob create are also in the request results,
        // number of requests should start with 2.
        size_t attempts = 2;

        buffer.resize(1024);
        fill_buffer_and_get_md5(buffer);
        stream.streambuf().putn_nocopy(buffer.data(), buffer.size()).wait();

        std::copy(buffer.begin(), buffer.end(), final_blob_contents.begin());

        stream.seek(5 * 1024);
        attempts++;

        fill_buffer_and_get_md5(buffer);
        stream.streambuf().putn_nocopy(buffer.data(), buffer.size()).wait();

        std::copy(buffer.begin(), buffer.end(), final_blob_contents.begin() + 5 * 1024);

        fill_buffer_and_get_md5(buffer);
        stream.streambuf().putn_nocopy(buffer.data(), buffer.size()).wait();

        std::copy(buffer.begin(), buffer.end(), final_blob_contents.begin() + 6 * 1024);

        stream.seek(512);
        attempts++;

        fill_buffer_and_get_md5(buffer);
        stream.streambuf().putn_nocopy(buffer.data(), buffer.size()).wait();

        std::copy(buffer.begin(), buffer.end(), final_blob_contents.begin() + 512);

        stream.close().wait();
        attempts++;

        CHECK_EQUAL(attempts, m_context.request_results().size());

        concurrency::streams::container_buffer<std::vector<uint8_t>> downloaded_blob;
        m_blob.download_to_stream(downloaded_blob.create_ostream(), azure::storage::access_condition(), options, m_context);

        CHECK_ARRAY_EQUAL(final_blob_contents, downloaded_blob.collection(), (int)final_blob_contents.size());
    }
TEST_FIXTURE(HandshakeFixture, RSADecryptionMatchesOpenSSLExample)
{
  uint8_t expected[41] =
    "\x3E\x78\xEB\x7A\x0B\xB2\x1D\xFB\xF2\xB1\x53\x4B\x06\xE5\x66\x12"
    "\x14\x5A\x01\xF1\x07\x0E\x40\xA1\x11\x88\xF7\x84\xC4\x9B\xC6\x53"
    "\xC5\x76\x94\xFC\x1F\x3B\x7D\x72";
  uint8_t aes_credentials[40];
  decipher_aes_credentials(private_key,
                           encrypted_aes_credentials,
                           aes_credentials);
  CHECK_ARRAY_EQUAL(expected, aes_credentials, 40);
}
Example #20
0
TEST_FIXTURE(AESFixture, SuccessfullyDecryptsOpenSSLExample)
{
  unsigned char buf[32];
  memcpy(buf, ciphertext, 32);

  unsigned char iv[16];
  memcpy(iv, credentials + 16, 16);

  aes_context ctx;
  aes_setkey_dec(&ctx, credentials, 128);
  aes_crypt_cbc(&ctx, AES_DECRYPT, 32, iv, buf, buf);

  CHECK_ARRAY_EQUAL(plaintext, buf, 32);
}
Example #21
0
TEST_FIXTURE(AESFixture, EncryptsSameAsOpenSSL)
{
  uint8_t buf[32];
  memcpy(buf, plaintext, 32);

  unsigned char iv[16];
  memcpy(iv, credentials + 16, 16);

  aes_context ctx;
  aes_setkey_enc(&ctx, credentials, 128);
  /* Note length 21, without PKCS #7 padding */
  aes_crypt_cbc(&ctx, AES_ENCRYPT, 21, iv, buf, buf);

  CHECK_ARRAY_EQUAL(ciphertext, buf, 32);
}
Example #22
0
 TEST_FIXTURE(CoAPFixture, DescriptionMatchesOpenSSL)
 {
   uint8_t expected[48] = {
     0xac, 0xc9, 0x8f, 0x76, 0x26, 0x76, 0xc9, 0x53,
     0xf5, 0xc2, 0x2a, 0x20, 0x70, 0xe6, 0x60, 0x28,
     0x5a, 0xa0, 0x95, 0x15, 0x1c, 0x4a, 0xdc, 0x53,
     0x19, 0xbf, 0x95, 0x44, 0x6c, 0xe5, 0x50, 0x59,
     0xf9, 0x26, 0xd4, 0x04, 0x36, 0x9b, 0x97, 0xef,
     0x4d, 0xe7, 0x45, 0xc6, 0xce, 0x13, 0xb3, 0x5d };
   unsigned char buf[48];
   memset(buf, 0, 48);
   init();
   spark_protocol.description(buf, 0x66, 0xf6, 0x49);
   CHECK_ARRAY_EQUAL(expected, buf, 48);
 }
TEST_FIXTURE(EnchantDictionarySuggest_TestFixture,
             EnchantDictionarySuggest_LenSpecified)
{
    size_t cSuggestions;
    _suggestions = enchant_dict_suggest(_dict, "helodisregard me", 4, &cSuggestions);
    CHECK(_suggestions);
    CHECK_EQUAL(std::string("helo"), suggestWord);
    CHECK_EQUAL(4, cSuggestions);

    std::vector<std::string> suggestions;
    if(_suggestions != NULL){
        suggestions.insert(suggestions.begin(), _suggestions, _suggestions+cSuggestions);
    }

    CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo"), suggestions, std::min((size_t)4,cSuggestions));
}
TEST_FIXTURE(EnchantDictionarySuggest_TestFixture,
             EnchantDictionarySuggest_DuplicateSuggestionsFromPersonal_notIncluded)
{
    size_t cSuggestions;

    enchant_dict_add(_dict, "aelo", -1);
    _suggestions = enchant_dict_suggest(_dict, "helo", -1, &cSuggestions);
    CHECK(_suggestions);
    CHECK_EQUAL(4, cSuggestions);

    std::vector<std::string> suggestions;
    if(_suggestions != NULL){
        suggestions.insert(suggestions.begin(), _suggestions, _suggestions+cSuggestions);
    }

    CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo"), suggestions, std::min((size_t)4,cSuggestions));
}
 TEST_FIXTURE(ConstructorFixture, EventLoopRespondsToFunctionCallWithACK)
 {
   uint8_t function_call[34] = {
     0x00, 0x20,
     0xc6, 0xf4, 0xcb, 0x28, 0x85, 0xd8, 0x1e, 0xdd,
     0x23, 0x1c, 0x56, 0xb8, 0xd8, 0x03, 0x28, 0x94,
     0xce, 0x89, 0xab, 0x42, 0x48, 0x86, 0x78, 0x76,
     0xa0, 0x14, 0x07, 0x23, 0xb5, 0xa9, 0xfe, 0x75 };
   memcpy(message_to_receive, function_call, 34);
   const uint8_t expected[18] = {
     0x00, 0x10,
     0x80, 0xef, 0x08, 0x49, 0xd1, 0xbe, 0xe3, 0x20,
     0xf5, 0x06, 0xf9, 0x7d, 0xf1, 0xce, 0x25, 0x36 };
   spark_protocol.handshake();
   bytes_received[0] = bytes_sent[0] = 0;
   spark_protocol.event_loop();
   CHECK_ARRAY_EQUAL(expected, sent_buf_0, 18);
 }
    TEST_FIXTURE(ConstructorFixture, EventLoopCallsOnlyHandlerOnPrefixMatchedEvent)
    {
      const char *event_name = "teapot";
      uint8_t event[34] = {
        0x00, 0x20,
        0x36, 0x0b, 0xed, 0xd1, 0x5a, 0x7b, 0x9b, 0xf0,
        0xe5, 0x44, 0xd2, 0xce, 0x49, 0x6f, 0x6b, 0xc1,
        0x27, 0xa7, 0x88, 0x9a, 0xa8, 0x72, 0xd3, 0x5a,
        0x8f, 0xce, 0x2a, 0x94, 0xbd, 0x49, 0x26, 0xdc };
      memcpy(message_to_receive, event, 34);
      spark_protocol.handshake();
      bytes_received[0] = bytes_sent[0] = 0;
      event_handlers_called_with[0].event_name[0] = 0;

      spark_protocol.add_event_handler(event_name, mock_event_handler_0);
      spark_protocol.event_loop();
      CHECK_ARRAY_EQUAL(event_name, event_handlers_called_with[0].event_name, strlen(event_name));
    }
    TEST_FIXTURE(page_blob_test_base, page_blob_file_upload)
    {
        azure::storage::blob_request_options options;
        options.set_store_blob_content_md5(true);

        utility::string_t md5_header;
        m_context.set_sending_request([&md5_header] (web::http::http_request& request, azure::storage::operation_context)
        {
            if (!request.headers().match(U("x-ms-blob-content-md5"), md5_header))
            {
                md5_header.clear();
            }
        });

        temp_file invalid_file(1000);
        CHECK_THROW(m_blob.upload_from_file(invalid_file.path(), 0, azure::storage::access_condition(), options, m_context), azure::storage::storage_exception);

        temp_file file(1024);
        m_blob.upload_from_file(file.path(), 0, azure::storage::access_condition(), options, m_context);
        CHECK_UTF8_EQUAL(file.content_md5(), md5_header);

        m_context.set_sending_request(std::function<void(web::http::http_request &, azure::storage::operation_context)>());

        temp_file file2(0);
        m_blob.download_to_file(file2.path(), azure::storage::access_condition(), options, m_context);

        concurrency::streams::container_buffer<std::vector<uint8_t>> original_file_buffer;
        auto original_file = concurrency::streams::file_stream<uint8_t>::open_istream(file.path()).get();
        original_file.read_to_end(original_file_buffer).wait();
        original_file.close().wait();

        concurrency::streams::container_buffer<std::vector<uint8_t>> downloaded_file_buffer;
        auto downloaded_file = concurrency::streams::file_stream<uint8_t>::open_istream(file2.path()).get();
        downloaded_file.read_to_end(downloaded_file_buffer).wait();
        downloaded_file.close().wait();

        CHECK_EQUAL(original_file_buffer.collection().size(), downloaded_file_buffer.collection().size());
        CHECK_ARRAY_EQUAL(original_file_buffer.collection(), downloaded_file_buffer.collection(), (int)downloaded_file_buffer.collection().size());

        m_blob.properties().set_content_md5(dummy_md5);
        m_blob.upload_properties();
        options.set_retry_policy(azure::storage::no_retry_policy());
        CHECK_THROW(m_blob.download_to_file(file2.path(), azure::storage::access_condition(), options, m_context), azure::storage::storage_exception);
    }
TEST_FIXTURE(EnchantDictionarySuggest_TestFixture,
             EnchantDictionarySuggest_SuggestionListWithInvalidUtf8_InvalidSuggestionIgnored_FreeCalled)
{
    suggestBehavior = returnFourOneInvalidUtf8;
    size_t cSuggestions;
    _suggestions = enchant_dict_suggest(_dict, "helo", -1, &cSuggestions);
    CHECK(_suggestions);
    CHECK(dictSuggestCalled);
    CHECK(providerFreeStringListCalled);

    CHECK_EQUAL(3, cSuggestions);

    std::vector<std::string> suggestions;
    if(_suggestions != NULL){
        suggestions.insert(suggestions.begin(), _suggestions, _suggestions+cSuggestions);
    }

    CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min((size_t)3,cSuggestions));
}
TEST_FIXTURE(EnchantDictionarySuggest_TestFixture,
             EnchantDictionarySuggest_SuggestionsFromPersonal_addedToEnd)
{
    size_t cSuggestions;
    enchant_dict_add(_dict, "hello", -1);
    _suggestions = enchant_dict_suggest(_dict, "helo", -1, &cSuggestions);
    CHECK(_suggestions);
    CHECK_EQUAL(5, cSuggestions);

    std::vector<std::string> suggestions;
    if(_suggestions != NULL){
        suggestions.insert(suggestions.begin(), _suggestions, _suggestions+cSuggestions);
    }

    std::vector<std::string> expected = GetExpectedSuggestions("helo");
    expected.push_back("hello");

    CHECK_ARRAY_EQUAL(expected, suggestions, std::min((size_t)5,cSuggestions));
}
size_t seek_read_and_compare(concurrency::streams::istream stream, std::vector<uint8_t> buffer_to_compare, utility::size64_t offset, size_t count, size_t expected_read_count)
{
    std::vector<uint8_t> buffer;
    buffer.resize(count);
    stream.seek(offset);
    auto read_count = stream.streambuf().getn(buffer.data(), count).get();
    CHECK_EQUAL(expected_read_count, read_count);
    CHECK_ARRAY_EQUAL(buffer_to_compare.data() + offset, buffer.data(), (int)read_count);
    return read_count;
}