Networking

BAN, LAN, MAN and WAN

Ring Topology, star, extended star

OSI Model
Application, presentation, session, transport, network, datalink, physical

Application:to allow access to network resource, FTP, http
Presentation: to translate, encrypt, & compress data
Session: to establish, manage, and terminate sessions
Transport: reliable process-to-process msg delivery, tcp, udp
network: packet transport and internetworking
data link: bits -> frames, hop-to-hop delivery
physical: transmit bits over a medium

Each layer communicate logically with intermediate node.

RTT: Round Trip Time
WAN is much greater than that of LAN.
TCP flow control -> Three way handshake,

CPS Concepts

Physical World
– sensing, actuation
Communication Network
Cyber System

IoT, Clud, M2M, WSN, Big Data

Interoperability attributes composability, scalability, heterogeneity
Predictability attributes accuracy, compositonality

Security objective: Availability
Network topology: static
Patching: infrequent updates

Random Testing (Fuzzing)

-Feed random inputs to a program
-Observe whether it behaves “correctly”
execution satisfies given specification
or just does not crash
-Special case of mutation analysis

“The infinite monkey theorem”

Thread 1:

...
p = null;

Thread 2:

...
if(p != null){
 ...
 p.close();
}

black-box and white box

black-box testing
-can work with code that cannot be modified
-does not need to analyze or study code
-code can be in any format(managed, binary, obfuscated)

White-box testing
-efficient test suite
-potentially better coverage

HttpPost localHttpPost = new HttpPost(...);
(new DefaultHttpClient()).execute(localHttpPost;

Automated testing is hard to do
probably impossible for entire system

A pre-condition is a predicate
A post-condition is a predicate

class Stack{
	T[] array;
	int size;

	Pre: s.size() > 0
	T pop() { return array[--size]; }
	Post: s'.size() == s.size() -1

	int size() { return size; }
}
int foo(int[] A, int[] B){
	int r = 0;
	for (int i = 0; i < A.length; i++){
		r += A[i] * B[i];
	}
	return r;
}

Software Testing

Learn methods to improve software quality
-reliability, security, performance
-Become a better software developer

Security Vulnerabilities
-Exploits of errors in programs

Dynamic, static, hyprid

An invariant at the end of the program

int p(int x){ return x * x; }
void main(){
	int z;
	if(getc() == 'a')
		z = p(6) + 6;
	else
		z = p(-7) -7;

	if (z != 42)
		disaster();
}
void main(){
	z = 3;
	while (true){
 	 if(x == 1)
 	 	y = 7;
 	 else
 	 	y = z + 4;
 	 assert (y == 7);
	}
}

Functions for Lines

from decimal import Decimal, getcontext

from vector import vector
getcontext().prec = 30


class Line(object):
	NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero element found'

	def __init__(self, normal_vector=None, constant_term=None):
		self.dimension = 2

		if not normal_vector:
			all_zeros = ['0']*self.dimension
			normal_vector = Vector(all_zeros)
		self.normal_vector = normal_vector

Ax + By + Cz = k1
Dx + Ey + Fz = k2
[A B C], [D E F] not parallel
dx = vector from x0 to x
Since x0, x in plane 1, dx is orth to n1

-One equation in two variables defines a line
-One equation in three variables define a plane

Caution: Count variables with coefficient 0
x + y = 2 defines a line in 2D
x + y = 2 defines a plane in 3D

systematically manipulate the system to make it easier to find the solution
– Manipulations should preserve the solution
– Manipulations should be reversible

Intersections

Geometric object = a set of points satisfying a given relationship(e.g. equations)

“Flat” = defined by linear equations
Linear equations:
-Can add and subtract variables and constants
-Can multiply a variable by a constant

Linear: x + 2y = 1, y/2 – 2z =x
Nonlinear: x^2 -1 = y, y/x = 3

Equations come from observed or modeled relationships between real-world quantities.

ML for trading
stock:A, B
Wa = proportion of portfolio invested in A
Wb = proportion of portfolio invested in B
0 < Wa, Wb < 1 β value = measure of correlation of a stock's price movements with market movement β of portfolio = weighted average of individual components' β values Wa + Wb = 1 B portfolio = 2Wb - Wa = 0 Variables: weights of the stocks Constraints: equations representing physical realities or objectives Lines in Two dimensions Two pieces of info define a line -basepoint x -direction vector v x(t) = x + tv Ax + By = 0 (A and B not both 0)

Inner Products

Inner Products(Dot Products)
v*w = ||v||*||w||*cosΘ

def dot(self, v):
	return sum([x*y for x,y in zip(self.coordinates, v.coordinates)])

	def angle_with(self, v, in_degrees=False)
		try:
			u1 = self.normalized()
			u2 = v.normalized()
			angle_in_raddians = acos(u1.dot(u2))

			if in_degrees:
				degrees_per_radian = 180./ pi
				return angle_in_radians * degrees_per_radian
			else:
				return angle_in_radians

			except Exception as e:
				if str(e) == self.CANNOT_NORMALIZE_ZERO_VECTOR_MSG:
					raise Exception('Cannot compute an angle with the zero vector')
				else:
					raise e

Magnitude and Direction

P -> Q
magnitude of v = distance between P and Q
h^2 = (△x)^2 + (△y)^2
v = √(Vx^2 + Vy^2)

-A unit vector is a vector whose magnitude is 1.
-A vector’s direction can be represented by a unit vector.

Normalization: process of finding a unit vector in the same direction as a given vector.
[0 0 0] = 0
||0|| = 0
1 / ||0|| = ?

def magnitude(self):
	coordinates_squared = [x**2 for x in self.coordinates]
	return sqrt(sum(coordinates_squared))

def normalized(self):
	try:
		magnitude = self.magnitude()
		return self.times_scalar(1./magnitude)

	except ZeroDivisionError:
		raise Exception('Cannot normalize the zero vector')

def plus(self, v):
	new_coordinates = [x+y for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

Operating on Vectors

– Addition
– Subtraction
[1 3] – [5 1] = [-4 2]
– Scalar Multiplication
2, 1, 1/2, -3/2

[8.218 -9.341]+[-1.129 2.111]=[7.089 -7.23]
[7.119 8.215] – [-8.223 0.878]=[15.342 7.337]
7.41[1.671 -1.012 -0.318]= [12.382 -7.499 -2.356]

def plus(self, v):
	new_coordinates = [x+y for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

def minus(self, v):
	new_coordinates = [x-y for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

def times_scalar(self, c):
	new_coordinates = [c*x for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

def __str__(self):
	return 'Vector: {}'.format(self.coordinates)

def __eg__(self, v):
	return self.coordinates == v.coordinates