text:
package network

import (
	"context"
	"fmt"
	"strconv"
	"strings"
	"time"

	"kubevirt.io/kubevirt/tests/console"
	"kubevirt.io/kubevirt/tests/exec"

	"kubevirt.io/kubevirt/tests/framework/checks"
	"kubevirt.io/kubevirt/tests/libnet/cluster"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	batchv1 "k8s.io/api/batch/v1"
	k8sv1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/rand"
	netutils "k8s.io/utils/net"

	"kubevirt.io/kubevirt/tests/util"

	v1 "kubevirt.io/api/core/v1"
	"kubevirt.io/client-go/kubecli"

	"kubevirt.io/kubevirt/tests"
	"kubevirt.io/kubevirt/tests/clientcmd"
	"kubevirt.io/kubevirt/tests/libnet"
	"kubevirt.io/kubevirt/tests/libvmi"
)

const (
	shouldExposeServiceViaVirtctl = "should expose a service via `virtctl expose ...`"
	gettingValidatingClusterIP    = "Getting and validating the cluster IP given for the service"
	iteratingClusterIPs           = "Iterating over the ClusterIPs and run hello-world job"
	overIPv6Family                = "over IPv6 IP family"
	overDualStackIPv4             = "over dual stack, primary ipv4"
	overDualStackIPv6             = "over dual stack, primary ipv6"
	shouldStartVM                 = "should have been able to start the VM"
)

func newLabeledVMI(label string) (vmi *v1.VirtualMachineInstance) {
	ports := []v1.Port{{Name: "http", Port: 80},
		{Name: "test-port-tcp", Port: 1500, Protocol: "TCP"},
		{Name: "udp", Port: 82, Protocol: "UDP"},
		{Name: "test-port-udp", Port: 1500, Protocol: "UDP"}}
	vmi = libvmi.NewAlpineWithTestTooling(
		libvmi.WithMasqueradeNetworking(ports...)...,
	)
	vmi.Labels = map[string]string{"expose": label}
	return
}

type ipFamily string

const (
	ipv4            ipFamily = "ipv4"
	ipv6            ipFamily = "ipv6"
	dualIPv4Primary ipFamily = "ipv4,ipv6"
	dualIPv6Primary ipFamily = "ipv6,ipv4"
)

func isDualStack(ipFamily ipFamily) bool {
	return ipFamily == dualIPv4Primary || ipFamily == dualIPv6Primary
}

func inlcudesIpv6(ipFamily ipFamily) bool {
	return ipFamily != ipv4
}

func includesIpv4(ipFamily ipFamily) bool {
	return ipFamily != ipv6
}

