1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/interest_group/trusted_signals_fetcher.h"
#include <stdint.h>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/containers/flat_set.h"
#include "base/containers/span.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "base/unguessable_token.h"
#include "base/values.h"
#include "components/cbor/writer.h"
#include "content/browser/interest_group/bidding_and_auction_server_key_fetcher.h"
#include "content/browser/interest_group/data_decoder_manager.h"
#include "content/public/browser/frame_tree_node_id.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test_utils.h"
#include "content/services/auction_worklet/public/cpp/auction_downloader.h"
#include "content/services/auction_worklet/public/cpp/cbor_test_util.h"
#include "content/services/auction_worklet/public/mojom/trusted_signals_cache.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/isolation_info.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/site_for_cookies.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/third_party/quiche/src/quiche/oblivious_http/common/oblivious_http_header_key_config.h"
#include "net/third_party/quiche/src/quiche/oblivious_http/oblivious_http_gateway.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/network/public/cpp/cross_origin_embedder_policy.h"
#include "services/network/public/cpp/document_isolation_policy.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_switches.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "services/network/public/mojom/cross_origin_embedder_policy.mojom.h"
#include "services/network/public/mojom/document_isolation_policy.mojom.h"
#include "services/network/public/mojom/ip_address_space.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/boringssl/src/include/openssl/hpke.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace {
// These keys were randomly generated as follows:
// EVP_HPKE_KEY keys;
// EVP_HPKE_KEY_generate(&keys, EVP_hpke_x25519_hkdf_sha256());
// and then EVP_HPKE_KEY_public_key and EVP_HPKE_KEY_private_key were used to
// extract the keys.
const uint8_t kTestPrivateKey[] = {
0xff, 0x1f, 0x47, 0xb1, 0x68, 0xb6, 0xb9, 0xea, 0x65, 0xf7, 0x97,
0x4f, 0xf2, 0x2e, 0xf2, 0x36, 0x94, 0xe2, 0xf6, 0xb6, 0x8d, 0x66,
0xf3, 0xa7, 0x64, 0x14, 0x28, 0xd4, 0x45, 0x35, 0x01, 0x8f,
};
const uint8_t kTestPublicKey[] = {
0xa1, 0x5f, 0x40, 0x65, 0x86, 0xfa, 0xc4, 0x7b, 0x99, 0x59, 0x70,
0xf1, 0x85, 0xd9, 0xd8, 0x91, 0xc7, 0x4d, 0xcf, 0x1e, 0xb9, 0x1a,
0x7d, 0x50, 0xa5, 0x8b, 0x01, 0x68, 0x3e, 0x60, 0x05, 0x2d,
};
const uint8_t kKeyId = 3;
const char kKeyIdStr[] = "03";
// Helper to create a CompressionGroupResult given all field values.
// `compression_group_data` is a string that will be CBOR encoded to form the
// expected compression group body.
TrustedSignalsFetcher::CompressionGroupResult CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme compression_scheme,
std::string_view compression_group_data,
base::TimeDelta ttl) {
TrustedSignalsFetcher::CompressionGroupResult out;
out.compression_scheme = compression_scheme;
std::optional<std::vector<uint8_t>> content_string =
cbor::Writer::Write(cbor::Value(compression_group_data));
CHECK(content_string);
out.compression_group_data = std::move(content_string).value();
out.ttl = ttl;
return out;
}
// Shared test fixture for bidding and scoring signals. Note that scoring
// signals tests focus on request body generation, with little coverage of
// response parsing, since that path is identical for bidding and scoring
// signals.
class TrustedSignalsFetcherTest : public testing::Test {
public:
// This is the expected request body that corresponds to the request returned
// by CreateBasicBiddingSignalsRequest(). Stored as a raw hex string to
// provide better coverage of padding logic than using
// CreateKVv2RequestBody(), which uses the same padding code as the fetcher.
// It is the deterministic CBOR representation of the following, with a prefix
// and padding added:
// {
// "acceptCompression": [ "none", "gzip" ],
// "metadata": { "hostname": "host.test" },
// "partitions": [
// {
// "compressionGroupId": 0,
// "id": 0,
// "arguments": [
// {
// "tags": [ "interestGroupNames" ],
// "data": [ "group1" ]
// },
// {
// "tags": [ "keys" ],
// "data": [ "key1" ]
// }
// ]
// }
// ]
// }
const std::string_view kBasicBiddingSignalsRequestBody =
"00000000A9A3686D65746164617461A168686F73746E616D6569686F73742E746573746A"
"706172746974696F6E7381A36269640069617267756D656E747382A26464617461816667"
"726F75703164746167738172696E74657265737447726F75704E616D6573A26464617461"
"81646B657931647461677381646B65797372636F6D7072657373696F6E47726F75704964"
"0071616363657074436F6D7072657373696F6E82646E6F6E6564677A6970000000000000"
"000000000000000000000000000000000000000000";
// This is the expected request body that corresponds to the request returned
// by CreateBasicScoringSignalsRequest(). Stored as a raw hex string to
// provide better coverage of padding logic than using
// CreateKVv2RequestBody(), which uses the same padding code as the fetcher.
// It is the deterministic CBOR representation of the following, with a prefix
// and padding added:
// {
// "acceptCompression": [ "none", "gzip" ],
// "metadata": { "hostname": "host.test" },
// "partitions": [
// {
// "compressionGroupId": 0,
// "id": 0,
// "arguments": [
// {
// "tags": [ "renderURLs" ],
// "data": [ "https://render_url.test/foo" ]
// }
// ]
// }
// ]
// }
const std::string_view kBasicScoringSignalsRequestBody =
"00000000A0A3686D65746164617461A168686F73746E616D6569686F73742E746573746A"
"706172746974696F6E7381A36269640069617267756D656E747381A2646461746181781B"
"68747470733A2F2F72656E6465725F75726C2E746573742F666F6F6474616773816A7265"
"6E64657255524C7372636F6D7072657373696F6E47726F75704964007161636365707443"
"6F6D7072657373696F6E82646E6F6E6564677A6970000000000000000000000000000000"
"000000000000000000000000000000000000000000";
TrustedSignalsFetcherTest() {
base::FieldTrialParams lna_checks_params;
lna_checks_params["LocalNetworkAccessChecksWarn"] = "false";
feature_list_.InitWithFeaturesAndParameters(
/*enabled_features=*/
{{network::features::kLocalNetworkAccessChecks, lna_checks_params},
// Enable `kProtectedAudienceCorsSafelistKVv2Signals` by default, so
// behavior matches the eventual expected behavior.
{network::features::kProtectedAudienceCorsSafelistKVv2Signals, {}}},
/*disabled_features=*/{});
embedded_test_server_.SetSSLConfig(
net::EmbeddedTestServer::CERT_TEST_NAMES);
embedded_test_server_.AddDefaultHandlers();
embedded_test_server_.RegisterRequestHandler(
base::BindRepeating(&TrustedSignalsFetcherTest::HandleSignalsRequest,
base::Unretained(this)));
EXPECT_TRUE(embedded_test_server_.Start());
SetResponseBodyAndAddHeader(DefaultResponseBody());
base::AutoLock auto_lock(lock_);
script_origin_ = embedded_test_server_.GetOrigin(kTrustedSignalsHost);
}
~TrustedSignalsFetcherTest() override {
base::AutoLock auto_lock(lock_);
// Any request body should have been verified.
EXPECT_FALSE(request_path_.has_value());
EXPECT_FALSE(request_body_.has_value());
}
// CBOR representation of a response with a single compression group. Same for
// both bidding and scoring signals.
static std::string DefaultResponseBody() {
return auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"ttlMs" : 100,
"content" : "compression group content"
}
]
})");
}
// Sets `script_origin_` to be cross origin to be cross-origin to the trusted
// signals URL. Additional, sets whether a CORS preflight request is expected
// to be observed, which should depend on whether the
// `kProtectedAudienceCorsSafelistKVv2Signals` Feature is enabled.
void SetCrossOrigin(bool cors_preflight_expected = false) {
base::AutoLock auto_lock(lock_);
// No requests are made to this origin, so doesn't need to come from the
// EmbeddedTestServer.
script_origin_ = url::Origin::Create(GURL("https://other-origin.test/"));
script_origin_is_same_origin_ = false;
cors_preflight_expected_ = cors_preflight_expected;
}
url::Origin GetScriptOrigin() {
base::AutoLock auto_lock(lock_);
return script_origin_;
}
GURL TrustedBiddingSignalsUrl() const {
return embedded_test_server_.GetURL(kTrustedSignalsHost,
kTrustedBiddingSignalsPath);
}
GURL TrustedScoringSignalsUrl() const {
return embedded_test_server_.GetURL(kTrustedSignalsHost,
kTrustedScoringSignalsPath);
}
// Creates a simple request with one compression group with a single
// partition with only one key, and no other optional parameters.
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
CreateBasicBiddingSignalsRequest() {
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions;
bidding_partitions.emplace_back(
/*partition_id=*/0, &kDefaultInterestGroupNames, &kDefaultKeys,
&kDefaultAdditionalParams, /*buyer_tkv_signals=*/nullptr);
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
bidding_signals_request;
bidding_signals_request.emplace(0, std::move(bidding_partitions));
return bidding_signals_request;
}
// Creates a simple request with one compression group with a single
// partition with only a render URL.
std::map<int, std::vector<TrustedSignalsFetcher::ScoringPartition>>
CreateBasicScoringSignalsRequest() {
std::vector<TrustedSignalsFetcher::ScoringPartition> scoring_partitions;
scoring_partitions.emplace_back(
/*partition_id=*/0, &kDefaultRenderUrl, &kDefaultAdComponentRenderUrls,
&kDefaultAdditionalParams, /*seller_tkv_signals=*/nullptr);
std::map<int, std::vector<TrustedSignalsFetcher::ScoringPartition>>
scoring_signals_request;
scoring_signals_request.emplace(0, std::move(scoring_partitions));
return scoring_signals_request;
}
TrustedSignalsFetcher::SignalsFetchResult
RequestBiddingSignalsAndWaitForResult(
const std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>&
compression_groups,
std::optional<GURL> signals_url = std::nullopt) {
GURL url = signals_url.value_or(TrustedBiddingSignalsUrl());
base::RunLoop run_loop;
TrustedSignalsFetcher::SignalsFetchResult out;
TrustedSignalsFetcher trusted_signals_fetcher;
trusted_signals_fetcher.FetchBiddingSignals(
data_decoder_manager_, url_loader_factory_.get(), FrameTreeNodeId(),
kAuctionDevtoolsIds, kDefaultMainFrameOrigin, ip_address_space_,
network_partition_nonce_, GetScriptOrigin(), url,
BiddingAndAuctionServerKey{
std::string(reinterpret_cast<const char*>(kTestPublicKey),
sizeof(kTestPublicKey)),
kKeyIdStr},
compression_groups,
base::BindLambdaForTesting(
[&](TrustedSignalsFetcher::SignalsFetchResult result) {
out = std::move(result);
run_loop.Quit();
}));
// Check that the correct DataDecoder is constructed on fetch start, to
// prewarm the data decoder process.
EXPECT_EQ(data_decoder_manager_.GetHandleCountForTesting(
kDefaultMainFrameOrigin, GetScriptOrigin()),
1u);
run_loop.Run();
base::AutoLock auto_lock(lock_);
if (expect_url_not_requested_) {
EXPECT_FALSE(request_path_);
} else {
EXPECT_EQ(request_path_, url.PathForRequestPiece());
}
request_path_.reset();
return out;
}
TrustedSignalsFetcher::SignalsFetchResult
RequestScoringSignalsAndWaitForResult(
const std::map<int, std::vector<TrustedSignalsFetcher::ScoringPartition>>&
compression_groups,
std::optional<GURL> signals_url = std::nullopt) {
GURL url = signals_url.value_or(TrustedScoringSignalsUrl());
base::RunLoop run_loop;
TrustedSignalsFetcher::SignalsFetchResult out;
TrustedSignalsFetcher trusted_signals_fetcher;
trusted_signals_fetcher.FetchScoringSignals(
data_decoder_manager_, url_loader_factory_.get(), FrameTreeNodeId(),
kAuctionDevtoolsIds, kDefaultMainFrameOrigin, ip_address_space_,
network_partition_nonce_, GetScriptOrigin(), url,
BiddingAndAuctionServerKey{
std::string(reinterpret_cast<const char*>(kTestPublicKey),
sizeof(kTestPublicKey)),
kKeyIdStr},
compression_groups,
base::BindLambdaForTesting(
[&](TrustedSignalsFetcher::SignalsFetchResult result) {
out = std::move(result);
run_loop.Quit();
}));
// Check that the correct DataDecoder is constructed on fetch start, to
// prewarm the data decoder process.
EXPECT_EQ(data_decoder_manager_.GetHandleCountForTesting(
kDefaultMainFrameOrigin, GetScriptOrigin()),
1u);
run_loop.Run();
base::AutoLock auto_lock(lock_);
if (expect_url_not_requested_) {
EXPECT_FALSE(request_path_);
} else {
EXPECT_EQ(request_path_, url.PathForRequestPiece());
}
request_path_.reset();
return out;
}
std::string GetRequestBody() {
base::AutoLock auto_lock(lock_);
CHECK(request_body_.has_value());
std::string out = std::move(request_body_).value();
request_body_.reset();
return out;
}
size_t GetEncryptedRequestBodyLength() {
base::AutoLock auto_lock(lock_);
return encrypted_request_body_length_;
}
// Checks that the request body matches the provided string, which contains a
// hex-encoded representation of the expected result.
void ValidateRequestBodyHex(std::string_view expected_request_hex) {
std::string actual_response = GetRequestBody();
EXPECT_EQ(base::HexEncode(actual_response), expected_request_hex);
// If there's a mismatch, compare the non-hex-encoded string as well. This
// may give a better idea what's wrong when looking at test output.
if (HasNonfatalFailure()) {
std::string expected_response;
EXPECT_TRUE(
base::HexStringToString(expected_request_hex, &expected_response));
EXPECT_EQ(actual_response, expected_response);
}
}
// Checks that the request body matches the provided JSON. Converts the JSON
// input to a cbor string, adds a framing header and padding, and then check
// that matches the request body.
void ValidateRequestBodyJson(std::string_view expected_request_json) {
auto expected_request = auction_worklet::test::CreateKVv2RequestBody(
auction_worklet::test::ToCborString(expected_request_json));
ValidateRequestBodyHex(base::HexEncode(expected_request));
}
// Sets the response body string.
void SetResponseBody(std::string response_body, bool use_cleartext = false) {
base::AutoLock auto_lock(lock_);
response_body_ = std::move(response_body);
use_cleartext_response_body_ = use_cleartext;
}
// Convenience wrapper that calls CreateKVv2ResponseBody() on the provided
// values, and sets the resulting string as the response body.
void SetResponseBodyAndAddHeader(
std::string_view cbor_response_body,
std::optional<size_t> advertised_cbor_length = std::nullopt,
size_t padding_length = 0,
uint8_t compression_scheme = 0) {
SetResponseBody(auction_worklet::test::CreateKVv2ResponseBody(
cbor_response_body, advertised_cbor_length, padding_length,
compression_scheme));
}
// Helper to, in the case of a successfully fetched result, compare `result`
// to `expected_result`. Has an assertion failure if result indicates a
// failure.
void ValidateFetchResult(
const TrustedSignalsFetcher::SignalsFetchResult& result,
const TrustedSignalsFetcher::CompressionGroupResultMap& expected_result) {
ASSERT_TRUE(result.has_value());
ASSERT_EQ(result->size(), expected_result.size());
for (auto result_it = result->begin(),
expected_result_it = expected_result.begin();
result_it != result->end(); ++result_it, ++expected_result_it) {
// This is the compression group index of the expected result.
SCOPED_TRACE(expected_result_it->first);
EXPECT_EQ(result_it->first, expected_result_it->first);
EXPECT_EQ(result_it->second.compression_scheme,
expected_result_it->second.compression_scheme);
EXPECT_EQ(result_it->second.compression_group_data,
expected_result_it->second.compression_group_data);
EXPECT_EQ(result_it->second.ttl, expected_result_it->second.ttl);
}
}
// Checks that the fetch result matches what `DefaultResponseBody()` is
// expected to be parsed as.
void ValidateDefaultFetchResult(
const TrustedSignalsFetcher::SignalsFetchResult& result) {
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"compression group content", base::Milliseconds(100)));
ValidateFetchResult(result, expected_result);
}
// Sets response headers (other than Content-Type) for responses.
void SetResponseHeaders(
const std::vector<std::pair<std::string, std::string>>&
response_headers) {
base::AutoLock auto_lock(lock_);
response_headers_ = response_headers;
}
protected:
std::unique_ptr<net::test_server::HttpResponse> HandleSignalsRequest(
const net::test_server::HttpRequest& request) {
base::AutoLock auto_lock(lock_);
EXPECT_FALSE(request_path_);
// Don't record path for preflights - it should be recorded for the final
// request instead.
if (request.method_string != net::HttpRequestHeaders::kOptionsMethod) {
request_path_ = request.relative_url;
}
if (request.relative_url == kTrustedBiddingSignalsPath ||
request.relative_url == kTrustedScoringSignalsPath) {
EXPECT_EQ(
cors_preflight_expected_,
request.method_string == net::HttpRequestHeaders::kOptionsMethod);
EXPECT_FALSE(request_body_.has_value());
EXPECT_EQ(request.headers.find("Cookie"), request.headers.end());
EXPECT_THAT(request.headers,
testing::Contains(std::pair("Sec-Fetch-Mode", "cors")));
EXPECT_THAT(request.headers, testing::Contains(std::pair(
"Origin", script_origin_.Serialize())));
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
if (script_origin_is_same_origin_) {
EXPECT_THAT(request.headers, testing::Contains(std::pair(
"Sec-Fetch-Site", "same-origin")));
} else {
EXPECT_THAT(request.headers, testing::Contains(std::pair(
"Sec-Fetch-Site", "cross-site")));
// This needs to be sent both for the preflight and the actual request
// in the cross-origin case.
response->AddCustomHeader("Access-Control-Allow-Origin",
script_origin_.Serialize());
// If haven't see the options request yet, expect to see it before the
// actual request.
if (cors_preflight_expected_) {
if (request.method_string !=
net::HttpRequestHeaders::kOptionsMethod) {
ADD_FAILURE() << "Options method expected but got "
<< request.method_string;
return nullptr;
}
cors_preflight_expected_ = false;
EXPECT_THAT(request.headers,
testing::Contains(std::pair(
"Access-Control-Request-Headers", "content-type")));
response->AddCustomHeader("Access-Control-Allow-Headers",
"Content-Type");
EXPECT_FALSE(request.has_content);
response->set_code(net::HttpStatusCode::HTTP_NO_CONTENT);
return response;
}
}
EXPECT_THAT(
request.headers,
testing::Contains(std::pair(
"Content-Type", TrustedSignalsFetcher::kRequestMediaType)));
EXPECT_THAT(request.headers,
testing::Contains(std::pair(
"Accept", TrustedSignalsFetcher::kResponseMediaType)));
EXPECT_TRUE(request.has_content);
EXPECT_EQ(request.method_string, net::HttpRequestHeaders::kPostMethod);
auto config = quiche::ObliviousHttpHeaderKeyConfig::Create(
kKeyId, EVP_HPKE_DHKEM_X25519_HKDF_SHA256, EVP_HPKE_HKDF_SHA256,
EVP_HPKE_AES_256_GCM);
EXPECT_TRUE(config.ok()) << config.status();
auto ohttp_gateway =
quiche::ObliviousHttpGateway::Create(
std::string(reinterpret_cast<const char*>(&kTestPrivateKey[0]),
sizeof(kTestPrivateKey)),
config.value())
.value();
encrypted_request_body_length_ = request.content.size();
auto plaintext_ohttp_request_body =
ohttp_gateway.DecryptObliviousHttpRequest(
request.content, TrustedSignalsFetcher::kRequestMediaType);
EXPECT_TRUE(plaintext_ohttp_request_body.ok())
<< plaintext_ohttp_request_body.status();
request_body_ = plaintext_ohttp_request_body->GetPlaintextData();
std::string response_body;
// Encryption doesn't support empty strings.
if (response_body_.size() > 0u && !use_cleartext_response_body_) {
auto context =
std::move(plaintext_ohttp_request_body).value().ReleaseContext();
auto ciphertext_ohttp_response_body =
ohttp_gateway.CreateObliviousHttpResponse(
response_body_, context,
TrustedSignalsFetcher::kResponseMediaType);
EXPECT_TRUE(ciphertext_ohttp_response_body.ok())
<< ciphertext_ohttp_response_body.status();
response_body =
ciphertext_ohttp_response_body->EncapsulateAndSerialize();
} else {
response_body = response_body_;
}
response->set_content_type(response_mime_type_);
response->set_code(response_status_code_);
response->set_content(response_body);
for (const auto& pair : response_headers_) {
response->AddCustomHeader(pair.first, pair.second);
}
return response;
}
return nullptr;
}
base::test::ScopedFeatureList feature_list_;
// Need to use an IO thread for the TestSharedURLLoaderFactory, which lives on
// the thread it's created on, to make network requests.
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::MainThreadType::IO};
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
// Using different paths for bidding and scoring signals is not necessary, but
// does provide a little extra test coverage that the right URLs are requested
// from the server.
const std::string kTrustedBiddingSignalsPath = "/bidder-signals";
const std::string kTrustedScoringSignalsPath = "/scoring-signals";
const std::string kTrustedSignalsHost = "a.test";
// This value doesn't actually matter, as it's not tested by this file.
const base::flat_set<std::string> kAuctionDevtoolsIds{"auction_devtools_id"};
// Default values used by both both CreateBasicBiddingSignalsRequest() and
// CreateBasicScoringSignalsRequest(). They need to be fields of the test
// fixture to keep them alive, since the returned BiddingPartition holds onto
// non-owning raw pointers.
const url::Origin kDefaultMainFrameOrigin =
url::Origin::Create(GURL("https://host.test"));
const base::Value::Dict kDefaultAdditionalParams;
// Default values used by CreateBasicBiddingSignalsRequest().
const std::set<std::string> kDefaultInterestGroupNames{"group1"};
const std::set<std::string> kDefaultKeys{"key1"};
// Default values used by CreateBasicScoringSignalsRequest().
const GURL kDefaultRenderUrl{"https://render_url.test/foo"};
const std::set<GURL> kDefaultAdComponentRenderUrls;
DataDecoderManager data_decoder_manager_;
// Values returned for requests to the test server for
// `kTrustedBiddingSignalsPath`.
std::string response_mime_type_{TrustedSignalsFetcher::kResponseMediaType};
net::HttpStatusCode response_status_code_{net::HTTP_OK};
base::UnguessableToken network_partition_nonce_ =
base::UnguessableToken::Create();
base::Lock lock_;
// The origin of the interest group owner or seller, and whether it's
// same-origin to the signals URL. Populated when starting test server.
url::Origin script_origin_ GUARDED_BY(lock_);
bool script_origin_is_same_origin_ GUARDED_BY(lock_) = true;
// IP address space of the origin
network::mojom::IPAddressSpace ip_address_space_ =
network::mojom::IPAddressSpace::kLocal;
// Whether an OPTIONS request is expected. When true, set to false once an
// options request is observed.
bool cors_preflight_expected_ GUARDED_BY(lock_) = false;
// If false, don't expect a request for signals to be handled.
bool expect_url_not_requested_ = false;
// Path of the last observed request. Don't record URL, because the embedded
// test server doesn't report the full requested URL.
std::optional<std::string> request_path_ GUARDED_BY(lock_);
// Size of the original encrypted request body.
size_t encrypted_request_body_length_ GUARDED_BY(lock_);
// The most recent request body received by the embedded test server,
// after decryption.
std::optional<std::string> request_body_ GUARDED_BY(lock_);
// The response body to reply with.
std::string response_body_ GUARDED_BY(lock_);
// If true, the response body is not encrypted, which should result in an
// error.
bool use_cleartext_response_body_ GUARDED_BY(lock_) = false;
// Header values to include in the response. Default value is needed to allow
// response to be used at all.
std::vector<std::pair<std::string, std::string>> response_headers_
GUARDED_BY(lock_){{"Ad-Auction-Allowed", "true"}};
net::test_server::EmbeddedTestServer embedded_test_server_{
net::test_server::EmbeddedTestServer::TYPE_HTTPS};
// URLLoaderFactory that makes real network requests.
scoped_refptr<network::TestSharedURLLoaderFactory> url_loader_factory_{
base::MakeRefCounted<network::TestSharedURLLoaderFactory>(
/*network_service=*/nullptr,
/*is_trusted=*/true)};
};
TEST_F(TrustedSignalsFetcherTest, BiddingSignals404) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
response_status_code_ = net::HTTP_NOT_FOUND;
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf("Failed to load %s HTTP status = 404 Not Found.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
// Test various permutations of the "Ad-Auction-Allowed" and "X-Allow-FLEDGE"
// header being present and absent.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsAdAuctionAllowed) {
const struct {
std::vector<std::pair<std::string, std::string>> headers;
bool expect_success;
} kTestCases[] = {
{{{"Ad-Auction-Allowed", "true"}}, true},
{{{"X-Allow-FLEDGE", "true"}}, true},
{{}, false},
{{{"Ad-Auction-Allowed", "false"}}, false},
{{{"X-Allow-FLEDGE", "false"}}, false},
};
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
for (const auto& test_case : kTestCases) {
SetResponseHeaders(test_case.headers);
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
EXPECT_EQ(result.has_value(), test_case.expect_success);
if (!result.has_value()) {
EXPECT_EQ(result.error(),
base::StringPrintf(
"Rejecting load of %s due to lack of Ad-Auction-Allowed: "
"true (or the deprecated X-Allow-FLEDGE: true).",
TrustedBiddingSignalsUrl().spec().c_str()));
}
}
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsRedirect) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
GURL server_redirect_url = embedded_test_server_.GetURL(
kTrustedSignalsHost,
"/server-redirect?" + TrustedBiddingSignalsUrl().spec());
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request,
server_redirect_url);
ASSERT_FALSE(result.has_value());
// RedirectMode::kError results in ERR_FAILED errors on redirects, which
// results in rather unhelpful error messages.
EXPECT_EQ(result.error(),
base::StringPrintf("Unexpected redirect on %s.",
server_redirect_url.spec().c_str()));
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsMimeType) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
// Use the request media type instead of the response one.
response_mime_type_ = TrustedSignalsFetcher::kRequestMediaType;
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf("Rejecting load of %s due to unexpected MIME type.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsCanSetNoCookies) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
// Request trusted bidding signals using a URL that tries to set a cookie.
GURL set_cookie_url = embedded_test_server_.GetURL(
kTrustedSignalsHost, "/set-cookie?a=1;Secure;SameSite=None");
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request,
set_cookie_url);
// Specific failure reason doesn't really matter for this test, or even that
// it failed. What does matter is the fetch response was successfully
// received, so best to test the request completed in the expected manner.
EXPECT_EQ(result.error(),
base::StringPrintf(
"Rejecting load of %s due to lack of Ad-Auction-Allowed: true "
"(or the deprecated X-Allow-FLEDGE: true).",
set_cookie_url.spec().c_str()));
// Make sure no cookie was set.
base::RunLoop run_loop;
mojo::Remote<network::mojom::CookieManager> cookie_manager;
url_loader_factory_->network_context()->GetCookieManager(
cookie_manager.BindNewPipeAndPassReceiver());
cookie_manager->GetAllCookies(
base::BindLambdaForTesting([&](const net::CookieList& cookies) {
EXPECT_TRUE(cookies.empty());
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsHasNoCookies) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
// Set a same-site none cookie on the trusted signals server's origin.
mojo::Remote<network::mojom::CookieManager> cookie_manager;
url_loader_factory_->network_context()->GetCookieManager(
cookie_manager.BindNewPipeAndPassReceiver());
net::CookieInclusionStatus status;
std::unique_ptr<net::CanonicalCookie> cookie = net::CanonicalCookie::Create(
TrustedBiddingSignalsUrl(), "a=1; Secure; SameSite=None",
base::Time::Now(),
/*server_time=*/std::nullopt,
/*cookie_partition_key=*/std::nullopt, net::CookieSourceType::kHTTP,
&status);
ASSERT_TRUE(cookie);
base::RunLoop run_loop;
cookie_manager->SetCanonicalCookie(
*cookie, TrustedBiddingSignalsUrl(),
net::CookieOptions::MakeAllInclusive(),
base::BindLambdaForTesting([&](net::CookieAccessResult result) {
EXPECT_TRUE(result.status.IsInclude());
run_loop.Quit();
}));
run_loop.Run();
// Request trusted bidding signals. The request handler will cause the test to
// fail if it sees a cookie header.
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsNoKeys) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
const std::set<std::string> kNoKeys;
bidding_signals_request[0][0].keys = kNoKeys;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": []
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsOneKey) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsMultipleKeys) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
const std::set<std::string> kKeys = {"key1", "key2", "key3"};
bidding_signals_request[0][0].keys = kKeys;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1", "key2", "key3" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsMultipleInterestGroups) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
const std::set<std::string> kInterestGroupNames = {"group1", "group2",
"group3"};
bidding_signals_request[0][0].interest_group_names = kInterestGroupNames;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1", "group2", "group3" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsOneAdditionalParam) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
base::Value::Dict additional_params;
additional_params.Set("foo", base::Value("bar"));
bidding_signals_request[0][0].additional_params = additional_params;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsMultipleAdditionalParams) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
base::Value::Dict additional_params;
additional_params.Set("foo", "bar");
additional_params.Set("Foo", "bAr");
additional_params.Set("oof", "rab");
bidding_signals_request[0][0].additional_params = additional_params;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"metadata": {
"foo": "bar",
"Foo": "bAr",
"oof": "rab",
},
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Test the simplest request case, with no optional parameters.
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsMinimalRequest) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
ValidateRequestBodyHex(kBasicScoringSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsOneAdComponentRenderUrl) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
const std::set<GURL> kComponentRenderUrls{GURL("https://component.test/bar")};
scoring_signals_request[0][0].component_render_urls = kComponentRenderUrls;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url.test/foo" ]
},
{
"tags": [ "adComponentRenderURLs" ],
"data": [ "https://component.test/bar" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsMultipleAdComponentRenderUrls) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
const std::set<GURL> kComponentRenderUrls{
GURL("https://component1.test/"),
GURL("https://component1.test/bar"),
GURL("https://component1.test/foo"),
GURL("https://component2.test/baz"),
kDefaultRenderUrl,
};
scoring_signals_request[0][0].component_render_urls = kComponentRenderUrls;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url.test/foo" ]
},
{
"tags": [ "adComponentRenderURLs" ],
"data": [
"https://component1.test/",
"https://component1.test/bar",
"https://component1.test/foo",
"https://component2.test/baz",
"https://render_url.test/foo"
]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsOneAdditionalParam) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
base::Value::Dict additional_params;
additional_params.Set("foo", base::Value("bar"));
scoring_signals_request[0][0].additional_params = additional_params;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url.test/foo" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsMultipleAdditionalParams) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
base::Value::Dict additional_params;
additional_params.Set("foo", "bar");
additional_params.Set("Foo", "bAr");
additional_params.Set("oof", "rab");
scoring_signals_request[0][0].additional_params = additional_params;
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"metadata": {
"foo": "bar",
"Foo": "bAr",
"oof": "rab",
},
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url.test/foo" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Test a single compression group with a single partition, where neither has
// the index 0.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsNoZeroIndices) {
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions;
bidding_partitions.emplace_back(/*partition_id=*/7,
&kDefaultInterestGroupNames, &kDefaultKeys,
&kDefaultAdditionalParams,
/*buyer_tkv_signals=*/nullptr);
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
bidding_signals_request;
bidding_signals_request.emplace(3, std::move(bidding_partitions));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 3,
"id": 7,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
}
]
})";
// The response similarly only includes information for compression group 3.
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 3,
"content": "content"
}
]
})"));
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
3, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Test that the expected amount of padding is added to requests.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsRequestPadding) {
const struct {
size_t interest_group_name_length;
// Test the encrypted and unecrypted request body. The encrypted body
// length, which should always be a power 2, is what's actually publicly
// visible. The others are useful for debugging.
size_t expected_encrypted_body_length;
size_t expected_body_length;
size_t expected_padding;
} kTestCases[] = {
{31, 256, 201, 1},
{32, 256, 201, 0},
{33, 512, 457, 255},
// 286 is less than 31+256 because strings in cbor have variable-length
// length prefixes.
{286, 512, 457, 1},
{287, 512, 457, 0},
{288, 1024, 969, 511},
};
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.interest_group_name_length);
std::string name = std::string(test_case.interest_group_name_length, 'a');
std::set<std::string> interest_group_names = {name};
bidding_signals_request[0][0].interest_group_names = interest_group_names;
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
EXPECT_EQ(GetEncryptedRequestBodyLength(),
test_case.expected_encrypted_body_length);
std::string request_body = GetRequestBody();
size_t padding =
request_body.size() - request_body.find_last_not_of('\0') - 1;
EXPECT_EQ(request_body.size(), test_case.expected_body_length);
EXPECT_EQ(padding, test_case.expected_padding);
// Also test the entire request body directly. The above checks provide some
// protection against issues in CreateKVv2RequestBody(), which is largely
// copied from TrustedSignalsFetcher.
EXPECT_EQ(request_body, auction_worklet::test::CreateKVv2RequestBody(
auction_worklet::test::ToCborString(JsReplace(
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ $1 ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
}
]
})",
name))));
}
}
// Test that the expected amount of padding is added to requests.
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsRequestPadding) {
const struct {
size_t render_url_path_length;
// Test the encrypted and unecrypted request body. The encrypted body
// length, which should always be a power 2, is what's actually publicly
// visible. The others are useful for debugging.
size_t expected_encrypted_body_length;
size_t expected_body_length;
size_t expected_padding;
} kTestCases[] = {
{45, 256, 201, 1},
{46, 256, 201, 0},
{47, 512, 457, 255},
// 300 is less than 45+256 because strings in cbor have variable-length
// length prefixes.
{300, 512, 457, 1},
{301, 512, 457, 0},
{302, 1024, 969, 511},
};
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.render_url_path_length);
GURL render_url = GURL("https://foo.test/" +
std::string(test_case.render_url_path_length, 'a'));
scoring_signals_request[0][0].render_url = render_url;
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
EXPECT_EQ(GetEncryptedRequestBodyLength(),
test_case.expected_encrypted_body_length);
std::string request_body = GetRequestBody();
size_t padding =
request_body.size() - request_body.find_last_not_of('\0') - 1;
EXPECT_EQ(request_body.size(), test_case.expected_body_length);
EXPECT_EQ(padding, test_case.expected_padding);
// Also test the entire request body directly. The above checks provide some
// protection against issues in CreateKVv2RequestBody(), which is largely
// copied from TrustedSignalsFetcher.
EXPECT_EQ(request_body, auction_worklet::test::CreateKVv2RequestBody(
auction_worklet::test::ToCborString(JsReplace(
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ $1 ]
}
]
}
]
})",
render_url))));
}
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsResponseBodyShorterThanHeader) {
for (int length = 0; length < 5; ++length) {
SetResponseBody(std::string(length, 0));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf(
"Failed to load %s: Response body is shorter than a "
"message/ad-auction-trusted-signals-response header.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsResponseBodyUnencrypted) {
SetResponseBody(DefaultResponseBody(), /*use_cleartext=*/true);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf("Failed to load %s: OHTTP decryption failed.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
// Receiving CBOR without a header in the response body should result in
// failure.
TEST_F(TrustedSignalsFetcherTest, NoResponseBodyHeader) {
SetResponseBody(DefaultResponseBody());
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
// Don't check the specific error - it depends on the specific details of the
// CBOR representation of DefaultResponseBody().
ASSERT_FALSE(result.has_value());
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
// This test does not actually gzip the body. The fetcher doesn't try to
// decompress anything, so that should be fine.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsCompressionSchemeGzip) {
SetResponseBodyAndAddHeader(DefaultResponseBody(),
/*advertised_cbor_length=*/std::nullopt,
/*padding_length=*/0,
/*compression_scheme=*/2);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kGzip,
"compression group content", base::Milliseconds(100)));
ValidateFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request),
expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsCompressionSchemeUnsupported) {
for (int compression_scheme : {1, 3}) {
SetResponseBodyAndAddHeader(DefaultResponseBody(),
/*advertised_cbor_length=*/std::nullopt,
/*padding_length=*/0, compression_scheme);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf(
"Failed to load %s: Unsupported compression scheme: %u.",
TrustedBiddingSignalsUrl().spec().c_str(), compression_scheme));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
TEST_F(TrustedSignalsFetcherTest,
BiddingSignalsCompressionSchemeHighOrderBitsIgnored) {
// Everything but the low order two bits of the compression scheme should be
// ignored, so this should be treated as scheme 2 - gzip.
SetResponseBodyAndAddHeader(DefaultResponseBody(),
/*advertised_cbor_length=*/std::nullopt,
/*padding_length=*/0,
/*compression_scheme=*/0xFE);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kGzip,
"compression group content", base::Milliseconds(100)));
ValidateFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request),
expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
// If the advertised length is longer than the response, the request should
// fail, even if it's otherwise a valid CBOR response. This test also checks the
// case where the maximum possible length is received, to make sure there are no
// overflow/underflow issues.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsAdvertisedLengthTooLong) {
const std::string response_body = DefaultResponseBody();
const size_t kTestCases[] = {response_body.length() + 1,
std::numeric_limits<uint32_t>::max()};
for (size_t advertised_cbor_length : kTestCases) {
SetResponseBodyAndAddHeader(response_body, advertised_cbor_length);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf(
"Failed to load %s: Length header exceeds body size.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
// If the advertised shorter is longer than the response, the remaining bytes
// should be ignored, even if they make an otherwise valid CBOR response.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsAdvertisedLengthTooShort) {
SetResponseBodyAndAddHeader(DefaultResponseBody(),
DefaultResponseBody().length() / 2 - 1);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf("Failed to load %s: Failed to parse response as CBOR.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsResponsePadding) {
// No need to check length 0 - that's the default amount of padding added by
// SetResponseBodyAndAddHeader().
for (size_t padding_length : {1, 2, 16, 1023, 1024}) {
SetResponseBodyAndAddHeader(DefaultResponseBody(),
/*advertised_cbor_length=*/std::nullopt,
padding_length);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
// Test the case where there are valid framing headers, but the response body is
// not CBOR.
TEST_F(TrustedSignalsFetcherTest, NotCbor) {
const std::string_view kTestCases[] = {
// This is "This is not CBOR." as a hex string.
"\x54\x68\x69\x73\x20\x69\x73\x20\x6E\x6F\x74\x20\x43\x42\x4F\x52\x2E",
// Null in CBOR, which is currently rejected as not being CBOR by the CBOR
// parser, which is a little weird. Seems
// best to test this case, though, and if we ever do parse it, move it
// into the next test.
"\xF6",
// CBOR has a lot of values that don't map to JSON or even to Javascript
// objects. This is a very incomplete set of some types of them, without
// delving into the spec.
// Undefined in CBOR.
"\xF7",
// An unassigned CBOR value.
"\xF0",
// A reserved CBOR value.
"\xF8\x20",
};
for (const std::string_view& test_string : kTestCases) {
SCOPED_TRACE(base::HexEncode(test_string));
SetResponseBodyAndAddHeader(test_string);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf(
"Failed to load %s: Failed to parse response as CBOR.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
// Test cases where there's a valid framing header, and the response is CBOR,
// but it's not a map.
TEST_F(TrustedSignalsFetcherTest, NotCborMap) {
const std::string_view kTestCases[] = {
R"(true)",
R"("This is a string")",
R"(42)",
R"(["array"])",
};
for (const std::string_view& test_string : kTestCases) {
SCOPED_TRACE(test_string);
SetResponseBodyAndAddHeader(
auction_worklet::test::ToKVv2ResponseCborString(test_string));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf("Failed to load %s: Response body is not a map.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
TEST_F(TrustedSignalsFetcherTest, NoCompressionGroupMap) {
// An empty map.
SetResponseBodyAndAddHeader(
auction_worklet::test::ToKVv2ResponseCborString("{}"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf(
"Failed to load %s: Response is missing compressionGroups array.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, CompressionGroupsNotArray) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({"compressionGroups": {}})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf(
"Failed to load %s: Response is missing compressionGroups array.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, NoCompressionGroups) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({"compressionGroups": []})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
// The fetch succeeds, and the result is an empty map. The cache layer, which
// maps requests to fetched compression groups, will consider this a failure,
// but the fetcher considers this a valid result.
ValidateFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request),
TrustedSignalsFetcher::CompressionGroupResultMap());
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, CompressionGroupNotMap) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({"compressionGroups": [[]]})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf(
"Failed to load %s: Compression group is not of type map.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest,
CompressionGroupWithBadOrNoCompressionGroupId) {
const std::string_view kTestCases[] = {
R"({
"compressionGroups": [
{
"content" : "content"
}
]
})",
R"({
"compressionGroups": [
{
"compressionGroupId": "Jim",
"content" : "content"
}
]
})",
R"({
"compressionGroups": [
{
"compressionGroupId": -1,
"content" : "content"
}
]
})",
R"({
"compressionGroups": [
{
"compressionGroupId": 0.0,
"content" : "content"
}
]
})",
};
for (const std::string_view test_string : kTestCases) {
SCOPED_TRACE(test_string);
SetResponseBodyAndAddHeader(
auction_worklet::test::ToKVv2ResponseCborString(test_string));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf("Failed to load %s: Compression group must have a "
"non-negative integer compressionGroupId.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
TEST_F(TrustedSignalsFetcherTest, CompressionGroupWithBadOrNoContent) {
// Each test case uses a different compression group ID to test the error
// output. Note that TrustedSignalsFetcher has no requirement that returned
// compression groups match requested compression groups. That's enforced by
// the cache layer, since it has to match compression groups to requests it
// sent out, anyways.
const std::vector<std::string_view> kTestCases = {
R"({
"compressionGroups": [
{
"compressionGroupId": 0
}
]
})",
R"({
"compressionGroups": [
{
"compressionGroupId": 1,
"content" : 5
}
]
})",
R"({
"compressionGroups": [
{
"compressionGroupId": 2,
"content" : ["content"]
}
]
})",
// This content type is a string instead of a binary string, which should
// result in an error.
R"({
"compressionGroups": [
{
"compressionGroupId": 3,
"content" : "content"
}
]
})",
};
for (size_t i = 0; i < kTestCases.size(); ++i) {
SCOPED_TRACE(kTestCases[i]);
// Note that this uses ToCborString() to convert the JSON to a CBOR string
// rather than ToKVv2ResponseCborString(). This results in the "content"
// fields not being encoded as binary strings, but rather as whatever CBOR
// type corresponds to the JSON type of the "content" field.
SetResponseBodyAndAddHeader(
auction_worklet::test::ToCborString(kTestCases[i]));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf("Failed to load %s: Compression group %" PRIuS
" missing binary string \"content\".",
TrustedBiddingSignalsUrl().spec().c_str(), i));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
TEST_F(TrustedSignalsFetcherTest, CompressionGroupWithBadTtl) {
// Each test case uses a different compression group ID to test the error
// output. Note that TrustedSignalsFetcher has no requirement that returned
// compression groups match requested compression groups. That's enforced by
// the cache layer, since it has to match compression groups to requests it
// sent out, anyways.
const std::vector<std::string_view> kTestCases = {
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content",
"ttlMs": "grapefruit"
}
]
})",
R"({
"compressionGroups": [
{
"compressionGroupId": 1,
"content": "content",
"ttlMs": 0.5
}
]
})",
};
for (size_t i = 0; i < kTestCases.size(); ++i) {
SCOPED_TRACE(kTestCases[i]);
SetResponseBodyAndAddHeader(
auction_worklet::test::ToKVv2ResponseCborString(kTestCases[i]));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf("Failed to load %s: Compression group %" PRIuS
" ttlMs value is not an integer.",
TrustedBiddingSignalsUrl().spec().c_str(), i));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
// `ttlMs` is an optional field. When not present, we currently default to a
// value of 0.
TEST_F(TrustedSignalsFetcherTest, CompressionGroupWithNoTtl) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, CompressionGroupWithZeroTtl) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content",
"ttlMs": 0
}
]
})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
// Negative TTLs are allows, and are treated as if they were zero.
TEST_F(TrustedSignalsFetcherTest, CompressionGroupWithNegativeTtl) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content",
"ttlMs": -1
}
]
})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsMultiplePartitions) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto* bidding_partitions = &bidding_signals_request[0];
const std::set<std::string> kInterestGroupNames2{"group2"};
const std::set<std::string> kKeys2{"key2"};
base::Value::Dict additional_params2;
additional_params2.Set("foo", "bar");
bidding_partitions->emplace_back(
/*partition_id=*/1, &kInterestGroupNames2, &kKeys2, &additional_params2,
/*buyer_tkv_signals=*/nullptr);
const std::set<std::string> kInterestGroupNames3{"group1", "group2",
"group3"};
const std::set<std::string> kKeys3{"key1", "key2", "key3"};
base::Value::Dict additional_params3;
additional_params3.Set("foo2", "bar2");
bidding_partitions->emplace_back(/*partition_id=*/2, &kInterestGroupNames3,
&kKeys3, &additional_params3,
/*buyer_tkv_signals=*/nullptr);
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
},
{
"compressionGroupId": 0,
"id": 1,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group2" ]
},
{
"tags": [ "keys" ],
"data": [ "key2" ]
}
]
},
{
"compressionGroupId": 0,
"id": 2,
"metadata": { "foo2": "bar2" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1", "group2", "group3" ]
},
{
"tags": [ "keys" ],
"data": [ "key1", "key2", "key3" ]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestBiddingSignalsAndWaitForResult(bidding_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsMultiplePartitions) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
auto* scoring_partitions = &scoring_signals_request[0];
const GURL renderUrl2("https://render_url2.test/");
const std::set<GURL> kAdComponentRenderUrls2{
GURL("https://component2.test/")};
base::Value::Dict additional_params2;
additional_params2.Set("foo", "bar");
scoring_partitions->emplace_back(
/*partition_id=*/1, &renderUrl2, &kAdComponentRenderUrls2,
&additional_params2, /*seller_tkv_signals=*/nullptr);
const GURL renderUrl3("https://render_url3.test/");
const std::set<GURL> kAdComponentRenderUrls3{
GURL("https://component3.test/bar"), GURL("https://component3.test/foo")};
base::Value::Dict additional_params3;
additional_params3.Set("foo2", "bar2");
scoring_partitions->emplace_back(
/*partition_id=*/2, &renderUrl3, &kAdComponentRenderUrls3,
&additional_params3, /*seller_tkv_signals=*/nullptr);
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url.test/foo" ]
}
]
},
{
"compressionGroupId": 0,
"id": 1,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url2.test/" ]
},
{
"tags": [ "adComponentRenderURLs" ],
"data": [ "https://component2.test/" ]
}
]
},
{
"compressionGroupId": 0,
"id": 2,
"metadata": { "foo2": "bar2" },
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url3.test/" ]
},
{
"tags": [ "adComponentRenderURLs" ],
"data": [
"https://component3.test/bar",
"https://component3.test/foo"
]
}
]
}
]
})";
ValidateDefaultFetchResult(
RequestScoringSignalsAndWaitForResult(scoring_signals_request));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Test that a fetch fails when there are two compression groups with the same
// ID in the response.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsDuplicateCompressionGroups) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
},
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf("Failed to load %s: Response contains two "
"compression groups with id 0.",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsMultipleCompressionGroups) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
const std::set<std::string> kInterestGroupNames2{"group2"};
const std::set<std::string> kKeys2{"key2"};
base::Value::Dict additional_params2;
additional_params2.Set("foo", "bar");
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions2;
bidding_partitions2.emplace_back(/*partition_id=*/0, &kInterestGroupNames2,
&kKeys2, &additional_params2,
/*buyer_tkv_signals=*/nullptr);
bidding_signals_request.emplace(1, std::move(bidding_partitions2));
const std::set<std::string> kInterestGroupNames3{"group1", "group2",
"group3"};
const std::set<std::string> kKeys3{"key1", "key2", "key3"};
const std::string kHostname3{"host3.test"};
base::Value::Dict additional_params3;
additional_params3.Set("foo2", "bar2");
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions3;
bidding_partitions3.emplace_back(/*partition_id=*/0, &kInterestGroupNames3,
&kKeys3, &additional_params3,
/*buyer_tkv_signals=*/nullptr);
bidding_signals_request.emplace(2, std::move(bidding_partitions3));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
},
{
"compressionGroupId": 1,
"id": 0,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group2" ]
},
{
"tags": [ "keys" ],
"data": [ "key2" ]
}
]
},
{
"compressionGroupId": 2,
"id": 0,
"metadata": { "foo2": "bar2" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1", "group2", "group3" ]
},
{
"tags": [ "keys" ],
"data": [ "key1", "key2", "key3" ]
}
]
}
]
})";
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content1",
"ttlMs": 10
},
{
"compressionGroupId": 1,
"content": "content2"
},
{
"compressionGroupId": 2,
"content": "content3",
"ttlMs": 150
}
]
})"));
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content1", base::Milliseconds(10)));
expected_result.try_emplace(
1, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content2", base::Milliseconds(0)));
expected_result.try_emplace(
2, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content3", base::Milliseconds(150)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsMultipleCompressionGroups) {
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
const GURL renderUrl2("https://render_url2.test/");
const std::set<GURL> kAdComponentRenderUrls2{
GURL("https://component2.test/")};
base::Value::Dict additional_params2;
additional_params2.Set("foo", "bar");
std::vector<TrustedSignalsFetcher::ScoringPartition> scoring_partitions2;
scoring_partitions2.emplace_back(
/*partition_id=*/0, &renderUrl2, &kAdComponentRenderUrls2,
&additional_params2, /*seller_tkv_signals=*/nullptr);
scoring_signals_request.emplace(1, std::move(scoring_partitions2));
const GURL renderUrl3("https://render_url3.test/");
const std::set<GURL> kAdComponentRenderUrls3{
GURL("https://component3.test/bar"), GURL("https://component3.test/foo")};
base::Value::Dict additional_params3;
additional_params3.Set("foo2", "bar2");
std::vector<TrustedSignalsFetcher::ScoringPartition> scoring_partitions3;
scoring_partitions3.emplace_back(
/*partition_id=*/0, &renderUrl3, &kAdComponentRenderUrls3,
&additional_params3, /*seller_tkv_signals=*/nullptr);
scoring_signals_request.emplace(2, std::move(scoring_partitions3));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url.test/foo" ]
}
]
},
{
"compressionGroupId": 1,
"id": 0,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url2.test/" ]
},
{
"tags": [ "adComponentRenderURLs" ],
"data": [ "https://component2.test/" ]
}
]
},
{
"compressionGroupId": 2,
"id": 0,
"metadata": { "foo2": "bar2" },
"arguments": [
{
"tags": [ "renderURLs" ],
"data": [ "https://render_url3.test/" ]
},
{
"tags": [ "adComponentRenderURLs" ],
"data": [
"https://component3.test/bar",
"https://component3.test/foo"
]
}
]
}
]
})";
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content1",
"ttlMs": 10
},
{
"compressionGroupId": 1,
"content": "content2"
},
{
"compressionGroupId": 2,
"content": "content3",
"ttlMs": 150
}
]
})"));
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content1", base::Milliseconds(10)));
expected_result.try_emplace(
1, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content2", base::Milliseconds(0)));
expected_result.try_emplace(
2, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content3", base::Milliseconds(150)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Test that the entire fetch fails when one of the requested partitions has an
// error.
TEST_F(TrustedSignalsFetcherTest,
BiddingSignalsMultipleCompressionGroupsFailsWhenOneBad) {
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
const std::set<std::string> kInterestGroupNames2{"group2"};
const std::set<std::string> kKeys2{"key2"};
base::Value::Dict additional_params2;
additional_params2.Set("foo", "bar");
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions2;
bidding_partitions2.emplace_back(/*partition_id=*/0, &kInterestGroupNames2,
&kKeys2, &additional_params2,
/*buyer_tkv_signals=*/nullptr);
bidding_signals_request.emplace(1, std::move(bidding_partitions2));
const std::set<std::string> kInterestGroupNames3{"group1", "group2",
"group3"};
const std::set<std::string> kKeys3{"key1", "key2", "key3"};
base::Value::Dict additional_params3;
additional_params3.Set("foo2", "bar2");
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions3;
bidding_partitions3.emplace_back(/*partition_id=*/0, &kInterestGroupNames3,
&kKeys3, &additional_params3,
/*buyer_tkv_signals=*/nullptr);
bidding_signals_request.emplace(2, std::move(bidding_partitions3));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"partitions": [
{
"compressionGroupId": 0,
"id": 0,
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1" ]
},
{
"tags": [ "keys" ],
"data": [ "key1" ]
}
]
},
{
"compressionGroupId": 1,
"id": 0,
"metadata": { "foo": "bar" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group2" ]
},
{
"tags": [ "keys" ],
"data": [ "key2" ]
}
]
},
{
"compressionGroupId": 2,
"id": 0,
"metadata": { "foo2": "bar2" },
"arguments": [
{
"tags": [ "interestGroupNames" ],
"data": [ "group1", "group2", "group3" ]
},
{
"tags": [ "keys" ],
"data": [ "key1", "key2", "key3" ]
}
]
}
]
})";
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content1",
"ttlMs": 10
},
{
"compressionGroupId": 1
},
{
"compressionGroupId": 2,
"content": "content3",
"ttlMs": 150
}
]
})"));
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf("Failed to load %s: Compression group 1 missing "
"binary string \"content\".",
TrustedBiddingSignalsUrl().spec().c_str()));
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsCrossOrigin) {
// Test cross-origin requests both in the case
// `kProtectedAudienceCorsSafelistKVv2Signals` is disabled and when it's
// enabled. In only the first case should there be a CORS preflight.
for (bool add_content_type_to_cors_safelist : {false, true}) {
SCOPED_TRACE(add_content_type_to_cors_safelist);
base::test::ScopedFeatureList feature_list;
if (add_content_type_to_cors_safelist) {
feature_list.InitAndEnableFeature(
network::features::kProtectedAudienceCorsSafelistKVv2Signals);
} else {
feature_list.InitAndDisableFeature(
network::features::kProtectedAudienceCorsSafelistKVv2Signals);
}
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
SetCrossOrigin(
/*cors_preflight_expected=*/!add_content_type_to_cors_safelist);
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result =
RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsCrossOriginLNAFailure) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
SetCrossOrigin();
// Set IP Address space of the origin to be public, making signal requests LNA
// requests (as embedded_test_server_ is in IPAddressSpace::kLocal)
ip_address_space_ = network::mojom::IPAddressSpace::kPublic;
// Don't expect signals requests to get handled.
expect_url_not_requested_ = true;
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf("Failed to load %s error = "
"net::ERR_BLOCKED_BY_PRIVATE_NETWORK_ACCESS_CHECKS.",
TrustedBiddingSignalsUrl().spec().c_str()));
}
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsCrossOriginNotLNASuccess) {
// Treat all requests for signals as coming to a server in
// IPAddressSpace::kPublic, so it shouldn't be considered an LNA request.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
network::switches::kIpAddressSpaceOverrides,
base::StringPrintf(
"%s=public",
embedded_test_server_.host_port_pair().ToString().c_str()));
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
SetCrossOrigin();
ip_address_space_ = network::mojom::IPAddressSpace::kPublic;
auto bidding_signals_request = CreateBasicBiddingSignalsRequest();
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicBiddingSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsCrossOrigin) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
SetCrossOrigin();
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicScoringSignalsRequestBody);
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsCrossOriginLNAFailure) {
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
SetCrossOrigin();
// Set IP Address space of the origin to be public, making signal requests LNA
// requests (as embedded_test_server_ is in IPAddressSpace::kLocal)
ip_address_space_ = network::mojom::IPAddressSpace::kPublic;
// Don't expect signals requests to get handled.
expect_url_not_requested_ = true;
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error(),
base::StringPrintf("Failed to load %s error = "
"net::ERR_BLOCKED_BY_PRIVATE_NETWORK_ACCESS_CHECKS.",
TrustedScoringSignalsUrl().spec().c_str()));
}
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsCrossOriginNotLNASuccess) {
// Treat all requests for signals as coming to a server in
// IPAddressSpace::kPublic, so it shouldn't be considered an LNA request.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
network::switches::kIpAddressSpaceOverrides,
base::StringPrintf(
"%s=public",
embedded_test_server_.host_port_pair().ToString().c_str()));
SetResponseBodyAndAddHeader(auction_worklet::test::ToKVv2ResponseCborString(
R"({
"compressionGroups": [
{
"compressionGroupId": 0,
"content": "content"
}
]
})"));
SetCrossOrigin();
ip_address_space_ = network::mojom::IPAddressSpace::kPublic;
auto scoring_signals_request = CreateBasicScoringSignalsRequest();
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
TrustedSignalsFetcher::CompressionGroupResultMap expected_result;
expected_result.try_emplace(
0, CreateCompressionGroupResult(
auction_worklet::mojom::TrustedSignalsCompressionScheme::kNone,
"content", base::Milliseconds(0)));
ValidateFetchResult(result, expected_result);
ValidateRequestBodyHex(kBasicScoringSignalsRequestBody);
}
// Tests that the correct IsolationInfo is used.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsIsolationInfo) {
// Unlike other tests, use a TestURLLoaderFactory, which intercepts requests
// and lets their fields be examined directly, rather than a
// TestSharedURLLoaderFactory, which makes real requests. This allows directly
// inspecting the created IsolationInfo. Validating the of the IsolationInfo
// value on actual results is, unfortunately, just too difficult to be
// practical.
network::TestURLLoaderFactory url_loader_factory;
TrustedSignalsFetcher trusted_signals_fetcher;
trusted_signals_fetcher.FetchBiddingSignals(
data_decoder_manager_, &url_loader_factory, FrameTreeNodeId(),
kAuctionDevtoolsIds, kDefaultMainFrameOrigin,
network::mojom::IPAddressSpace::kLocal, network_partition_nonce_,
GetScriptOrigin(), TrustedBiddingSignalsUrl(),
BiddingAndAuctionServerKey{
std::string(reinterpret_cast<const char*>(kTestPublicKey),
sizeof(kTestPublicKey)),
kKeyIdStr},
CreateBasicBiddingSignalsRequest(),
base::BindLambdaForTesting(
[](TrustedSignalsFetcher::SignalsFetchResult result) {
ADD_FAILURE() << "This callback should not be invoked";
}));
url_loader_factory.WaitForRequest(TrustedBiddingSignalsUrl());
ASSERT_EQ(url_loader_factory.NumPending(), 1);
const auto* request = url_loader_factory.GetPendingRequest(0);
EXPECT_EQ(request->request.url, TrustedBiddingSignalsUrl());
ASSERT_TRUE(request->request.trusted_params);
const net::IsolationInfo& isolation_info =
request->request.trusted_params->isolation_info;
EXPECT_TRUE(isolation_info.IsEqualForTesting(net::IsolationInfo::Create(
net::IsolationInfo::RequestType::kOther, kDefaultMainFrameOrigin,
kDefaultMainFrameOrigin, net::SiteForCookies(),
network_partition_nonce_)));
}
// Tests that the correct IsolationInfo is used.
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsIsolationInfo) {
// Unlike other tests, use a TestURLLoaderFactory, which intercepts requests
// and lets their fields be examined directly, rather than a
// TestSharedURLLoaderFactory, which makes real requests. This allows directly
// inspecting the created IsolationInfo. Validating the of the IsolationInfo
// value on actual results is, unfortunately, just too difficult to be
// practical.
network::TestURLLoaderFactory url_loader_factory;
TrustedSignalsFetcher trusted_signals_fetcher;
trusted_signals_fetcher.FetchScoringSignals(
data_decoder_manager_, &url_loader_factory, FrameTreeNodeId(),
kAuctionDevtoolsIds, kDefaultMainFrameOrigin,
network::mojom::IPAddressSpace::kLocal, network_partition_nonce_,
GetScriptOrigin(), TrustedScoringSignalsUrl(),
BiddingAndAuctionServerKey{
std::string(reinterpret_cast<const char*>(kTestPublicKey),
sizeof(kTestPublicKey)),
kKeyIdStr},
CreateBasicScoringSignalsRequest(),
base::BindLambdaForTesting(
[](TrustedSignalsFetcher::SignalsFetchResult result) {
ADD_FAILURE() << "This callback should not be invoked";
}));
url_loader_factory.WaitForRequest(TrustedScoringSignalsUrl());
ASSERT_EQ(url_loader_factory.NumPending(), 1);
const auto* request = url_loader_factory.GetPendingRequest(0);
EXPECT_EQ(request->request.url, TrustedScoringSignalsUrl());
ASSERT_TRUE(request->request.trusted_params);
const net::IsolationInfo& isolation_info =
request->request.trusted_params->isolation_info;
EXPECT_TRUE(isolation_info.IsEqualForTesting(net::IsolationInfo::Create(
net::IsolationInfo::RequestType::kOther, kDefaultMainFrameOrigin,
kDefaultMainFrameOrigin, net::SiteForCookies(),
network_partition_nonce_)));
}
// Construct two compression groups with a total of three partitions, each
// having the same buyerTKVSignals.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsIdenticalBuyerTKVSignals) {
const std::set<std::string> kKeys;
const std::string kBuyerTKVSignals = "signal";
std::vector<TrustedSignalsFetcher::BiddingPartition> group0_partitions;
const std::set<std::string> kInterestGroupNames1{"groupA"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames1, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals);
const std::set<std::string> kInterestGroupNames2{"groupB"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames2, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals);
std::vector<TrustedSignalsFetcher::BiddingPartition> group1_partitions;
const std::set<std::string> kInterestGroupNames3{"groupC"};
group1_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames3, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals);
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
bidding_signals_request;
bidding_signals_request.emplace(0, std::move(group0_partitions));
bidding_signals_request.emplace(1, std::move(group1_partitions));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"perPartitionMetadata": {
"contextualData": [
{
"value": "signal"
}
]
},
"partitions": [
{
"id": 0,
"arguments": [
{
"data": [ "groupA" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [ "groupB" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [ "groupC" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 1
}
]
})";
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Construct compression groups: Group 1 (partitions A, B), Group 2 (partition
// C). A and C share the same buyerTKVSignals signals; B has none.
TEST_F(TrustedSignalsFetcherTest,
BiddingSignalsPartialIdenticalBuyerTKVSignals) {
const std::set<std::string> kKeys;
const std::string kBuyerTKVSignals = "signal";
std::vector<TrustedSignalsFetcher::BiddingPartition> group0_partitions;
const std::set<std::string> kInterestGroupNames1{"groupA"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames1, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals);
const std::set<std::string> kInterestGroupNames2{"groupB"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames2, &kKeys,
&kDefaultAdditionalParams,
/*buyer_tkv_signals=*/nullptr);
std::vector<TrustedSignalsFetcher::BiddingPartition> group1_partitions;
const std::set<std::string> kInterestGroupNames3{"groupC"};
group1_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames3, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals);
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
bidding_signals_request;
bidding_signals_request.emplace(0, std::move(group0_partitions));
bidding_signals_request.emplace(1, std::move(group1_partitions));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"perPartitionMetadata": {
"contextualData": [
{
"ids": [
[0, 0],
[1, 0]
],
"value": "signal"
}
]
},
"partitions": [
{
"id": 0,
"arguments": [
{
"data": [ "groupA" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [ "groupB" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [ "groupC" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 1
}
]
})";
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Construct compression groups: Group 1 (partitions A, B), Group 2 (partition
// C). A and C have different buyerTKVSignals signals; B has none.
TEST_F(TrustedSignalsFetcherTest, BiddingSignalsDifferentBuyerTKVSignals) {
const std::set<std::string> kKeys;
std::vector<TrustedSignalsFetcher::BiddingPartition> group0_partitions;
const std::set<std::string> kInterestGroupNames1{"groupA"};
const std::string kBuyerTKVSignals1 = "signalA";
group0_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames1, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals1);
const std::set<std::string> kInterestGroupNames2{"groupB"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames2, &kKeys,
&kDefaultAdditionalParams,
/*buyer_tkv_signals=*/nullptr);
std::vector<TrustedSignalsFetcher::BiddingPartition> group1_partitions;
const std::set<std::string> kInterestGroupNames3{"groupC"};
const std::string kBuyerTKVSignals3 = "signalC";
group1_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames3, &kKeys,
&kDefaultAdditionalParams, &kBuyerTKVSignals3);
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
bidding_signals_request;
bidding_signals_request.emplace(0, std::move(group0_partitions));
bidding_signals_request.emplace(1, std::move(group1_partitions));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"perPartitionMetadata": {
"contextualData": [
{
"ids": [
[ 0, 0 ]
],
"value": "signalA"
},
{
"ids": [
[ 1, 0 ]
],
"value": "signalC"
}
]
},
"partitions": [
{
"id": 0,
"arguments": [
{
"data": [ "groupA" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [ "groupB" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [ "groupC" ],
"tags": [ "interestGroupNames" ]
},
{
"data": [],
"tags": [ "keys" ]
}
],
"compressionGroupId": 1
}
]
})";
auto result = RequestBiddingSignalsAndWaitForResult(bidding_signals_request);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Construct two compression groups with a total of three partitions, each
// having the same sellerTKVSignals.
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsIdenticalSellerTKVSignals) {
const std::set<GURL> kAdComponentRenderUrls;
const std::string kSellerTKVSignals = "signal";
std::vector<TrustedSignalsFetcher::ScoringPartition> group0_partitions;
const GURL kRenderUrl1{"https://render_urla.test/"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kRenderUrl1, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals);
const GURL kRenderUrl2{"https://render_urlb.test/"};
group0_partitions.emplace_back(
/*partition_id=*/1, &kRenderUrl2, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals);
std::vector<TrustedSignalsFetcher::ScoringPartition> group1_partitions;
const GURL kRenderUrl3{"https://render_urlc.test/"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kRenderUrl3, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals);
std::map<int, std::vector<TrustedSignalsFetcher::ScoringPartition>>
scoring_signals_request;
scoring_signals_request.emplace(0, std::move(group0_partitions));
scoring_signals_request.emplace(1, std::move(group1_partitions));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"perPartitionMetadata": {
"contextualData": [
{
"value": "signal"
}
]
},
"partitions": [
{
"id": 0,
"arguments": [
{
"data": [
"https://render_urla.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
},
{
"id": 1,
"arguments": [
{
"data": [
"https://render_urlb.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [
"https://render_urlc.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
}
]
})";
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Construct compression groups: Group 1 (partitions A, B), Group 2 (partition
// C). A and C share the same sellerTKVSignals signals; B has none.
TEST_F(TrustedSignalsFetcherTest,
ScoringSignalsPartialIdenticalSellerTKVSignals) {
const std::set<GURL> kAdComponentRenderUrls;
const std::string kSellerTKVSignals = "signal";
std::vector<TrustedSignalsFetcher::ScoringPartition> group0_partitions;
const GURL kRenderUrl1{"https://render_urla.test/"};
group0_partitions.emplace_back(
/*partition_id=*/0, &kRenderUrl1, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals);
const GURL kRenderUrl2{"https://render_urlb.test/"};
group0_partitions.emplace_back(
/*partition_id=*/1, &kRenderUrl2, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, /*seller_tkv_signals=*/nullptr);
std::vector<TrustedSignalsFetcher::ScoringPartition> group1_partitions;
const GURL kRenderUrl3{"https://render_urlc.test/"};
group1_partitions.emplace_back(
/*partition_id=*/0, &kRenderUrl3, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals);
std::map<int, std::vector<TrustedSignalsFetcher::ScoringPartition>>
scoring_signals_request;
scoring_signals_request.emplace(0, std::move(group0_partitions));
scoring_signals_request.emplace(1, std::move(group1_partitions));
// Request body as a JSON string. Will be converted to CBOR and have a framing
// header and padding added before beign compared to actual body.
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"perPartitionMetadata": {
"contextualData": [
{
"ids": [
[ 0, 0 ],
[ 1, 0 ]
],
"value": "signal"
}
]
},
"partitions": [
{
"id": 0,
"arguments": [
{
"data": [
"https://render_urla.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
},
{
"id": 1,
"arguments": [
{
"data": [
"https://render_urlb.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [
"https://render_urlc.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 1
}
]
})";
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Construct compression groups: Group 1 (partitions A, B), Group 2 (partition
// C). A and C have different sellerTKVSignals signals; B has none.
TEST_F(TrustedSignalsFetcherTest, ScoringSignalsDifferentSellerTKVSignals) {
const std::set<GURL> kAdComponentRenderUrls;
std::vector<TrustedSignalsFetcher::ScoringPartition> group0_partitions;
const GURL kRenderUrl1{"https://render_urla.test/"};
const std::string kSellerTKVSignals1 = "signalA";
group0_partitions.emplace_back(
/*partition_id=*/0, &kRenderUrl1, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals1);
const GURL kRenderUrl2{"https://render_urlb.test/"};
group0_partitions.emplace_back(
/*partition_id=*/1, &kRenderUrl2, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, /*seller_tkv_signals=*/nullptr);
std::vector<TrustedSignalsFetcher::ScoringPartition> group1_partitions;
const GURL kRenderUrl3{"https://render_urlc.test/"};
const std::string kSellerTKVSignals2 = "signalC";
group1_partitions.emplace_back(
/*partition_id=*/0, &kRenderUrl3, &kAdComponentRenderUrls,
&kDefaultAdditionalParams, &kSellerTKVSignals2);
std::map<int, std::vector<TrustedSignalsFetcher::ScoringPartition>>
scoring_signals_request;
scoring_signals_request.emplace(0, std::move(group0_partitions));
scoring_signals_request.emplace(1, std::move(group1_partitions));
const std::string_view kExpectedRequestBodyJson =
R"({
"acceptCompression": [ "none", "gzip" ],
"metadata": { "hostname": "host.test" },
"perPartitionMetadata": {
"contextualData": [
{
"ids": [
[ 0, 0 ]
],
"value": "signalA"
},
{
"ids": [
[ 1, 0 ]
],
"value": "signalC"
}
]
},
"partitions": [
{
"id": 0,
"arguments": [
{
"data": [
"https://render_urla.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
},
{
"id": 1,
"arguments": [
{
"data": [
"https://render_urlb.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 0
},
{
"id": 0,
"arguments": [
{
"data": [
"https://render_urlc.test/"
],
"tags": [
"renderURLs"
]
}
],
"compressionGroupId": 1
}
]
})";
auto result = RequestScoringSignalsAndWaitForResult(scoring_signals_request);
ValidateRequestBodyJson(kExpectedRequestBodyJson);
}
// Test that the request timeout (which should use the value of
// AuctionDownloader::kRequestTimeout) is respected. Unfortunately, can't use
// MOCK_TIME with TrustedSignalsFetcherTest test fixture, since the embedded
// test server uses its own independent thread, so the task environment may
// think it's idle and automatically advance the time while spinning the message
// loop. Even if it did use a task-environment thread, though, the platform
// socket APIs may not guarantee that socket operations occur before the task
// environment notices it has no pending events, and thus advances the time.
TEST(TrustedSignalsFetcherTimeoutTest, BiddingSignalsTimeout) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
data_decoder::test::InProcessDataDecoder in_process_data_decoder;
// URLLoaderFactory that's never configured to return any results, so requests
// to it hang.
network::TestURLLoaderFactory url_loader_factory;
// None of the parameters for this test actually matter, apart from needing to
// be valid.
const GURL kSignalsUrl("https://a.test/");
const url::Origin kSignalsOrigin = url::Origin::Create(kSignalsUrl);
const std::set<std::string> kInterestGroupNames{"group1"};
const std::set<std::string> kKeys;
const base::Value::Dict kAdditionalParams;
std::vector<TrustedSignalsFetcher::BiddingPartition> bidding_partitions;
bidding_partitions.emplace_back(
/*partition_id=*/0, &kInterestGroupNames, &kKeys, &kAdditionalParams,
/*buyer_tkv_signals=*/nullptr);
std::map<int, std::vector<TrustedSignalsFetcher::BiddingPartition>>
bidding_signals_request;
bidding_signals_request.emplace(0, std::move(bidding_partitions));
// Start a request that should complete with a timeout error.
base::RunLoop run_loop;
DataDecoderManager data_decoder_manager;
TrustedSignalsFetcher::SignalsFetchResult out;
TrustedSignalsFetcher trusted_signals_fetcher;
trusted_signals_fetcher.FetchBiddingSignals(
data_decoder_manager, &url_loader_factory, FrameTreeNodeId(),
{"auction_devtools_id"},
/*main_frame_origin=*/kSignalsOrigin,
network::mojom::IPAddressSpace::kLocal,
/*network_partition_nonce=*/base::UnguessableToken::Create(),
kSignalsOrigin, kSignalsUrl,
BiddingAndAuctionServerKey{
std::string(reinterpret_cast<const char*>(kTestPublicKey),
sizeof(kTestPublicKey)),
kKeyIdStr},
bidding_signals_request,
base::BindLambdaForTesting(
[&](TrustedSignalsFetcher::SignalsFetchResult result) {
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error(),
base::StringPrintf(
"Failed to load %s error = net::ERR_TIMED_OUT.",
kSignalsUrl.spec().c_str()));
run_loop.Quit();
}));
constexpr base::TimeDelta kTinyTime = base::Milliseconds(1);
// Run until just before the timeout duration. The request should not time
// out.
task_environment.FastForwardBy(
auction_worklet::AuctionDownloader::kRequestTimeout - kTinyTime);
EXPECT_FALSE(run_loop.AnyQuitCalled());
// Wait until the timeout duration has passed. The request should have timed
// out.
task_environment.FastForwardBy(kTinyTime);
EXPECT_TRUE(run_loop.AnyQuitCalled());
}
} // namespace
} // namespace content
|