Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: tls: add tests for poll behavior

Make sure we don't generate premature POLLIN events.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jakub Kicinski and committed by
David S. Miller
23fcb62b 121dca78

+131
+131
tools/testing/selftests/net/tls.c
··· 15 15 #include <linux/tcp.h> 16 16 #include <linux/socket.h> 17 17 18 + #include <sys/epoll.h> 18 19 #include <sys/types.h> 19 20 #include <sys/sendfile.h> 20 21 #include <sys/socket.h> ··· 1635 1634 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1); 1636 1635 EXPECT_EQ(errno, EAGAIN); 1637 1636 exit(0); 1637 + } 1638 + } 1639 + 1640 + TEST_F(tls_err, poll_partial_rec) 1641 + { 1642 + struct pollfd pfd = { }; 1643 + ssize_t rec_len; 1644 + char rec[256]; 1645 + char buf[128]; 1646 + 1647 + if (self->notls) 1648 + SKIP(return, "no TLS support"); 1649 + 1650 + pfd.fd = self->cfd2; 1651 + pfd.events = POLLIN; 1652 + EXPECT_EQ(poll(&pfd, 1, 1), 0); 1653 + 1654 + memrnd(buf, sizeof(buf)); 1655 + EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf)); 1656 + rec_len = recv(self->cfd, rec, sizeof(rec), 0); 1657 + EXPECT_GT(rec_len, sizeof(buf)); 1658 + 1659 + /* Write 100B, not the full record ... */ 1660 + EXPECT_EQ(send(self->fd2, rec, 100, 0), 100); 1661 + /* ... no full record should mean no POLLIN */ 1662 + pfd.fd = self->cfd2; 1663 + pfd.events = POLLIN; 1664 + EXPECT_EQ(poll(&pfd, 1, 1), 0); 1665 + /* Now write the rest, and it should all pop out of the other end. */ 1666 + EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100); 1667 + pfd.fd = self->cfd2; 1668 + pfd.events = POLLIN; 1669 + EXPECT_EQ(poll(&pfd, 1, 1), 1); 1670 + EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf)); 1671 + EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0); 1672 + } 1673 + 1674 + TEST_F(tls_err, epoll_partial_rec) 1675 + { 1676 + struct epoll_event ev, events[10]; 1677 + ssize_t rec_len; 1678 + char rec[256]; 1679 + char buf[128]; 1680 + int epollfd; 1681 + 1682 + if (self->notls) 1683 + SKIP(return, "no TLS support"); 1684 + 1685 + epollfd = epoll_create1(0); 1686 + ASSERT_GE(epollfd, 0); 1687 + 1688 + memset(&ev, 0, sizeof(ev)); 1689 + ev.events = EPOLLIN; 1690 + ev.data.fd = self->cfd2; 1691 + ASSERT_GE(epoll_ctl(epollfd, EPOLL_CTL_ADD, self->cfd2, &ev), 0); 1692 + 1693 + EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0); 1694 + 1695 + memrnd(buf, sizeof(buf)); 1696 + EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf)); 1697 + rec_len = recv(self->cfd, rec, sizeof(rec), 0); 1698 + EXPECT_GT(rec_len, sizeof(buf)); 1699 + 1700 + /* Write 100B, not the full record ... */ 1701 + EXPECT_EQ(send(self->fd2, rec, 100, 0), 100); 1702 + /* ... no full record should mean no POLLIN */ 1703 + EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0); 1704 + /* Now write the rest, and it should all pop out of the other end. */ 1705 + EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100); 1706 + EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 1); 1707 + EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf)); 1708 + EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0); 1709 + 1710 + close(epollfd); 1711 + } 1712 + 1713 + TEST_F(tls_err, poll_partial_rec_async) 1714 + { 1715 + struct pollfd pfd = { }; 1716 + ssize_t rec_len; 1717 + char rec[256]; 1718 + char buf[128]; 1719 + char token; 1720 + int p[2]; 1721 + int ret; 1722 + 1723 + if (self->notls) 1724 + SKIP(return, "no TLS support"); 1725 + 1726 + ASSERT_GE(pipe(p), 0); 1727 + 1728 + memrnd(buf, sizeof(buf)); 1729 + EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf)); 1730 + rec_len = recv(self->cfd, rec, sizeof(rec), 0); 1731 + EXPECT_GT(rec_len, sizeof(buf)); 1732 + 1733 + ret = fork(); 1734 + ASSERT_GE(ret, 0); 1735 + 1736 + if (ret) { 1737 + int status, pid2; 1738 + 1739 + close(p[1]); 1740 + usleep(1000); /* Give child a head start */ 1741 + 1742 + EXPECT_EQ(send(self->fd2, rec, 100, 0), 100); 1743 + 1744 + EXPECT_EQ(read(p[0], &token, 1), 1); /* Barrier #1 */ 1745 + 1746 + EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), 1747 + rec_len - 100); 1748 + 1749 + pid2 = wait(&status); 1750 + EXPECT_EQ(pid2, ret); 1751 + EXPECT_EQ(status, 0); 1752 + } else { 1753 + close(p[0]); 1754 + 1755 + /* Child should sleep in poll(), never get a wake */ 1756 + pfd.fd = self->cfd2; 1757 + pfd.events = POLLIN; 1758 + EXPECT_EQ(poll(&pfd, 1, 5), 0); 1759 + 1760 + EXPECT_EQ(write(p[1], &token, 1), 1); /* Barrier #1 */ 1761 + 1762 + pfd.fd = self->cfd2; 1763 + pfd.events = POLLIN; 1764 + EXPECT_EQ(poll(&pfd, 1, 5), 1); 1765 + 1766 + exit(!_metadata->passed); 1638 1767 } 1639 1768 } 1640 1769