var _ = SIGDescribe("[rfe_id:253][crit:medium][vendor:cnv-qe@redhat.com][level:component]Expose", func() {

	var virtClient kubecli.KubevirtClient
	var err error

	const testPort = 1500

	BeforeEach(func() {
		virtClient, err = kubecli.GetKubevirtClient()
		util.PanicOnError(err)
	})

	randomizeName := func(currentName string) string {
		return currentName + rand.String(5)
	}

	validateClusterIp := func(clusterIp string, ipFamily ipFamily) error {
		var correctPrimaryFamily bool
		switch ipFamily {
		case ipv4, dualIPv4Primary:
			correctPrimaryFamily = netutils.IsIPv4String(clusterIp)
		case ipv6, dualIPv6Primary:
			correctPrimaryFamily = netutils.IsIPv6String(clusterIp)
		}
		if !correctPrimaryFamily {
			return fmt.Errorf("the ClusterIP %s belongs to the wrong ip family", clusterIp)
		}
		return nil
	}

	skipIfNotSupportedCluster := func(ipFamily ipFamily) {
		if includesIpv4(ipFamily) {
			libnet.SkipWhenClusterNotSupportIpv4(virtClient)
		}
		if inlcudesIpv6(ipFamily) {
			libnet.SkipWhenClusterNotSupportIpv6(virtClient)
		}
		if isDualStack(ipFamily) {
			checks.SkipIfVersionBelow("Dual stack service requires v1.20 and above", "1.20")
		}
	}

	appendIpFamilyToExposeArgs := func(ipFamily ipFamily, vmiExposeArgs []string) []string {
		if inlcudesIpv6(ipFamily) {
			vmiExposeArgs = append(vmiExposeArgs, "--ip-family", string(ipFamily))
		}
		return vmiExposeArgs
	}

	createAndWaitForJobToSucceed := func(jobFactory func(host, port string) *batchv1.Job, namespace, ip, port, viaMessage string) error {
		By(fmt.Sprintf("Starting a job which tries to reach the VMI via the %s", viaMessage))
		job := jobFactory(ip, port)
		job, err := virtClient.BatchV1().Jobs(namespace).Create(context.Background(), job, metav1.CreateOptions{})
		ExpectWithOffset(1, err).ToNot(HaveOccurred())

		By("Waiting for the job to report a successful connection attempt")
		return tests.WaitForJobToSucceed(job, time.Duration(120)*time.Second)
	}

	executeVirtctlExposeCommand := func(ExposeArgs []string) error {
		virtctl := clientcmd.NewRepeatableVirtctlCommand(ExposeArgs...)
		return virtctl()
	}

	getService := func(namespace, serviceName string) (*k8sv1.Service, error) {
		svc, err := virtClient.CoreV1().Services(namespace).Get(context.Background(), serviceName, k8smetav1.GetOptions{})
		if err != nil {
			return nil, err
		}

		return svc, nil
	}

	runJobsAgainstService := func(svc *k8sv1.Service, namespace string, jobFactories ...func(host, port string) *batchv1.Job) {
		serviceIPs := svc.Spec.ClusterIPs
		for _, jobFactory := range jobFactories {
			for ipOrderNum, ip := range serviceIPs {
				servicePort := fmt.Sprint(svc.Spec.Ports[0].Port)
				Expect(createAndWaitForJobToSucceed(jobFactory, namespace, ip, servicePort, fmt.Sprintf("%d ClusterIP", ipOrderNum+1))).To(Succeed())
			}
		}
	}

	Context("Expose service on a VM", func() {
		var tcpVM *v1.VirtualMachineInstance
		BeforeEach(func() {
			tcpVM = newLabeledVMI("vm")
			tcpVM = tests.RunVMIAndExpectLaunch(tcpVM, 180)
			tests.GenerateHelloWorldServer(tcpVM, testPort, "tcp", console.LoginToAlpine, false)
		})

		Context("Expose ClusterIP service", func() {
			const servicePort = "27017"
			const serviceNamePrefix = "cluster-ip-vmi"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(tcpVM,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort(strconv.Itoa(testPort)))
			})

			DescribeTable("[label:masquerade_binding_connectivity]Should expose a Cluster IP service on a VMI and connect to it", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmiExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmiExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By(gettingValidatingClusterIP)
				svc, err := getService(tcpVM.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())
				err = validateClusterIp(svc.Spec.ClusterIP, ipFamily)
				Expect(err).ToNot(HaveOccurred())

				By(iteratingClusterIPs)
				runJobsAgainstService(svc, tcpVM.Namespace, tests.NewHelloWorldJobTCP)
			},
				Entry("[test_id:1531] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})

		Context("Expose ClusterIP service with string target-port", func() {
			const servicePort = "27017"
			const serviceNamePrefix = "cluster-ip-target-vmi"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(tcpVM,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort("http"))
			})

			DescribeTable("Should expose a ClusterIP service and connect to the vm on port 80", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmiExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmiExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By("Waiting for kubernetes to create the relevant endpoint")
				getEndpoint := func() error {
					_, err := virtClient.CoreV1().Endpoints(util.NamespaceTestDefault).Get(context.Background(), serviceName, k8smetav1.GetOptions{})
					return err
				}
				Eventually(getEndpoint, 60, 1).Should(BeNil())

				endpoints, err := virtClient.CoreV1().Endpoints(util.NamespaceTestDefault).Get(context.Background(), serviceName, k8smetav1.GetOptions{})
				Expect(err).ToNot(HaveOccurred())

				Expect(endpoints.Subsets).To(HaveLen(1))
				endpoint := endpoints.Subsets[0]
				Expect(endpoint.Ports).To(HaveLen(1))
				Expect(endpoint.Ports[0].Port).To(Equal(int32(80)))

				endpointSlices, err := virtClient.DiscoveryV1().EndpointSlices(util.NamespaceTestDefault).List(context.Background(), metav1.ListOptions{})
				Expect(err).ToNot(HaveOccurred())

				numOfExpectedAddresses := 1
				addresses := []string{}
				isDualStack := isDualStack(ipFamily)
				if isDualStack {
					numOfExpectedAddresses = 2
				}

				for _, endpointSlice := range endpointSlices.Items {
					for _, endpoint := range endpointSlice.Endpoints {
						addresses = append(addresses, endpoint.Addresses...)
					}
				}
				Expect(addresses).To(HaveLen(numOfExpectedAddresses))
			},
				Entry("[test_id:1532] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})

		Context("Expose ClusterIP service with ports on the vmi defined", func() {
			const serviceNamePrefix = "cluster-ip-target-multiple-ports-vmi"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(tcpVM,
					libnet.WithServiceName(serviceName))
			})

			DescribeTable("Should expose a ClusterIP service and connect to all ports defined on the vmi", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmiExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmiExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By("Waiting for kubernetes to create the relevant endpoint")
				getEndpoint := func() error {
					_, err := virtClient.CoreV1().Endpoints(util.NamespaceTestDefault).Get(context.Background(), serviceName, k8smetav1.GetOptions{})
					return err
				}
				Eventually(getEndpoint, 60, 1).Should(BeNil())

				endpoints, err := virtClient.CoreV1().Endpoints(util.NamespaceTestDefault).Get(context.Background(), serviceName, k8smetav1.GetOptions{})
				Expect(err).ToNot(HaveOccurred())

				Expect(endpoints.Subsets).To(HaveLen(1))
				endpoint := endpoints.Subsets[0]
				Expect(endpoint.Ports).To(HaveLen(4))
				Expect(endpoint.Ports).To(ContainElement(k8sv1.EndpointPort{Name: "port-1", Port: 80, Protocol: "TCP"}))
				Expect(endpoint.Ports).To(ContainElement(k8sv1.EndpointPort{Name: "port-2", Port: 1500, Protocol: "TCP"}))
				Expect(endpoint.Ports).To(ContainElement(k8sv1.EndpointPort{Name: "port-3", Port: 82, Protocol: "UDP"}))
				Expect(endpoint.Ports).To(ContainElement(k8sv1.EndpointPort{Name: "port-4", Port: 1500, Protocol: "UDP"}))
			},
				Entry("[test_id:1533] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})

		Context("Expose ClusterIP service with IPFamilyPolicy", func() {
			const serviceNamePrefix = "cluster-ip-with-ip-family-policy"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(tcpVM,
					libnet.WithServiceName(serviceName))
			})

			DescribeTable("Should expose a ClusterIP service with the correct IPFamilyPolicy", func(ipFamiyPolicy k8sv1.IPFamilyPolicyType) {
				checks.SkipIfVersionBelow("IPFamilyPolicy property on a service requires v1.20 and above", "1.20")

				if ipFamiyPolicy == k8sv1.IPFamilyPolicyRequireDualStack {
					libnet.SkipWhenNotDualStackCluster(virtClient)
				}

				calcNumOfClusterIPs := func() int {
					switch ipFamiyPolicy {
					case k8sv1.IPFamilyPolicySingleStack:
						return 1
					case k8sv1.IPFamilyPolicyPreferDualStack:
						isClusterDualStack, err := cluster.DualStack(virtClient)
						ExpectWithOffset(1, err).NotTo(HaveOccurred(), "should have been able to infer if the cluster is dual stack")
						if isClusterDualStack {
							return 2
						}
						return 1
					case k8sv1.IPFamilyPolicyRequireDualStack:
						return 2
					}
					return 0
				}

				vmiExposeArgs = append(vmiExposeArgs, "--ip-family-policy", string(ipFamiyPolicy))

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By("Getting the service")
				svc, err := getService(tcpVM.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())

				By("Validating the num of cluster ips")
				Expect(svc.Spec.ClusterIPs).To(HaveLen(calcNumOfClusterIPs()))
			},
				Entry("over SingleStack IP family policy", k8sv1.IPFamilyPolicySingleStack),
				Entry("over PreferDualStack IP family policy", k8sv1.IPFamilyPolicyPreferDualStack),
				Entry("over RequireDualStack IP family policy", k8sv1.IPFamilyPolicyRequireDualStack),
			)
		})

		Context("Expose NodePort service", func() {
			const servicePort = "27017"
			const serviceNamePrefix = "node-port-vmi"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(tcpVM,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort(strconv.Itoa(testPort)),
					libnet.WithType("NodePort"))
			})

			DescribeTable("[label:masquerade_binding_connectivity]Should expose a NodePort service on a VMI and connect to it", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmiExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmiExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By("Getting the service")
				svc, err := getService(tcpVM.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())

				nodePort := svc.Spec.Ports[0].NodePort
				Expect(nodePort).To(BeNumerically(">", 0))

				By("Getting the node IP from all nodes")
				nodes, err := virtClient.CoreV1().Nodes().List(context.Background(), k8smetav1.ListOptions{})
				Expect(err).ToNot(HaveOccurred())
				Expect(nodes.Items).ToNot(BeEmpty())
				for _, node := range nodes.Items {
					Expect(node.Status.Addresses).ToNot(BeEmpty())
					nodeIP := node.Status.Addresses[0].Address
					var ipv6NodeIP string

					if includesIpv4(ipFamily) {
						By("Connecting to IPv4 node IP")
						Expect(createAndWaitForJobToSucceed(tests.NewHelloWorldJobTCP, tcpVM.Namespace, nodeIP, strconv.Itoa(int(nodePort)), fmt.Sprintf("NodePort using %s node ip", ipFamily))).To(Succeed())
					}
					if inlcudesIpv6(ipFamily) {
						ipv6NodeIP, err = resolveNodeIPAddrByFamily(
							virtClient,
							libvmi.GetPodByVirtualMachineInstance(tcpVM, tcpVM.GetNamespace()),
							node,
							k8sv1.IPv6Protocol)
						Expect(err).NotTo(HaveOccurred(), "must have been able to resolve an IP address from the node name")
						Expect(ipv6NodeIP).NotTo(BeEmpty(), "must have been able to resolve the IPv6 address of the node")

						By("Connecting to IPv6 node IP")
						Expect(createAndWaitForJobToSucceed(tests.NewHelloWorldJobTCP, tcpVM.Namespace, ipv6NodeIP, strconv.Itoa(int(nodePort)), fmt.Sprintf("NodePort using %s node ip", ipFamily))).To(Succeed())
					}
				}
			},
				Entry("[test_id:1534] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})
	})

	Context("Expose UDP service on a VMI", func() {
		var udpVM *v1.VirtualMachineInstance
		BeforeEach(func() {
			udpVM = newLabeledVMI("udp-vm")
			udpVM = tests.RunVMIAndExpectLaunch(udpVM, 180)
			tests.GenerateHelloWorldServer(udpVM, testPort, "udp", console.LoginToAlpine, false)
		})

		Context("Expose ClusterIP UDP service", func() {
			const servicePort = "28017"
			const serviceNamePrefix = "cluster-ip-udp-vmi"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(udpVM,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort(strconv.Itoa(testPort)),
					libnet.WithProtocol("UDP"))
			})

			DescribeTable("[label:masquerade_binding_connectivity]Should expose a ClusterIP service on a VMI and connect to it", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmiExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmiExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By(gettingValidatingClusterIP)
				svc, err := getService(udpVM.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())
				err = validateClusterIp(svc.Spec.ClusterIP, ipFamily)
				Expect(err).ToNot(HaveOccurred())

				By(iteratingClusterIPs)
				runJobsAgainstService(svc, udpVM.Namespace, tests.NewHelloWorldJobUDP)
			},
				Entry("[test_id:1535] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})

		Context("Expose NodePort UDP service", func() {
			const servicePort = "29017"
			const serviceNamePrefix = "node-port-udp-vmi"

			var serviceName string
			var vmiExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmiExposeArgs = libnet.NewVMIExposeArgs(udpVM,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort(strconv.Itoa(testPort)),
					libnet.WithType("NodePort"),
					libnet.WithProtocol("UDP"))
			})

			DescribeTable("[label:masquerade_binding_connectivity]Should expose a NodePort service on a VMI and connect to it", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmiExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmiExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmiExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By(gettingValidatingClusterIP)
				svc, err := getService(udpVM.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())
				err = validateClusterIp(svc.Spec.ClusterIP, ipFamily)
				Expect(err).ToNot(HaveOccurred())

				nodePort := svc.Spec.Ports[0].NodePort
				Expect(nodePort).To(BeNumerically(">", 0))

				By(iteratingClusterIPs)
				runJobsAgainstService(svc, udpVM.Namespace, tests.NewHelloWorldJobUDP)

				By("Getting the node IP from all nodes")
				nodes, err := virtClient.CoreV1().Nodes().List(context.Background(), k8smetav1.ListOptions{})
				Expect(err).ToNot(HaveOccurred())
				Expect(nodes.Items).ToNot(BeEmpty())
				for _, node := range nodes.Items {
					Expect(node.Status.Addresses).ToNot(BeEmpty())
					nodeIP := node.Status.Addresses[0].Address

					var ipv6NodeIP string
					if inlcudesIpv6(ipFamily) {
						ipv6NodeIP, err = resolveNodeIPAddrByFamily(
							virtClient,
							libvmi.GetPodByVirtualMachineInstance(udpVM, udpVM.GetNamespace()),
							node,
							k8sv1.IPv6Protocol)
						Expect(err).NotTo(HaveOccurred(), "must have been able to resolve an IP address from the node name")
						Expect(ipv6NodeIP).NotTo(BeEmpty(), "must have been able to resolve the IPv6 address of the node")
					}

					if includesIpv4(ipFamily) {
						By("Connecting to IPv4 node IP")
						Expect(createAndWaitForJobToSucceed(tests.NewHelloWorldJobUDP, udpVM.Namespace, nodeIP, strconv.Itoa(int(nodePort)), "NodePort ipv4 address")).To(Succeed())
					}
					if inlcudesIpv6(ipFamily) {
						By("Connecting to IPv6 node IP")
						Expect(createAndWaitForJobToSucceed(tests.NewHelloWorldJobUDP, udpVM.Namespace, ipv6NodeIP, strconv.Itoa(int(nodePort)), "NodePort ipv6 address")).To(Succeed())
					}
				}
			},
				Entry("[test_id:1536] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})
	})

	Context("Expose service on a VMI replica set", func() {
		const numberOfVMs = 2

		var vmrs *v1.VirtualMachineInstanceReplicaSet
		BeforeEach(func() {
			By("Creating a VMRS object with 2 replicas")
			vmrs = tests.NewRandomReplicaSetFromVMI(newLabeledVMI("vmirs"), int32(numberOfVMs))
			vmrs.Labels = map[string]string{"expose": "vmirs"}

			By("Start the replica set")
			vmrs, err = virtClient.ReplicaSet(util.NamespaceTestDefault).Create(vmrs)
			Expect(err).ToNot(HaveOccurred())

			By("Checking the number of ready replicas")
			Eventually(func() int {
				rs, err := virtClient.ReplicaSet(util.NamespaceTestDefault).Get(vmrs.ObjectMeta.Name, k8smetav1.GetOptions{})
				Expect(err).ToNot(HaveOccurred())
				return int(rs.Status.ReadyReplicas)
			}, 120*time.Second, 1*time.Second).Should(Equal(numberOfVMs))

			By("Add an 'hello world' server on each VMI in the replica set")
			// TODO: add label to list options
			// check size of list
			// remove check for owner
			vms, err := virtClient.VirtualMachineInstance(vmrs.ObjectMeta.Namespace).List(&k8smetav1.ListOptions{})
			Expect(err).ToNot(HaveOccurred())
			for _, vm := range vms.Items {
				if vm.OwnerReferences != nil {
					tests.GenerateHelloWorldServer(&vm, testPort, "tcp", console.LoginToAlpine, false)
				}
			}
		})

		Context("Expose ClusterIP service", func() {
			const servicePort = "27017"
			const serviceNamePrefix = "cluster-ip-vmirs"

			var serviceName string
			var vmirsExposeArgs []string

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmirsExposeArgs = libnet.NewVMIRSExposeArgs(vmrs,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort(strconv.Itoa(testPort)))
			})

			DescribeTable("[label:masquerade_binding_connectivity]Should create a ClusterIP service on VMRS and connect to it", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmirsExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmirsExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmirsExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				By(gettingValidatingClusterIP)
				svc, err := getService(vmrs.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())
				err = validateClusterIp(svc.Spec.ClusterIP, ipFamily)
				Expect(err).ToNot(HaveOccurred())

				By(iteratingClusterIPs)
				runJobsAgainstService(svc, vmrs.Namespace, tests.NewHelloWorldJobTCP)
			},
				Entry("[test_id:1537] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})
	})

	Context("Expose a VM as a service.", func() {
		const servicePort = "27017"
		const serviceNamePrefix = "cluster-ip-vm"
		var vm *v1.VirtualMachine

		var vmExposeArgs []string

		startVMIFromVMTemplate := func(virtClient kubecli.KubevirtClient, name string, namespace string) *v1.VirtualMachineInstance {
			By("Calling the start command")
			virtctl := clientcmd.NewRepeatableVirtctlCommand("start", "--namespace", namespace, name)
			Expect(virtctl()).To(Succeed(), "should succeed starting a VMI via `virtctl start ...`")

			By("Getting the status of the VMI")
			Eventually(func() bool {
				vm, err := virtClient.VirtualMachine(namespace).Get(name, &k8smetav1.GetOptions{})
				Expect(err).ToNot(HaveOccurred())
				return vm.Status.Ready
			}, 120*time.Second, 1*time.Second).Should(BeTrue())

			By("Getting the running VMI")
			var vmi *v1.VirtualMachineInstance
			Eventually(func() bool {
				vmi, err = virtClient.VirtualMachineInstance(namespace).Get(name, &k8smetav1.GetOptions{})
				Expect(err).ToNot(HaveOccurred())
				return vmi.Status.Phase == v1.Running
			}, 120*time.Second, 1*time.Second).Should(BeTrue())

			return vmi
		}

		createStoppedVM := func(virtClient kubecli.KubevirtClient, namespace string) (*v1.VirtualMachine, error) {
			By("Creating an VM object")
			vm := tests.NewRandomVirtualMachine(newLabeledVMI("vm"), false)

			By("Creating the VM")
			vm, err = virtClient.VirtualMachine(namespace).Create(vm)
			return vm, err
		}

		startVMWithServer := func(virtClient kubecli.KubevirtClient, protocol string, port int) *v1.VirtualMachineInstance {
			By("Calling the start command on the stopped VM")
			vmi := startVMIFromVMTemplate(virtClient, vm.GetName(), vm.GetNamespace())
			if vmi == nil {
				return nil
			}
			tests.GenerateHelloWorldServer(vmi, port, protocol, console.LoginToAlpine, false)
			return vmi
		}

		Context("Expose a VM as a ClusterIP service.", func() {
			var serviceName string

			BeforeEach(func() {
				vm, err = createStoppedVM(virtClient, util.NamespaceTestDefault)
				Expect(err).NotTo(HaveOccurred(), "should create a stopped VM.")
			})

			BeforeEach(func() {
				serviceName = randomizeName(serviceNamePrefix)
				vmExposeArgs = libnet.NewVMExposeArgs(vm,
					libnet.WithPort(servicePort),
					libnet.WithServiceName(serviceName),
					libnet.WithTargetPort(strconv.Itoa(testPort)))
			})

			DescribeTable("[label:masquerade_binding_connectivity]Connect to ClusterIP service that was set when VM was offline.", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				vmi := startVMWithServer(virtClient, "tcp", testPort)
				Expect(vmi).NotTo(BeNil(), shouldStartVM)

				// This TC also covers:
				// [test_id:1795] Exposed VM (as a service) can be reconnected multiple times.
				By(gettingValidatingClusterIP)
				svc, err := getService(vm.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())
				err = validateClusterIp(svc.Spec.ClusterIP, ipFamily)
				Expect(err).ToNot(HaveOccurred())

				By(iteratingClusterIPs)
				runJobsAgainstService(svc, vm.Namespace, tests.NewHelloWorldJobTCP)
			},
				Entry("[test_id:1538] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)

			DescribeTable("[label:masquerade_binding_connectivity]Should verify the exposed service is functional before and after VM restart.", func(ipFamily ipFamily) {
				skipIfNotSupportedCluster(ipFamily)
				vmExposeArgs = appendIpFamilyToExposeArgs(ipFamily, vmExposeArgs)

				By("Exposing the service via virtctl command")
				Expect(executeVirtctlExposeCommand(vmExposeArgs)).To(Succeed(), shouldExposeServiceViaVirtctl)

				vmi := startVMWithServer(virtClient, "tcp", testPort)
				Expect(vmi).NotTo(BeNil(), shouldStartVM)

				vmObj := vm

				By(gettingValidatingClusterIP)
				svc, err := getService(vmObj.Namespace, serviceName)
				Expect(err).ToNot(HaveOccurred())
				err = validateClusterIp(svc.Spec.ClusterIP, ipFamily)
				Expect(err).ToNot(HaveOccurred())

				By(iteratingClusterIPs)
				runJobsAgainstService(svc, vmObj.Namespace, tests.NewHelloWorldJobTCP)

				// Retrieve the current VMI UID, to be compared with the new UID after restart.
				vmi, err = virtClient.VirtualMachineInstance(vmObj.Namespace).Get(vmObj.Name, &k8smetav1.GetOptions{})
				Expect(err).ToNot(HaveOccurred())
				vmiUIdBeforeRestart := vmi.GetObjectMeta().GetUID()

				By("Restarting the running VM.")
				virtctl := clientcmd.NewRepeatableVirtctlCommand("restart", "--namespace", vmObj.Namespace, vmObj.Name)
				err = virtctl()
				Expect(err).ToNot(HaveOccurred())

				By("Verifying the VMI is back up AFTER restart (in Running status with new UID).")
				Eventually(func() bool {
					vmi, err = virtClient.VirtualMachineInstance(vmObj.Namespace).Get(vmObj.Name, &k8smetav1.GetOptions{})
					if errors.IsNotFound(err) {
						return false
					}
					Expect(err).ToNot(HaveOccurred())
					vmiUIdAfterRestart := vmi.GetObjectMeta().GetUID()
					newUId := vmiUIdAfterRestart != vmiUIdBeforeRestart
					return vmi.Status.Phase == v1.Running && newUId
				}, 120*time.Second, 1*time.Second).Should(BeTrue())

				By("Creating a TCP server on the VM.")
				tests.GenerateHelloWorldServer(vmi, testPort, "tcp", console.LoginToAlpine, false)

				By("Repeating the sequence as prior to restarting the VM: Connect to exposed ClusterIP service.")
				By(iteratingClusterIPs)
				runJobsAgainstService(svc, vmObj.Namespace, tests.NewHelloWorldJobTCP)
			},
				Entry("[test_id:345] over default IPv4 IP family", ipv4),
				Entry(overIPv6Family, ipv6),
				Entry(overDualStackIPv4, dualIPv4Primary),
				Entry(overDualStackIPv6, dualIPv6Primary),
			)
		})
	})
})

func getNodeHostname(nodeAddresses []k8sv1.NodeAddress) *string {
	for _, address := range nodeAddresses {
		if address.Type == k8sv1.NodeHostName {
			return &address.Address
		}
	}
	return nil
}

func resolveNodeIp(virtclient kubecli.KubevirtClient, pod *k8sv1.Pod, hostname string, ipFamily k8sv1.IPFamily) (string, error) {
	ahostsCmd := string("ahosts" + ipFamily[2:])
	output, err := exec.ExecuteCommandOnPod(
		virtclient,
		pod,
		"compute",
		[]string{"getent", ahostsCmd, hostname})

	if err != nil {
		return "", err
	}

	splitGetent := strings.Split(output, "\n")
	if len(splitGetent) > 0 {
		ip := strings.Split(splitGetent[0], " ")[0]
		return ip, nil
	}

	return "", fmt.Errorf("could not resolve an %s address from %s name", ipFamily, hostname)
}

func resolveNodeIPAddrByFamily(virtClient kubecli.KubevirtClient, sourcePod *k8sv1.Pod, node k8sv1.Node, ipFamily k8sv1.IPFamily) (string, error) {
	hostname := getNodeHostname(node.Status.Addresses)
	if hostname == nil {
		return "", fmt.Errorf("could not get node hostname")
	}
	return resolveNodeIp(virtClient, sourcePod, *hostname, ipFamily)
}


text:
Developer guides are intended to be human friendly documents which will help the developer
start using Nails, as well as be a thorough explanation of all functionality.

The Developer Documentation can be found [here](http://docs.nailsapp.co.uk).

## API Documentation

We generate Nail's API Documentation using [apigen](http://www.apigen.org/).

The API Documentation can be found [here](http://docs.nailsapp.co.uk/api).

### Installing apigen

	wget http://apigen.org/apigen.phar
	chmod +x apigen.phar
	mv apigen.phar /usr/local/bin/apigen
	apigen --version

### Generating Documentation

**Important:** Before generating or updating docs ensure that you have an up-to-date copy
*all* Nails modules. All repos should be consolidated under a single folder and not have
any other dependencies. A folder structure like this, for example:

	nailsapp/
	 - nails/
	 - common/
	 - module-admin/
	 - module-cdn/
	 - ...

Generate/update the API Documentation using the following command:

    apigen generate -s /path/to/nailsapp -d /path/to/site-docs/api --exclude common/CodeIgniter --template-theme bootstrap

**Note:** we are excluding the CodeIgniter files from documentation, for clarity.

redpajama_set_name: RedPajamaGithub


text:
#ifndef _ROC_DEV_PRIV_H
#define _ROC_DEV_PRIV_H

#define DEV_HWCAP_F_VF BIT_ULL(0) /* VF device */

#define RVU_PFVF_PF_SHIFT   10
#define RVU_PFVF_PF_MASK    0x3F
#define RVU_PFVF_FUNC_SHIFT 0
#define RVU_PFVF_FUNC_MASK  0x3FF
#define RVU_MAX_VF	    64 /* RVU_PF_VFPF_MBOX_INT(0..1) */
#define RVU_MAX_INT_RETRY   3

/* PF/VF message handling timer */
#define VF_PF_MBOX_TIMER_MS (20 * 1000)

typedef struct {
/* 128 devices translate to two 64 bits dwords */
#define MAX_VFPF_DWORD_BITS 2
	uint64_t bits[MAX_VFPF_DWORD_BITS];
} dev_intr_t;

/* Link status update callback */
typedef void (*link_info_t)(void *roc_nix,
			    struct cgx_link_user_info *link);

/* PTP info callback */
typedef int (*ptp_info_t)(void *roc_nix, bool enable);

/* Link status get callback */
typedef void (*link_status_get_t)(void *roc_nix,
				  struct cgx_link_user_info *link);

struct dev_ops {
	link_info_t link_status_update;
	ptp_info_t ptp_info_update;
	link_status_get_t link_status_get;
};

#define dev_is_vf(dev) ((dev)->hwcap & DEV_HWCAP_F_VF)

static inline int
dev_get_vf(uint16_t pf_func)
{
	return (((pf_func >> RVU_PFVF_FUNC_SHIFT) & RVU_PFVF_FUNC_MASK) - 1);
}

static inline int
dev_get_pf(uint16_t pf_func)
{
	return (pf_func >> RVU_PFVF_PF_SHIFT) & RVU_PFVF_PF_MASK;
}

static inline int
dev_pf_func(int pf, int vf)
{
	return (pf << RVU_PFVF_PF_SHIFT) | ((vf << RVU_PFVF_FUNC_SHIFT) + 1);
}

static inline int
dev_is_afvf(uint16_t pf_func)
{
	return !(pf_func & ~RVU_PFVF_FUNC_MASK);
}

struct dev {
	uint16_t pf;
	int16_t vf;
	uint16_t pf_func;
	uint8_t mbox_active;
	bool drv_inited;
	uint64_t active_vfs[MAX_VFPF_DWORD_BITS];
	uintptr_t bar2;
	uintptr_t bar4;
	uintptr_t lmt_base;
	struct mbox mbox_local;
	struct mbox mbox_up;
	struct mbox mbox_vfpf;
	struct mbox mbox_vfpf_up;
	dev_intr_t intr;
	int timer_set; /* ~0 : no alarm handling */
	uint64_t hwcap;
	struct npa_lf npa;
	struct mbox *mbox;
	uint16_t maxvf;
	struct dev_ops *ops;
	void *roc_nix;
	void *roc_cpt;
	bool disable_shared_lmt; /* false(default): shared lmt mode enabled */
	const struct plt_memzone *lmt_mz;
} __plt_cache_aligned;

struct npa {
	struct plt_pci_device *pci_dev;
	struct dev dev;
} __plt_cache_aligned;

extern uint16_t dev_rclk_freq;
extern uint16_t dev_sclk_freq;

int dev_init(struct dev *dev, struct plt_pci_device *pci_dev);
int dev_fini(struct dev *dev, struct plt_pci_device *pci_dev);
int dev_active_vfs(struct dev *dev);

int dev_irq_register(struct plt_intr_handle *intr_handle,
		     plt_intr_callback_fn cb, void *data, unsigned int vec);
void dev_irq_unregister(struct plt_intr_handle *intr_handle,
			plt_intr_callback_fn cb, void *data, unsigned int vec);
int dev_irqs_disable(struct plt_intr_handle *intr_handle);

#endif /* _ROC_DEV_PRIV_H */


text:
'use strict';

var express = require('express'),
  logger = require('bunyan-request-logger'),
  noCache = require('connect-cache-control'),
  errorHandler = require('../error-handler.js'),
  log = logger(),
  app = express(),
  env = process.env,
  port = env.myapp_port || 3000,
  http = require('http'),
  server;

app.use( log.requestLogger() );

// Route to handle client side log messages.
//
// Counter to intuition, client side logging
// works best with GET requests.
//
// AJAX POST sends headers and body in two steps,
// which slows it down.
//
// This route prepends the cache-control
// middleware so that the browser always logs
// to the server instead of fetching a useless
// OK message from its cache.
app.get('/log', noCache,
    function logHandler(req, res) {

  // Since all requests are automatically logged,
  // all you need to do is send the response:
  res.send(200);
});

// Route that triggers a sample error:
app.get('/error', function createError(req,
    res, next) {
  var err = new Error('Sample error');
  err.status = 500;
  next(err);
});

// Route that triggers a sample error:
app.all('/*', errorHandler.httpError(404));

// Log request errors:
app.use( log.errorLogger() );

// Create the server object that we can pass
// in to the error handler:
server = http.createServer(app);

// Respond to errors and conditionally shut
// down the server. Pass in the server object
// so the error handler can shut it down
// gracefully:
app.use( errorHandler({server: server}) );

server.listen(port, function () {
  log.info('Listening on port ' + port);
});


text:
<?php
/**
 * Created by PhpStorm.
 * User: dimitar
 * Date: 31.10.17
 * Time: 08:13
 */

namespace SportMonks\API\Resources;

use SportMonks\API\Traits\Resource\Find;
use SportMonks\API\Traits\Utility\InitTrait;

/**
 * Class FixturesDate
 * @package SportMonks\API\Resources
 */
class FixturesDate extends ResourceAbstract
{
    use InitTrait;

    use Find {
        find as traitFind;
    }

    /**
     * @param \DateTime $date
     * @param array $params
     *
     * @return null|\stdClass
     * @throws \SportMonks\API\Exceptions\MissingParameterException
     */
    public function day(\DateTime $date, $params = [])
    {
        $this->setRoute('fixtures_date', 'fixtures/date/{date}');
        $this->setRouteParameters([
            'date' => $date->format('Y-m-d')
        ]);

        return $this->traitFind(null, $params, 'fixtures_date');
    }
}


text:
package com.google.common.collect;

import java.util.Random;

/**
 * Utility class for being able to seed a {@link Random} value with a passed
 * in seed from a benchmark parameter.
 *
 * TODO: Remove this class once Caliper has a better way.
 *
 * @author Nicholaus Shupe
 */
public final class SpecialRandom extends Random {
  public static SpecialRandom valueOf(String s) {
    return (s.length() == 0)
        ? new SpecialRandom()
        : new SpecialRandom(Long.parseLong(s));
  }

  private final boolean hasSeed;
  private final long seed;

  public SpecialRandom() {
    this.hasSeed = false;
    this.seed = 0;
  }

  public SpecialRandom(long seed) {
    super(seed);
    this.hasSeed = true;
    this.seed = seed;
  }

  @Override public String toString() {
    return hasSeed ? "(seed:" + seed : "(default seed)";
  }

  private static final long serialVersionUID = 0;
}


text:
import { getValue, getBidIdParameter } from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
const BIDDER_CODE = 'videoreach';
const ENDPOINT_URL = 'https://a.videoreach.com/hb/';
const GVLID = 547;

export const spec = {
  code: BIDDER_CODE,
  gvlid: GVLID,
  supportedMediaTypes: ['banner'],

  isBidRequestValid: function(bid) {
    return !!(bid.params.TagId);
  },

  buildRequests: function(validBidRequests, bidderRequest) {
    let data = {
      data: validBidRequests.map(function(bid) {
        return {
          TagId: getValue(bid.params, 'TagId'),
          adUnitCode: getBidIdParameter('adUnitCode', bid),
          bidId: getBidIdParameter('bidId', bid),
          bidderRequestId: getBidIdParameter('bidderRequestId', bid),
          auctionId: getBidIdParameter('auctionId', bid),
          transactionId: getBidIdParameter('transactionId', bid)
        }
      })
    };

    if (bidderRequest && bidderRequest.refererInfo) {
      data.referrer = bidderRequest.refererInfo.referer;
    }

    if (bidderRequest && bidderRequest.gdprConsent) {
      data.gdpr = {
        consent_string: bidderRequest.gdprConsent.consentString,
        consent_required: bidderRequest.gdprConsent.gdprApplies
      };
    }

    return {
      method: 'POST',
      url: ENDPOINT_URL,
      data: JSON.stringify(data)
    };
  },

  interpretResponse: function(serverResponse) {
    const bidResponses = [];
    serverResponse = serverResponse.body;

    if (serverResponse.responses) {
      serverResponse.responses.forEach(function (bid) {
        const bidResponse = {
          cpm: bid.cpm,
          width: bid.width,
          height: bid.height,
          currency: bid.currency,
          netRevenue: true,
          ttl: bid.ttl,
          ad: bid.ad,
          requestId: bid.bidId,
          creativeId: bid.creativeId,
          meta: {
            advertiserDomains: bid && bid.adomain ? bid.adomain : []
          }
        };
        bidResponses.push(bidResponse);
      });
    }
    return bidResponses;
  },

  getUserSyncs: function(syncOptions, responses, gdprConsent) {
    const syncs = [];

    if (responses.length && responses[0].body.responses.length) {
      let params = '';
      var gdpr;

      if (gdprConsent && typeof gdprConsent.consentString === 'string') {
        if (typeof gdprConsent.gdprApplies === 'boolean') {
          params += 'gdpr=' + gdprConsent.gdprApplies + '&gdpr_consent=' + gdprConsent.consentString;
        } else {
          params += 'gdpr_consent=' + gdprConsent.consentString;
        }
      }

      if (syncOptions.pixelEnabled) {
        const SyncPixels = responses[0].body.responses[0].sync;

        if (SyncPixels) {
          SyncPixels.forEach(sync => {
            gdpr = (params) ? ((sync.split('?')[1] ? '&' : '?') + params) : '';

            syncs.push({
              type: 'image',
              url: sync + gdpr
            });
          });
        }
      }

      if (syncOptions.iframeEnabled) {
        const SyncFrame = responses[0].body.responses[0].syncframe;

        if (SyncFrame) {
          SyncFrame.forEach(sync => {
            gdpr = (params) ? ((sync.split('?')[1] ? '&' : '?') + params) : '';

            syncs.push({
              type: 'iframe',
              url: sync + gdpr
            });
          });
        }
      }
    }

    return syncs;
  }
};

registerBidder(spec);


text:
using namespace llvm;

#define DEBUG_TYPE "loop-accesses"

PreservedAnalyses
LoopAccessInfoPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
                               LoopStandardAnalysisResults &AR, LPMUpdater &) {
  Function &F = *L.getHeader()->getParent();
  auto &LAI = AM.getResult<LoopAccessAnalysis>(L, AR);
  OS << "Loop access info in function '" << F.getName() << "':\n";
  OS.indent(2) << L.getHeader()->getName() << ":\n";
  LAI.print(OS, 4);
  return PreservedAnalyses::all();
}


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Napack.Analyst.ApiSpec;
using Napack.Common;

namespace Napack.Analyst
{
    /// <summary>
    /// Describes how to analyze Napacks to determine their public API and properly enforce semantic versioning.
    /// </summary>
    /// <remarks>
    /// The "Public API" in Napack terms is anything a consumer could use to reference the napack, without using C# reflection.
    /// Additional details on the restrictions imposed by the Napack system are listed in the documentation and will be called out in the function signatures below.
    ///
    /// A naive implementation of this analyst could compile the provided Napack files into an in-memory DLL and use C# reflection itself to validate the Napack.
    /// However, the naive implementation has several blocking problems:
    /// - What default C# assemblies should be included in the compilation process?
    /// - How many dependent Napacks are required (hint -- may be several) to compile the provided Napacks?
    /// - How do we detect files that aren't scheduled for compilation?
    /// - How easy is it to reflect compilation failures to the end-user? How secure is running the compilation process server-side?
    ///
    /// Another implementation would be to write a custom parser/lexer as per the C# Language Specifications (https://www.microsoft.com/en-us/download/details.aspx?id=7029),
    ///  instead of relying on C# reflection or compilation. While conceptually simple, implementing that idea is quite a lot of work and effectively duplicates the Roslyn API!
    ///
    /// This implementation uses the Roslyn API to analyze Napack files *without* compilation to focus on providing meaningful Napack restrictions without reimplementing C# parsing.
    /// </remarks>
    public static class NapackAnalyst
    {
        private static PackageValidationConfig PackageValidationConfig = null;

        public enum UpversionType
        {
            Major = 0,
            Minor = 1,
            Patch = 2
        }

        /// <summary>
        /// Initializes the static configuration for this analyzer.
        /// </summary>
        public static void Initialize(string packageConfigFilePath, string nameConfigFilePath)
        {
            NapackAnalyst.PackageValidationConfig = Serializer.Deserialize<PackageValidationConfig>(File.ReadAllText(packageConfigFilePath));
            NapackAnalyst.NameValidationConfig = Serializer.Deserialize<NameValidationConfig>(File.ReadAllText(nameConfigFilePath));
        }

        public static NameValidationConfig NameValidationConfig { get; private set; }

        /// <summary>
        /// Creates a Napack spec for the defined napack files.
        /// </summary>
        /// <param name="napackName">The Napack package name.</param>
        /// <param name="napackFiles">A mapping of file path + name -> file itself.</param>
        /// <remarks>
        /// Restrictions:
        /// - All Napack files with MSBuild type <see cref="NapackFile.CompileType"/> must be able to be analyzed by this system.
        /// - The namespace of all analyzable-files must be the specified napack name (CASE-SENSITIVE, as per C# spec).
        /// - Although C# allows multiple namespaces in a single file, as they all must be the specified napack name doing so is prohibited.
        /// - Partial classes are disallowed as any non-code files won't have their namespace updated. (This may be changed to better support UI elements, etc in the future).
        /// </remarks>
        /// <exception cref="InvalidNapackFileExtensionException">If a Napack file contains a prohibited extension.</exception>
        /// <exception cref="InvalidNamespaceException">If a compilable Napack file is in the wrong namespace.</exception>
        /// <exception cref="InvalidNapackFileException">If a Napack file is listed with MSBuild type <see cref="NapackFile.CompileType"/>, but could not be analyzed.</exception>
        /// <exception cref="UnsupportedNapackFileException">If a Napack file uses C# functionality or syntax that the Napack system explicitly prohibits.</exception>
        public static NapackSpec CreateNapackSpec(string napackName, IDictionary<string, NapackFile> napackFiles)
        {
            NapackAnalyst.NameValidationConfig.Validate(napackName);

            NapackSpec spec = new NapackSpec();
            foreach (KeyValuePair<string, NapackFile> fileEntry in napackFiles)
            {
                // Validate the extension.
                string filename = Path.GetFileName(fileEntry.Key);
                string extension = Path.GetExtension(fileEntry.Key);
                if (!string.IsNullOrWhiteSpace(extension))
                {
                    NapackAnalyst.PackageValidationConfig.ValidateExtension(filename, extension);
                }

                if (fileEntry.Value.MsbuildType.Equals(NapackFile.CompileType, StringComparison.InvariantCultureIgnoreCase))
                {
                    NapackSpec specForSingleFile = NapackFileAnalyzer.Analyze(napackName, filename, fileEntry.Value.Contents);
                    spec.Classes.AddRange(specForSingleFile.Classes);
                    spec.Interfaces.AddRange(specForSingleFile.Interfaces);
                }
                else
                {
                    spec.UnknownFiles.Add(fileEntry.Value);
                }
            }

            return spec;
        }

        /// <summary>
        /// Determines the requried upversioning when code transitions from the old Napack to the new Napack.
        /// </summary>
        /// <remarks>
        /// Major == breaking changes.
        /// Minor == publically-facing API was added (but not changed or removed) from the new Napack.
        /// Patch == The publically-facing API is identical between both Napacks, excluding documentation.
        /// </remarks>
        public static UpversionType DeterminedRequiredUpversioning(NapackSpec oldNapack, NapackSpec newNapack)
        {
            UpversionType maxUpversionType = UpversionType.Patch;
            foreach (ClassSpec oldClassSpec in oldNapack.Classes)
            {
                ClassSpec newClassSpec = newNapack.Classes
                    .FirstOrDefault(cls => cls.Name.Name.Equals(oldClassSpec.Name.Name, StringComparison.InvariantCulture));
                UpversionType upversionType = NapackAnalyst.AnalyzeClass(oldClassSpec, newClassSpec);
                if (upversionType == UpversionType.Major)
                {
                    // Exit early, as we found a breaking change.
                    return UpversionType.Major;
                }
                else if (upversionType == UpversionType.Minor)
                {
                    maxUpversionType = UpversionType.Minor;
                }
            }

            foreach (InterfaceSpec oldInterfaceSpec in oldNapack.Interfaces)
            {
                InterfaceSpec newInterfaceSpec = newNapack.Interfaces
                    .FirstOrDefault(cls => cls.Name.Name.Equals(oldInterfaceSpec.Name.Name, StringComparison.InvariantCulture));
                UpversionType upversionType = NapackAnalyst.AnalyzeInterface(oldInterfaceSpec, newInterfaceSpec);
                if (upversionType == UpversionType.Major)
                {
                    // Exit early, as we found a breaking change.
                    return UpversionType.Major;
                }
                else if (upversionType == UpversionType.Minor)
                {
                    maxUpversionType = UpversionType.Minor;
                }
            }

            // No APIs added in the prior classes. Did the new package add classes?
            if (maxUpversionType == UpversionType.Patch && (newNapack.Classes.Count != oldNapack.Classes.Count || newNapack.Interfaces.Count != oldNapack.Interfaces.Count))
            {
                maxUpversionType = UpversionType.Minor;
            }

            return maxUpversionType;
        }

        private static UpversionType AnalyzeInterface(InterfaceSpec oldInterfaceSpec, InterfaceSpec newInterfaceSpec)
        {
            UpversionType maxUpversionType = UpversionType.Patch;
            if (newInterfaceSpec == null)
            {
                // The new spec removed a publically facing class.
                return UpversionType.Major;
            }

            UpversionType upversionType = NapackAnalyst.AnalyzeProperties(oldInterfaceSpec.Properties, newInterfaceSpec.Properties);
            if (upversionType == UpversionType.Major)
            {
                return upversionType;
            }
            else if (upversionType == UpversionType.Minor)
            {
                maxUpversionType = UpversionType.Minor;
            }

            upversionType = NapackAnalyst.AnalyzeMethods(oldInterfaceSpec.Methods, newInterfaceSpec.Methods);
            if (upversionType == UpversionType.Major)
            {
                return upversionType;
            }
            else if (upversionType == UpversionType.Minor)
            {
                maxUpversionType = UpversionType.Minor;
            }

            return maxUpversionType;
        }

        private static UpversionType AnalyzeClass(ClassSpec oldClassSpec, ClassSpec newClassSpec)
        {
            UpversionType maxUpversionType = UpversionType.Patch;
            if (newClassSpec == null)
            {
                // The new spec removed a publically facing class.
                return UpversionType.Major;
            }

            // Check if any modifier changes force a major (breaking) change.
            if ((!oldClassSpec.IsAbstract && newClassSpec.IsAbstract) ||
                (oldClassSpec.IsStatic != newClassSpec.IsStatic) ||
                (!oldClassSpec.IsSealed && newClassSpec.IsSealed))
            {
                return UpversionType.Major;
            }

            // Verify all old fields exist
            UpversionType upversionType = NapackAnalyst.AnalyzeFields(oldClassSpec.PublicFields, newClassSpec.PublicFields);
            if (upversionType == UpversionType.Major)
            {
                return upversionType;
            }
            else if (upversionType == UpversionType.Minor)
            {
                maxUpversionType = UpversionType.Minor;
            }

            upversionType = NapackAnalyst.AnalyzeProperties(oldClassSpec.PublicProperties, newClassSpec.PublicProperties);
            if (upversionType == UpversionType.Major)
            {
                return upversionType;
            }
            else if (upversionType == UpversionType.Minor)
            {
                maxUpversionType = UpversionType.Minor;
            }

            upversionType = NapackAnalyst.AnalyzeConstructors(oldClassSpec.PublicConstructors, newClassSpec.PublicConstructors);
            if (upversionType == UpversionType.Major)
            {
                return upversionType;
            }
            else if (upversionType == UpversionType.Minor)
            {
                maxUpversionType = UpversionType.Minor;
            }

            upversionType = NapackAnalyst.AnalyzeMethods(oldClassSpec.PublicMethods, newClassSpec.PublicMethods);
            if (upversionType == UpversionType.Major)
            {
                return upversionType;
            }
            else if (upversionType == UpversionType.Minor)
            {
                maxUpversionType = UpversionType.Minor;
            }

            // Analyze classes recursively.
            foreach (ClassSpec oldClass in oldClassSpec.PublicClasses)
            {
                ClassSpec newClass = newClassSpec.PublicClasses
                    .FirstOrDefault(cls => cls.Name.Name.Equals(oldClass.Name.Name, StringComparison.InvariantCulture));
                upversionType = NapackAnalyst.AnalyzeClass(oldClass, newClass);
                if (upversionType == UpversionType.Major)
                {
                    return upversionType;
                }
                else if (upversionType == UpversionType.Minor)
                {
                    maxUpversionType = UpversionType.Minor;
                }
            }

            if (maxUpversionType == UpversionType.Patch && oldClassSpec.PublicClasses.Count != newClassSpec.PublicClasses.Count)
            {
                maxUpversionType = UpversionType.Minor;
            }

            return maxUpversionType;
        }

        private static UpversionType AnalyzeFields(IList<FieldSpec> oldFields, IList<FieldSpec> newFields)
        {
            foreach (FieldSpec oldField in oldFields)
            {
                FieldSpec newField = newFields
                    .FirstOrDefault(field => field.Name.Name.Equals(oldField.Name.Name, StringComparison.InvariantCulture));
                if (newField == null)
                {
                    return UpversionType.Major;
                }

                // Verify types are equal. This will false-positive on string/String conversions, but that's a minor issue for later consideration.
                if (!oldField.Type.Equals(newField.Type, StringComparison.InvariantCulture))
                {
                    return UpversionType.Major;
                }

                // Validate modifiers.
                if ((oldField.IsUserModifiable && !newField.IsUserModifiable) ||
                    (oldField.IsStatic != newField.IsStatic))
                {
                    return UpversionType.Major;
                }
            }

            if (newFields.Count != oldFields.Count)
            {
                return UpversionType.Minor;
            }

            return UpversionType.Patch;
        }

        private static UpversionType AnalyzeProperties(IList<PropertySpec> oldProperties, IList<PropertySpec> newProperties)
        {
            foreach (PropertySpec oldProperty in oldProperties)
            {
                PropertySpec newProperty = newProperties
                    .FirstOrDefault(prop => prop.Name.Name.Equals(oldProperty.Name.Name, StringComparison.InvariantCulture));
                if (newProperty == null)
                {
                    return UpversionType.Major;
                }

                // Verify types are equal. This will false-positive on string/String conversions, but that's a minor issue for later consideration.
                if (!oldProperty.Type.Equals(newProperty.Type, StringComparison.InvariantCulture))
                {
                    return UpversionType.Major;
                }

                // Validate modifier
                if (oldProperty.IsStatic != newProperty.IsStatic)
                {
                    return UpversionType.Major;
                }
            }

            if (newProperties.Count != oldProperties.Count)
            {
                return UpversionType.Minor;
            }

            return UpversionType.Patch;
        }

        private static UpversionType AnalyzeConstructors(IList<ConstructorSpec> oldConstructors, IList<ConstructorSpec> newConstructors)
        {
            UpversionType maxUpversionType = UpversionType.Patch;
            foreach (ConstructorSpec oldConstructor in oldConstructors)
            {
                // Constructor matching is complicated as all constructors have the same name, but different parameters.
                // For now, perform an inefficient n^2 matching by comparing each old constructor against each new one.
                bool anyWithoutMajorChanges = false;
                foreach (ConstructorSpec newConstructor in newConstructors)
                {
                    if (!NapackAnalyst.AnayzeParameters(oldConstructor.Parameters, newConstructor.Parameters))
                    {
                        anyWithoutMajorChanges = true;
                        break;
                    }
                }

                if (!anyWithoutMajorChanges)
                {
                    return UpversionType.Major;
                }
            }

            if (maxUpversionType == UpversionType.Patch && newConstructors.Count != oldConstructors.Count)
            {
                return UpversionType.Minor;
            }

            return maxUpversionType;
        }

        private static UpversionType AnalyzeMethods(IList<MethodSpec> oldMethods, IList<MethodSpec> newMethods)
        {
            UpversionType maxUpversionType = UpversionType.Patch;
            foreach (MethodSpec oldMethod in oldMethods)
            {
                MethodSpec newMethod = newMethods
                    .FirstOrDefault(field => field.Name.Name.Equals(oldMethod.Name.Name, StringComparison.InvariantCulture));
                if (newMethod == null)
                {
                    return UpversionType.Major;
                }

                if (!newMethod.ReturnType.Equals(oldMethod.ReturnType, StringComparison.InvariantCulture))
                {
                    return UpversionType.Major;
                }

                // Check parameters
                bool isMajorChange = NapackAnalyst.AnayzeParameters(oldMethod.Parameters, newMethod.Parameters);
                if (isMajorChange)
                {
                    return UpversionType.Major;
                }
            }

            if (maxUpversionType == UpversionType.Patch && newMethods.Count != oldMethods.Count)
            {
                return UpversionType.Minor;
            }

            return maxUpversionType;
        }

        /// <summary>
        /// Returns true on a breaking change (MAJOR), false otherwise (PATCH)
        /// </summary>
        private static bool AnayzeParameters(List<ParameterSpec> oldParameters, List<ParameterSpec> newParameters)
        {
            if (newParameters.Count != oldParameters.Count)
            {
                return true;
            }

            // If parameter ordering changes, that's a breaking change.
            for (int i = 0; i < oldParameters.Count; i++)
            {
                if (!oldParameters[i].Name.Equals(newParameters[i].Name, StringComparison.InvariantCulture) ||
                    !oldParameters[i].Type.Equals(newParameters[i].Type, StringComparison.InvariantCulture) ||
                    !oldParameters[i].Modifier.Equals(newParameters[i].Modifier, StringComparison.InvariantCulture) ||
                    !oldParameters[i].Default.Equals(newParameters[i].Default, StringComparison.InvariantCulture))
                {
                    return true;
                }
            }

            return false;
        }
    }
}


text:
package org.apache.activemq.jmdns;

// REMIND: Listener should follow Java idiom for listener or have a different
//         name.

/**
 * DNSListener.
 * Listener for record updates.
 *
 * @author Werner Randelshofer, Rick Blair
 * @version 1.0  May 22, 2004  Created.
 */
interface DNSListener
{
    /**
     * Update a DNS record.
     */
    void updateRecord(JmDNS jmdns, long now, DNSRecord record);
}


text:
<?php
/**
 * FormModelBehavior class file.
 *
 * @author Jan Was <jwas@nets.com.pl>
 */

/**
 * FormModelBehavior is a base class for behaviors that are attached to a form model component.
 * The model should extend from {@link CFormModel} or its child classes.
 *
 * @property CFormModel $owner The owner model that this behavior is attached to.
 *
 * @author Jan Was <jwas@nets.com.pl>
 */
abstract class FormModelBehavior extends CModelBehavior
{
	private static $_names=array();

	private $_ruleOptions = array();

	/**
	 * Validation rules for attributes of this behavior, that should be merged with rules in the owner model.
	 * @return array validation rules
	 * @see CModel::rules()
	 */
	public function rules()
	{
		return array();
	}

	/**
	 * Labels for attributes of this behavior, that should be merged with labels in the owner model.
	 * @return array attribute labels (name => label)
	 * @see CModel::attributeLabels()
	 */
	public function attributeLabels()
	{
		return array();
	}

	/**
	 * Returns the list of attribute names.
	 * By default, this method returns all public non-static properties of the class.
	 * You may override this method to change the default behavior.
	 * @return array list of attribute names.
	 */
	public function attributeNames()
	{
		$className=get_class($this);
		if(!isset(self::$_names[$className]))
		{
			$class=new ReflectionClass(get_class($this));
			$names=array();
			foreach($class->getProperties() as $property)
			{
				$name=$property->getName();
				if($property->isPublic() && !$property->isStatic())
					$names[]=$name;
			}
			return self::$_names[$className]=$names;
		}
		else
			return self::$_names[$className];
	}

	/**
	 * Adds current rule options to the given set of rules.
	 * @param array $rules
	 * @return array
	 */
	public function applyRuleOptions($rules)
	{
		foreach($rules as $key=>$rule) {
			foreach($this->_ruleOptions as $name=>$value) {
				$rules[$key][$name] = $value;
			}
		}
		return $rules;
	}

	/**
	 * @return array
	 */
	public function getRuleOptions()
	{
		return $this->_ruleOptions;
	}

	/**
	 * @param $value array
	 */
	public function setRuleOptions(array $value)
	{
		$this->_ruleOptions = $value;
	}
}


text:
module.exports = {
    createPlayerTable : `CREATE TABLE IF NOT EXISTS
        players (
            assists INTEGER,
            faceoffWinPctg NUMERIC,
            gameWinningGoals INTEGER,
            gamesPlayed INTEGER,
            goals INTEGER,
            otGoals INTEGER,
            penaltyMinutes NUMERIC,
            playerBirthCity TEXT,
            playerBirthCountry TEXT,
            playerBirthDate TEXT,
            playerBirthStateProvince TEXT,
            playerDraftOverallPickNo INTEGER,
            playerDraftRoundNo INTEGER,
            playerDraftYear INTEGER,
            playerFirstName TEXT,
            playerHeight NUMERIC,
            playerId INTEGER,
            playerInHockeyHof INTEGER,
            playerLastName TEXT,
            playerName TEXT,
            playerNationality TEXT,
            playerPositionCode TEXT,
            playerShootsCatches TEXT,
            playerTeamsPlayedFor TEXT,
            playerWeight NUMERIC,
            plusMinus INTEGER,
            points INTEGER,
            pointsPerGame NUMERIC,
            ppGoals INTEGER,
            ppPoints INTEGER,
            seasonId TEXT,
            shGoals INTEGER,
            shPoints INTEGER,
            shiftsPerGame NUMERIC,
            shootingPctg NUMERIC,
            shots INTEGER,
            timeOnIcePerGame NUMERIC,
            timeOnIce NUMERIC,

            gamesStarted INTEGER,
            goalsAgainst INTEGER,
            goalsAgainstAverage NUMERIC,
            losses INTEGER,
            otLosses INTEGER,
            savePctg NUMERIC,
            saves INTEGER,
            shotsAgainst INTEGER,
            shutouts INTEGER,
            ties INTEGER,
            wins INTEGER,

            lastUpdated INTEGER,
            active INTEGER,
            injury TEXT
        )`,
        findPlayer : 'SELECT * FROM players WHERE playerId = ?',
        findAll: 'SELECT * FROM players'

};


text:
<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: google/cloud/video/livestream/v1/service.proto

namespace Google\Cloud\Video\LiveStream\V1;

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;

/**
 * Request message for "LivestreamService.ListChannels".
 *
 * Generated from protobuf message google.cloud.video.livestream.v1.ListChannelsRequest
 */
class ListChannelsRequest extends \Google\Protobuf\Internal\Message
{
    /**
     * Required. The parent location for the resource, in the form of:
     * `projects/{project}/locations/{location}`.
     *
     * Generated from protobuf field string parent = 1 [(.google.api.field_behavior) = REQUIRED, (.google.api.resource_reference) = {
     */
    private $parent = '';
    /**
     * The maximum number of items to return. If unspecified, server
     * will pick an appropriate default. Server may return fewer items than
     * requested. A caller should only rely on response's
     * [next_page_token][google.cloud.video.livestream.v1.ListChannelsResponse.next_page_token] to
     * determine if there are more items left to be queried.
     *
     * Generated from protobuf field int32 page_size = 2;
     */
    private $page_size = 0;
    /**
     * The next_page_token value returned from a previous List request, if any.
     *
     * Generated from protobuf field string page_token = 3;
     */
    private $page_token = '';
    /**
     * The filter to apply to list results.
     *
     * Generated from protobuf field string filter = 4;
     */
    private $filter = '';
    /**
     * Specifies the ordering of results following syntax at
     * https://cloud.google.com/apis/design/design_patterns#sorting_order.
     *
     * Generated from protobuf field string order_by = 5;
     */
    private $order_by = '';

    /**
     * Constructor.
     *
     * @param array $data {
     *     Optional. Data for populating the Message object.
     *
     *     @type string $parent
     *           Required. The parent location for the resource, in the form of:
     *           `projects/{project}/locations/{location}`.
     *     @type int $page_size
     *           The maximum number of items to return. If unspecified, server
     *           will pick an appropriate default. Server may return fewer items than
     *           requested. A caller should only rely on response's
     *           [next_page_token][google.cloud.video.livestream.v1.ListChannelsResponse.next_page_token] to
     *           determine if there are more items left to be queried.
     *     @type string $page_token
     *           The next_page_token value returned from a previous List request, if any.
     *     @type string $filter
     *           The filter to apply to list results.
     *     @type string $order_by
     *           Specifies the ordering of results following syntax at
     *           https://cloud.google.com/apis/design/design_patterns#sorting_order.
     * }
     */
    public function __construct($data = NULL) {
        \GPBMetadata\Google\Cloud\Video\Livestream\V1\Service::initOnce();
        parent::__construct($data);
    }

    /**
     * Required. The parent location for the resource, in the form of:
     * `projects/{project}/locations/{location}`.
     *
     * Generated from protobuf field string parent = 1 [(.google.api.field_behavior) = REQUIRED, (.google.api.resource_reference) = {
     * @return string
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Required. The parent location for the resource, in the form of:
     * `projects/{project}/locations/{location}`.
     *
     * Generated from protobuf field string parent = 1 [(.google.api.field_behavior) = REQUIRED, (.google.api.resource_reference) = {
     * @param string $var
     * @return $this
     */
    public function setParent($var)
    {
        GPBUtil::checkString($var, True);
        $this->parent = $var;

        return $this;
    }

    /**
     * The maximum number of items to return. If unspecified, server
     * will pick an appropriate default. Server may return fewer items than
     * requested. A caller should only rely on response's
     * [next_page_token][google.cloud.video.livestream.v1.ListChannelsResponse.next_page_token] to
     * determine if there are more items left to be queried.
     *
     * Generated from protobuf field int32 page_size = 2;
     * @return int
     */
    public function getPageSize()
    {
        return $this->page_size;
    }

    /**
     * The maximum number of items to return. If unspecified, server
     * will pick an appropriate default. Server may return fewer items than
     * requested. A caller should only rely on response's
     * [next_page_token][google.cloud.video.livestream.v1.ListChannelsResponse.next_page_token] to
     * determine if there are more items left to be queried.
     *
     * Generated from protobuf field int32 page_size = 2;
     * @param int $var
     * @return $this
     */
    public function setPageSize($var)
    {
        GPBUtil::checkInt32($var);
        $this->page_size = $var;

        return $this;
    }

    /**
     * The next_page_token value returned from a previous List request, if any.
     *
     * Generated from protobuf field string page_token = 3;
     * @return string
     */
    public function getPageToken()
    {
        return $this->page_token;
    }

    /**
     * The next_page_token value returned from a previous List request, if any.
     *
     * Generated from protobuf field string page_token = 3;
     * @param string $var
     * @return $this
     */
    public function setPageToken($var)
    {
        GPBUtil::checkString($var, True);
        $this->page_token = $var;

        return $this;
    }

    /**
     * The filter to apply to list results.
     *
     * Generated from protobuf field string filter = 4;
     * @return string
     */
    public function getFilter()
    {
        return $this->filter;
    }

    /**
     * The filter to apply to list results.
     *
     * Generated from protobuf field string filter = 4;
     * @param string $var
     * @return $this
     */
    public function setFilter($var)
    {
        GPBUtil::checkString($var, True);
        $this->filter = $var;

        return $this;
    }

    /**
     * Specifies the ordering of results following syntax at
     * https://cloud.google.com/apis/design/design_patterns#sorting_order.
     *
     * Generated from protobuf field string order_by = 5;
     * @return string
     */
    public function getOrderBy()
    {
        return $this->order_by;
    }

    /**
     * Specifies the ordering of results following syntax at
     * https://cloud.google.com/apis/design/design_patterns#sorting_order.
     *
     * Generated from protobuf field string order_by = 5;
     * @param string $var
     * @return $this
     */
    public function setOrderBy($var)
    {
        GPBUtil::checkString($var, True);
        $this->order_by = $var;

        return $this;
    }

}


text:
me.Trap = function (attributes, map, owner) {
  var properties = me.merge({
        pos: {x: 0, y: 0},
        tileIndex: 120,
        size: 1,
        duration: 4
      }, attributes),
      events = me.Events()
  ;

  function processTurn() {
    var a = map.actors.collision(properties.pos.x, properties.pos.y);
    properties.duration--;
    if (properties.duration <= 0) {
      events.emit('Kill');
      return false;
    }

    events.emit('Turn');

    if (a !== false && a !== owner) {
      a.kill();
      events.emit('Kill');
      return;
    }

    return true;
  }

  return {
    on: events.on,
    properties: properties,
    processTurn: processTurn,
    pos: function() { return properties.pos; }
  }
};


text:
package com.tvd12.ezyfox.sfs2x.testing.data;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSDataType;
import com.smartfoxserver.v2.entities.data.SFSDataWrapper;
import com.tvd12.ezyfox.core.annotation.ResponseParam;
import com.tvd12.ezyfox.sfs2x.data.impl.SimpleTransformer;
import com.tvd12.ezyfox.sfs2x.testing.command.BaseCommandTest2;
import com.tvd12.ezyfox.sfs2x.testing.context.PokerResult;

import lombok.Data;

/**
 * @author tavandung12
 * Created on Jul 5, 2016
 *
 */
public class SimpleTransformer2Test extends BaseCommandTest2 {

    @Test
    public void test() {
        SimpleTransformer transformer = new SimpleTransformer(context);
        SFSDataWrapper wrapper = transformer.transform(new PokerResult());
        ISFSObject sfsobject = (ISFSObject)wrapper.getObject();
        assertEquals(wrapper.getTypeId(), SFSDataType.SFS_OBJECT);
        assertEquals(sfsobject.getUtfString("name"), "name");

        wrapper = transformer.transform(new Result());
        sfsobject = (ISFSObject)wrapper.getObject();
        assertEquals(wrapper.getTypeId(), SFSDataType.SFS_OBJECT);
        assertEquals(sfsobject.getUtfString("2"), "r");
    }

    @Data
    public static class Result {
        @ResponseParam("1")
        private int id = 1;
        @ResponseParam("2")
        private String name = "r";
    }

}


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>{% block title %}Purgatory{% endblock %}</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    {% block head %}{% endblock %}

      <button class="navbar-toggler navbar-toggler-right hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">

      </button>
      Purgatory

            Home (current)

    {% block main %}{% endblock %}


text:
While some applications target a single timezone, others target
many different timezones. To satisfy such needs and centralize datetime
operations, ABP provides a common infrastructure for datetime operations.

### Clock

**Clock** is the main class used to work with **DateTime** values. It
defines the following **static** properties and methods:

-   **Now**: Gets the current time according to the current provider.
-   **Kind**: Gets the DateTimeKind of the current provider.
-   **SupportsMultipleTimezone**: Gets a value that indicates whether or not a current
    provider can be used for applications that need multiple timezones.
-   **Normalize**: Normalizes/converts a given DateTime using the current
    provider.

So, instead of using *DateTime.Now*, we use **Clock.Now**, which
abstracts it:

    DateTime now = Clock.Now;

Clock uses the clock providers inside it. There are three types of
**built-in clock providers**:

-   **ClockProviders.Unspecified** (UnspecifiedClockProvider): This is
    the **default** clock provider and behaves just like
    **DateTime.Now**. It acts as if you don't use the Clock class at all.
-   **ClockProviders.Utc** (UtcClockProvider): Works in UTC datetime.
    **DateTime.UtcNow** for Clock.Now. The Normalize method converts a given
    datetime to a utc datetime and then sets it's kind to DateTimeKind.UTC. It
    **supports multiple timezones**.
-   **ClockProviders.Local** (LocalClockProvider): Works in the local
    computer's time. The Normalize method converts a given datetime to a local
    datetime and sets it's kind to DateTimeKind.Local.

You can set Clock.Provider in order to use a different clock provider:

    Clock.Provider = ClockProviders.Utc;

This is generally done at the beginning of an application (do
it in the Application\_Start of a web application).

#### Client-Side

The clock can be used on the client-side using the **abp.clock** object in
JavaScript. When you set Clock.Provider on the server-side, ABP
automatically sets the value of **abp.clock.provider** on the client-side.

### Time Zones

ABP defines a [setting](Setting-Management.md) named
**Abp.Timing.TimeZone** (*TimingSettingNames.TimeZone* constant) for
storing the selected timezone of the host, tenant and user. ABP assumes that
the value of a timezone setting is a valid **Windows timezone name**. It also
defines a timezone mapping file to convert a Windows Timezone to an
**IANA** timezone since some common libraries are using the IANA
timezone id. **UtcClockProvider** must be used in order to support
**multiple timezones**. Because if UtcClockProvider is used, all
datetime values will be stored in UTC and all datetimes will be sent to
clients in UTC format. Then on the client-side we can convert a UTC
datetime to the user's timezone by using the user's current timezone setting.

#### Client-Side

ABP creates a JavaScript object named **abp.timing.timeZoneInfo** which
contains timezone information for the current user. This information
contains Windows and IANA timezone ids and some extra information for
windows timezone info. This information can be used to make client-side
datetime convertions by showing a datetime to the user in his/her timezone.

### Binders and Converters

-   ABP automatically normalizes DateTimes received (in an input class) from **clients** in
    MVC, Web API and ASP.NET Core applications, based on the current
    clock provider.
-   ABP automatically normalizes DateTimes received from the **database**
    based on the current clock provider, when
    [EntityFramework](EntityFramework-Integration.md) or
    [NHibernate](NHibernate-Integration.md) modules are used.

If the UTC clock provider is used, then all DateTimes stored in the database are
assumed as UTC values, and all DateTimes received from clients are assumed
as UTC values unless explicitly specified.


text:
On the *Data* directory, you can find:

* **md5.txt**: MD5 of unique samples (2012-2015).
* **md5.2017.txt**: MD5 of unique samples (2012-2017).
* **hash.names.txt**: MD5-names of many samples.
* **ext.txt**: Sample's extensions.
* **ftypes.txt**: Sample's filetypes.
* **top_packers.txt**: All identified packers **without** filters.
* **top_packers_parsed.txt**: Counting identified packer **without** filters.
* **av_total.txt**: AV detection for each sample.
* **cpl.txt**: Samples which presented itself as *CPL*.
* **run_keys.txt**: Registry *\Run* key writes **without** filters.
* **dotnet.txt**: Samples which presented *.Net* characteristics.
* **peframe.db**: PEframe database.
* **js.txt**: MD5 hashes of collected JS files.
* **js.deobfuscated.txt**: MD5 hashes of deobfuscated JS files.
* **vbe.txt**: MD5 hashes of VBE (encoded) files.
* **vbs.txt**: MD5 hashes of VBS (decoded) files.
* **jar.txt**: MD5 hashes of JAR files.
* **jar.classes.txt**: MD5 hashes of Java classes (source) files.

### Using the database

The database has the following schema:

```
CREATE TABLE short (directory TEXT, pefversion NUMERIC, datetime TEXT, id INTEGER PRIMARY KEY, filename TEXT, filesize TEXT, timestamp TEXT, dll TEXT, sections NUMERIC, md5 TEXT, sha1 TEXT, imphash TEXT, packer TEXT, antidbg TEXT, antivm TEXT);
CREATE TABLE show_antidbg (id NUMERIC, name TEXT);
CREATE TABLE show_antivm (id NUMERIC, name TEXT);
CREATE TABLE show_apisuspicious (id NUMERIC, name TEXT);
CREATE TABLE show_meta (id NUMERIC, meta TEXT);
CREATE TABLE show_secsuspicious (sha1 TEXT, md5 TEXT, id NUMERIC, name TEXT);
CREATE TABLE show_packer (name TEXT, id NUMERIC);
CREATE TABLE show_file (id NUMERIC, file TEXT);
CREATE TABLE show_url (id NUMERIC, url TEXT);
```

So you can perform queries such as:

```
sqlite> select samples.md5,anti.name from short as samples,show_antivm as anti where samples.id==anti.id and anti.name LIKE '%QEmu%';
```

Getting the result:

```
c112e9bcd913f6e627beee6027864c6d|Bochs & QEmu CPUID Trick
01a2117f1bddf31fefd5274a196e6866|Bochs & QEmu CPUID Trick
0411910dc4d4db20011b0e07c1d19808|Bochs & QEmu CPUID Trick
2a31aefcb399cbba85aba0d938d13dd2|Bochs & QEmu CPUID Trick
2ef5caa75e0ea93e5a05ac2f6d73aeda|Bochs & QEmu CPUID Trick
36d1dd2c5180c5d4183e49aa8f2e2ad9|Bochs & QEmu CPUID Trick
8dd20b7e63fe62e8562658c054d3a8f3|Bochs & QEmu CPUID Trick
db4c42351b425289236f412285bc9aae|Bochs & QEmu CPUID Trick
dcf62559013dbbc04dbf2d8426c7181f|Bochs & QEmu CPUID Trick
e9610e3e8ec4043767601f5f16c6d4ec|Bochs & QEmu CPUID Trick
```

### Reproducibility Notice

We think reproducibility is very import, so we provide the data we used to
perfom analysis. However, you should take care of metrics when trying to
reproduce the presented experiments. When doing so, consider the applied
metric, such as whether the number of samples is the total or **uniques**,
total or **by filetype**, total or **by extension**, total or those which
**presented a given behavior**, and so on. You should also care about the
date of the data you are looking into, since we conducted distinct studies
on distinct years.

If you are looking for some data which is not available here, please
get in contact, so I can update the repository.

## Tools

The *Tools* directory contains scripts I've written to help parsing and data analysis.

## Deobfuscation

The *Deobfuscation* directory contains some simple function I have written to obfuscate/deobfuscate/understand the samples presented as example on 2017's paper.

## Plot

On the *Plot* directory, you can find gnuplot data and scripts for reproducing some graphs from the paper. If you are interested on how we automated script generation, take a look [here](https://github.com/marcusbotacin/Writing.Tools.git).

## Additional Results

Analysis not included on the final paper:

### Taxonomy

[BehEMOT's Taxonomy](https://github.com/marcusbotacin/BehEMOT-NG/) results for the observed behaviors.

The following behavior classes results were found along the years:

The following behavior labels results were found along the years:

We didn't fully analyzed these data, so further analysis is required. The
growth of the *Modifier* class may be related to the growth of the
*Download* label. We don't have any keys about why the *Evasion* class
decreased. An experiment would be associate such behavior with the
increasing number of .Net and CPL files.

### Names

Besides the general classes presented on the paper, we also detailed sub-classes for each category, as presented below.

It reflects people's interests for popular applications.

Among companies, telecoms are the most target. It can be explained by the attacker's interest on pre-paid accounts.

On the financial subject, attackers try to get credit information by sending fake payment bills.

The largest entity involved is the *Receita Federal*, due to the online tax-payment software.

Among the subjects related to personal shames, the most frequent is the *photos* topic.

On the security area, most users are fooled by rogue security solutions.