summaryrefslogtreecommitdiffstats
path: root/netwerk/test/gtest/TestSupportAlpn.cpp
blob: ee67c854e61fcd91015fbc9311a782a4aae0a38e (plain)
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
#include "gtest/gtest.h"

#include "mozilla/Preferences.h"
#include "nsIHttpProtocolHandler.h"
#include "nsHttp.h"

namespace mozilla {
namespace net {

TEST(TestSupportAlpn, testSvcParamKeyAlpn)
{
  Preferences::SetBool("network.http.http3.enabled", true);
  // initialize gHttphandler.
  nsCOMPtr<nsIHttpProtocolHandler> http =
      do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "http");

  // h2 and h3 are enabled, so first appearance h3 alpn-id is returned.
  nsTArray<nsCString> alpn = {"h3-28"_ns, "h3-27"_ns, "h2"_ns, "http/1.1"_ns};
  Tuple<nsCString, bool> result = SelectAlpnFromAlpnList(alpn, false, false);
  ASSERT_EQ(Get<0>(result), "h3-28"_ns);
  ASSERT_EQ(Get<1>(result), true);

  // We don't support h3-26, so we should choose h2.
  alpn = {"h3-26"_ns, "h2"_ns, "http/1.1"_ns};
  result = SelectAlpnFromAlpnList(alpn, false, false);
  ASSERT_EQ(Get<0>(result), "h2"_ns);
  ASSERT_EQ(Get<1>(result), false);

  // h3 is disabled, so we will use h2.
  alpn = {"h3-28"_ns, "h3-27"_ns, "h2"_ns, "http/1.1"_ns};
  result = SelectAlpnFromAlpnList(alpn, false, true);
  ASSERT_EQ(Get<0>(result), "h2"_ns);
  ASSERT_EQ(Get<1>(result), false);

  // h3 is disabled and h2 is not found, so we will select http/1.1.
  alpn = {"h3-28"_ns, "h3-27"_ns, "http/1.1"_ns};
  result = SelectAlpnFromAlpnList(alpn, false, true);
  ASSERT_EQ(Get<0>(result), "http/1.1"_ns);
  ASSERT_EQ(Get<1>(result), false);

  // h3 and h2 are disabled, so we will select http/1.1.
  alpn = {"h3-28"_ns, "h3-27"_ns, "h2"_ns, "http/1.1"_ns};
  result = SelectAlpnFromAlpnList(alpn, true, true);
  ASSERT_EQ(Get<0>(result), "http/1.1"_ns);
  ASSERT_EQ(Get<1>(result), false);

  // h3 and h2 are disabled and http1.1 is not found, we return an empty string.
  alpn = {"h3-28"_ns, "h3-27"_ns, "h2"_ns};
  result = SelectAlpnFromAlpnList(alpn, true, true);
  ASSERT_EQ(Get<0>(result), ""_ns);
  ASSERT_EQ(Get<1>(result), false);

  // No supported alpn.
  alpn = {"ftp"_ns, "h2c"_ns};
  result = SelectAlpnFromAlpnList(alpn, true, true);
  ASSERT_EQ(Get<0>(result), ""_ns);
  ASSERT_EQ(Get<1>(result), false);
}

}  // namespace net
}  // namespace mozilla