File size: 4,902 Bytes
0e239c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package io.grpc.xds;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/** Invoke the production virtual-host selector, never its private hostname helper. */
public final class AB04Consumer {
  interface Check { void run() throws Exception; }
  static int total, failed;
  static void check(String name, Check body) {
    total++;
    try { body.run(); System.out.println("CONTRACT_PASS " + name); }
    catch (Throwable e) {
      failed++;
      System.out.println("CONTRACT_FAIL " + name + " " + e.getClass().getName() + ": " + e.getMessage());
      e.printStackTrace(System.out);
    }
  }
  static VirtualHost host(String name, String... domains) {
    return VirtualHost.create(name, Arrays.asList(domains), Collections.<VirtualHost.Route>emptyList(), Collections.emptyMap());
  }
  static void select(String request, VirtualHost expected, VirtualHost... all) {
    VirtualHost actual = RoutingUtils.findVirtualHostForHostName(Arrays.asList(all), request);
    if (actual != expected) throw new AssertionError("wrong selected virtual host for " + request);
  }
  static void chain(String label) throws Exception {
    JsonObject data = JsonParser.parseString(new String(Files.readAllBytes(Paths.get("/artifacts/ab04-routes/" + label + ".domains.json")), StandardCharsets.UTF_8)).getAsJsonObject();
    String hostname = data.get("hostname").getAsString();
    List<String> absolute = new ArrayList<>();
    data.getAsJsonArray("domains").forEach(value -> {
      // Project the exact, producer-emitted absolute domain for the task's reproduction.
      // Do not fabricate a missing provider output or normalize the wire before consumption.
      if (value.getAsString().equals(hostname + ".")) absolute.add(value.getAsString());
    });
    if (absolute.isEmpty()) throw new AssertionError("actual Istio route has no absolute domain for " + hostname);
    VirtualHost target = host("producer-" + label, absolute.toArray(new String[0]));
    VirtualHost other = host("other", "unrelated.example.test");
    VirtualHost fallback = host("fallback", "*");
    for (String request : Arrays.asList(hostname, hostname + ".", hostname.toUpperCase(Locale.ROOT), hostname.toUpperCase(Locale.ROOT) + ".")) {
      select(request, target, other, fallback, target);
    }
    System.out.println("CHAIN_EVIDENCE " + data.toString());
  }
  public static void main(String[] args) {
    if (args[0].equals("f2p")) {
      for (String label : Arrays.asList("default", "other_namespace", "custom_suffix", "nondefault_port")) check("chain_" + label, () -> chain(label));
      check("absolute_request", () -> { VirtualHost v=host("exact","API.Example.test");select("api.example.test.",v,v); });
      check("absolute_pattern", () -> { VirtualHost v=host("exact","API.Example.test.");select("api.example.test",v,v); });
      check("absolute_both", () -> { VirtualHost v=host("exact","API.Example.test.");select("api.example.test.",v,v); });
      check("absolute_wildcards", () -> {
        VirtualHost suffix=host("suffix","*.example.test.");VirtualHost prefix=host("prefix","api.*.");VirtualHost exact=host("exact","api.example.test.");VirtualHost any=host("any","*");
        select("api.example.test",exact,any,suffix,prefix,exact);
        select("other.example.test",suffix,any,suffix);
        select("api.other.test.",prefix,any,prefix);
        select("example.test",any,suffix,any);
      });
    } else if (args[0].equals("p2p")) {
      check("relative_exact_case", () -> {VirtualHost v=host("v","Api.Example.test");VirtualHost w=host("wild","*.example.test");select("api.example.test",v,w,v);});
      check("no_match", () -> {VirtualHost v=host("v","api.example.test");select("other.example.test",null,v);select("api.example",null,v);});
      check("wildcard_precedence", () -> {
        VirtualHost any=host("any","*");VirtualHost a=host("a","*.example.test");VirtualHost b=host("b","*.sub.example.test");VirtualHost p=host("p","api.*");
        select("api.sub.example.test",b,any,p,a,b);select("api.other.test",p,any,p);select("unrelated.test",any,any,p,a,b);
      });
      check("wildcard_boundaries", () -> {
        VirtualHost suffix=host("suffix","*.example.test");VirtualHost prefix=host("prefix","api-*");VirtualHost invalid=host("invalid","a*pi.example.test","*example*");
        select("example.test",null,suffix);select("api-",null,prefix);select("api-v1",prefix,prefix);select("api.example.test",null,invalid);
      });
    } else throw new IllegalArgumentException("unknown phase");
    System.out.println("CONTRACT_SUMMARY {\"total\":"+total+",\"failed\":"+failed+",\"skipped\":0}");
    if (failed != 0) System.exit(1);
  }
}