#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "../lve-ctl.h"

int main(void)
{
	uint32_t cookie, cookie1;
	int rc,reenter;
	int32_t cpu, io;
	struct liblve *lve;
	int ve_id;

	ve_id=29;reenter=0;

	printf("*\t*  LVE API TEST 2  *\t*\n");
	
	rc = lve_set_default(10,11);
	if (rc) {
		printf("can't set default \n");
	}

	rc = lve_instance_init(NULL);
	printf("size for lve struct %d\n", rc);
	lve = malloc(rc);

	rc = lve_instance_init(lve);
	if (rc != 0) {
		printf("can't init lve instance %d\n", rc);
		return 1;
	}
	/**************************************************************/
//	printf("create uid %d / gid %d - %x\n", getuid(), getgid(), cookie);
	
	/* 20 this for custom setup */
	rc = lve_create(ve_id);
	if (rc) {
		printf("can't create VE %d - %d\n", ve_id, rc);
		return 1;
	}
//---------------------------------------------------------------
	printf("lve_setup\t\t\t");
	rc = lve_setup(ve_id, 20, 20, 20);
	if (rc)  {
		printf("ERROR %d\n", rc);
//		return 1;
	} else printf("OK\n");
//---------------------------------------------------------------
	printf("lve_info\t\t\t");
	rc = lve_info(ve_id, &cpu, &io);
	if (rc) {
		printf("ERROR %d\n",rc);
//		return 0;
	} else printf("OK\n");
//---------------------------------------------------------------
	printf("lve_destroy\t\t\t");
	rc = lve_destroy(ve_id);
	if (rc) {
		printf("ERROR!\n");
	} else printf("OK\n");
//---------------------------------------------------------------
//we don't need this for now - guys from parallels are fixing this in fairsched
	printf("TEST:destroy with ps inside\t");
	rc = lve_enter(lve, ve_id, -1, -1, &cookie);
	if (rc < 0) {
		printf("ERROR enter %d\n", rc);
	}

	rc = lve_destroy(ve_id);
	if (rc == 0) {
		printf("ERROR (destroyed)\n");
//		return 1;
		ve_id++;
	} else printf("OK\n");
//---------------------------------------------------------------
	printf("TEST: Leave from VE %d\t\t",ve_id);
	rc = lve_leave(lve, &cookie);
	if (rc==0) {
		printf("ERROR (leaved from destroyed ve %d\n", ve_id);
	} else printf("OK\n");
//---------------------------------------------------------------
	printf("TEST: destroy VE %d\t\t",ve_id);
	rc = lve_destroy(ve_id);
	if (rc == 0) {
		printf("Destroyed OK\n");
	} else printf("OK (Couldn't destroy)\n");
//---------------------------------------------------------------
	printf("TEST: enter VE from VE\t\t");
	rc = lve_enter(lve, 10, -1, -1, &cookie);
	if (rc < 0) {
		printf("ERROR enter first VE %d\n", rc);
		return;
	} else {
		rc = lve_enter(lve, 10, -1, -1, &cookie);
		if (rc == 0) {
			printf("ERROR - entered once more time same VE \n");
			reenter=1;
//			return;
		}
		rc = lve_leave(lve, &cookie);
		if (rc < 0) {
			printf("ERROR leaving VE %d\n", rc);
			return;
		}
	};
	if (!reenter) printf("OK (Couldn't enter)\n");
//---------------------------------------------------------------
	/* must last test - because of kill */
	printf("TEST:lve kill\t\t\t");
	rc = lve_enter(lve, 10, -1, -1, &cookie);
	if (rc < 0) {
		printf("error enter %d\n", rc);
		return;
	}

	rc = lve_leave(lve, &cookie1);
	if (rc < 0) {
		printf("error leave %d\n", rc);
		return;
	} else printf("OK\n");

	return 0;
}
