2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-06-11 11:01:35 -04:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
2018-07-06 03:19:19 -04:00
|
|
|
#include "deno.h"
|
2018-06-11 11:01:35 -04:00
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, InitializesCorrectly) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 14:49:57 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "1 + 2"));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, CanCallFunction) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 15:30:58 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js",
|
|
|
|
"if (CanCallFunction() != 'foo') throw Error();"));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, ErrorsCorrectly) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 14:49:57 -04:00
|
|
|
EXPECT_FALSE(deno_execute(d, "a.js", "throw Error()"));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:35:34 -04:00
|
|
|
deno_buf strbuf(const char* str) {
|
|
|
|
auto len = strlen(str);
|
|
|
|
|
|
|
|
deno_buf buf;
|
|
|
|
buf.alloc_ptr = reinterpret_cast<uint8_t*>(strdup(str));
|
|
|
|
buf.alloc_len = len + 1;
|
|
|
|
buf.data_ptr = buf.alloc_ptr;
|
|
|
|
buf.data_len = len;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2018-06-11 12:17:28 -04:00
|
|
|
|
2018-07-23 14:11:41 -04:00
|
|
|
// Same as strbuf but with null alloc_ptr.
|
|
|
|
deno_buf StrBufNullAllocPtr(const char* str) {
|
|
|
|
auto len = strlen(str);
|
|
|
|
deno_buf buf;
|
|
|
|
buf.alloc_ptr = nullptr;
|
|
|
|
buf.alloc_len = 0;
|
|
|
|
buf.data_ptr = reinterpret_cast<uint8_t*>(strdup(str));
|
|
|
|
buf.data_len = len;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SendSuccess) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 12:07:12 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()"));
|
2018-07-06 16:26:34 -04:00
|
|
|
EXPECT_TRUE(deno_send(d, strbuf("abc")));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SendWrongByteLength) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-23 14:10:48 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SendWrongByteLength()"));
|
|
|
|
// deno_send the wrong sized message, it should throw.
|
2018-07-06 16:26:34 -04:00
|
|
|
EXPECT_FALSE(deno_send(d, strbuf("abcd")));
|
2018-07-23 14:10:48 -04:00
|
|
|
std::string exception = deno_last_exception(d);
|
2018-07-26 11:03:45 -04:00
|
|
|
EXPECT_GT(exception.length(), 1u);
|
2018-07-23 14:10:48 -04:00
|
|
|
EXPECT_NE(exception.find("assert"), std::string::npos);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SendNoCallback) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 12:07:12 -04:00
|
|
|
// We didn't call deno.recv() in JS, should fail.
|
2018-07-06 16:26:34 -04:00
|
|
|
EXPECT_FALSE(deno_send(d, strbuf("abc")));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, RecvReturnEmpty) {
|
2018-06-11 14:18:56 -04:00
|
|
|
static int count = 0;
|
2018-07-06 16:26:34 -04:00
|
|
|
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
|
2018-06-11 14:18:56 -04:00
|
|
|
count++;
|
2018-07-08 21:35:34 -04:00
|
|
|
EXPECT_EQ(static_cast<size_t>(3), buf.data_len);
|
|
|
|
EXPECT_EQ(buf.data_ptr[0], 'a');
|
|
|
|
EXPECT_EQ(buf.data_ptr[1], 'b');
|
|
|
|
EXPECT_EQ(buf.data_ptr[2], 'c');
|
2018-06-11 14:18:56 -04:00
|
|
|
});
|
2018-07-01 12:07:12 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnEmpty()"));
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(count, 2);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 14:18:56 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, RecvReturnBar) {
|
2018-06-11 14:18:56 -04:00
|
|
|
static int count = 0;
|
2018-07-06 16:26:34 -04:00
|
|
|
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
|
2018-06-11 14:18:56 -04:00
|
|
|
count++;
|
2018-07-08 21:35:34 -04:00
|
|
|
EXPECT_EQ(static_cast<size_t>(3), buf.data_len);
|
|
|
|
EXPECT_EQ(buf.data_ptr[0], 'a');
|
|
|
|
EXPECT_EQ(buf.data_ptr[1], 'b');
|
|
|
|
EXPECT_EQ(buf.data_ptr[2], 'c');
|
2018-06-13 13:38:22 -04:00
|
|
|
deno_set_response(deno, strbuf("bar"));
|
2018-06-11 14:18:56 -04:00
|
|
|
});
|
2018-07-01 12:07:12 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnBar()"));
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(count, 1);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 14:18:56 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, DoubleRecvFails) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 12:07:12 -04:00
|
|
|
EXPECT_FALSE(deno_execute(d, "a.js", "DoubleRecvFails()"));
|
2018-06-11 16:51:11 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SendRecvSlice) {
|
2018-07-08 21:35:34 -04:00
|
|
|
static int count = 0;
|
|
|
|
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
|
|
|
|
static const size_t alloc_len = 1024;
|
|
|
|
size_t i = count++;
|
|
|
|
// Check the size and offset of the slice.
|
|
|
|
size_t data_offset = buf.data_ptr - buf.alloc_ptr;
|
|
|
|
EXPECT_EQ(data_offset, i * 11);
|
|
|
|
EXPECT_EQ(buf.data_len, alloc_len - i * 30);
|
|
|
|
EXPECT_EQ(buf.alloc_len, alloc_len);
|
|
|
|
// Check values written by the JS side.
|
|
|
|
EXPECT_EQ(buf.data_ptr[0], 100 + i);
|
|
|
|
EXPECT_EQ(buf.data_ptr[buf.data_len - 1], 100 - i);
|
|
|
|
// Make copy of the backing buffer -- this is currently necessary because
|
|
|
|
// deno_set_response() takes ownership over the buffer, but we are not given
|
|
|
|
// ownership of `buf` by our caller.
|
|
|
|
uint8_t* alloc_ptr = reinterpret_cast<uint8_t*>(malloc(alloc_len));
|
|
|
|
memcpy(alloc_ptr, buf.alloc_ptr, alloc_len);
|
|
|
|
// Make a slice that is a bit shorter than the original.
|
|
|
|
deno_buf buf2{alloc_ptr, alloc_len, alloc_ptr + data_offset,
|
|
|
|
buf.data_len - 19};
|
|
|
|
// Place some values into the buffer for the JS side to verify.
|
|
|
|
buf2.data_ptr[0] = 200 + i;
|
|
|
|
buf2.data_ptr[buf2.data_len - 1] = 200 - i;
|
|
|
|
// Send back.
|
|
|
|
deno_set_response(deno, buf2);
|
|
|
|
});
|
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SendRecvSlice()"));
|
|
|
|
EXPECT_EQ(count, 5);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, JSSendArrayBufferViewTypes) {
|
2018-07-08 21:35:34 -04:00
|
|
|
static int count = 0;
|
|
|
|
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
|
|
|
|
count++;
|
|
|
|
size_t data_offset = buf.data_ptr - buf.alloc_ptr;
|
2018-07-16 18:04:54 -04:00
|
|
|
EXPECT_EQ(data_offset, 2468u);
|
|
|
|
EXPECT_EQ(buf.data_len, 1000u);
|
|
|
|
EXPECT_EQ(buf.alloc_len, 4321u);
|
2018-07-08 21:35:34 -04:00
|
|
|
EXPECT_EQ(buf.data_ptr[0], count);
|
|
|
|
});
|
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "JSSendArrayBufferViewTypes()"));
|
|
|
|
EXPECT_EQ(count, 3);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, JSSendNeutersBuffer) {
|
2018-07-08 21:35:34 -04:00
|
|
|
static int count = 0;
|
|
|
|
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
|
|
|
|
count++;
|
2018-07-16 18:04:54 -04:00
|
|
|
EXPECT_EQ(buf.data_len, 1u);
|
2018-07-08 21:35:34 -04:00
|
|
|
EXPECT_EQ(buf.data_ptr[0], 42);
|
|
|
|
});
|
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "JSSendNeutersBuffer()"));
|
|
|
|
EXPECT_EQ(count, 1);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, TypedArraySnapshots) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-12 00:36:01 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "TypedArraySnapshots()"));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SnapshotBug) {
|
2018-06-18 09:55:36 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SnapshotBug()"));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:57:16 -04:00
|
|
|
TEST(LibDenoTest, GlobalErrorHandling) {
|
2018-06-22 08:57:49 -04:00
|
|
|
static int count = 0;
|
2018-08-26 03:57:16 -04:00
|
|
|
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
|
2018-06-22 08:57:49 -04:00
|
|
|
count++;
|
2018-07-08 21:35:34 -04:00
|
|
|
EXPECT_EQ(static_cast<size_t>(1), buf.data_len);
|
|
|
|
EXPECT_EQ(buf.data_ptr[0], 42);
|
2018-06-22 08:57:49 -04:00
|
|
|
});
|
2018-08-26 03:57:16 -04:00
|
|
|
EXPECT_FALSE(deno_execute(d, "a.js", "GlobalErrorHandling()"));
|
2018-06-22 08:57:49 -04:00
|
|
|
EXPECT_EQ(count, 1);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
2018-07-23 14:11:41 -04:00
|
|
|
|
2018-08-26 03:57:16 -04:00
|
|
|
TEST(LibDenoTest, DoubleGlobalErrorHandlingFails) {
|
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
|
|
|
EXPECT_FALSE(deno_execute(d, "a.js", "DoubleGlobalErrorHandlingFails()"));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SendNullAllocPtr) {
|
2018-07-23 14:11:41 -04:00
|
|
|
static int count = 0;
|
|
|
|
Deno* d = deno_new(nullptr, [](auto _, auto buf) { count++; });
|
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SendNullAllocPtr()"));
|
|
|
|
deno_buf buf = StrBufNullAllocPtr("abcd");
|
|
|
|
EXPECT_EQ(buf.alloc_ptr, nullptr);
|
|
|
|
EXPECT_EQ(buf.data_len, 4u);
|
|
|
|
EXPECT_TRUE(deno_send(d, buf));
|
|
|
|
EXPECT_EQ(count, 0);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|